Skip to content

Commit

Permalink
fix: Controller and Form can now also be used on Page.
Browse files Browse the repository at this point in the history
  • Loading branch information
mathrunet committed Apr 21, 2024
1 parent 3e692c2 commit 018f87d
Show file tree
Hide file tree
Showing 6 changed files with 164 additions and 261 deletions.
108 changes: 102 additions & 6 deletions packages/katana_scoped/lib/src/scoped_query.dart
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,7 @@ class _ScopedQueryImpl<Result, TRef extends Ref>
/// ```
/// {@endtemplate}
@immutable
class ScopedQuery<Result>
extends ScopedQueryBase<Result, AppScopedValueOrAppRef> {
class ScopedQuery<Result> extends ScopedQueryBase<Result, Ref> {
/// {@template scoped_query}
/// [ScopedQuery] makes it possible to define values globally and manage state individually and safely.
///
Expand Down Expand Up @@ -147,6 +146,25 @@ class ScopedQuery<Result>
});
}

/// [ScopedQuery] available at app scope.
///
/// Appスコープで利用可能な[ScopedQuery]
///
/// {@macro scoped_query}
class AppScopedQuery<Result>
extends ScopedQueryBase<Result, AppScopedValueOrAppRef> {
/// [ScopedQuery] available at app scope.
///
/// Appスコープで利用可能な[ScopedQuery]
///
/// {@macro scoped_query}
const AppScopedQuery(
super.provider, {
super.name,
super.autoDisposeWhenUnreferenced = false,
});
}

/// [ScopedQuery] available at page scope.
///
/// ページスコープで利用可能な[ScopedQuery]
Expand Down Expand Up @@ -269,7 +287,7 @@ class _ChangeNotifierScopedQueryImpl<Result, TRef extends Ref>
/// {@endtemplate}
@immutable
class ChangeNotifierScopedQuery<Result extends Listenable?>
extends ChangeNotifierScopedQueryBase<Result, AppScopedValueOrAppRef> {
extends ChangeNotifierScopedQueryBase<Result, Ref> {
/// {@template change_notifier_scoped_query}
/// [ChangeNotifierScopedQuery] makes it possible to define values globally and manage states individually and safely.
///
Expand Down Expand Up @@ -315,6 +333,26 @@ class ChangeNotifierScopedQuery<Result extends Listenable?>
});
}

/// [ChangeNotifierScopedQuery] available at app scope.
///
/// Appスコープで利用可能な[ChangeNotifierScopedQuery]
///
/// {@macro change_notifier_scoped_query}
@immutable
class ChangeNotifierAppScopedQuery<Result extends Listenable?>
extends ChangeNotifierScopedQueryBase<Result, AppScopedValueOrAppRef> {
/// [ChangeNotifierScopedQuery] available at app scope.
///
/// Appスコープで利用可能な[ChangeNotifierScopedQuery]
///
/// {@macro change_notifier_scoped_query}
const ChangeNotifierAppScopedQuery(
super.provider, {
super.name,
super.autoDisposeWhenUnreferenced,
});
}

