Skip to content

Commit

Permalink
Merge pull request #11
Browse files Browse the repository at this point in the history
improve_view_model
  • Loading branch information
shubham16g authored Jan 22, 2023
2 parents 22a4bfa + 6fe9260 commit fc6f7b2
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 28 deletions.
12 changes: 0 additions & 12 deletions .github/workflows/publish.yaml

This file was deleted.

8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
30 changes: 29 additions & 1 deletion lib/src/flow.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
12 changes: 12 additions & 0 deletions lib/src/modified_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
12 changes: 0 additions & 12 deletions lib/src/view_model_provider.dart
Original file line number Diff line number Diff line change
@@ -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(
Expand All @@ -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();
}

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.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
Expand Down

0 comments on commit fc6f7b2

Please sign in to comment.