ugc_bloc.dart 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import 'package:eye_video/bizmodule/main/selections/blocs/ugc/ugc_event.dart';
  2. import 'package:eye_video/bizmodule/main/selections/blocs/ugc/ugc_state.dart';
  3. import 'package:eye_video/bizmodule/main/selections/model/ugc/ugc_model.dart';
  4. import 'package:eye_video/bizmodule/main/selections/respositories/ugc_repository.dart';
  5. import 'package:flutter/cupertino.dart';
  6. import 'package:flutter_bloc/flutter_bloc.dart';
  7. class UgcBloc extends Bloc<UgcEvent, UgcState> {
  8. final UgcRepository ugcRepository;
  9. List<UgcModel> mUgcList = [];
  10. int currPage = 1;
  11. int totalCount = 0;
  12. UgcBloc({required this.ugcRepository}) : super(null) {
  13. add(UgcEventRequest(isFirst: true, isRefresh: true));
  14. }
  15. @override
  16. Stream<UgcState> mapEventToState(UgcEvent event) async* {
  17. try {
  18. if (event is UgcEventRequest) {
  19. if (event.isFirst) {
  20. yield UgcStateRequestLoading();
  21. }
  22. if (event.isRefresh) {
  23. mUgcList.clear();
  24. currPage = 1;
  25. var ugcListModel = await ugcRepository.fetchUgcList(page: currPage, pageSize: 20);
  26. totalCount = ugcListModel.totalCount;
  27. mUgcList.addAll(ugcListModel.list);
  28. } else {
  29. if (_hasNextPage) {
  30. var ugcListModel = await ugcRepository.fetchUgcList(
  31. page: ++currPage, pageSize: 20);
  32. totalCount = ugcListModel.totalCount;
  33. mUgcList.addAll(ugcListModel.list);
  34. }
  35. }
  36. if (mUgcList.isEmpty) {
  37. yield UgcStateRequestEmpty();
  38. } else {
  39. yield UgcStateRequestSuccess(List.of(mUgcList), _hasNextPage);
  40. }
  41. }
  42. } catch (e) {
  43. yield UgcStateRequestFailure();
  44. }
  45. }
  46. bool get _hasNextPage => totalCount > mUgcList.length;
  47. }