Skip to content

Commit

Permalink
Update name of state for impl
Browse files Browse the repository at this point in the history
  • Loading branch information
JhonaCodes committed Dec 19, 2024
1 parent 25ac546 commit c9df198
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 18 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# 2.4.0
- Update name of state and documentation for `StateNotifierImpl`.

# 2.4.0

### Breaking Changes 🚨
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ Add this to your package's `pubspec.yaml` file:

```yaml
dependencies:
reactive_notifier: ^2.4.0
reactive_notifier: ^2.4.1
```
## Quick Start
Expand Down
3 changes: 1 addition & 2 deletions example/reactive_notifier_example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ class ConnectionStateWidget extends StatelessWidget {
return ReactiveBuilder<ConnectionManagerVM>(
notifier: ConnectionService.instance,
builder: (service, keep) {
final state = service.notifier;

final state = service.data;
return Card(
elevation: 4,
child: Padding(
Expand Down
32 changes: 20 additions & 12 deletions lib/src/implements/notifier_impl.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import 'package:flutter/foundation.dart';

/// value return.
/// [NotifierImpl]
/// Contains all the elements of the viewmodel, such as the main functions and attributes, which are used to modify data.
/// [ReactiveBuilder] and [ReactiveNotifier]
///
@protected
abstract class NotifierImpl<T> extends ChangeNotifier {
T _notifier;
Expand Down Expand Up @@ -47,49 +50,54 @@ abstract class NotifierImpl<T> extends ChangeNotifier {
bool get hasListeners => super.hasListeners;
}

/// [StateNotifierImpl]
/// Contains the data that is modified by `NotifierImpl`, it takes the data type declared in the viewmodel -
/// to use as a data type and returns the data from that viewmodel.
/// [ViewModelImpl] and [ViewModelStateImpl]
@protected
abstract class StateNotifierImpl<T> extends ChangeNotifier {
T _notifier;
StateNotifierImpl(this._notifier) {
T _data;
StateNotifierImpl(this._data) {
if (kFlutterMemoryAllocationsEnabled) {
ChangeNotifier.maybeDispatchObjectCreation(this);
}
}

@protected
T get notifier => _notifier;

@immutable
T get data => _data;

/// [updateState]
/// Updates the state and notifies listeners if the value has changed.
void updateState(T newState) {
if (_notifier.hashCode == newState.hashCode) {
if (_data.hashCode == newState.hashCode) {
return;
}

_notifier = newState;
_data = newState;
notifyListeners();
}

void transformState(T Function(T data) data) {
final dataNotifier = data(_notifier);
if (dataNotifier.hashCode == _notifier.hashCode) {
final dataNotifier = data(_data);
if (dataNotifier.hashCode == _data.hashCode) {
return;
}
_notifier = data(_notifier);
_data = data(_data);
notifyListeners();
}

/// [updateSilently]
/// Updates the value silently without notifying listeners.
@protected
void updateSilently(T newState) {
_notifier = newState;
_data = newState;
}

@protected
@override
String toString() => '${describeIdentity(this)}($notifier)';
String toString() => '${describeIdentity(this)}($data)';

@protected
@override
Expand Down
12 changes: 10 additions & 2 deletions lib/src/viewmodel/viewmodel_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ abstract class ViewModelImpl<T> extends StateNotifierImpl<T> {
}
}

@protected
@immutable
void init();

bool _initialized = false;
Expand All @@ -51,6 +51,10 @@ abstract class ViewModelImpl<T> extends StateNotifierImpl<T> {
StateTracker.trackStateChange(_id);
}
}


@override
T get data => this.data;
}

/// [ViewModelStateImpl]
Expand All @@ -70,6 +74,7 @@ abstract class ViewModelStateImpl<T> extends StateNotifierImpl<T> {
}
}

@immutable
void init();

bool _initialized = false;
Expand All @@ -83,18 +88,21 @@ abstract class ViewModelStateImpl<T> extends StateNotifierImpl<T> {
}
}

@immutable
void addDependencyTracker(String notifyId, String dependentId) {
if (!kReleaseMode) {
StateTracker.addDependency(notifyId, dependentId);
}
}

@immutable
void currentTracker() {
if (!kReleaseMode && _id != null) {
StateTracker.trackStateChange(_id);
}
}


@override
T get notifier => this.notifier;
T get data => this.data;
}
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: reactive_notifier
description: A Dart library for managing reactive state efficiently, supporting multiples related state.

version: 2.4.0
version: 2.4.1
homepage: https://github.com/JhonaCodes/reactive_notifier.git

environment:
Expand Down

0 comments on commit c9df198

Please sign in to comment.