Skip to content

Commit

Permalink
fix: Change AsyncCountValue to AsyncModelValue and add loading.
Browse files Browse the repository at this point in the history
  • Loading branch information
mathrunet committed Dec 22, 2023
1 parent a08f458 commit 82702bc
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 16 deletions.
26 changes: 18 additions & 8 deletions packages/katana_model/lib/src/async_count_value.dart
Original file line number Diff line number Diff line change
@@ -1,20 +1,30 @@
part of '/katana_model.dart';

/// Class for asynchronously retrieving values returned from [CollectionBase.count].
/// This class is used to asynchronously retrieve values returned from [CollectionBase.count], etc.
///
/// You can get the number of elements in the collection from [value].
///
/// [CollectionBase.count]から返される値を非同期で取得するためのクラスです
/// [CollectionBase.count]などから返される値を非同期で取得するためのクラスです
///
/// [value]からコレクションの要素数を取得できます。
class AsyncCountValue extends ChangeNotifier implements ValueListenable<int?> {
AsyncCountValue._(Future<int> future) {
future.then((value) {
_value = value;
notifyListeners();
});
class AsyncModelValue extends ChangeNotifier implements ValueListenable<int?> {
AsyncModelValue._(Future<int> future) : _loading = future {
_process();
}

Future<void> _process() async {
await _loading;
_value = value;
_loading = null;
notifyListeners();
}

/// You can wait for it to load.
///
/// 読み込みを待つことができます。
Future<int>? get loading => _loading;
Future<int>? _loading;

@override
int? get value => _value;
int? _value;
Expand Down
17 changes: 12 additions & 5 deletions packages/katana_model/lib/src/collection_base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -339,13 +339,20 @@ abstract class CollectionBase<TModel extends DocumentBase>

/// Get the number of elements in all collections existing in the database.
///
/// [AsyncCountValue] is returned, wait a moment and then retrieve the value from [AsyncCountValue.value].
/// [AsyncModelValue] is returned, wait a moment and then retrieve the value from [AsyncModelValue.value].
///
/// Normally, the value loaded the first time is retained, but if [reload] is set to `true`, it is loaded again.
///
/// データベース上に存在するすべてのコレクションの要素数を取得します。
///
/// [AsyncCountValue]が返されるので少し待ったあと[AsyncCountValue.value]から値を取得してください。
AsyncCountValue count() {
_count ??= AsyncCountValue._(_countRequest());
/// [AsyncModelValue]が返されるので少し待ったあと[AsyncModelValue.value]から値を取得してください。
///
/// 通常は初回に読み込んだ値が保持されますが[reload]`true`にすると再度読み込みを行います。
AsyncModelValue count({bool reload = false}) {
if (reload) {
_count = null;
}
_count ??= AsyncModelValue._(_countRequest());
return _count!;
}

Expand All @@ -360,7 +367,7 @@ abstract class CollectionBase<TModel extends DocumentBase>
return count;
}

AsyncCountValue? _count;
AsyncModelValue? _count;

/// {@macro model_transaction}
///
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -363,8 +363,12 @@ class FirestoreModelAdapter extends ModelAdapter
await FirebaseCore.initialize(options: options);
final snapshot = await Future.wait<AggregateQuerySnapshot>(
_collectionReference(
query.copyWith(query: query.query.remove(ModelQueryFilterType.limit)),
).map((reference) => reference.count().get()),
query.copyWith(
query: query.query.remove(ModelQueryFilterType.limit),
),
).map(
(reference) => reference.count().get(),
),
);
final res = snapshot.fold<int>(0, (p, e) => p + e.count);
return res;
Expand Down
2 changes: 1 addition & 1 deletion packages/katana_model_firestore/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ packages:
path: "../katana_model"
relative: true
source: path
version: "2.16.1"
version: "2.17.0"
lints:
dependency: transitive
description:
Expand Down

0 comments on commit 82702bc

Please sign in to comment.