/// [ChangeNotifierScopedQuery] available at page scope.
///
/// ページスコープで利用可能な[ChangeNotifierScopedQuery]
Expand Down Expand Up @@ -410,7 +448,7 @@ abstract class ScopedQueryFamilyBase<Result, TRef extends Ref, Param> {
/// {@macro scoped_query}
@immutable
class ScopedQueryFamily<Result, Param>
extends ScopedQueryFamilyBase<Result, AppScopedValueOrAppRef, Param> {
extends ScopedQueryFamilyBase<Result, Ref, Param> {
/// You can pass one parameter [ScopedQuery].
///
/// パラメーターを一つ渡すことができる[ScopedQuery]
Expand All @@ -433,6 +471,36 @@ class ScopedQueryFamily<Result, Param>
);
}

/// You can pass one parameter [AppScopedQuery].
///
/// パラメーターを一つ渡すことができる[AppScopedQuery]
///
/// {@macro scoped_query}
@immutable
class AppScopedQueryFamily<Result, Param>
extends ScopedQueryFamilyBase<Result, AppScopedValueOrAppRef, Param> {
/// You can pass one parameter [AppScopedQuery].
///
/// パラメーターを一つ渡すことができる[AppScopedQuery]
///
/// {@macro scoped_query}
const AppScopedQueryFamily(
super.provider, {
super.name,
super.autoDisposeWhenUnreferenced = false,
});

/// By passing [param], the corresponding [ScopedQuery] is returned.
///
/// [param]を渡すことで対応した[ScopedQuery]を返します。
@override
AppScopedQuery<Result> call(Param param) => AppScopedQuery(
(ref) => provider(ref, param),
name: "${_name ?? hashCode}#${param.hashCode}",
autoDisposeWhenUnreferenced: autoDisposeWhenUnreferenced,
);
}

/// You can pass one parameter [PageScopedQuery].
///
/// パラメーターを一つ渡すことができる[PageScopedQuery]
Expand Down Expand Up @@ -530,8 +598,7 @@ class ChangeNotifierScopedQueryFamilyBase<
/// {@macro change_notifier_scoped_query}
@immutable
class ChangeNotifierScopedQueryFamily<Result extends Listenable?, Param>
extends ChangeNotifierScopedQueryFamilyBase<Result, AppScopedValueOrAppRef,
Param> {
extends ChangeNotifierScopedQueryFamilyBase<Result, Ref, Param> {
/// You can pass one parameter [ChangeNotifierScopedQuery].
///
/// パラメーターを一つ渡すことができる[ChangeNotifierScopedQuery]
Expand All @@ -552,6 +619,35 @@ class ChangeNotifierScopedQueryFamily<Result extends Listenable?, Param>
);
}

/// You can pass one parameter [ChangeNotifierAppScopedQuery].
///
/// パラメーターを一つ渡すことができる[ChangeNotifierAppScopedQuery]
///
/// {@macro change_notifier_scoped_query}
@immutable
class ChangeNotifierAppScopedQueryFamily<Result extends Listenable?, Param>
extends ChangeNotifierScopedQueryFamilyBase<Result, AppScopedValueOrAppRef,
Param> {
/// You can pass one parameter [ChangeNotifierAppScopedQuery].
///
/// パラメーターを一つ渡すことができる[ChangeNotifierAppScopedQuery]
///
/// {@macro change_notifier_scoped_query}
const ChangeNotifierAppScopedQueryFamily(
super.provider, {
super.name,
super.autoDisposeWhenUnreferenced,
});

@override
ChangeNotifierAppScopedQuery<Result> call(Param param) =>
ChangeNotifierAppScopedQuery<Result>(
(ref) => provider(ref, param),
name: "${_name ?? hashCode}#${param.hashCode}",
autoDisposeWhenUnreferenced: autoDisposeWhenUnreferenced,
);
}

/// You can pass one parameter [ChangeNotifierPageScopedQuery].
///
/// パラメーターを一つ渡すことができる[ChangeNotifierPageScopedQuery]
Expand Down
3 changes: 2 additions & 1 deletion packages/katana_scoped/lib/src/scoped_ref.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ part of '/katana_scoped.dart';
///
/// [scopedValueContainer]を指定すると独自の[ScopedValueContainer]を利用可能です。テスト等でご利用ください。
@immutable
class AppRef implements Ref, AppScopedValueOrAppRef {
class AppRef
implements Ref, PageOrAppScopedValueOrAppRef, AppScopedValueOrAppRef {
/// [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
49 changes: 37 additions & 12 deletions packages/katana_scoped/lib/src/scoped_value_ref.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ part of '/katana_scoped.dart';
/// ```
@immutable
class AppScopedValueRef extends ScopedValueRef
implements AppScopedValueOrAppRef {
implements AppScopedValueOrAppRef, PageOrAppScopedValueOrAppRef {
const AppScopedValueRef._({
required super.listener,
required super.context,
Expand Down Expand Up @@ -53,7 +53,7 @@ class AppScopedValueRef extends ScopedValueRef
/// ```
@immutable
class PageScopedValueRef extends ScopedValueRef
implements PageOrWidgetScopedValueRef {
implements PageOrAppScopedValueOrAppRef, PageOrWidgetScopedValueRef {
const PageScopedValueRef._({
required super.listener,
required super.context,
Expand Down Expand Up @@ -90,16 +90,16 @@ class WidgetScopedValueRef extends ScopedValueRef
}) : super._();
}

/// [ScopedValueRef] of the Page or Widget (whose state can be monitored).
/// [AppRef] or [AppScopedValueRef] to store state in App scope.
///
/// This can be extended with an extension to add a method to handle [ScopedValue] that stores a value per page.
/// This can be extended with an extension to add a method to handle [Ref] that stores the value for each app.
///
/// PageもしくはWidget(状態を監視可能なもの)の[ScopedValueRef]
/// Appスコープで状態を保存する[AppRef]もしくは[AppScopedValueRef]
///
/// これをextensionで拡張することでページごとに値を保存する[ScopedValue]を扱うメソッドを追加することができます。
/// これをextensionで拡張することでアプリごとに値を保存する[Ref]を扱うメソッドを追加することができます。
///
/// ```dart
/// extension RefWatchExtensions on PageOrWidgetScopedValueRef {
/// extension RefWatchExtensions on AppScopedValueOrAppRef {
/// T watch<T extends Listenable>(
/// T Function() callback, {
/// List<Object> keys = const [],
Expand All @@ -112,18 +112,19 @@ class WidgetScopedValueRef extends ScopedValueRef
/// }
/// ```
@immutable
abstract class PageOrWidgetScopedValueRef implements ScopedValueRef {}
abstract class AppScopedValueOrAppRef
implements PageOrAppScopedValueOrAppRef, Ref {}

/// [AppRef] or [AppScopedValueRef] to store state in App scope.
/// [AppScopedValueOrAppRef] that stores state in page scope and App scope.
///
/// This can be extended with an extension to add a method to handle [Ref] that stores the value for each app.
///
/// Appスコープで状態を保存する[AppRef]もしくは[AppScopedValueRef]
/// ページスコープおよびAppスコープで状態を保存する[AppScopedValueOrAppRef]
///
/// これをextensionで拡張することでアプリごとに値を保存する[Ref]を扱うメソッドを追加することができます。
///
/// ```dart
/// extension RefWatchExtensions on AppScopedValueOrAppRef {
/// extension RefWatchExtensions on PageOrAppScopedValueOrAppRef {
/// T watch<T extends Listenable>(
/// T Function() callback, {
/// List<Object> keys = const [],
Expand All @@ -136,7 +137,31 @@ abstract class PageOrWidgetScopedValueRef implements ScopedValueRef {}
/// }
/// ```
@immutable
abstract class AppScopedValueOrAppRef implements Ref {}
abstract class PageOrAppScopedValueOrAppRef implements Ref {}

/// [ScopedValueRef] of the Page or Widget (whose state can be monitored).
///
/// This can be extended with an extension to add a method to handle [ScopedValue] that stores a value per page.
///
/// PageもしくはWidget(状態を監視可能なもの)の[ScopedValueRef]
///
/// これをextensionで拡張することでページごとに値を保存する[ScopedValue]を扱うメソッドを追加することができます。
///
/// ```dart
/// extension RefWatchExtensions on PageOrWidgetScopedValueRef {
/// T watch<T extends Listenable>(
/// T Function() callback, {
/// List<Object> keys = const [],
/// }) {
/// return getScopedValue<T, _WatchValue<T>>(
/// () => _WatchValue<T>(callback: callback, keys: keys),
/// listen: true,
/// );
/// }
/// }
/// ```
@immutable
abstract class PageOrWidgetScopedValueRef implements ScopedValueRef {}

/// [Ref] associated with the widget.
///
Expand Down

0 comments on commit 018f87d

Please sign in to comment.