selection_bloc.dart 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import 'package:eye_video/bizmodule/main/selections/blocs/selection_event.dart';
  2. import 'package:eye_video/bizmodule/main/selections/blocs/selection_state.dart';
  3. import 'package:eye_video/bizmodule/main/selections/model/selection_model.dart';
  4. import 'package:eye_video/bizmodule/main/selections/respositories/selection_repository.dart';
  5. import 'package:flutter/cupertino.dart';
  6. import 'package:eye_video/bizmodule/main/selections/extension/ext_selection.dart';
  7. import 'package:flutter_bloc/flutter_bloc.dart';
  8. class SelectionBloc extends Bloc<SelectionEvent, SelectionState> {
  9. final SelectionRepository selectionRepository;
  10. List<Selection> mSelections = [];
  11. String nextPageUrl;
  12. SelectionBloc({required this.selectionRepository}) : super(null) {
  13. add(EventRequest(isFirst: true, isRefresh: true));
  14. }
  15. @override
  16. Stream<SelectionState> mapEventToState(SelectionEvent event) async* {
  17. if (event is EventRequest) {
  18. if (event.isFirst) {
  19. yield StateRequestLoading();
  20. }
  21. try {
  22. if (event.isRefresh) {
  23. //刷新
  24. mSelections.clear();
  25. var selectionModel = await selectionRepository.fetchSelections();
  26. nextPageUrl = selectionModel.nextPageUrl ?? "";
  27. mSelections.addAll(selectionModel.selectionList);
  28. if (mSelections.isNotEmpty && mSelections.first.isSquareCard) {
  29. var selectionFirst = mSelections.first;
  30. mSelections.add(
  31. Selection(
  32. 'textCard',
  33. SelectionData(
  34. 'TextCard',
  35. null,
  36. null,
  37. null,
  38. null,
  39. null,
  40. 'header5',
  41. selectionFirst.data.header.title,
  42. null,
  43. null,
  44. null,
  45. null,
  46. null,
  47. null,
  48. null,
  49. null,
  50. null,
  51. null,
  52. null,
  53. null,
  54. null,
  55. null,
  56. null,
  57. null,
  58. null,
  59. null,
  60. null,
  61. null,
  62. null,
  63. null,
  64. null,
  65. null,
  66. ),
  67. null,
  68. 0,
  69. 0,
  70. ),
  71. );
  72. mSelections.addAll(selectionFirst.data.selectionList);
  73. }
  74. } else {
  75. //加载更多
  76. if (_hasNextPage(nextPageUrl)) {
  77. var selectionModel = await selectionRepository.fetchSelections(
  78. nextPageUrl: nextPageUrl);
  79. nextPageUrl = selectionModel.nextPageUrl ?? "";
  80. mSelections.addAll(selectionModel.selectionList);
  81. }
  82. }
  83. if (mSelections.isEmpty) {
  84. yield StateRequestEmpty();
  85. } else {
  86. yield StateRequestSuccess(
  87. List.of(mSelections), _hasNextPage(nextPageUrl));
  88. }
  89. } catch (e) {
  90. print('request error: ${e.toString()}');
  91. yield StateRequestFailure();
  92. }
  93. }
  94. }
  95. bool _hasNextPage(String nextPageUrl) {
  96. if (nextPageUrl == null || nextPageUrl.isEmpty) {
  97. return false;
  98. }
  99. try {
  100. Uri uri = Uri.parse(nextPageUrl);
  101. if (uri.queryParameters['isTag'] == 'false' &&
  102. uri.queryParameters['page'] == '0') {
  103. return false;
  104. }
  105. } catch (e) {
  106. return false;
  107. }
  108. return true;
  109. }
  110. }