mirror of
https://github.com/wger-project/flutter.git
synced 2026-11-29 09:20:56 +01:00
Further refactor how the new providers fetch data
This commit is contained in:
@@ -46,7 +46,7 @@ final class PowerSyncInstanceProvider
|
||||
}
|
||||
}
|
||||
|
||||
String _$powerSyncInstanceHash() => r'cc51119f46b1a2838312e5bb60cec1db8748caac';
|
||||
String _$powerSyncInstanceHash() => r'25fdd0cce6fad18d2859035f1d5475caac992dfb';
|
||||
|
||||
@ProviderFor(didCompleteSync)
|
||||
final didCompleteSyncProvider = DidCompleteSyncFamily._();
|
||||
|
||||
@@ -137,7 +137,7 @@ class MainApp extends ConsumerWidget {
|
||||
Widget _getHomeScreen(AuthState auth) {
|
||||
switch (auth.status) {
|
||||
case AuthStatus.loggedIn:
|
||||
return const EagerInitialization();
|
||||
return const HomeTabsScreen();
|
||||
case AuthStatus.updateRequired:
|
||||
return const UpdateAppScreen();
|
||||
case AuthStatus.serverUpdateRequired:
|
||||
@@ -175,7 +175,7 @@ class MainApp extends ConsumerWidget {
|
||||
FormScreen.routeName: (ctx) => const FormScreen(),
|
||||
GalleryScreen.routeName: (ctx) => const GalleryScreen(),
|
||||
GymModeScreen.routeName: (ctx) => const GymModeScreen(),
|
||||
HomeTabsScreen.routeName: (ctx) => HomeTabsScreen(),
|
||||
HomeTabsScreen.routeName: (ctx) => const HomeTabsScreen(),
|
||||
MeasurementCategoriesScreen.routeName: (ctx) => const MeasurementCategoriesScreen(),
|
||||
MeasurementEntriesScreen.routeName: (ctx) => const MeasurementEntriesScreen(),
|
||||
NutritionalPlansScreen.routeName: (ctx) => const NutritionalPlansScreen(),
|
||||
|
||||
@@ -313,11 +313,6 @@ class AuthNotifier extends _$AuthNotifier {
|
||||
state = AsyncData(current.copyWith(serverVersion: v));
|
||||
}
|
||||
|
||||
/// Marks that the initial eager data-load has completed.
|
||||
void setDataInit(bool value) {
|
||||
state = AsyncData(_currentOrBlank().copyWith(dataInit: value));
|
||||
}
|
||||
|
||||
Future<String> _fetchServerVersion(String serverUrl) async {
|
||||
final response = await _client.get(makeUri(serverUrl, SERVER_VERSION_URL));
|
||||
return json.decode(response.body);
|
||||
|
||||
@@ -32,7 +32,7 @@ final class AuthNotifierProvider extends $AsyncNotifierProvider<AuthNotifier, Au
|
||||
AuthNotifier create() => AuthNotifier();
|
||||
}
|
||||
|
||||
String _$authNotifierHash() => r'a0d999826d2a4b46be8fc40ca1466c0b1690797f';
|
||||
String _$authNotifierHash() => r'48b66ac1508c92dea7daa3a0c091e9f7f56aa28a';
|
||||
|
||||
abstract class _$AuthNotifier extends $AsyncNotifier<AuthState> {
|
||||
FutureOr<AuthState> build();
|
||||
|
||||
@@ -41,7 +41,6 @@ sealed class AuthState with _$AuthState {
|
||||
String? serverUrl,
|
||||
String? serverVersion,
|
||||
PackageInfo? applicationVersion,
|
||||
@Default(false) bool dataInit,
|
||||
}) = _AuthState;
|
||||
|
||||
const AuthState._();
|
||||
|
||||
@@ -14,7 +14,7 @@ T _$identity<T>(T value) => value;
|
||||
/// @nodoc
|
||||
mixin _$AuthState {
|
||||
|
||||
AuthStatus get status; String? get token; String? get serverUrl; String? get serverVersion; PackageInfo? get applicationVersion; bool get dataInit;
|
||||
AuthStatus get status; String? get token; String? get serverUrl; String? get serverVersion; PackageInfo? get applicationVersion;
|
||||
/// Create a copy of AuthState
|
||||
/// with the given fields replaced by the non-null parameter values.
|
||||
@JsonKey(includeFromJson: false, includeToJson: false)
|
||||
@@ -25,16 +25,16 @@ $AuthStateCopyWith<AuthState> get copyWith => _$AuthStateCopyWithImpl<AuthState>
|
||||
|
||||
@override
|
||||
bool operator ==(Object other) {
|
||||
return identical(this, other) || (other.runtimeType == runtimeType&&other is AuthState&&(identical(other.status, status) || other.status == status)&&(identical(other.token, token) || other.token == token)&&(identical(other.serverUrl, serverUrl) || other.serverUrl == serverUrl)&&(identical(other.serverVersion, serverVersion) || other.serverVersion == serverVersion)&&(identical(other.applicationVersion, applicationVersion) || other.applicationVersion == applicationVersion)&&(identical(other.dataInit, dataInit) || other.dataInit == dataInit));
|
||||
return identical(this, other) || (other.runtimeType == runtimeType&&other is AuthState&&(identical(other.status, status) || other.status == status)&&(identical(other.token, token) || other.token == token)&&(identical(other.serverUrl, serverUrl) || other.serverUrl == serverUrl)&&(identical(other.serverVersion, serverVersion) || other.serverVersion == serverVersion)&&(identical(other.applicationVersion, applicationVersion) || other.applicationVersion == applicationVersion));
|
||||
}
|
||||
|
||||
|
||||
@override
|
||||
int get hashCode => Object.hash(runtimeType,status,token,serverUrl,serverVersion,applicationVersion,dataInit);
|
||||
int get hashCode => Object.hash(runtimeType,status,token,serverUrl,serverVersion,applicationVersion);
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
return 'AuthState(status: $status, token: $token, serverUrl: $serverUrl, serverVersion: $serverVersion, applicationVersion: $applicationVersion, dataInit: $dataInit)';
|
||||
return 'AuthState(status: $status, token: $token, serverUrl: $serverUrl, serverVersion: $serverVersion, applicationVersion: $applicationVersion)';
|
||||
}
|
||||
|
||||
|
||||
@@ -45,7 +45,7 @@ abstract mixin class $AuthStateCopyWith<$Res> {
|
||||
factory $AuthStateCopyWith(AuthState value, $Res Function(AuthState) _then) = _$AuthStateCopyWithImpl;
|
||||
@useResult
|
||||
$Res call({
|
||||
AuthStatus status, String? token, String? serverUrl, String? serverVersion, PackageInfo? applicationVersion, bool dataInit
|
||||
AuthStatus status, String? token, String? serverUrl, String? serverVersion, PackageInfo? applicationVersion
|
||||
});
|
||||
|
||||
|
||||
@@ -62,15 +62,14 @@ class _$AuthStateCopyWithImpl<$Res>
|
||||
|
||||
/// Create a copy of AuthState
|
||||
/// with the given fields replaced by the non-null parameter values.
|
||||
@pragma('vm:prefer-inline') @override $Res call({Object? status = null,Object? token = freezed,Object? serverUrl = freezed,Object? serverVersion = freezed,Object? applicationVersion = freezed,Object? dataInit = null,}) {
|
||||
@pragma('vm:prefer-inline') @override $Res call({Object? status = null,Object? token = freezed,Object? serverUrl = freezed,Object? serverVersion = freezed,Object? applicationVersion = freezed,}) {
|
||||
return _then(_self.copyWith(
|
||||
status: null == status ? _self.status : status // ignore: cast_nullable_to_non_nullable
|
||||
as AuthStatus,token: freezed == token ? _self.token : token // ignore: cast_nullable_to_non_nullable
|
||||
as String?,serverUrl: freezed == serverUrl ? _self.serverUrl : serverUrl // ignore: cast_nullable_to_non_nullable
|
||||
as String?,serverVersion: freezed == serverVersion ? _self.serverVersion : serverVersion // ignore: cast_nullable_to_non_nullable
|
||||
as String?,applicationVersion: freezed == applicationVersion ? _self.applicationVersion : applicationVersion // ignore: cast_nullable_to_non_nullable
|
||||
as PackageInfo?,dataInit: null == dataInit ? _self.dataInit : dataInit // ignore: cast_nullable_to_non_nullable
|
||||
as bool,
|
||||
as PackageInfo?,
|
||||
));
|
||||
}
|
||||
|
||||
@@ -152,10 +151,10 @@ return $default(_that);case _:
|
||||
/// }
|
||||
/// ```
|
||||
|
||||
@optionalTypeArgs TResult maybeWhen<TResult extends Object?>(TResult Function( AuthStatus status, String? token, String? serverUrl, String? serverVersion, PackageInfo? applicationVersion, bool dataInit)? $default,{required TResult orElse(),}) {final _that = this;
|
||||
@optionalTypeArgs TResult maybeWhen<TResult extends Object?>(TResult Function( AuthStatus status, String? token, String? serverUrl, String? serverVersion, PackageInfo? applicationVersion)? $default,{required TResult orElse(),}) {final _that = this;
|
||||
switch (_that) {
|
||||
case _AuthState() when $default != null:
|
||||
return $default(_that.status,_that.token,_that.serverUrl,_that.serverVersion,_that.applicationVersion,_that.dataInit);case _:
|
||||
return $default(_that.status,_that.token,_that.serverUrl,_that.serverVersion,_that.applicationVersion);case _:
|
||||
return orElse();
|
||||
|
||||
}
|
||||
@@ -173,10 +172,10 @@ return $default(_that.status,_that.token,_that.serverUrl,_that.serverVersion,_th
|
||||
/// }
|
||||
/// ```
|
||||
|
||||
@optionalTypeArgs TResult when<TResult extends Object?>(TResult Function( AuthStatus status, String? token, String? serverUrl, String? serverVersion, PackageInfo? applicationVersion, bool dataInit) $default,) {final _that = this;
|
||||
@optionalTypeArgs TResult when<TResult extends Object?>(TResult Function( AuthStatus status, String? token, String? serverUrl, String? serverVersion, PackageInfo? applicationVersion) $default,) {final _that = this;
|
||||
switch (_that) {
|
||||
case _AuthState():
|
||||
return $default(_that.status,_that.token,_that.serverUrl,_that.serverVersion,_that.applicationVersion,_that.dataInit);}
|
||||
return $default(_that.status,_that.token,_that.serverUrl,_that.serverVersion,_that.applicationVersion);}
|
||||
}
|
||||
/// A variant of `when` that fallback to returning `null`
|
||||
///
|
||||
@@ -190,10 +189,10 @@ return $default(_that.status,_that.token,_that.serverUrl,_that.serverVersion,_th
|
||||
/// }
|
||||
/// ```
|
||||
|
||||
@optionalTypeArgs TResult? whenOrNull<TResult extends Object?>(TResult? Function( AuthStatus status, String? token, String? serverUrl, String? serverVersion, PackageInfo? applicationVersion, bool dataInit)? $default,) {final _that = this;
|
||||
@optionalTypeArgs TResult? whenOrNull<TResult extends Object?>(TResult? Function( AuthStatus status, String? token, String? serverUrl, String? serverVersion, PackageInfo? applicationVersion)? $default,) {final _that = this;
|
||||
switch (_that) {
|
||||
case _AuthState() when $default != null:
|
||||
return $default(_that.status,_that.token,_that.serverUrl,_that.serverVersion,_that.applicationVersion,_that.dataInit);case _:
|
||||
return $default(_that.status,_that.token,_that.serverUrl,_that.serverVersion,_that.applicationVersion);case _:
|
||||
return null;
|
||||
|
||||
}
|
||||
@@ -205,7 +204,7 @@ return $default(_that.status,_that.token,_that.serverUrl,_that.serverVersion,_th
|
||||
|
||||
|
||||
class _AuthState extends AuthState {
|
||||
const _AuthState({this.status = AuthStatus.loggedOut, this.token, this.serverUrl, this.serverVersion, this.applicationVersion, this.dataInit = false}): super._();
|
||||
const _AuthState({this.status = AuthStatus.loggedOut, this.token, this.serverUrl, this.serverVersion, this.applicationVersion}): super._();
|
||||
|
||||
|
||||
@override@JsonKey() final AuthStatus status;
|
||||
@@ -213,7 +212,6 @@ class _AuthState extends AuthState {
|
||||
@override final String? serverUrl;
|
||||
@override final String? serverVersion;
|
||||
@override final PackageInfo? applicationVersion;
|
||||
@override@JsonKey() final bool dataInit;
|
||||
|
||||
/// Create a copy of AuthState
|
||||
/// with the given fields replaced by the non-null parameter values.
|
||||
@@ -225,16 +223,16 @@ _$AuthStateCopyWith<_AuthState> get copyWith => __$AuthStateCopyWithImpl<_AuthSt
|
||||
|
||||
@override
|
||||
bool operator ==(Object other) {
|
||||
return identical(this, other) || (other.runtimeType == runtimeType&&other is _AuthState&&(identical(other.status, status) || other.status == status)&&(identical(other.token, token) || other.token == token)&&(identical(other.serverUrl, serverUrl) || other.serverUrl == serverUrl)&&(identical(other.serverVersion, serverVersion) || other.serverVersion == serverVersion)&&(identical(other.applicationVersion, applicationVersion) || other.applicationVersion == applicationVersion)&&(identical(other.dataInit, dataInit) || other.dataInit == dataInit));
|
||||
return identical(this, other) || (other.runtimeType == runtimeType&&other is _AuthState&&(identical(other.status, status) || other.status == status)&&(identical(other.token, token) || other.token == token)&&(identical(other.serverUrl, serverUrl) || other.serverUrl == serverUrl)&&(identical(other.serverVersion, serverVersion) || other.serverVersion == serverVersion)&&(identical(other.applicationVersion, applicationVersion) || other.applicationVersion == applicationVersion));
|
||||
}
|
||||
|
||||
|
||||
@override
|
||||
int get hashCode => Object.hash(runtimeType,status,token,serverUrl,serverVersion,applicationVersion,dataInit);
|
||||
int get hashCode => Object.hash(runtimeType,status,token,serverUrl,serverVersion,applicationVersion);
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
return 'AuthState(status: $status, token: $token, serverUrl: $serverUrl, serverVersion: $serverVersion, applicationVersion: $applicationVersion, dataInit: $dataInit)';
|
||||
return 'AuthState(status: $status, token: $token, serverUrl: $serverUrl, serverVersion: $serverVersion, applicationVersion: $applicationVersion)';
|
||||
}
|
||||
|
||||
|
||||
@@ -245,7 +243,7 @@ abstract mixin class _$AuthStateCopyWith<$Res> implements $AuthStateCopyWith<$Re
|
||||
factory _$AuthStateCopyWith(_AuthState value, $Res Function(_AuthState) _then) = __$AuthStateCopyWithImpl;
|
||||
@override @useResult
|
||||
$Res call({
|
||||
AuthStatus status, String? token, String? serverUrl, String? serverVersion, PackageInfo? applicationVersion, bool dataInit
|
||||
AuthStatus status, String? token, String? serverUrl, String? serverVersion, PackageInfo? applicationVersion
|
||||
});
|
||||
|
||||
|
||||
@@ -262,15 +260,14 @@ class __$AuthStateCopyWithImpl<$Res>
|
||||
|
||||
/// Create a copy of AuthState
|
||||
/// with the given fields replaced by the non-null parameter values.
|
||||
@override @pragma('vm:prefer-inline') $Res call({Object? status = null,Object? token = freezed,Object? serverUrl = freezed,Object? serverVersion = freezed,Object? applicationVersion = freezed,Object? dataInit = null,}) {
|
||||
@override @pragma('vm:prefer-inline') $Res call({Object? status = null,Object? token = freezed,Object? serverUrl = freezed,Object? serverVersion = freezed,Object? applicationVersion = freezed,}) {
|
||||
return _then(_AuthState(
|
||||
status: null == status ? _self.status : status // ignore: cast_nullable_to_non_nullable
|
||||
as AuthStatus,token: freezed == token ? _self.token : token // ignore: cast_nullable_to_non_nullable
|
||||
as String?,serverUrl: freezed == serverUrl ? _self.serverUrl : serverUrl // ignore: cast_nullable_to_non_nullable
|
||||
as String?,serverVersion: freezed == serverVersion ? _self.serverVersion : serverVersion // ignore: cast_nullable_to_non_nullable
|
||||
as String?,applicationVersion: freezed == applicationVersion ? _self.applicationVersion : applicationVersion // ignore: cast_nullable_to_non_nullable
|
||||
as PackageInfo?,dataInit: null == dataInit ? _self.dataInit : dataInit // ignore: cast_nullable_to_non_nullable
|
||||
as bool,
|
||||
as PackageInfo?,
|
||||
));
|
||||
}
|
||||
|
||||
|
||||
@@ -44,7 +44,13 @@ class NutritionNotifier extends _$NutritionNotifier {
|
||||
Future<List<NutritionalPlan>> build() async {
|
||||
_nutritionRepo = ref.read(nutritionRepositoryProvider);
|
||||
_ingredientRepo = ref.read(ingredientRepositoryProvider);
|
||||
return [];
|
||||
|
||||
// Auto-fetch the sparse plan list on first read. Consumers that need
|
||||
// a forced refresh (e.g. RefreshIndicator) can still call
|
||||
// [fetchAndSetAllPlansSparse] explicitly.
|
||||
final data = await _nutritionRepo.fetchAllPlans();
|
||||
return data.map((e) => NutritionalPlan.fromJson(e)).toList()
|
||||
..sort((a, b) => b.creationDate.compareTo(a.creationDate));
|
||||
}
|
||||
|
||||
List<NutritionalPlan> get _plans => state.asData?.value ?? [];
|
||||
|
||||
@@ -33,7 +33,7 @@ final class NutritionNotifierProvider
|
||||
NutritionNotifier create() => NutritionNotifier();
|
||||
}
|
||||
|
||||
String _$nutritionNotifierHash() => r'a96cda5646bff0a63b53bf70793370915e92bd9c';
|
||||
String _$nutritionNotifierHash() => r'958bf02bc0aa8dc739be26a04b4efbb1004bfd72';
|
||||
|
||||
abstract class _$NutritionNotifier extends $AsyncNotifier<List<NutritionalPlan>> {
|
||||
FutureOr<List<NutritionalPlan>> build();
|
||||
|
||||
@@ -96,6 +96,18 @@ class RoutinesRiverpod extends _$RoutinesRiverpod {
|
||||
@override
|
||||
RoutinesState build() {
|
||||
_logger.fine('Building Routines Riverpod notifier');
|
||||
|
||||
// Kick off the initial sparse load. UI consumers `watch`ing this
|
||||
// provider rebuild automatically once `state` is updated. Errors are
|
||||
// logged but don't crash the build.
|
||||
Future.microtask(() async {
|
||||
try {
|
||||
await fetchAllRoutinesSparse();
|
||||
} catch (e, s) {
|
||||
_logger.warning('initial routine fetch failed', e, s);
|
||||
}
|
||||
});
|
||||
|
||||
return const RoutinesState();
|
||||
}
|
||||
|
||||
|
||||
@@ -118,7 +118,7 @@ final class RoutinesRiverpodProvider extends $NotifierProvider<RoutinesRiverpod,
|
||||
}
|
||||
}
|
||||
|
||||
String _$routinesRiverpodHash() => r'7a097555c9718249a21328ac16234ef26e23c453';
|
||||
String _$routinesRiverpodHash() => r'83e7e418dcd7adc36e8897598927c6a7b47a33c9';
|
||||
|
||||
abstract class _$RoutinesRiverpod extends $Notifier<RoutinesState> {
|
||||
RoutinesState build();
|
||||
|
||||
@@ -132,6 +132,16 @@ final class TrophyStateNotifier extends _$TrophyStateNotifier {
|
||||
|
||||
@override
|
||||
TrophyState build() {
|
||||
// Kick off the initial trophy load. Sync notifier, so we can't await
|
||||
// here — UI consumers rebuild via state mutation when data arrives.
|
||||
Future.microtask(() async {
|
||||
try {
|
||||
await fetchAll();
|
||||
} catch (e, s) {
|
||||
_logger.warning('initial trophy fetch failed', e, s);
|
||||
}
|
||||
});
|
||||
|
||||
return TrophyState();
|
||||
}
|
||||
|
||||
|
||||
@@ -82,7 +82,7 @@ final class TrophyStateNotifierProvider
|
||||
}
|
||||
}
|
||||
|
||||
String _$trophyStateNotifierHash() => r'c80c732272cf843b698f28152f60b9a5f37ee449';
|
||||
String _$trophyStateNotifierHash() => r'624d9b9f4e6fb2c8a31a1e7c5dd9ec8dce870c2f';
|
||||
|
||||
abstract class _$TrophyStateNotifier extends $Notifier<TrophyState> {
|
||||
TrophyState build();
|
||||
|
||||
@@ -80,16 +80,6 @@ class _DashboardCalendarWidgetState extends riverpod.ConsumerState<DashboardCale
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
loadEvents();
|
||||
});
|
||||
|
||||
// React to changes in the providers so the calendar updates automatically
|
||||
// TODO: refine this
|
||||
ref.listen<riverpod.AsyncValue<List<WeightEntry>>>(weightEntryProvider, (prev, next) {
|
||||
loadEvents();
|
||||
});
|
||||
|
||||
ref.listen<riverpod.AsyncValue<List<MeasurementCategory>>>(measurementProvider, (prev, next) {
|
||||
loadEvents();
|
||||
});
|
||||
}
|
||||
|
||||
/// Loads and organizes all events from various providers into the calendar.
|
||||
@@ -257,6 +247,21 @@ class _DashboardCalendarWidgetState extends riverpod.ConsumerState<DashboardCale
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
// React to changes in the providers so the calendar updates automatically.
|
||||
// ref.listen must be invoked from build(), not initState().
|
||||
// TODO: also listen to routinesRiverpodProvider once it has been migrated
|
||||
// to an AsyncNotifier, so workout sessions refresh without a
|
||||
// manual reload.
|
||||
ref.listen<riverpod.AsyncValue<List<WeightEntry>>>(weightEntryProvider, (prev, next) {
|
||||
loadEvents();
|
||||
});
|
||||
ref.listen<riverpod.AsyncValue<List<MeasurementCategory>>>(measurementProvider, (prev, next) {
|
||||
loadEvents();
|
||||
});
|
||||
ref.listen(nutritionProvider, (prev, next) {
|
||||
loadEvents();
|
||||
});
|
||||
|
||||
return Card(
|
||||
child: Column(
|
||||
children: [
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
/*
|
||||
* This file is part of wger Workout Manager <https://github.com/wger-project>.
|
||||
* Copyright (C) 2020, 2021 wger Team
|
||||
* Copyright (c) 2020 - 2026 wger Team
|
||||
*
|
||||
* wger Workout Manager is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* wger Workout Manager is distributed in the hope that it will be useful,
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
@@ -21,7 +21,6 @@ import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:flutter_svg_icons/flutter_svg_icons.dart';
|
||||
import 'package:intl/intl.dart';
|
||||
import 'package:wger/l10n/generated/app_localizations.dart';
|
||||
import 'package:wger/models/nutrition/nutritional_plan.dart';
|
||||
import 'package:wger/providers/nutrition_notifier.dart';
|
||||
import 'package:wger/screens/form_screen.dart';
|
||||
import 'package:wger/screens/log_meals_screen.dart';
|
||||
@@ -30,39 +29,30 @@ import 'package:wger/widgets/dashboard/widgets/nothing_found.dart';
|
||||
import 'package:wger/widgets/nutrition/charts.dart';
|
||||
import 'package:wger/widgets/nutrition/forms.dart';
|
||||
|
||||
class DashboardNutritionWidget extends ConsumerStatefulWidget {
|
||||
class DashboardNutritionWidget extends ConsumerWidget {
|
||||
const DashboardNutritionWidget();
|
||||
|
||||
@override
|
||||
ConsumerState<DashboardNutritionWidget> createState() => _DashboardNutritionWidgetState();
|
||||
}
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
// Watch so the widget rebuilds when plans arrive from the initial fetch.
|
||||
// currentPlan is derived from the watched state, so no separate read.
|
||||
ref.watch(nutritionProvider);
|
||||
final plan = ref.read(nutritionProvider.notifier).currentPlan;
|
||||
final hasContent = plan != null;
|
||||
|
||||
class _DashboardNutritionWidgetState extends ConsumerState<DashboardNutritionWidget> {
|
||||
NutritionalPlan? _plan;
|
||||
bool _hasContent = false;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_plan = ref.read(nutritionProvider.notifier).currentPlan;
|
||||
_hasContent = _plan != null;
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Card(
|
||||
child: Column(
|
||||
children: [
|
||||
ListTile(
|
||||
title: Text(
|
||||
_hasContent ? _plan!.description : AppLocalizations.of(context).nutritionalPlan,
|
||||
hasContent ? plan.description : AppLocalizations.of(context).nutritionalPlan,
|
||||
style: Theme.of(context).textTheme.headlineSmall,
|
||||
),
|
||||
subtitle: Text(
|
||||
_hasContent
|
||||
hasContent
|
||||
? DateFormat.yMd(
|
||||
Localizations.localeOf(context).languageCode,
|
||||
).format(_plan!.creationDate)
|
||||
).format(plan.creationDate)
|
||||
: '',
|
||||
),
|
||||
leading: Icon(
|
||||
@@ -70,12 +60,12 @@ class _DashboardNutritionWidgetState extends ConsumerState<DashboardNutritionWid
|
||||
color: Theme.of(context).textTheme.headlineSmall!.color,
|
||||
),
|
||||
),
|
||||
if (_hasContent)
|
||||
if (hasContent)
|
||||
Padding(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 10),
|
||||
child: Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 5, vertical: 15),
|
||||
child: FlNutritionalPlanGoalWidget(nutritionalPlan: _plan!),
|
||||
child: FlNutritionalPlanGoalWidget(nutritionalPlan: plan),
|
||||
),
|
||||
)
|
||||
else
|
||||
@@ -84,7 +74,7 @@ class _DashboardNutritionWidgetState extends ConsumerState<DashboardNutritionWid
|
||||
AppLocalizations.of(context).newNutritionalPlan,
|
||||
PlanForm(),
|
||||
),
|
||||
if (_hasContent)
|
||||
if (hasContent)
|
||||
LayoutBuilder(
|
||||
builder: (context, constraints) {
|
||||
return SingleChildScrollView(
|
||||
@@ -100,7 +90,7 @@ class _DashboardNutritionWidgetState extends ConsumerState<DashboardNutritionWid
|
||||
onPressed: () {
|
||||
Navigator.of(context).pushNamed(
|
||||
NutritionalPlanScreen.routeName,
|
||||
arguments: _plan,
|
||||
arguments: plan,
|
||||
);
|
||||
},
|
||||
),
|
||||
@@ -117,7 +107,7 @@ class _DashboardNutritionWidgetState extends ConsumerState<DashboardNutritionWid
|
||||
FormScreen.routeName,
|
||||
arguments: FormScreenArguments(
|
||||
AppLocalizations.of(context).logIngredient,
|
||||
getIngredientLogForm(_plan!),
|
||||
getIngredientLogForm(plan),
|
||||
hasListView: true,
|
||||
),
|
||||
);
|
||||
@@ -131,7 +121,7 @@ class _DashboardNutritionWidgetState extends ConsumerState<DashboardNutritionWid
|
||||
onPressed: () {
|
||||
Navigator.of(
|
||||
context,
|
||||
).pushNamed(LogMealsScreen.routeName, arguments: _plan);
|
||||
).pushNamed(LogMealsScreen.routeName, arguments: plan);
|
||||
},
|
||||
),
|
||||
],
|
||||
|
||||
@@ -28,7 +28,7 @@ class DashboardTrophiesWidget extends ConsumerWidget {
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final trophiesState = ref.read(trophyStateProvider);
|
||||
final trophiesState = ref.watch(trophyStateProvider);
|
||||
final i18n = AppLocalizations.of(context);
|
||||
|
||||
return Card(
|
||||
|
||||
@@ -67,6 +67,12 @@ void main() {
|
||||
|
||||
// By default the local ingredient repo has no data - fall back to HTTP.
|
||||
when(mockIngredientRepo.getById(any)).thenAnswer((_) async => null);
|
||||
|
||||
// NutritionNotifier.build() auto-fetches all plans on first read.
|
||||
// Default to an empty list so tests that don't care about the plan list
|
||||
// don't have to stub it; tests that need real plans override via
|
||||
// [containerWithPlans].
|
||||
when(mockRepo.fetchAllPlans()).thenAnswer((_) async => []);
|
||||
});
|
||||
|
||||
group('fetchAndSetPlanFull', () {
|
||||
|
||||
@@ -51,6 +51,9 @@ void main() {
|
||||
|
||||
when(mockRepo.updateMeal(any, any)).thenAnswer((_) async => Meal(id: 1, plan: 1).toJson());
|
||||
when(mockRepo.createMeal(any)).thenAnswer((_) async => Meal(id: 99, plan: 1).toJson());
|
||||
// NutritionNotifier.build() auto-fetches plans on first read; the
|
||||
// explicit state seed below replaces this empty value.
|
||||
when(mockRepo.fetchAllPlans()).thenAnswer((_) async => []);
|
||||
|
||||
container = ProviderContainer(
|
||||
overrides: [
|
||||
|
||||
@@ -126,6 +126,8 @@ void main() {
|
||||
when(mockRepo.fetchIngredient(any)).thenAnswer((_) async => ingredient);
|
||||
when(mockRepo.fetchWeightUnits(1)).thenAnswer((_) => Future.value([]));
|
||||
when(mockRepo.fetchWeightUnits(2)).thenAnswer((_) => Future.value([]));
|
||||
// NutritionNotifier.build() auto-fetches plans on first read.
|
||||
when(mockRepo.fetchAllPlans()).thenAnswer((_) async => []);
|
||||
|
||||
container = ProviderContainer(
|
||||
overrides: [
|
||||
|
||||
@@ -51,6 +51,8 @@ void main() {
|
||||
|
||||
when(mockRepo.updatePlan(any, any)).thenAnswer((_) async {});
|
||||
when(mockRepo.createPlan(any)).thenAnswer((_) async => plan1.toJson());
|
||||
// NutritionNotifier.build() auto-fetches plans on first read.
|
||||
when(mockRepo.fetchAllPlans()).thenAnswer((_) async => []);
|
||||
});
|
||||
|
||||
Widget createHomeScreen(NutritionalPlan plan, {locale = 'en'}) {
|
||||
|
||||
@@ -64,6 +64,8 @@ void main() {
|
||||
mockIngredientRepo = MockIngredientRepository();
|
||||
|
||||
when(mockIngredientRepo.getById(any)).thenAnswer((_) async => null);
|
||||
// NutritionNotifier.build() auto-fetches plans on first read.
|
||||
when(mockNutritionRepo.fetchAllPlans()).thenAnswer((_) async => []);
|
||||
|
||||
nutritionalPlanInfoResponse = jsonDecode(
|
||||
fixture('nutrition/nutritional_plan_info_detail_response.json'),
|
||||
|
||||
@@ -83,6 +83,8 @@ void main() {
|
||||
when(mockNutritionRepo.deletePlan(any)).thenAnswer(
|
||||
(_) async => http.Response('', 200),
|
||||
);
|
||||
// NutritionNotifier.build() auto-fetches plans on first read.
|
||||
when(mockNutritionRepo.fetchAllPlans()).thenAnswer((_) async => []);
|
||||
|
||||
container = ProviderContainer(
|
||||
overrides: [
|
||||
|
||||
@@ -51,8 +51,14 @@ void main() {
|
||||
overrides: [routinesRepositoryProvider.overrideWithValue(mockRepo)],
|
||||
);
|
||||
|
||||
// Act
|
||||
// Drain the microtask queue so the build()-time auto-fetch lands
|
||||
// *before* we exercise the explicit call below. Then clear interactions
|
||||
// so the assertion only counts the call we're testing.
|
||||
final notifier = container.read(routinesRiverpodProvider.notifier);
|
||||
await Future<void>.delayed(Duration.zero);
|
||||
clearInteractions(mockRepo);
|
||||
|
||||
// Act
|
||||
await notifier.fetchAllRoutinesSparse();
|
||||
|
||||
// Assert
|
||||
|
||||
Reference in New Issue
Block a user