Don't make the body weight repository a family, this is not needed

Also, fix nutritional_plan_screen_test.dart, this was failing on github's CI
This commit is contained in:
Roland Geider
2025-11-10 17:02:19 +01:00
parent 6f1a6a9ddf
commit 2afad29832
12 changed files with 84 additions and 76 deletions

View File

@@ -31,8 +31,8 @@ final class WeightEntryNotifier extends _$WeightEntryNotifier {
@override
// BodyWeightState build() {
Stream<List<WeightEntry>> build([BodyWeightRepository? repository]) {
_repo = repository ?? ref.read(bodyWeightRepositoryProvider);
Stream<List<WeightEntry>> build() {
_repo = ref.read(bodyWeightRepositoryProvider);
_logger.finer('Building WeightEntryNotifier');
// final state = BodyWeightState();

View File

@@ -10,82 +10,37 @@ part of 'body_weight.dart';
// ignore_for_file: type=lint, type=warning
@ProviderFor(WeightEntryNotifier)
const weightEntryProvider = WeightEntryNotifierFamily._();
const weightEntryProvider = WeightEntryNotifierProvider._();
final class WeightEntryNotifierProvider
extends $StreamNotifierProvider<WeightEntryNotifier, List<WeightEntry>> {
const WeightEntryNotifierProvider._({
required WeightEntryNotifierFamily super.from,
required BodyWeightRepository? super.argument,
}) : super(
retry: null,
name: r'weightEntryProvider',
isAutoDispose: true,
dependencies: null,
$allTransitiveDependencies: null,
);
const WeightEntryNotifierProvider._()
: super(
from: null,
argument: null,
retry: null,
name: r'weightEntryProvider',
isAutoDispose: true,
dependencies: null,
$allTransitiveDependencies: null,
);
@override
String debugGetCreateSourceHash() => _$weightEntryNotifierHash();
@override
String toString() {
return r'weightEntryProvider'
''
'($argument)';
}
@$internal
@override
WeightEntryNotifier create() => WeightEntryNotifier();
@override
bool operator ==(Object other) {
return other is WeightEntryNotifierProvider && other.argument == argument;
}
@override
int get hashCode {
return argument.hashCode;
}
}
String _$weightEntryNotifierHash() => r'4f3c19bc827ca6f702f17fb0399b431c6209a47a';
final class WeightEntryNotifierFamily extends $Family
with
$ClassFamilyOverride<
WeightEntryNotifier,
AsyncValue<List<WeightEntry>>,
List<WeightEntry>,
Stream<List<WeightEntry>>,
BodyWeightRepository?
> {
const WeightEntryNotifierFamily._()
: super(
retry: null,
name: r'weightEntryProvider',
dependencies: null,
$allTransitiveDependencies: null,
isAutoDispose: true,
);
WeightEntryNotifierProvider call([BodyWeightRepository? repository]) =>
WeightEntryNotifierProvider._(argument: repository, from: this);
@override
String toString() => r'weightEntryProvider';
}
String _$weightEntryNotifierHash() => r'd7ebd22ba0ee1f4aa4b27367f921e4e4c6c7c619';
abstract class _$WeightEntryNotifier extends $StreamNotifier<List<WeightEntry>> {
late final _$args = ref.$arg as BodyWeightRepository?;
BodyWeightRepository? get repository => _$args;
Stream<List<WeightEntry>> build([BodyWeightRepository? repository]);
Stream<List<WeightEntry>> build();
@$mustCallSuper
@override
void runBuild() {
final created = build(_$args);
final created = build();
final ref = this.ref as $Ref<AsyncValue<List<WeightEntry>>, List<WeightEntry>>;
final element =
ref.element

View File

@@ -124,7 +124,7 @@ class _HomeTabsScreenState extends riverpod.ConsumerState<HomeTabsScreen>
// await ref.read(routineStateReadyProvider.future);
// await ref.read(sessionStateReadyProvider.future);
// ref.watch(workoutLogProvider);
// ref.read(weightEntryProvider());
// ref.read(weightEntryProvider);
// await ref.watch(workoutLogProvider.future);
// await ref.read(workoutLogProvider.future);

View File

@@ -109,9 +109,9 @@ class _DashboardCalendarWidgetState extends riverpod.ConsumerState<DashboardCale
// Process weight entries
List<WeightEntry> entries = [];
try {
entries = await ref.read(weightEntryProvider().future);
entries = await ref.read(weightEntryProvider.future);
} catch (_) {
entries = ref.watch(weightEntryProvider()).asData?.value ?? [];
entries = ref.watch(weightEntryProvider).asData?.value ?? [];
}
for (final entry in entries) {
final date = DateFormatLists.format(entry.date);

View File

@@ -37,7 +37,7 @@ class DashboardWeightWidget extends ConsumerWidget {
@override
Widget build(BuildContext context, WidgetRef ref) {
final profile = context.read<UserProvider>().profile;
final entriesList = ref.watch(weightEntryProvider());
final entriesList = ref.watch(weightEntryProvider);
return Card(
child: Column(

View File

@@ -36,7 +36,7 @@ class NutritionalPlanDetailWidget extends riverpod.ConsumerWidget {
@override
Widget build(BuildContext context, riverpod.WidgetRef ref) {
final nutritionalGoals = _nutritionalPlan.nutritionalGoals;
final entriesList = ref.watch(weightEntryProvider()).asData?.value ?? [];
final entriesList = ref.watch(weightEntryProvider).asData?.value ?? [];
final WeightEntry? lastWeightEntry = entriesList.isNotEmpty ? entriesList.first : null;
final nutritionalGoalsGperKg = lastWeightEntry != null
? nutritionalGoals / lastWeightEntry.weight.toDouble()

View File

@@ -41,7 +41,7 @@ class NutritionalPlansList extends riverpod.ConsumerWidget {
DateTime startDate,
DateTime? endDate,
) {
final entriesList = ref.watch(weightEntryProvider()).asData?.value ?? [];
final entriesList = ref.watch(weightEntryProvider).asData?.value ?? [];
final entriesAll = entriesList.map((e) => MeasurementChartEntry(e.weight, e.date)).toList();
final entries7dAvg = moving7dAverage(entriesAll).whereDateWithInterpolation(startDate, endDate);

View File

@@ -209,7 +209,7 @@ class WeightForm extends riverpod.ConsumerWidget {
_form.currentState!.save();
// Save the entry on the server
final notifier = ref.read(weightEntryProvider().notifier);
final notifier = ref.read(weightEntryProvider.notifier);
_weightEntry.id == null
? await notifier.addEntry(_weightEntry)
: await notifier.updateEntry(_weightEntry);

View File

@@ -39,7 +39,7 @@ class WeightOverview extends riverpod.ConsumerWidget {
final numberFormat = NumberFormat.decimalPattern(Localizations.localeOf(context).toString());
final plans = context.read<NutritionPlansProvider>().items;
final entries = ref.watch(weightEntryProvider());
final entries = ref.watch(weightEntryProvider);
return entries.when(
data: (entriesList) {
final entriesAll = entriesList.map((e) => MeasurementChartEntry(e.weight, e.date)).toList();
@@ -105,7 +105,7 @@ class WeightOverview extends riverpod.ConsumerWidget {
child: Text(AppLocalizations.of(context).delete),
onTap: () async {
await ref
.read(weightEntryProvider().notifier)
.read(weightEntryProvider.notifier)
.deleteEntry(currentEntry.id!);
// and inform the user

View File

@@ -24,22 +24,28 @@ import 'package:flutter_riverpod/flutter_riverpod.dart' as riverpod;
import 'package:flutter_test/flutter_test.dart';
import 'package:http/http.dart' as http;
import 'package:mockito/annotations.dart';
import 'package:mockito/mockito.dart';
import 'package:provider/provider.dart';
import 'package:wger/database/ingredients/ingredients_database.dart';
import 'package:wger/l10n/generated/app_localizations.dart';
import 'package:wger/providers/auth.dart';
import 'package:wger/providers/base_provider.dart';
import 'package:wger/providers/body_weight_repository.dart';
import 'package:wger/providers/nutrition.dart';
import 'package:wger/screens/nutritional_plan_screen.dart';
import '../../test_data/body_weight.dart';
import '../../test_data/nutritional_plans.dart';
import 'nutritional_plan_screen_test.mocks.dart';
@GenerateMocks([WgerBaseProvider, AuthProvider, http.Client])
@GenerateMocks([WgerBaseProvider, AuthProvider, http.Client, BodyWeightRepository])
void main() {
late IngredientDatabase database;
late BodyWeightRepository mockRepository;
setUp(() {
mockRepository = MockBodyWeightRepository();
when(mockRepository.watchAllDrift()).thenAnswer((_) => Stream.value([testWeightEntry1]));
database = IngredientDatabase.inMemory(NativeDatabase.memory());
});
@@ -53,6 +59,9 @@ void main() {
final plan = getNutritionalPlan();
return riverpod.ProviderScope(
overrides: [
bodyWeightRepositoryProvider.overrideWithValue(mockRepository),
],
child: MultiProvider(
providers: [
ChangeNotifierProvider<NutritionPlansProvider>(

View File

@@ -12,8 +12,10 @@ import 'package:http/http.dart' as _i3;
import 'package:mockito/mockito.dart' as _i1;
import 'package:mockito/src/dummies.dart' as _i7;
import 'package:package_info_plus/package_info_plus.dart' as _i6;
import 'package:wger/models/body_weight/weight_entry.dart' as _i12;
import 'package:wger/providers/auth.dart' as _i2;
import 'package:wger/providers/base_provider.dart' as _i4;
import 'package:wger/providers/body_weight_repository.dart' as _i11;
// ignore_for_file: type=lint
// ignore_for_file: avoid_redundant_argument_values
@@ -589,3 +591,47 @@ class MockClient extends _i1.Mock implements _i3.Client {
returnValueForMissingStub: null,
);
}
/// A class which mocks [BodyWeightRepository].
///
/// See the documentation for Mockito's code generation for more information.
class MockBodyWeightRepository extends _i1.Mock implements _i11.BodyWeightRepository {
MockBodyWeightRepository() {
_i1.throwOnMissingStub(this);
}
@override
_i5.Stream<List<_i12.WeightEntry>> watchAllDrift() =>
(super.noSuchMethod(
Invocation.method(#watchAllDrift, []),
returnValue: _i5.Stream<List<_i12.WeightEntry>>.empty(),
)
as _i5.Stream<List<_i12.WeightEntry>>);
@override
_i5.Future<void> deleteLocalDrift(String? id) =>
(super.noSuchMethod(
Invocation.method(#deleteLocalDrift, [id]),
returnValue: _i5.Future<void>.value(),
returnValueForMissingStub: _i5.Future<void>.value(),
)
as _i5.Future<void>);
@override
_i5.Future<void> updateLocalDrift(_i12.WeightEntry? entry) =>
(super.noSuchMethod(
Invocation.method(#updateLocalDrift, [entry]),
returnValue: _i5.Future<void>.value(),
returnValueForMissingStub: _i5.Future<void>.value(),
)
as _i5.Future<void>);
@override
_i5.Future<void> addLocalDrift(_i12.WeightEntry? entry) =>
(super.noSuchMethod(
Invocation.method(#addLocalDrift, [entry]),
returnValue: _i5.Future<void>.value(),
returnValueForMissingStub: _i5.Future<void>.value(),
)
as _i5.Future<void>);
}

View File

@@ -36,23 +36,21 @@ void main() {
when(mockBodyWeightRepository.watchAllDrift()).thenAnswer((_) => Stream.value(<WeightEntry>[]));
when(mockBodyWeightRepository.deleteLocalDrift(any)).thenAnswer((_) async {});
when(mockBodyWeightRepository.updateLocalDrift(any)).thenAnswer((_) async {});
container = ProviderContainer(
container = ProviderContainer.test(
overrides: [bodyWeightRepositoryProvider.overrideWithValue(mockBodyWeightRepository)],
);
addTearDown(container.dispose);
});
group('test body weight provider', () {
test('deleteEntry correctly calls repository.deleteLocalDrift', () async {
final notifier = container.read(weightEntryProvider().notifier);
final notifier = container.read(weightEntryProvider.notifier);
await notifier.deleteEntry('123');
verify(mockBodyWeightRepository.deleteLocalDrift('123')).called(1);
});
test('updateEntry correctly calls repository.updateLocalDrift', () async {
final notifier = container.read(weightEntryProvider().notifier);
final notifier = container.read(weightEntryProvider.notifier);
final entry = WeightEntry(id: '123', date: DateTime.now(), weight: 70.0);
await notifier.updateEntry(entry);