Skip to content

Commit

Permalink
fix: Changed to be able to track states managed by ScopedValueContainer.
Browse files Browse the repository at this point in the history
  • Loading branch information
mathrunet committed Feb 7, 2023
1 parent eb671ff commit 559179f
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 27 deletions.
16 changes: 7 additions & 9 deletions packages/katana_scoped/example/pubspec.lock
Expand Up @@ -348,20 +348,19 @@ packages:
source: hosted
version: "6.6.1"
katana:
dependency: transitive
dependency: "direct overridden"
description:
name: katana
sha256: "9a3ec047c35ec88e027139e88ce45202b022dd2111b38848c85159101b0028ce"
url: "https://pub.dev"
source: hosted
path: "../../katana"
relative: true
source: path
version: "1.0.5"
katana_scoped:
dependency: "direct main"
description:
path: ".."
relative: true
source: path
version: "1.7.8"
version: "1.7.9"
lints:
dependency: transitive
description:
Expand Down Expand Up @@ -579,10 +578,10 @@ packages:
dependency: transitive
description:
name: tint
sha256: d856019547532d4ea24171f554b319081c004c37741e7946eae30cb09f24e1c7
sha256: "9652d9a589f4536d5e392cf790263d120474f15da3cf1bee7f1fdb31b4de5f46"
url: "https://pub.dev"
source: hosted
version: "2.0.0"
version: "2.0.1"
typed_data:
dependency: transitive
description:
Expand Down Expand Up @@ -633,4 +632,3 @@ packages:
version: "3.1.1"
sdks:
dart: ">=2.19.0 <4.0.0"
flutter: ">=2.0.0"
8 changes: 4 additions & 4 deletions packages/katana_scoped/lib/src/scoped_ref.dart
@@ -1,6 +1,6 @@
part of katana_scoped;

