123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
- import 'package:eye_video/bizmodule/main/selections/blocs/selection_event.dart';
- import 'package:eye_video/bizmodule/main/selections/blocs/selection_state.dart';
- import 'package:eye_video/bizmodule/main/selections/model/selection_model.dart';
- import 'package:eye_video/bizmodule/main/selections/respositories/selection_repository.dart';
- import 'package:flutter/cupertino.dart';
- import 'package:eye_video/bizmodule/main/selections/extension/ext_selection.dart';
- import 'package:flutter_bloc/flutter_bloc.dart';
- class SelectionBloc extends Bloc<SelectionEvent, SelectionState> {
- final SelectionRepository selectionRepository;
- List<Selection> mSelections = [];
- String nextPageUrl;
- SelectionBloc({@required this.selectionRepository}) : super(null) {
- add(EventRequest(isFirst: true, isRefresh: true));
- }
- @override
- Stream<SelectionState> mapEventToState(SelectionEvent event) async* {
- if (event is EventRequest) {
- if (event.isFirst) {
- yield StateRequestLoading();
- }
- try {
- if (event.isRefresh) {
- //刷新
- mSelections.clear();
- var selectionModel = await selectionRepository.fetchSelections();
- nextPageUrl = selectionModel.nextPageUrl ?? "";
- mSelections.addAll(selectionModel.selectionList);
- if (mSelections.isNotEmpty && mSelections.first.isSquareCard) {
- var selectionFirst = mSelections.first;
- mSelections.add(
- Selection(
- 'textCard',
- SelectionData(
- 'TextCard',
- null,
- null,
- null,
- null,
- null,
- 'header5',
- selectionFirst.data.header.title,
- null,
- null,
- null,
- null,
- null,
- null,
- null,
- null,
- null,
- null,
- null,
- null,
- null,
- null,
- null,
- null,
- null,
- null,
- null,
- null,
- null,
- null,
- null,
- null,
- ),
- null,
- 0,
- 0,
- ),
- );
- mSelections.addAll(selectionFirst.data.selectionList);
- }
- } else {
- //加载更多
- if (_hasNextPage(nextPageUrl)) {
- var selectionModel = await selectionRepository.fetchSelections(
- nextPageUrl: nextPageUrl);
- nextPageUrl = selectionModel.nextPageUrl ?? "";
- mSelections.addAll(selectionModel.selectionList);
- }
- }
- if (mSelections.isEmpty) {
- yield StateRequestEmpty();
- } else {
- yield StateRequestSuccess(
- List.of(mSelections), _hasNextPage(nextPageUrl));
- }
- } catch (e) {
- print('request error: ${e.toString()}');
- yield StateRequestFailure();
- }
- }
- }
- bool _hasNextPage(String nextPageUrl) {
- if (nextPageUrl == null || nextPageUrl.isEmpty) {
- return false;
- }
- try {
- Uri uri = Uri.parse(nextPageUrl);
- if (uri.queryParameters['isTag'] == 'false' &&
- uri.queryParameters['page'] == '0') {
- return false;
- }
- } catch (e) {
- return false;
- }
- return true;
- }
- }
|