mirror of
https://github.com/zapstore/zapstore.git
synced 2026-10-05 20:48:24 +00:00
New preloading state notifier, use for latest
This commit is contained in:
@@ -188,7 +188,7 @@ class RelayListenerNotifier extends Notifier<void> {
|
||||
|
||||
Future<void> fetch() async {
|
||||
await Future.microtask(() async {
|
||||
await ref.read(latestReleasesAppProvider.notifier).fetch();
|
||||
await ref.read(latestReleasesAppProvider.notifier).fetchRemote();
|
||||
await ref.apps.appAdapter.checkForUpdates();
|
||||
});
|
||||
}
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
import 'package:flutter_data/flutter_data.dart';
|
||||
|
||||
abstract class PreloadingStateNotifier<T> extends StateNotifier<AsyncValue<T>> {
|
||||
final Ref ref;
|
||||
PreloadingStateNotifier(this.ref) : super(AsyncLoading()) {
|
||||
state = fetchLocal();
|
||||
fetchRemote();
|
||||
}
|
||||
|
||||
AsyncValue<T> fetchLocal();
|
||||
Future<void> fetchRemote();
|
||||
}
|
||||
@@ -6,6 +6,7 @@ import 'package:gap/gap.dart';
|
||||
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
||||
import 'package:zapstore/main.data.dart';
|
||||
import 'package:zapstore/models/app.dart';
|
||||
import 'package:zapstore/utils/notifier.dart';
|
||||
import 'package:zapstore/widgets/app_card.dart';
|
||||
|
||||
class LatestReleasesContainer extends HookConsumerWidget {
|
||||
@@ -58,7 +59,7 @@ class LatestReleasesContainer extends HookConsumerWidget {
|
||||
onPressed: () async {
|
||||
return ref
|
||||
.read(latestReleasesAppProvider.notifier)
|
||||
.fetch(next: true);
|
||||
.fetchRemote(next: true);
|
||||
},
|
||||
builder: (context, child, callback, state) {
|
||||
return Padding(
|
||||
@@ -83,16 +84,14 @@ class LatestReleasesContainer extends HookConsumerWidget {
|
||||
}
|
||||
}
|
||||
|
||||
class LatestReleasesAppNotifier extends StateNotifier<AsyncValue<List<App>>> {
|
||||
class LatestReleasesAppNotifier extends PreloadingStateNotifier<List<App>> {
|
||||
DateTime? _oldestCreatedAt;
|
||||
int _page = 1;
|
||||
final Ref ref;
|
||||
|
||||
LatestReleasesAppNotifier(this.ref) : super(AsyncLoading()) {
|
||||
fetch();
|
||||
}
|
||||
LatestReleasesAppNotifier(super.ref);
|
||||
|
||||
Future<void> fetch({bool next = false}) async {
|
||||
@override
|
||||
Future<void> fetchRemote({bool next = false}) async {
|
||||
if (next) {
|
||||
_page++;
|
||||
}
|
||||
@@ -107,12 +106,17 @@ class LatestReleasesAppNotifier extends StateNotifier<AsyncValue<List<App>>> {
|
||||
},
|
||||
);
|
||||
|
||||
state = AsyncData(_fetchLocal());
|
||||
state = fetchLocal();
|
||||
}
|
||||
|
||||
List<App> _fetchLocal() {
|
||||
@override
|
||||
AsyncValue<List<App>> fetchLocal() {
|
||||
final model = ref.apps.findAllLocal();
|
||||
|
||||
if (model.isEmpty) {
|
||||
return AsyncLoading();
|
||||
}
|
||||
|
||||
final apps = model
|
||||
.where((a) => a.latestMetadata != null)
|
||||
.sortedByLatest
|
||||
@@ -123,7 +127,7 @@ class LatestReleasesAppNotifier extends StateNotifier<AsyncValue<List<App>>> {
|
||||
if (apps.isNotEmpty) {
|
||||
_oldestCreatedAt = apps.last.createdAt;
|
||||
}
|
||||
return apps;
|
||||
return AsyncData(apps);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user