|
| 1 | +import 'dart:async'; |
| 2 | + |
| 3 | +import 'package:flutter_bloc/flutter_bloc.dart'; |
| 4 | +import 'package:dart_mappable/dart_mappable.dart'; |
| 5 | +import 'package:equatable/equatable.dart'; |
| 6 | +import 'package:flow/cubits/flow.dart'; |
| 7 | +import 'package:flow_api/models/model.dart'; |
| 8 | +import 'package:flow_api/services/source.dart'; |
| 9 | + |
| 10 | +part 'sourced_paging_event.dart'; |
| 11 | +part 'sourced_paging_state.dart'; |
| 12 | + |
| 13 | +part 'sourced_paging.mapper.dart'; |
| 14 | + |
| 15 | +class SourcedPagingBloc<T> |
| 16 | + extends Bloc<SourcedPagingEvent, SourcedPagingState<T>> { |
| 17 | + final FlowCubit cubit; |
| 18 | + final int pageSize; |
| 19 | + final Future<List<T>?> Function( |
| 20 | + String source, |
| 21 | + SourceService service, |
| 22 | + int offset, |
| 23 | + int limit, |
| 24 | + ) _fetch; |
| 25 | + |
| 26 | + SourcedPagingBloc( |
| 27 | + {required this.cubit, |
| 28 | + required Future<List<T>?> Function(String, SourceService, int, int) fetch, |
| 29 | + this.pageSize = 50}) |
| 30 | + : _fetch = fetch, |
| 31 | + super(const SourcedPagingInitial()) { |
| 32 | + on<SourcedPagingFetched<T>>(_onFetched); |
| 33 | + } |
| 34 | + |
| 35 | + Future<void> _onFetched( |
| 36 | + SourcedPagingFetched<T> event, |
| 37 | + Emitter<SourcedPagingState<T>> emit, |
| 38 | + ) async { |
| 39 | + final state = this.state; |
| 40 | + if (state.hasReachedMax) return; |
| 41 | + |
| 42 | + try { |
| 43 | + final currentPageKey = state.currentPageKey ?? |
| 44 | + SourcedModel(cubit.getCurrentSources().first, 0); |
| 45 | + final previousItems = state is SourcedPagingSuccess<T> ? state.items : []; |
| 46 | + |
| 47 | + final fetchedItems = (await _fetch( |
| 48 | + currentPageKey.source, |
| 49 | + cubit.getService(currentPageKey.source), |
| 50 | + currentPageKey.model * pageSize, |
| 51 | + pageSize) ?? |
| 52 | + <T>[]) |
| 53 | + .map((e) => SourcedModel(currentPageKey.source, e)) |
| 54 | + .toList(); |
| 55 | + |
| 56 | + final sources = cubit.getCurrentSources(); |
| 57 | + final currentSourceIndex = sources.indexOf(currentPageKey.source); |
| 58 | + final keepSource = fetchedItems.length >= pageSize; |
| 59 | + final isLastSource = currentSourceIndex >= sources.length - 1; |
| 60 | + |
| 61 | + if (isLastSource && !keepSource) { |
| 62 | + emit(SourcedPagingSuccess( |
| 63 | + currentPageKey: currentPageKey, |
| 64 | + items: [...previousItems, ...fetchedItems], |
| 65 | + hasReachedMax: true, |
| 66 | + )); |
| 67 | + } else if (keepSource) { |
| 68 | + emit(SourcedPagingSuccess( |
| 69 | + items: [...previousItems, ...fetchedItems], |
| 70 | + currentPageKey: SourcedModel( |
| 71 | + currentPageKey.source, |
| 72 | + currentPageKey.model + 1, |
| 73 | + ), |
| 74 | + )); |
| 75 | + } else { |
| 76 | + final nextSource = sources[currentSourceIndex + 1]; |
| 77 | + emit(SourcedPagingSuccess( |
| 78 | + items: [...previousItems, ...fetchedItems], |
| 79 | + currentPageKey: SourcedModel(nextSource, 0), |
| 80 | + )); |
| 81 | + } |
| 82 | + } catch (e) { |
| 83 | + emit(SourcedPagingFailure(e)); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + void refresh() => add(SourcedPagingRefresh()); |
| 88 | + void fetch() => add(SourcedPagingFetched<T>()); |
| 89 | +} |
0 commit comments