mirror of
https://github.com/kodjodevf/mangayomi.git
synced 2026-01-11 22:40:36 +00:00
refactor(statistics): update statistics provider to use functional provider pattern
This commit is contained in:
parent
4e9af30e8e
commit
e48c475fcb
5 changed files with 74 additions and 91 deletions
|
|
@ -64,7 +64,7 @@ final class UpdateMangaDetailProvider
|
|||
}
|
||||
}
|
||||
|
||||
String _$updateMangaDetailHash() => r'd056f9ff4213f437039edb67786c96dfda99dae0';
|
||||
String _$updateMangaDetailHash() => r'37da5f23f30126d15cedfaf42087f9ce11c3fc26';
|
||||
|
||||
final class UpdateMangaDetailFamily extends $Family
|
||||
with
|
||||
|
|
|
|||
|
|
@ -43,7 +43,7 @@ final class TotalChapterCacheSizeStateProvider
|
|||
}
|
||||
|
||||
String _$totalChapterCacheSizeStateHash() =>
|
||||
r'bb403516c1d94652146d0a38101b51ffe8ea72f8';
|
||||
r'6e92eec01cc21fbea3996d220c0b2edaadec3786';
|
||||
|
||||
abstract class _$TotalChapterCacheSizeState extends $Notifier<String> {
|
||||
String build();
|
||||
|
|
|
|||
|
|
@ -23,51 +23,51 @@ class StatisticsData {
|
|||
}
|
||||
|
||||
@riverpod
|
||||
class StatisticsState extends _$StatisticsState {
|
||||
@override
|
||||
Future<StatisticsData> build(ItemType itemType) async {
|
||||
final items = await isar.mangas
|
||||
.filter()
|
||||
.idIsNotNull()
|
||||
.favoriteEqualTo(true)
|
||||
.itemTypeEqualTo(itemType)
|
||||
.findAll();
|
||||
Future<StatisticsData> getStatistics(
|
||||
Ref ref, {
|
||||
required ItemType itemType,
|
||||
}) async {
|
||||
final items = await isar.mangas
|
||||
.filter()
|
||||
.idIsNotNull()
|
||||
.favoriteEqualTo(true)
|
||||
.itemTypeEqualTo(itemType)
|
||||
.findAll();
|
||||
|
||||
final chapters = await isar.chapters
|
||||
.filter()
|
||||
.idIsNotNull()
|
||||
.manga((q) => q.favoriteEqualTo(true).itemTypeEqualTo(itemType))
|
||||
.findAll();
|
||||
final chapters = await isar.chapters
|
||||
.filter()
|
||||
.idIsNotNull()
|
||||
.manga((q) => q.favoriteEqualTo(true).itemTypeEqualTo(itemType))
|
||||
.findAll();
|
||||
|
||||
final downloadedCount = await isar.downloads
|
||||
.filter()
|
||||
.idIsNotNull()
|
||||
.chapter((q) => q.manga((m) => m.itemTypeEqualTo(itemType)))
|
||||
.chapter((q) => q.manga((m) => m.favoriteEqualTo(true)))
|
||||
.isDownloadEqualTo(true)
|
||||
.count();
|
||||
final downloadedCount = await isar.downloads
|
||||
.filter()
|
||||
.idIsNotNull()
|
||||
.chapter((q) => q.manga((m) => m.itemTypeEqualTo(itemType)))
|
||||
.chapter((q) => q.manga((m) => m.favoriteEqualTo(true)))
|
||||
.isDownloadEqualTo(true)
|
||||
.count();
|
||||
|
||||
final totalItems = items.length;
|
||||
final totalChapters = chapters.length;
|
||||
final readChapters = chapters.where((c) => c.isRead ?? false).length;
|
||||
final totalItems = items.length;
|
||||
final totalChapters = chapters.length;
|
||||
final readChapters = chapters.where((c) => c.isRead ?? false).length;
|
||||
|
||||
int completedItems = 0;
|
||||
for (var item in items) {
|
||||
if (item.status == Status.completed) {
|
||||
final itemChapters = item.chapters.toList();
|
||||
if (itemChapters.isNotEmpty &&
|
||||
itemChapters.every((element) => element.isRead ?? false)) {
|
||||
completedItems++;
|
||||
}
|
||||
int completedItems = 0;
|
||||
for (var item in items) {
|
||||
if (item.status == Status.completed) {
|
||||
final itemChapters = item.chapters.toList();
|
||||
if (itemChapters.isNotEmpty &&
|
||||
itemChapters.every((element) => element.isRead ?? false)) {
|
||||
completedItems++;
|
||||
}
|
||||
}
|
||||
|
||||
return StatisticsData(
|
||||
totalItems: totalItems,
|
||||
totalChapters: totalChapters,
|
||||
readChapters: readChapters,
|
||||
completedItems: completedItems,
|
||||
downloadedItems: downloadedCount,
|
||||
);
|
||||
}
|
||||
|
||||
return StatisticsData(
|
||||
totalItems: totalItems,
|
||||
totalChapters: totalChapters,
|
||||
readChapters: readChapters,
|
||||
completedItems: completedItems,
|
||||
downloadedItems: downloadedCount,
|
||||
);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,47 +9,53 @@ part of 'statistics_provider.dart';
|
|||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||
// ignore_for_file: type=lint, type=warning
|
||||
|
||||
@ProviderFor(StatisticsState)
|
||||
const statisticsStateProvider = StatisticsStateFamily._();
|
||||
@ProviderFor(getStatistics)
|
||||
const getStatisticsProvider = GetStatisticsFamily._();
|
||||
|
||||
final class StatisticsStateProvider
|
||||
extends $NotifierProvider<StatisticsState, void> {
|
||||
const StatisticsStateProvider._({
|
||||
required StatisticsStateFamily super.from,
|
||||
final class GetStatisticsProvider
|
||||
extends
|
||||
$FunctionalProvider<
|
||||
AsyncValue<StatisticsData>,
|
||||
StatisticsData,
|
||||
FutureOr<StatisticsData>
|
||||
>
|
||||
with $FutureModifier<StatisticsData>, $FutureProvider<StatisticsData> {
|
||||
const GetStatisticsProvider._({
|
||||
required GetStatisticsFamily super.from,
|
||||
required ItemType super.argument,
|
||||
}) : super(
|
||||
retry: null,
|
||||
name: r'statisticsStateProvider',
|
||||
name: r'getStatisticsProvider',
|
||||
isAutoDispose: true,
|
||||
dependencies: null,
|
||||
$allTransitiveDependencies: null,
|
||||
);
|
||||
|
||||
@override
|
||||
String debugGetCreateSourceHash() => _$statisticsStateHash();
|
||||
String debugGetCreateSourceHash() => _$getStatisticsHash();
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
return r'statisticsStateProvider'
|
||||
return r'getStatisticsProvider'
|
||||
''
|
||||
'($argument)';
|
||||
}
|
||||
|
||||
@$internal
|
||||
@override
|
||||
StatisticsState create() => StatisticsState();
|
||||
$FutureProviderElement<StatisticsData> $createElement(
|
||||
$ProviderPointer pointer,
|
||||
) => $FutureProviderElement(pointer);
|
||||
|
||||
/// {@macro riverpod.override_with_value}
|
||||
Override overrideWithValue(void value) {
|
||||
return $ProviderOverride(
|
||||
origin: this,
|
||||
providerOverride: $SyncValueProvider<void>(value),
|
||||
);
|
||||
@override
|
||||
FutureOr<StatisticsData> create(Ref ref) {
|
||||
final argument = this.argument as ItemType;
|
||||
return getStatistics(ref, itemType: argument);
|
||||
}
|
||||
|
||||
@override
|
||||
bool operator ==(Object other) {
|
||||
return other is StatisticsStateProvider && other.argument == argument;
|
||||
return other is GetStatisticsProvider && other.argument == argument;
|
||||
}
|
||||
|
||||
@override
|
||||
|
|
@ -58,44 +64,22 @@ final class StatisticsStateProvider
|
|||
}
|
||||
}
|
||||
|
||||
String _$statisticsStateHash() => r'81e1957e0e39a9863a8e7d0e1dc565c4eb0e6f9a';
|
||||
String _$getStatisticsHash() => r'f4a11dfa53b9560da765b1822fadc758a0a23cba';
|
||||
|
||||
final class StatisticsStateFamily extends $Family
|
||||
with $ClassFamilyOverride<StatisticsState, void, void, void, ItemType> {
|
||||
const StatisticsStateFamily._()
|
||||
final class GetStatisticsFamily extends $Family
|
||||
with $FunctionalFamilyOverride<FutureOr<StatisticsData>, ItemType> {
|
||||
const GetStatisticsFamily._()
|
||||
: super(
|
||||
retry: null,
|
||||
name: r'statisticsStateProvider',
|
||||
name: r'getStatisticsProvider',
|
||||
dependencies: null,
|
||||
$allTransitiveDependencies: null,
|
||||
isAutoDispose: true,
|
||||
);
|
||||
|
||||
StatisticsStateProvider call(ItemType itemType) =>
|
||||
StatisticsStateProvider._(argument: itemType, from: this);
|
||||
GetStatisticsProvider call({required ItemType itemType}) =>
|
||||
GetStatisticsProvider._(argument: itemType, from: this);
|
||||
|
||||
@override
|
||||
String toString() => r'statisticsStateProvider';
|
||||
}
|
||||
|
||||
abstract class _$StatisticsState extends $Notifier<void> {
|
||||
late final _$args = ref.$arg as ItemType;
|
||||
ItemType get itemType => _$args;
|
||||
|
||||
void build(ItemType itemType);
|
||||
@$mustCallSuper
|
||||
@override
|
||||
void runBuild() {
|
||||
build(_$args);
|
||||
final ref = this.ref as $Ref<void, void>;
|
||||
final element =
|
||||
ref.element
|
||||
as $ClassProviderElement<
|
||||
AnyNotifier<void, void>,
|
||||
void,
|
||||
Object?,
|
||||
Object?
|
||||
>;
|
||||
element.handleValue(ref, null);
|
||||
}
|
||||
String toString() => r'getStatisticsProvider';
|
||||
}
|
||||
|
|
|
|||
|
|
@ -75,8 +75,7 @@ class _StatisticsScreenState extends ConsumerState<StatisticsScreen>
|
|||
|
||||
Widget _buildStatisticsTab({required ItemType itemType}) {
|
||||
final l10n = context.l10n;
|
||||
// final stats = ref.watch(statisticsStateProvider(itemType).notifier);
|
||||
final stats = ref.watch(statisticsStateProvider(itemType));
|
||||
final stats = ref.watch(getStatisticsProvider(itemType: itemType));
|
||||
|
||||
final title = switch (itemType) {
|
||||
ItemType.manga => l10n.manga,
|
||||
|
|
|
|||
Loading…
Reference in a new issue