Skip to content

Commit

Permalink
Merge pull request #5
Browse files Browse the repository at this point in the history
PostFrameCallback added
  • Loading branch information
shubham16g authored Jan 11, 2023
2 parents 7567b48 + 16020b5 commit 00a3d4a
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 4 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
25 changes: 24 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
@@ -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>();

Expand All @@ -15,4 +15,9 @@ class SecondViewModel extends ViewModel {
void dispose() {
_messageSharedFlow.dispose();
}

@override
void onPostFrameCallback(Duration timestamp) {
_messageSharedFlow.emit("onPostFrameCallback from SecondViewModel");
}
}
2 changes: 1 addition & 1 deletion example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
8 changes: 8 additions & 0 deletions lib/src/view_model.dart
Original file line number Diff line number Diff line change
@@ -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);
}
5 changes: 5 additions & 0 deletions lib/src/view_model_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit 00a3d4a

Please sign in to comment.