♻️ Rebuilt fetching state machine
This commit is contained in:
@@ -18,7 +18,8 @@ final indexedCloudFileListProvider = AsyncNotifierProvider.autoDispose(
|
||||
IndexedCloudFileListNotifier.new,
|
||||
);
|
||||
|
||||
class IndexedCloudFileListNotifier extends AsyncNotifier<List<FileListItem>>
|
||||
class IndexedCloudFileListNotifier
|
||||
extends AsyncNotifier<PaginationState<FileListItem>>
|
||||
with AsyncPaginationController<FileListItem> {
|
||||
String _currentPath = '/';
|
||||
String? _poolId;
|
||||
@@ -51,6 +52,19 @@ class IndexedCloudFileListNotifier extends AsyncNotifier<List<FileListItem>>
|
||||
ref.invalidateSelf();
|
||||
}
|
||||
|
||||
@override
|
||||
FutureOr<PaginationState<FileListItem>> build() async {
|
||||
final items = await fetch();
|
||||
return PaginationState(
|
||||
items: items,
|
||||
isLoading: false,
|
||||
isReloading: false,
|
||||
totalCount: null,
|
||||
hasMore: false,
|
||||
cursor: null,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<List<FileListItem>> fetch() async {
|
||||
final client = ref.read(apiClientProvider);
|
||||
@@ -96,7 +110,8 @@ final unindexedFileListProvider = AsyncNotifierProvider.autoDispose(
|
||||
UnindexedFileListNotifier.new,
|
||||
);
|
||||
|
||||
class UnindexedFileListNotifier extends AsyncNotifier<List<FileListItem>>
|
||||
class UnindexedFileListNotifier
|
||||
extends AsyncNotifier<PaginationState<FileListItem>>
|
||||
with AsyncPaginationController<FileListItem> {
|
||||
String? _poolId;
|
||||
bool _recycled = false;
|
||||
@@ -131,6 +146,19 @@ class UnindexedFileListNotifier extends AsyncNotifier<List<FileListItem>>
|
||||
|
||||
static const int pageSize = 20;
|
||||
|
||||
@override
|
||||
FutureOr<PaginationState<FileListItem>> build() async {
|
||||
final items = await fetch();
|
||||
return PaginationState(
|
||||
items: items,
|
||||
isLoading: false,
|
||||
isReloading: false,
|
||||
totalCount: totalCount,
|
||||
hasMore: hasMore,
|
||||
cursor: cursor,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<List<FileListItem>> fetch() async {
|
||||
final client = ref.read(apiClientProvider);
|
||||
|
||||
@@ -2,6 +2,42 @@ import 'dart:async';
|
||||
|
||||
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
||||
|
||||
class PaginationState<T> {
|
||||
final List<T> items;
|
||||
final bool isLoading;
|
||||
final bool isReloading;
|
||||
final int? totalCount;
|
||||
final bool hasMore;
|
||||
final String? cursor;
|
||||
|
||||
const PaginationState({
|
||||
required this.items,
|
||||
required this.isLoading,
|
||||
required this.isReloading,
|
||||
required this.totalCount,
|
||||
required this.hasMore,
|
||||
required this.cursor,
|
||||
});
|
||||
|
||||
PaginationState<T> copyWith({
|
||||
List<T>? items,
|
||||
bool? isLoading,
|
||||
bool? isReloading,
|
||||
int? totalCount,
|
||||
bool? hasMore,
|
||||
String? cursor,
|
||||
}) {
|
||||
return PaginationState<T>(
|
||||
items: items ?? this.items,
|
||||
isLoading: isLoading ?? this.isLoading,
|
||||
isReloading: isReloading ?? this.isReloading,
|
||||
totalCount: totalCount ?? this.totalCount,
|
||||
hasMore: hasMore ?? this.hasMore,
|
||||
cursor: cursor ?? this.cursor,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
abstract class PaginationController<T> {
|
||||
int? get totalCount;
|
||||
int get fetchedCount;
|
||||
@@ -27,51 +63,84 @@ abstract class PaginationFiltered<F> {
|
||||
Future<void> applyFilter(F filter);
|
||||
}
|
||||
|
||||
mixin AsyncPaginationController<T> on AsyncNotifier<List<T>>
|
||||
mixin AsyncPaginationController<T> on AsyncNotifier<PaginationState<T>>
|
||||
implements PaginationController<T> {
|
||||
@override
|
||||
int? totalCount;
|
||||
|
||||
@override
|
||||
int get fetchedCount => isReloading ? 0 : state.value?.length ?? 0;
|
||||
int get fetchedCount =>
|
||||
state.value?.isReloading == true ? 0 : state.value?.items.length ?? 0;
|
||||
|
||||
@override
|
||||
bool get fetchedAll =>
|
||||
!hasMore || (totalCount != null && fetchedCount >= totalCount!);
|
||||
!(state.value?.hasMore ?? true) ||
|
||||
((state.value?.totalCount != null &&
|
||||
fetchedCount >= state.value!.totalCount!));
|
||||
|
||||
@override
|
||||
bool isLoading = false;
|
||||
bool get isLoading => state.value?.isLoading ?? false;
|
||||
|
||||
@override
|
||||
bool isReloading = false;
|
||||
bool get isReloading => state.value?.isReloading ?? false;
|
||||
|
||||
@override
|
||||
bool hasMore = true;
|
||||
bool get hasMore => state.value?.hasMore ?? true;
|
||||
|
||||
@override
|
||||
String? cursor;
|
||||
String? get cursor => state.value?.cursor;
|
||||
|
||||
@override
|
||||
FutureOr<List<T>> build() async {
|
||||
cursor = null;
|
||||
return fetch();
|
||||
set hasMore(bool value) {
|
||||
if (state is AsyncData) {
|
||||
state = AsyncData((state as AsyncData).value.copyWith(hasMore: value));
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
set cursor(String? value) {
|
||||
if (state is AsyncData) {
|
||||
state = AsyncData((state as AsyncData).value.copyWith(cursor: value));
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
FutureOr<PaginationState<T>> build() async {
|
||||
final items = await fetch();
|
||||
return PaginationState(
|
||||
items: items,
|
||||
isLoading: false,
|
||||
isReloading: false,
|
||||
totalCount: totalCount,
|
||||
hasMore: hasMore,
|
||||
cursor: cursor,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> refresh() async {
|
||||
isLoading = true;
|
||||
isReloading = true;
|
||||
totalCount = null;
|
||||
hasMore = true;
|
||||
cursor = null;
|
||||
state = AsyncLoading<List<T>>();
|
||||
state = AsyncData(
|
||||
state.value!.copyWith(
|
||||
isLoading: true,
|
||||
isReloading: true,
|
||||
totalCount: null,
|
||||
hasMore: true,
|
||||
cursor: null,
|
||||
),
|
||||
);
|
||||
|
||||
final newState = await AsyncValue.guard<List<T>>(() async {
|
||||
return await fetch();
|
||||
});
|
||||
isReloading = false;
|
||||
isLoading = false;
|
||||
state = newState;
|
||||
final newItems = await fetch();
|
||||
|
||||
state = AsyncData(
|
||||
state.value!.copyWith(
|
||||
items: newItems,
|
||||
isLoading: false,
|
||||
isReloading: false,
|
||||
totalCount: totalCount,
|
||||
hasMore: hasMore,
|
||||
cursor: cursor,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
@@ -79,16 +148,16 @@ mixin AsyncPaginationController<T> on AsyncNotifier<List<T>>
|
||||
if (fetchedAll) return;
|
||||
if (isLoading) return;
|
||||
|
||||
isLoading = true;
|
||||
state = AsyncLoading<List<T>>();
|
||||
state = AsyncData(state.value!.copyWith(isLoading: true));
|
||||
|
||||
final newState = await AsyncValue.guard<List<T>>(() async {
|
||||
final elements = await fetch();
|
||||
return [...?state.value, ...elements];
|
||||
});
|
||||
final newItems = await fetch();
|
||||
|
||||
isLoading = false;
|
||||
state = newState;
|
||||
state = AsyncData(
|
||||
state.value!.copyWith(
|
||||
items: [...state.value!.items, ...newItems],
|
||||
isLoading: false,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -97,20 +166,29 @@ mixin AsyncPaginationFilter<F, T> on AsyncPaginationController<T>
|
||||
@override
|
||||
Future<void> applyFilter(F filter) async {
|
||||
if (currentFilter == filter) return;
|
||||
// Reset the data
|
||||
isReloading = true;
|
||||
isLoading = true;
|
||||
totalCount = null;
|
||||
hasMore = true;
|
||||
cursor = null;
|
||||
state = AsyncLoading<List<T>>();
|
||||
|
||||
state = AsyncData(
|
||||
state.value!.copyWith(
|
||||
isReloading: true,
|
||||
isLoading: true,
|
||||
totalCount: null,
|
||||
hasMore: true,
|
||||
cursor: null,
|
||||
),
|
||||
);
|
||||
currentFilter = filter;
|
||||
|
||||
final newState = await AsyncValue.guard<List<T>>(() async {
|
||||
return await fetch();
|
||||
});
|
||||
isLoading = false;
|
||||
isReloading = false;
|
||||
state = newState;
|
||||
final newItems = await fetch();
|
||||
|
||||
state = AsyncData(
|
||||
state.value!.copyWith(
|
||||
items: newItems,
|
||||
isLoading: false,
|
||||
isReloading: false,
|
||||
totalCount: totalCount,
|
||||
hasMore: hasMore,
|
||||
cursor: cursor,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
// Post Categories Notifier
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
||||
import 'package:island/models/post_category.dart';
|
||||
import 'package:island/models/post_tag.dart';
|
||||
@@ -8,11 +10,25 @@ import 'package:island/pods/paging.dart';
|
||||
final postCategoriesProvider =
|
||||
AsyncNotifierProvider.autoDispose<
|
||||
PostCategoriesNotifier,
|
||||
List<SnPostCategory>
|
||||
PaginationState<SnPostCategory>
|
||||
>(PostCategoriesNotifier.new);
|
||||
|
||||
class PostCategoriesNotifier extends AsyncNotifier<List<SnPostCategory>>
|
||||
class PostCategoriesNotifier
|
||||
extends AsyncNotifier<PaginationState<SnPostCategory>>
|
||||
with AsyncPaginationController<SnPostCategory> {
|
||||
@override
|
||||
FutureOr<PaginationState<SnPostCategory>> build() async {
|
||||
final items = await fetch();
|
||||
return PaginationState(
|
||||
items: items,
|
||||
isLoading: false,
|
||||
isReloading: false,
|
||||
totalCount: totalCount,
|
||||
hasMore: hasMore,
|
||||
cursor: cursor,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<List<SnPostCategory>> fetch() async {
|
||||
final client = ref.read(apiClientProvider);
|
||||
@@ -30,12 +46,26 @@ class PostCategoriesNotifier extends AsyncNotifier<List<SnPostCategory>>
|
||||
|
||||
// Post Tags Notifier
|
||||
final postTagsProvider =
|
||||
AsyncNotifierProvider.autoDispose<PostTagsNotifier, List<SnPostTag>>(
|
||||
PostTagsNotifier.new,
|
||||
);
|
||||
AsyncNotifierProvider.autoDispose<
|
||||
PostTagsNotifier,
|
||||
PaginationState<SnPostTag>
|
||||
>(PostTagsNotifier.new);
|
||||
|
||||
class PostTagsNotifier extends AsyncNotifier<List<SnPostTag>>
|
||||
class PostTagsNotifier extends AsyncNotifier<PaginationState<SnPostTag>>
|
||||
with AsyncPaginationController<SnPostTag> {
|
||||
@override
|
||||
FutureOr<PaginationState<SnPostTag>> build() async {
|
||||
final items = await fetch();
|
||||
return PaginationState(
|
||||
items: items,
|
||||
isLoading: false,
|
||||
isReloading: false,
|
||||
totalCount: totalCount,
|
||||
hasMore: hasMore,
|
||||
cursor: cursor,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<List<SnPostTag>> fetch() async {
|
||||
final client = ref.read(apiClientProvider);
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:freezed_annotation/freezed_annotation.dart';
|
||||
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
||||
import 'package:island/models/post.dart';
|
||||
@@ -39,7 +41,7 @@ final postListProvider = AsyncNotifierProvider.autoDispose.family(
|
||||
PostListNotifier.new,
|
||||
);
|
||||
|
||||
class PostListNotifier extends AsyncNotifier<List<SnPost>>
|
||||
class PostListNotifier extends AsyncNotifier<PaginationState<SnPost>>
|
||||
with
|
||||
AsyncPaginationController<SnPost>,
|
||||
AsyncPaginationFilter<PostListQuery, SnPost> {
|
||||
@@ -53,9 +55,17 @@ class PostListNotifier extends AsyncNotifier<List<SnPost>>
|
||||
late PostListQuery currentFilter;
|
||||
|
||||
@override
|
||||
Future<List<SnPost>> build() async {
|
||||
FutureOr<PaginationState<SnPost>> build() async {
|
||||
currentFilter = config.initialFilter;
|
||||
return fetch();
|
||||
final items = await fetch();
|
||||
return PaginationState(
|
||||
items: items,
|
||||
isLoading: false,
|
||||
isReloading: false,
|
||||
totalCount: totalCount,
|
||||
hasMore: hasMore,
|
||||
cursor: cursor,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
||||
import 'package:island/models/activity.dart';
|
||||
import 'package:island/pods/network.dart';
|
||||
@@ -7,12 +9,26 @@ final activityListProvider = AsyncNotifierProvider.autoDispose(
|
||||
ActivityListNotifier.new,
|
||||
);
|
||||
|
||||
class ActivityListNotifier extends AsyncNotifier<List<SnTimelineEvent>>
|
||||
class ActivityListNotifier
|
||||
extends AsyncNotifier<PaginationState<SnTimelineEvent>>
|
||||
with
|
||||
AsyncPaginationController<SnTimelineEvent>,
|
||||
AsyncPaginationFilter<String?, SnTimelineEvent> {
|
||||
static const int pageSize = 20;
|
||||
|
||||
@override
|
||||
FutureOr<PaginationState<SnTimelineEvent>> build() async {
|
||||
final items = await fetch();
|
||||
return PaginationState(
|
||||
items: items,
|
||||
isLoading: false,
|
||||
isReloading: false,
|
||||
totalCount: totalCount,
|
||||
hasMore: hasMore,
|
||||
cursor: cursor,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
String? currentFilter;
|
||||
|
||||
@@ -54,9 +70,9 @@ class ActivityListNotifier extends AsyncNotifier<List<SnTimelineEvent>>
|
||||
final currentState = state.value;
|
||||
if (currentState == null) return;
|
||||
|
||||
final updatedItems = [...currentState];
|
||||
final updatedItems = [...currentState.items];
|
||||
updatedItems[index] = activity;
|
||||
|
||||
state = AsyncData(updatedItems);
|
||||
state = AsyncData(currentState.copyWith(items: updatedItems));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user