/// ScopedValue] in the scope of the application.
/// [ScopedValue] in the scope of the application.
///
/// Since there is no object to monitor the status, we only read [ScopedValue], but if we define the value globally, we can call it from any timing.
///
Expand All @@ -17,7 +17,7 @@ part of katana_scoped;
/// [scopedValueContainer]を指定すると独自の[ScopedValueContainer]を利用可能です。テスト等でご利用ください。
@immutable
class AppRef implements Ref {
/// ScopedValue] in the scope of the application.
/// [ScopedValue] in the scope of the application.
///
/// Since there is no object to monitor the status, we only read [ScopedValue], but if we define the value globally, we can call it from any timing.
///
Expand Down Expand Up @@ -79,8 +79,8 @@ class AppRef implements Ref {
/// すべてのアプリスコープの状態を破棄してクリアします。
///
/// ログアウト時など一旦アプリをリセットする場合にご利用ください。
void dispose() {
_scopedValueContainer.dispose();
void reset() {
_scopedValueContainer.reset();
}
}

Expand Down
46 changes: 42 additions & 4 deletions packages/katana_scoped/lib/src/scoped_value_container.dart
Expand Up @@ -6,25 +6,48 @@ part of katana_scoped;
///
/// A database of [ScopedValue] is maintained internally, and the corresponding [ScopedValueState] can be read and edited by [getScopedValueState].
///
/// It inherits from [ChangeNotifier] and can monitor changes in its contents.
///
/// The contents cannot be changed directly, but can be viewed at [data].
///
/// [ScopedValue]を保存するためのコンテナオブジェクト。
///
/// 通常は[ScopedValueListener]を介して呼び出されます。
///
/// 内部に[ScopedValue]のデータベースを保持しており、[getScopedValueState][ScopedValue]に対応する[ScopedValueState]を読込み編集を加えることが可能です。
class ScopedValueContainer {
///
/// [ChangeNotifier]を継承しており、中身の変更を監視できます。
///
/// 中身は直接変更することができませんが[data]で中身を確認することが可能です。
class ScopedValueContainer extends ChangeNotifier {
/// Container object for storing [ScopedValue].
///
/// It is usually called via [ScopedValueListener].
///
/// A database of [ScopedValue] is maintained internally, and the corresponding [ScopedValueState] can be read and edited by [getScopedValueState].
///
/// It inherits from [ChangeNotifier] and can monitor changes in its contents.
///
/// The contents cannot be changed directly, but can be viewed at [data].
///
/// [ScopedValue]を保存するためのコンテナオブジェクト。
///
/// 通常は[ScopedValueListener]を介して呼び出されます。
///
/// 内部に[ScopedValue]のデータベースを保持しており、[getScopedValueState][ScopedValue]に対応する[ScopedValueState]を読込み編集を加えることが可能です。
///
/// [ChangeNotifier]を継承しており、中身の変更を監視できます。
///
/// 中身は直接変更することができませんが[data]で中身を確認することが可能です。
ScopedValueContainer();

/// It is possible to check the contents of [ScopedValueContainer].
///
/// [ScopedValueContainer]の中身を確認することが可能です。
Map<String, ScopedValueState> get data {
return Map.from(_data);
}

final Map<String, ScopedValueState> _data = {};

/// By passing a callback that returns [TScopedValue] to [provider], it creates a state for that [ScopedValue] and performs the appropriate processing, returning [ScopedValueState].
Expand Down Expand Up @@ -65,6 +88,7 @@ class ScopedValueContainer {
state._setValue(provider.call());
onInitOrUpdate?.call(state);
found.didUpdateValue(oldValue);
notifyListeners();
return state;
} else {
final value = provider.call();
Expand All @@ -74,6 +98,7 @@ class ScopedValueContainer {
onInitOrUpdate?.call(state);
state.initValue();
_data[key] = state;
notifyListeners();
return state;
}
}
Expand Down Expand Up @@ -119,17 +144,30 @@ class ScopedValueContainer {
}
}

/// Called when [ScopedValueContainer] is destroyed.
/// The contents of [ScopedValueContainer] are discarded and reset once.
///
/// ScopedValueState.dispose] of the retained state is executed.
///
/// [ScopedValueContainer]が破棄される歳に呼ばれます
/// [ScopedValueContainer]の中身を破棄し一旦リセットします
///
/// 保持している状態の[ScopedValueState.dispose]が実行されます。
void dispose() {
void reset() {
for (final val in _data.values) {
val.dispose();
}
_data.clear();
}

/// Called when [ScopedValueContainer] is destroyed.
///
/// ScopedValueState.dispose] of the retained state is executed.
///
/// [ScopedValueContainer]が破棄される歳に呼ばれます。
///
/// 保持している状態の[ScopedValueState.dispose]が実行されます。
@override
void dispose() {
super.dispose();
reset();
}
}
15 changes: 7 additions & 8 deletions packages/katana_scoped/pubspec.lock
Expand Up @@ -85,10 +85,10 @@ packages:
dependency: transitive
description:
name: coverage
sha256: "961c4aebd27917269b1896382c7cb1b1ba81629ba669ba09c27a7e5710ec9040"
sha256: "2fb815080e44a09b85e0f2ca8a820b15053982b2e714b59267719e8a9ff17097"
url: "https://pub.dev"
source: hosted
version: "1.6.2"
version: "1.6.3"
crypto:
dependency: transitive
description:
Expand Down Expand Up @@ -201,10 +201,9 @@ packages:
katana:
dependency: "direct main"
description:
name: katana
sha256: "9a3ec047c35ec88e027139e88ce45202b022dd2111b38848c85159101b0028ce"
url: "https://pub.dev"
source: hosted
path: "../katana"
relative: true
source: path
version: "1.0.5"
lints:
dependency: transitive
Expand Down Expand Up @@ -439,10 +438,10 @@ packages:
dependency: transitive
description:
name: tint
sha256: d856019547532d4ea24171f554b319081c004c37741e7946eae30cb09f24e1c7
sha256: "9652d9a589f4536d5e392cf790263d120474f15da3cf1bee7f1fdb31b4de5f46"
url: "https://pub.dev"
source: hosted
version: "2.0.0"
version: "2.0.1"
typed_data:
dependency: transitive
description:
Expand Down
4 changes: 2 additions & 2 deletions packages/katana_scoped/test/scoped_test.dart
Expand Up @@ -8,7 +8,7 @@ import 'package:test/test.dart';
import 'package:katana_scoped/katana_scoped.dart';

void main() {
test("Dispose Test", () async {
test("Reset Test", () async {
WidgetsFlutterBinding.ensureInitialized();
final container = ScopedValueContainer();
final appRef = AppRef(scopedValueContainer: container);
Expand All @@ -18,7 +18,7 @@ void main() {
final n2 = appRef.watch((ref) => ValueNotifier(0));
expect(n2.value, 1);
expect(n1 == n2, true);
appRef.dispose();
appRef.reset();
final n3 = appRef.watch((ref) => ValueNotifier(2));
expect(n3.value, 2);
expect(n1 != n3, true);
Expand Down

0 comments on commit 559179f

Please sign in to comment.