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<int> 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<CustomViewModel>() ``` +## Builder, Listener, and Consumer Flutter Widgets + ### StateFlowBuilder `StateFlowBuilder` is used to rebuild the widgets inside of it. 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<String>(); @@ -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/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/lib/src/view_model.dart b/lib/src/view_model.dart index c3f5abe..bdf9d01 100644 --- a/lib/src/view_model.dart +++ b/lib/src/view_model.dart @@ -1,5 +1,13 @@ /// [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..19eec3c 100644 --- a/lib/src/view_model_provider.dart +++ b/lib/src/view_model_provider.dart @@ -44,6 +44,11 @@ class _ViewModelProviderState<T extends ViewModel> void initState() { _viewModel = widget.create(context); super.initState(); + _viewModel.init(); + if (_viewModel is PostFrameCallback) { + WidgetsBinding.instance.addPostFrameCallback( + (_viewModel as PostFrameCallback).onPostFrameCallback); + } } @override 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