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

feat: add experimental updateWith method on ValueWrapper #254

Merged
merged 1 commit into from
Jan 5, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
13 changes: 7 additions & 6 deletions examples/hevy_smolov_jr/lib/steps/exercise_selection.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'package:flutter_rearch/flutter_rearch.dart';
import 'package:fuzzywuzzy/fuzzywuzzy.dart';
import 'package:hevy_smolov_jr/api/wrapped_hevy_api.dart';
import 'package:hevy_smolov_jr/smolov_jr_config/config.dart';
import 'package:rearch/experimental.dart';
import 'package:rearch/rearch.dart';

/// Displays the exercise selection step contents.
Expand Down Expand Up @@ -51,10 +52,9 @@ class _CuratedExercisePicker extends StatelessWidget {
ChoiceChip(
label: Text(exercise.title),
selected: smolovJrConfig.value.exercise == exercise,
onSelected: (selected) {
smolovJrConfig.value =
smolovJrConfig.value.copyWith(exercise: exercise);
},
onSelected: (selected) => smolovJrConfig.updateWith(
(config) => config.copyWith(exercise: exercise),
),
),
],
),
Expand Down Expand Up @@ -103,8 +103,9 @@ class _AllExercisePicker extends RearchConsumer {
subtitle: Text(exercise.primaryMuscleGroup.replaceAll('_', ' ')),
onTap: () {
controller.closeView(exercise.title);
smolovJrConfig.value =
smolovJrConfig.value.copyWith(exercise: exercise);
smolovJrConfig.updateWith(
(config) => config.copyWith(exercise: exercise),
);
},
),
];
Expand Down
24 changes: 14 additions & 10 deletions examples/hevy_smolov_jr/lib/steps/program_config.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:flutter/material.dart';
import 'package:hevy_smolov_jr/smolov_jr_config/config.dart';
import 'package:rearch/experimental.dart';
import 'package:rearch/rearch.dart';

/// Displays the program configuration step/
Expand All @@ -20,8 +21,9 @@ class ProgramConfigInputStep extends StatelessWidget {
child: TextFormField(
keyboardType: TextInputType.number,
initialValue: smolovJrConfig.value.restSeconds.toString(),
onChanged: (s) => smolovJrConfig.value =
smolovJrConfig.value.copyWith(restSeconds: int.tryParse(s)),
onChanged: (s) => smolovJrConfig.updateWith(
(config) => config.copyWith(restSeconds: int.tryParse(s)),
),
decoration: const InputDecoration(
hintText: 'rest',
suffixIcon: Text('seconds'),
Expand All @@ -40,8 +42,7 @@ class ProgramConfigInputStep extends StatelessWidget {
initialSelection: smolovJrConfig.value.unit,
onSelected: (unit) {
if (unit == null) return;
smolovJrConfig.value =
smolovJrConfig.value.copyWith(unit: unit);
smolovJrConfig.updateWith((c) => c.copyWith(unit: unit));
},
dropdownMenuEntries: [
for (final unit in WeightUnit.values)
Expand All @@ -64,8 +65,9 @@ class ProgramConfigInputStep extends StatelessWidget {
keyboardType:
const TextInputType.numberWithOptions(decimal: true),
initialValue: smolovJrConfig.value.increment.toString(),
onChanged: (s) => smolovJrConfig.value =
smolovJrConfig.value.copyWith(increment: num.parse(s)),
onChanged: (s) => smolovJrConfig.updateWith(
(config) => config.copyWith(increment: num.parse(s)),
),
decoration: InputDecoration(
hintText: 'weight',
suffixIcon: Text(smolovJrConfig.value.unit.name),
Expand All @@ -86,8 +88,9 @@ class ProgramConfigInputStep extends StatelessWidget {
keyboardType:
const TextInputType.numberWithOptions(decimal: true),
initialValue: smolovJrConfig.value.oneRepMax.toString(),
onChanged: (s) => smolovJrConfig.value =
smolovJrConfig.value.copyWith(oneRepMax: num.parse(s)),
onChanged: (s) => smolovJrConfig.updateWith(
(config) => config.copyWith(oneRepMax: num.parse(s)),
),
decoration: InputDecoration(
hintText: 'weight',
suffixIcon: Text(smolovJrConfig.value.unit.name),
Expand All @@ -107,8 +110,9 @@ class ProgramConfigInputStep extends StatelessWidget {
keyboardType:
const TextInputType.numberWithOptions(decimal: true),
initialValue: smolovJrConfig.value.bodyWeight.toString(),
onChanged: (s) => smolovJrConfig.value =
smolovJrConfig.value.copyWith(bodyWeight: num.parse(s)),
onChanged: (s) => smolovJrConfig.updateWith(
(config) => config.copyWith(bodyWeight: num.parse(s)),
),
decoration: InputDecoration(
hintText: 'weight',
suffixIcon: Text(smolovJrConfig.value.unit.name),
Expand Down
16 changes: 16 additions & 0 deletions packages/rearch/lib/experimental.dart
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,19 @@ extension DynamicCapsuleCreationConvenience on CapsuleCreationConvenience {
return (CapsuleHandle use) => use.dynamic(dyn);
}
}

/// Provides [updateWith].
extension ValueWrapperUpdateWith<T> on ValueWrapper<T> {
/// Updates the current [ValueWrapperProperty.value]
/// with the result of calling [mutater] on it.
///
/// This can make it easier to update states using their `copyWith` methods:
/// ```dart
/// final myDataManager = capsule((use) => use.data(MyData()));
/// // ...
/// use(myDataManager).updateWith((data) => data.copyWith(foo: bar));
/// ```
/// NOTE: this is experimental because I think a future macro that generates a
/// dedicated `updateWith` for each type of `ValueWrapper<T>` could be nice.
void updateWith(T Function(T) mutater) => value = mutater(value);
}
Loading