Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#65 - SetAsNewPure added and edited old resetToPure method #67

Merged
merged 5 commits into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions glade_forms/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
## DEV:
- **[Add]**: Support skipping particular validation with `shouldValidate` callback.

## 2.3.0
- **[Breaking]**: The `resetToPure` method on both GladeInput and GladeModel has been renamed to `setAsNewPure`. This change better reflects the method's behavior of setting a new pure state rather than resetting to the original state.
tenhobi marked this conversation as resolved.
Show resolved Hide resolved
- **[Add]**: New `resetToPure` method added to both GladeInput and GladeModel. This method truly resets the input(s) to their initial value(s) and marks them as pure.

## 2.2.0
- **[Add]**: Add `resetToPure` on model level.

Expand Down
19 changes: 15 additions & 4 deletions glade_forms/lib/src/core/glade_input.dart
Original file line number Diff line number Diff line change
Expand Up @@ -533,11 +533,11 @@ class GladeInput<T> {
updateValue(value, shouldTriggerOnChange: shouldTriggerOnChange);
}

/// Resets input into pure state.
/// Sets a new pure state for the input.
///
/// Allows to sets new initialValue and value if needed.
/// By default ([invokeUpdate]=`true`) setting value will trigger listeners.
void resetToPure({
/// Allows to set new initialValue and value if needed.
/// By default ([invokeUpdate]=`true`) setting value will trigger listeners.
void setAsNewPure({
ValueGetter<T>? value,
ValueGetter<T>? initialValue,
bool invokeUpdate = true,
Expand Down Expand Up @@ -570,6 +570,17 @@ class GladeInput<T> {
}
}

/// Resets the input value to its initial value and sets it as pure.
void resetToPure() {
this._isPure = true;
if (_useTextEditingController) {
_syncValueWithController(_initialValue as T, shouldTriggerOnChange: true);
} else {
_value = _initialValue as T;
}
_bindedModel?.notifyInputUpdated(this);
}

@protected
GladeInput<T> copyWith({
String? inputKey,
Expand Down
29 changes: 23 additions & 6 deletions glade_forms/lib/src/model/glade_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ abstract class GladeModel extends ChangeNotifier {
_lastUpdates.add(input);
} else {
_lastUpdates = [input];
notifyDependecies();
notifyDependencies();
notifyListeners();
}
}
Expand All @@ -116,12 +116,12 @@ abstract class GladeModel extends ChangeNotifier {

_groupEdit = false;

notifyDependecies();
notifyDependencies();

notifyListeners();
}

void notifyDependecies() {
void notifyDependencies() {
final updatedKeys = _lastUpdates.map((e) => e.inputKey);
for (final input in inputs) {
final union = input.dependencies.map((e) => e.inputKey).toSet().union(updatedKeys.toSet());
Expand All @@ -130,12 +130,29 @@ abstract class GladeModel extends ChangeNotifier {
}
}

/// Resets all inputs to pure state.
/// Sets a new pure state for all inputs in the model.
///
/// When [copyValueToInitialValue] is true, input's initialValue is overriden by current value.
void resetToPure({bool copyValueToInitialValue = false}) {
void setAsNewPure({
bool invokeUpdate = true,
bool copyValueToInitialValue = false,
}) {
for (final input in inputs) {
input.resetToPure(copyValueToInitialValue: copyValueToInitialValue);
input.setAsNewPure(
invokeUpdate: invokeUpdate,
copyValueToInitialValue: copyValueToInitialValue,
);
}
if (invokeUpdate) {
notifyListeners();
}
}

/// Resets all inputs in the model to their initial values and sets them as pure.
void resetToPure() {
for (final input in inputs) {
input.resetToPure();
}
notifyListeners();
}
}
1 change: 0 additions & 1 deletion glade_forms/lib/src/widgets/glade_form_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ class GladeFormBuilder<M extends GladeModel> extends StatelessWidget {
// ignore: prefer-correct-callback-field-name, ok name
final CreateModelFunction<M>? create;
final M? value;
// ignore: prefer-correct-callback-field-name, ok name
final GladeFormWidgetBuilder<M> builder;
final Widget? child;

Expand Down
1 change: 0 additions & 1 deletion glade_forms/lib/src/widgets/glade_form_consumer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import 'package:glade_forms/src/widgets/glade_form_builder.dart';
import 'package:glade_forms/src/widgets/glade_form_listener.dart';

class GladeFormConsumer<M extends GladeModel> extends StatelessWidget {
// ignore: prefer-correct-callback-field-name, ok name
final GladeFormWidgetBuilder<M> builder;
// ignore: prefer-correct-callback-field-name, ok name
final GladeFormListenerFn<M>? listener;
Expand Down