diff --git a/.github/workflows/publish.yaml b/.github/workflows/publish.yaml deleted file mode 100644 index c46868b..0000000 --- a/.github/workflows/publish.yaml +++ /dev/null @@ -1,12 +0,0 @@ -name: Publish to pub.dev - -on: - push: - tags: - - '[0-9]+.[0-9]+.[0-9]+*' - -jobs: - publish: - uses: dart-lang/setup-dart/blob/main/.github/workflows/publish.yml - # with: - # working-directory: path/to/package/within/repository \ No newline at end of file diff --git a/CHANGELOG.md b/CHANGELOG.md index a29885c..8ab9dac 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ +## 0.5.4 +StateFlow watch added in experimental +- Added ChangeNotifierProvider.value +- Removed VMP bug +- stateFlow.watch(context) added in experimental + ## 0.5.3 -Fixed ViewModel Dispose major bug +Fixed ViewModel dispose major bug - ViewModel not disposing fixed - Responsive Example added diff --git a/README.md b/README.md index 0e5bae6..f4171b2 100644 --- a/README.md +++ b/README.md @@ -228,6 +228,14 @@ StateFlowBuilder( ) ``` + +### Experimental `StateFlow.watch(BuildContext)` +We can use `watch` with any `StateFlow` which observe the value change and update the ui. +This is similar to Provider's `context.watch<MyChangeNotifier>()`. +```dart +final value = context.vm<CustomViewModel>().myStateFlow.watch(context); +``` + ### StateFlowConsumer `StateFlowConsumer` is used to rebuild the widgets inside of it and call the listener. diff --git a/example/pubspec.lock b/example/pubspec.lock index 21c5097..9bc9b78 100644 --- a/example/pubspec.lock +++ b/example/pubspec.lock @@ -176,7 +176,7 @@ packages: path: ".." relative: true source: path - version: "0.5.3" + version: "0.5.4" sdks: dart: ">=2.18.6 <3.0.0" flutter: ">=1.17.0" diff --git a/lib/src/flow.dart b/lib/src/flow.dart index 2026dc8..fbd548e 100644 --- a/lib/src/flow.dart +++ b/lib/src/flow.dart @@ -27,7 +27,35 @@ class StateFlow<T> extends ChangeNotifier { /// If [notifyOnSameValue] is set to false, whenever you call `stateFlow.value = newValue` /// where newValue is same as current value, it will not notify listeners. by default it is set to true. - StateFlow(this._value, {this.notifyOnSameValue = true}); + StateFlow(this._value, {this.notifyOnSameValue = true}) { + addListener(_defaultListener); + } + + /// watch is experimental for now, it will rebuild the widget of context when value is changed or updated. + T watch(BuildContext context) { + contexts[context] = true; + return _value; + } + + final Map<BuildContext, bool> contexts = {}; + + @override + void dispose() { + contexts.clear(); + removeListener(_defaultListener); + super.dispose(); + } + + void _defaultListener() { + for (final context in contexts.keys) { + try { + if (context is Element) { + (context).markNeedsBuild(); + } + } catch (_) {} + } + contexts.clear(); + } } /// [MutableStateFlow] is inherited from [StateFlow]. It can change/update the value. diff --git a/lib/src/modified_provider.dart b/lib/src/modified_provider.dart index 448dbb8..9c780eb 100644 --- a/lib/src/modified_provider.dart +++ b/lib/src/modified_provider.dart @@ -12,6 +12,18 @@ class ChangeNotifierProvider<T extends ChangeNotifier> ChangeNotifierProvider( {super.key, required super.create, super.child, super.lazy}); + ChangeNotifierProvider.value({ + Key? key, + required T value, + TransitionBuilder? builder, + Widget? child, + }) : super.value( + key: key, + builder: builder, + value: value, + child: child, + ); + static F of<F extends ChangeNotifier>(BuildContext context, {bool listen = true}) => p.Provider.of<F>(context, listen: true); diff --git a/lib/src/view_model_provider.dart b/lib/src/view_model_provider.dart index ebfa513..a2d8841 100644 --- a/lib/src/view_model_provider.dart +++ b/lib/src/view_model_provider.dart @@ -1,22 +1,11 @@ import 'package:flutter/widgets.dart'; import 'package:provider/provider.dart'; -import 'package:provider/single_child_widget.dart'; import 'provider_single_child_widget.dart'; import 'view_model.dart'; /// [ViewModelProvider] is used to wrap the widget with your custom [ViewModel]. /// This requires [create] which accepts custom [ViewModel] and [child] Widget. -class VMP<T extends ViewModel> extends SingleChildStatelessWidget { - const VMP({super.key, super.child}); - - @override - Widget buildWithChild(BuildContext context, Widget? child) { - // TODO: implement buildWithChild - throw UnimplementedError(); - } -} - class ViewModelProvider<T extends ViewModel> extends Provider<T> with ProviderSingleChildWidget { ViewModelProvider( @@ -28,7 +17,6 @@ class ViewModelProvider<T extends ViewModel> extends Provider<T> : super(dispose: _dispose); static void _dispose<T extends ViewModel>(BuildContext context, T viewModel) { - debugPrint('provider dispose'); viewModel.dispose(); } diff --git a/pubspec.yaml b/pubspec.yaml index 5e8af02..9a7e5fc 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: view_model_x description: An Android similar state management package (StateFlow and SharedFlow with ViewModel) which helps to implement MVVM pattern easily. -version: 0.5.3 +version: 0.5.4 homepage: https://github.com/shubham-gupta-16/view_model_x repository: https://github.com/shubham-gupta-16/view_model_x issue_tracker: https://github.com/shubham-gupta-16/view_model_x/issues