From 62dd8a2e4832844e32fa8f2e373783a6db3c86b3 Mon Sep 17 00:00:00 2001 From: Shubham Gupta Date: Wed, 11 Jan 2023 15:46:39 +0530 Subject: [PATCH 1/3] PostFrameCallback added --- .../view_model/second_view_model.dart | 7 ++++++- lib/src/view_model.dart | 9 +++++++++ lib/src/view_model_provider.dart | 5 +++++ 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/example/lib/multiple_view_models_example/view_model/second_view_model.dart b/example/lib/multiple_view_models_example/view_model/second_view_model.dart index 53a6f92..a6888a9 100644 --- a/example/lib/multiple_view_models_example/view_model/second_view_model.dart +++ b/example/lib/multiple_view_models_example/view_model/second_view_model.dart @@ -1,6 +1,6 @@ import 'package:view_model_x/view_model_x.dart'; -class SecondViewModel extends ViewModel { +class SecondViewModel extends ViewModel with PostFrameCallback { // initialize SharedFlow final _messageSharedFlow = MutableSharedFlow(); @@ -15,4 +15,9 @@ class SecondViewModel extends ViewModel { void dispose() { _messageSharedFlow.dispose(); } + + @override + void onPostFrameCallback(Duration timestamp) { + _messageSharedFlow.emit("onPostFrameCallback from SecondViewModel"); + } } diff --git a/lib/src/view_model.dart b/lib/src/view_model.dart index c3f5abe..a0ba029 100644 --- a/lib/src/view_model.dart +++ b/lib/src/view_model.dart @@ -1,5 +1,14 @@ /// [ViewModel] is an abstract class with an abstract method [dispose]. abstract class ViewModel { + + void init(){} /// used to dispose all the flows. void dispose(); } + +/// This will help to easily implement PostFrameCallback event into ViewModel. +/// [onPostFrameCallback] will trigger after the ui build completed. +abstract class PostFrameCallback { + + void onPostFrameCallback(Duration timestamp); +} diff --git a/lib/src/view_model_provider.dart b/lib/src/view_model_provider.dart index 1e687a8..47f2735 100644 --- a/lib/src/view_model_provider.dart +++ b/lib/src/view_model_provider.dart @@ -44,6 +44,11 @@ class _ViewModelProviderState void initState() { _viewModel = widget.create(context); super.initState(); + _viewModel.init(); + if (_viewModel is PostFrameCallback) { + WidgetsBinding.instance + .addPostFrameCallback((_viewModel as PostFrameCallback).onPostFrameCallback); + } } @override From b5798233adacb009941b10b3eeb1aba7f4424413 Mon Sep 17 00:00:00 2001 From: Shubham Gupta Date: Wed, 11 Jan 2023 15:58:39 +0530 Subject: [PATCH 2/3] Documentation and version updated --- CHANGELOG.md | 5 +++++ README.md | 25 ++++++++++++++++++++++++- example/pubspec.lock | 2 +- pubspec.yaml | 2 +- 4 files changed, 31 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a87ef12..e798827 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +##0.4.1 +PostFrameCallback added +- ViewModel can access onPostFrameCallback event +- ViewModel can access init event (initState) + ## 0.4.0 Provider removed, Nested added - MultiFlowListener added diff --git a/README.md b/README.md index 6aa3d83..26ed374 100644 --- a/README.md +++ b/README.md @@ -129,6 +129,27 @@ class CustomViewModel extends ViewModel { } ``` +### PostFrameCallback with ViewModel +This will help to get `onPostFrameCallback` event inside `ViewModel` easily. +By using `PostFrameCallback`, we can go from: + +```dart +WidgetsBinding.instance.addPostFrameCallback((_){ + // do stuffs here +}) +``` +to +```dart +class CustomViewModel extends ViewModel with PostFrameCallback { + //... + + @override + void onPostFrameCallback(Duration timestamp) { + // do stuffs here + } +} +``` + ### MutableStateFlow and StateFlow `MutableStateFlow` is inherited from `StateFlow`. It stores value and notify listeners whenever it changes. It can change/update the value. @@ -173,7 +194,7 @@ SharedFlow get mySharedFlow => _mySharedFlow; _myStateFlow.emit("Hello from ViewModel!"); // listeners were automatically notified ``` -## ViewModel Flutter Widgets +## Integrate ViewModel Into Flutter Widget ### ViewModelProvider @@ -232,6 +253,8 @@ OR context.vm() ``` +## Builder, Listener, and Consumer Flutter Widgets + ### StateFlowBuilder `StateFlowBuilder` is used to rebuild the widgets inside of it. diff --git a/example/pubspec.lock b/example/pubspec.lock index 2c51620..8a4b309 100644 --- a/example/pubspec.lock +++ b/example/pubspec.lock @@ -169,7 +169,7 @@ packages: path: ".." relative: true source: path - version: "0.4.0" + version: "0.4.1" sdks: dart: ">=2.18.6 <3.0.0" flutter: ">=1.17.0" diff --git a/pubspec.yaml b/pubspec.yaml index 79f3eb2..9e02f95 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.4.0 +version: 0.4.1 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 From 16020b52d10a4d65fa04557da73ba7f3079bf46c Mon Sep 17 00:00:00 2001 From: Shubham Gupta Date: Wed, 11 Jan 2023 15:59:23 +0530 Subject: [PATCH 3/3] Format done --- lib/src/view_model.dart | 3 +-- lib/src/view_model_provider.dart | 4 ++-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/lib/src/view_model.dart b/lib/src/view_model.dart index a0ba029..bdf9d01 100644 --- a/lib/src/view_model.dart +++ b/lib/src/view_model.dart @@ -1,7 +1,7 @@ /// [ViewModel] is an abstract class with an abstract method [dispose]. abstract class ViewModel { + void init() {} - void init(){} /// used to dispose all the flows. void dispose(); } @@ -9,6 +9,5 @@ abstract class ViewModel { /// This will help to easily implement PostFrameCallback event into ViewModel. /// [onPostFrameCallback] will trigger after the ui build completed. abstract class PostFrameCallback { - void onPostFrameCallback(Duration timestamp); } diff --git a/lib/src/view_model_provider.dart b/lib/src/view_model_provider.dart index 47f2735..19eec3c 100644 --- a/lib/src/view_model_provider.dart +++ b/lib/src/view_model_provider.dart @@ -46,8 +46,8 @@ class _ViewModelProviderState super.initState(); _viewModel.init(); if (_viewModel is PostFrameCallback) { - WidgetsBinding.instance - .addPostFrameCallback((_viewModel as PostFrameCallback).onPostFrameCallback); + WidgetsBinding.instance.addPostFrameCallback( + (_viewModel as PostFrameCallback).onPostFrameCallback); } }