478 lines
13 KiB
Dart
478 lines
13 KiB
Dart
part of 'widgets.dart';
|
|
|
|
/// [TypeDefs]
|
|
|
|
typedef EnhancedListViewKey<B, H, F>
|
|
= GlobalKey<EnhancedListViewState<B, H, F>>;
|
|
|
|
typedef PaginatedListViewHeaderBuilder<H> = Widget Function(
|
|
Future<List<H?>> Function() headerItems);
|
|
typedef PaginatedListViewBodyBuilder<T> = Widget Function(
|
|
BuildContext context, T item, int index);
|
|
typedef PaginatedListViewFooterBuilder<F> = Widget Function(
|
|
Future<List<F?>> Function() footerItems);
|
|
|
|
typedef Query<T> = T?;
|
|
|
|
typedef BodyItemsBuilder<T> = Future<List<T?>> Function(
|
|
int page, int pageSize, Query query);
|
|
typedef HeaderItemsBuilder<H> = Future<List<H?>> Function();
|
|
typedef FooterItemsBuilder<F> = Future<List<F?>> Function();
|
|
|
|
/// [Extensions]
|
|
extension PaginatedListMergeExtensions<T>
|
|
on Stream<Result<EnhancedPaginatedList<T>>> {
|
|
Stream<EnhancedPaginatedList<T>> mergeWithPaginatedList(
|
|
BehaviorSubject<EnhancedPaginatedList<T>> currentList) {
|
|
return map(
|
|
(result) {
|
|
final current = currentList.value;
|
|
if (result is ResultSuccess<EnhancedPaginatedList<T>>) {
|
|
final newPaginated = result.data;
|
|
return current.items.isEmpty
|
|
? newPaginated
|
|
: current.copyWith(
|
|
list: [...current.items, ...newPaginated.items],
|
|
currentPage: newPaginated.currentPage,
|
|
totalCount: newPaginated.totalCount,
|
|
error: newPaginated.error,
|
|
);
|
|
} else if (result is ResultError<EnhancedPaginatedList<T>>) {
|
|
return current.copyWith(error: result.error);
|
|
} else {
|
|
return current;
|
|
}
|
|
},
|
|
);
|
|
}
|
|
}
|
|
|
|
extension PublishSubjectExtensions<T> on PublishSubject<T> {
|
|
Stream<T> startWith(T initial) => Rx.concat([Stream.value(initial), this]);
|
|
}
|
|
|
|
extension StreamStartWithExtension<T> on Stream<T> {
|
|
Stream<T> startWith(T initial) => Rx.concat([Stream.value(initial), this]);
|
|
}
|
|
|
|
extension QueryBlocStreamExtensions<T> on Stream<bool> {
|
|
Stream<Result<EnhancedPaginatedList<T>>> fetchData(
|
|
EnhancedListViewRepository<T> repository,
|
|
PaginatedListViewBodyBuilder<T> builder,
|
|
BehaviorSubject<EnhancedPaginatedList<T>> paginatedList,
|
|
) =>
|
|
switchMap((reset) {
|
|
if (reset) {
|
|
paginatedList.add(paginatedList.value.resetAll());
|
|
}
|
|
final nextPage = paginatedList.value.currentPage + 1;
|
|
return repository
|
|
.fetchPage(nextPage, paginatedList.value.pageSize, builder)
|
|
.asResultStream();
|
|
});
|
|
}
|
|
|
|
/// [Widgets]
|
|
|
|
/// [EnhancedListView]
|
|
|
|
interface class EnhancedPaginatedList<T> extends PaginatedList<T> {
|
|
@override
|
|
final Exception? error;
|
|
final bool isInitialized;
|
|
@override
|
|
final bool isLoading;
|
|
final List<T> items;
|
|
@override
|
|
final int pageSize;
|
|
@override
|
|
final int? totalCount;
|
|
final int currentPage;
|
|
|
|
EnhancedPaginatedList({
|
|
required this.items,
|
|
required this.pageSize,
|
|
this.currentPage = 0,
|
|
this.totalCount,
|
|
required this.error,
|
|
required this.isInitialized,
|
|
required this.isLoading,
|
|
}) : super(
|
|
error: error,
|
|
isInitialized: isInitialized,
|
|
isLoading: isLoading,
|
|
list: items,
|
|
pageSize: pageSize,
|
|
totalCount: totalCount,
|
|
);
|
|
|
|
EnhancedPaginatedList<T> resetAll() => EnhancedPaginatedList<T>(
|
|
error: null,
|
|
isInitialized: false,
|
|
isLoading: false,
|
|
items: const [],
|
|
pageSize: pageSize,
|
|
totalCount: null,
|
|
currentPage: 0,
|
|
);
|
|
|
|
@override
|
|
EnhancedPaginatedList<T> copyWith({
|
|
List<T>? list,
|
|
bool? isLoading,
|
|
int? totalCount,
|
|
Exception? error,
|
|
int? pageSize,
|
|
bool? isInitialized,
|
|
int? currentPage,
|
|
}) =>
|
|
EnhancedPaginatedList<T>(
|
|
error: error ?? this.error,
|
|
isInitialized: isInitialized ?? this.isInitialized,
|
|
isLoading: isLoading ?? this.isLoading,
|
|
items: list ?? this.items,
|
|
pageSize: pageSize ?? this.pageSize,
|
|
totalCount: totalCount ?? this.totalCount,
|
|
currentPage: currentPage ?? this.currentPage,
|
|
);
|
|
|
|
@override
|
|
int get itemCount => items.length;
|
|
|
|
@override
|
|
T? getItem(int index) => index < items.length ? items[index] : null;
|
|
|
|
Future<void> awaitLoad() async => Future.value();
|
|
}
|
|
|
|
abstract interface class EnhancedListViewBase<T, H, F> extends StatefulWidget {
|
|
const EnhancedListViewBase({super.key});
|
|
}
|
|
|
|
abstract interface class EnhancedListViewBaseState<T>
|
|
extends State<EnhancedListViewBase> {}
|
|
|
|
// ignore: must_be_immutable
|
|
class EnhancedListView<BodyType, HeaderType, FooterType>
|
|
extends EnhancedListViewBase<BodyType, HeaderType, FooterType> {
|
|
BodyItemsBuilder<BodyType> bodyItems;
|
|
PaginatedListViewBodyBuilder<BodyType> bodyBuilder;
|
|
HeaderItemsBuilder<HeaderType>? headerItems;
|
|
PaginatedListViewHeaderBuilder<HeaderType>? headerBuilder;
|
|
FooterItemsBuilder<FooterType>? footerItems;
|
|
PaginatedListViewFooterBuilder<FooterType>? footerBuilder;
|
|
|
|
EnhancedListView({
|
|
Key? key,
|
|
required this.bodyItems,
|
|
required this.bodyBuilder,
|
|
this.headerItems,
|
|
this.headerBuilder,
|
|
this.footerItems,
|
|
this.footerBuilder,
|
|
}) : super(key: key);
|
|
|
|
@override
|
|
EnhancedListViewState<BodyType, HeaderType, FooterType> createState() =>
|
|
EnhancedListViewState<BodyType, HeaderType, FooterType>();
|
|
}
|
|
|
|
class EnhancedListViewState<ItemType, HeaderType, FooterType>
|
|
extends State<EnhancedListView<ItemType, HeaderType, FooterType>> {
|
|
final ScrollController _scrollController = ScrollController();
|
|
bool _isLoadingMore = false;
|
|
List<ItemType?> items = [];
|
|
List<HeaderType?> headerItems = [];
|
|
List<FooterType?> footerItems = [];
|
|
int currentPage = 1;
|
|
Query<ItemType> query;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_scrollController.addListener(_onScroll);
|
|
_loadBodyItems();
|
|
_loadHeaderItems();
|
|
_loadFooterItems();
|
|
}
|
|
|
|
void _onScroll() {
|
|
if (_scrollController.position.pixels ==
|
|
_scrollController.position.maxScrollExtent &&
|
|
!_isLoadingMore) {
|
|
_loadMoreBodyItems();
|
|
}
|
|
}
|
|
|
|
Future<void> _loadBodyItems() async {
|
|
log('loadInitialItems');
|
|
final newItems = await widget.bodyItems(1, 10, query);
|
|
setState(() {
|
|
items = newItems;
|
|
});
|
|
}
|
|
|
|
Future<void> _loadMoreBodyItems() async {
|
|
log('loadMoreItems');
|
|
setState(() {
|
|
_isLoadingMore = true;
|
|
});
|
|
final newItems = await widget.bodyItems(currentPage + 1, 10, query);
|
|
setState(() {
|
|
_isLoadingMore = false;
|
|
if (newItems.isNotEmpty) {
|
|
items.addAll(newItems);
|
|
currentPage++;
|
|
}
|
|
});
|
|
}
|
|
|
|
Future<void> _loadHeaderItems() async {
|
|
if (widget.headerItems != null) {
|
|
log('loadHeaderItems');
|
|
final newHeaderItems = await widget.headerItems!();
|
|
setState(() {
|
|
headerItems = newHeaderItems;
|
|
});
|
|
}
|
|
}
|
|
|
|
Future<void> _loadFooterItems() async {
|
|
if (widget.footerItems != null) {
|
|
log('loadFooterItems');
|
|
final newFooterItems = await widget.footerItems!();
|
|
setState(() {
|
|
footerItems = newFooterItems;
|
|
});
|
|
}
|
|
}
|
|
|
|
Future<void> filterBodyItems(Query<ItemType> newQuery) async {
|
|
log('filterItems B: ${newQuery.toString()}');
|
|
setState(() {
|
|
query = newQuery;
|
|
items = [];
|
|
currentPage = 1;
|
|
});
|
|
await _loadBodyItems();
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
log('key: ${widget.key}');
|
|
return ListView.builder(
|
|
controller: _scrollController,
|
|
itemCount: items.length +
|
|
(headerItems.isNotEmpty ? 1 : 0) +
|
|
(footerItems.isNotEmpty ? 1 : 0) +
|
|
(_isLoadingMore ? 1 : 0),
|
|
itemBuilder: (context, index) {
|
|
if (headerItems.isNotEmpty && index == 0) {
|
|
return widget.headerBuilder!(() => Future.value(headerItems));
|
|
}
|
|
if (footerItems.isNotEmpty &&
|
|
index == items.length + (headerItems.isNotEmpty ? 1 : 0)) {
|
|
return widget.footerBuilder!(() => Future.value(footerItems));
|
|
}
|
|
if (_isLoadingMore &&
|
|
index ==
|
|
items.length +
|
|
(headerItems.isNotEmpty ? 1 : 0) +
|
|
(footerItems.isNotEmpty ? 1 : 0)) {
|
|
return const EnhancedProgressIndicator();
|
|
}
|
|
final item = items[index - (headerItems.isNotEmpty ? 1 : 0)];
|
|
return widget.bodyBuilder(context, item as ItemType, index);
|
|
},
|
|
);
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
_scrollController.dispose();
|
|
super.dispose();
|
|
}
|
|
}
|
|
|
|
/// [Utils]
|
|
|
|
class EnhancedListTile<T extends Widget> extends StatelessWidget {
|
|
const EnhancedListTile(
|
|
{required this.leading, required this.title, super.key});
|
|
|
|
final T leading;
|
|
final T title;
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Card(
|
|
child: ListTile(
|
|
leading: leading,
|
|
title: title,
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|
|
class EnhancedErrorWidget extends StatelessWidget {
|
|
final Object? error;
|
|
const EnhancedErrorWidget({required this.error, super.key});
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
log('error: $error');
|
|
return Padding(
|
|
padding: const EdgeInsets.all(16.0),
|
|
child: Text(
|
|
error.toString(),
|
|
style: const TextStyle(color: Colors.red),
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|
|
class EnhancedProgressIndicator extends StatelessWidget {
|
|
const EnhancedProgressIndicator({super.key});
|
|
|
|
@override
|
|
Widget build(BuildContext context) => const Center(
|
|
child: Padding(
|
|
padding: EdgeInsets.symmetric(vertical: 12),
|
|
child: CircularProgressIndicator(),
|
|
),
|
|
);
|
|
}
|
|
|
|
class NetworkError implements Exception {
|
|
final String message;
|
|
NetworkError(this.message);
|
|
}
|
|
|
|
class ParsingError implements Exception {
|
|
final String message;
|
|
ParsingError(this.message);
|
|
}
|
|
|
|
class AuthorizationError implements Exception {
|
|
final String message;
|
|
AuthorizationError(this.message);
|
|
}
|
|
|
|
/// [State Managment]
|
|
Stream<bool> get loadingState => Stream<bool>.empty();
|
|
Stream<Exception> get errorState => Stream<Exception>.empty();
|
|
|
|
abstract class EnhancedListViewRepository<T> {
|
|
Future<EnhancedPaginatedList<T>> fetchPage(
|
|
int page, int pageSize, PaginatedListViewBodyBuilder<T> builder);
|
|
}
|
|
|
|
abstract class EnhancedListViewBlocStates<T> {
|
|
Stream<bool> get isLoading;
|
|
Stream<String> get errors;
|
|
Stream<EnhancedPaginatedList<T>> get paginatedList;
|
|
|
|
@RxBlocIgnoreState()
|
|
Future<void> get refreshDone;
|
|
}
|
|
|
|
abstract class EnhancedListViewEvents<T> {
|
|
void loadPage({bool reset = false});
|
|
}
|
|
|
|
abstract class EnhancedListViewBlocType<T> extends RxBlocTypeBase {
|
|
EnhancedListViewEvents<T> get events;
|
|
EnhancedListViewBlocStates<T> get states;
|
|
}
|
|
|
|
abstract class $EnhancedListViewBloc<T> extends RxBlocBase
|
|
implements
|
|
EnhancedListViewEvents<T>,
|
|
EnhancedListViewBlocStates<T>,
|
|
EnhancedListViewBlocType<T> {
|
|
final _compositeSubscription = CompositeSubscription();
|
|
|
|
final _$loadPageEvent = PublishSubject<bool>();
|
|
|
|
late final Stream<bool> _isLoadingState = _mapToIsLoadingState();
|
|
late final Stream<String> _errorsState = _mapToErrorsState();
|
|
late final Stream<EnhancedPaginatedList<T>> _paginatedListState =
|
|
_mapToPaginatedListState();
|
|
|
|
@override
|
|
void loadPage({bool reset = false}) => _$loadPageEvent.add(reset);
|
|
|
|
@override
|
|
Stream<bool> get isLoading => _isLoadingState;
|
|
@override
|
|
Stream<String> get errors => _errorsState;
|
|
@override
|
|
Stream<EnhancedPaginatedList<T>> get paginatedList => _paginatedListState;
|
|
|
|
Stream<bool> _mapToIsLoadingState();
|
|
Stream<String> _mapToErrorsState();
|
|
Stream<EnhancedPaginatedList<T>> _mapToPaginatedListState();
|
|
|
|
@override
|
|
EnhancedListViewEvents<T> get events => this;
|
|
@override
|
|
EnhancedListViewBlocStates<T> get states => this;
|
|
|
|
@override
|
|
void dispose() {
|
|
_$loadPageEvent.close();
|
|
_compositeSubscription.dispose();
|
|
super.dispose();
|
|
}
|
|
}
|
|
|
|
class EnhancedListViewBloc<T> extends $EnhancedListViewBloc<T> {
|
|
EnhancedListViewBloc({
|
|
required EnhancedListViewRepository<T> repository,
|
|
required PaginatedListViewBodyBuilder<T> builder,
|
|
required T item,
|
|
int initialPageSize = 50,
|
|
}) {
|
|
_$loadPageEvent
|
|
.startWith(true)
|
|
.fetchData(
|
|
repository,
|
|
(context, item, index) => builder(context, item, index),
|
|
_paginatedList,
|
|
)
|
|
.setResultStateHandler(this)
|
|
.mergeWithPaginatedList(_paginatedList)
|
|
.bind(_paginatedList)
|
|
.addTo(_compositeSubscription);
|
|
}
|
|
|
|
final _paginatedList = BehaviorSubject<EnhancedPaginatedList<T>>.seeded(
|
|
EnhancedPaginatedList<T>(
|
|
items: [],
|
|
pageSize: 1,
|
|
currentPage: 1,
|
|
error: Exception(),
|
|
isInitialized: true,
|
|
isLoading: false,
|
|
totalCount: 0,
|
|
),
|
|
);
|
|
|
|
@override
|
|
Future<void> get refreshDone async => _paginatedList.value.awaitLoad();
|
|
|
|
@override
|
|
Stream<EnhancedPaginatedList<T>> _mapToPaginatedListState() => _paginatedList;
|
|
@override
|
|
Stream<String> _mapToErrorsState() =>
|
|
errorState.map((error) => error.toString());
|
|
@override
|
|
Stream<bool> _mapToIsLoadingState() => loadingState;
|
|
|
|
@override
|
|
void dispose() {
|
|
_paginatedList.close();
|
|
super.dispose();
|
|
}
|
|
}
|