Skip to content
Open
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
113 changes: 63 additions & 50 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import 'package:wger/providers/exercises.dart';
import 'package:wger/providers/gallery.dart';
import 'package:wger/providers/measurement.dart';
import 'package:wger/providers/nutrition.dart';
import 'package:wger/providers/plate_weights.dart';
import 'package:wger/providers/routines.dart';
import 'package:wger/providers/user.dart';
import 'package:wger/providers/wger_base_riverpod.dart';
Expand Down Expand Up @@ -178,15 +179,6 @@ class MainApp extends StatelessWidget {
update: (context, base, previous) =>
previous ?? ExercisesProvider(WgerBaseProvider(base)),
),
ChangeNotifierProxyProvider2<AuthProvider, ExercisesProvider, RoutinesProvider>(
create: (context) => RoutinesProvider(
WgerBaseProvider(Provider.of(context, listen: false)),
Provider.of(context, listen: false),
[],
),
update: (context, auth, exercises, previous) =>
previous ?? RoutinesProvider(WgerBaseProvider(auth), exercises, []),
),
ChangeNotifierProxyProvider<AuthProvider, NutritionPlansProvider>(
create: (context) => NutritionPlansProvider(
WgerBaseProvider(Provider.of(context, listen: false)),
Expand Down Expand Up @@ -240,47 +232,68 @@ class MainApp extends StatelessWidget {
],
child: riverpod.Consumer(
builder: (rpCtx, ref, _) {
return MaterialApp(
title: 'wger',
navigatorKey: navigatorKey,
theme: wgerLightTheme,
darkTheme: wgerDarkTheme,
highContrastTheme: wgerLightThemeHc,
highContrastDarkTheme: wgerDarkThemeHc,
themeMode: user.themeMode,
home: _getHomeScreen(auth),
routes: {
DashboardScreen.routeName: (ctx) => const DashboardScreen(),
FormScreen.routeName: (ctx) => const FormScreen(),
GalleryScreen.routeName: (ctx) => const GalleryScreen(),
GymModeScreen.routeName: (ctx) => const GymModeScreen(),
HomeTabsScreen.routeName: (ctx) => HomeTabsScreen(),
MeasurementCategoriesScreen.routeName: (ctx) =>
const MeasurementCategoriesScreen(),
MeasurementEntriesScreen.routeName: (ctx) => const MeasurementEntriesScreen(),
NutritionalPlansScreen.routeName: (ctx) => const NutritionalPlansScreen(),
NutritionalDiaryScreen.routeName: (ctx) => const NutritionalDiaryScreen(),
NutritionalPlanScreen.routeName: (ctx) => const NutritionalPlanScreen(),
LogMealsScreen.routeName: (ctx) => const LogMealsScreen(),
LogMealScreen.routeName: (ctx) => const LogMealScreen(),
WeightScreen.routeName: (ctx) => const WeightScreen(),
RoutineScreen.routeName: (ctx) => const RoutineScreen(),
RoutineEditScreen.routeName: (ctx) => const RoutineEditScreen(),
WorkoutLogsScreen.routeName: (ctx) => const WorkoutLogsScreen(),
RoutineListScreen.routeName: (ctx) => const RoutineListScreen(),
ExercisesScreen.routeName: (ctx) => const ExercisesScreen(),
ExerciseDetailScreen.routeName: (ctx) => const ExerciseDetailScreen(),
AddExerciseScreen.routeName: (ctx) => const AddExerciseScreen(),
AboutPage.routeName: (ctx) => const AboutPage(),
SettingsPage.routeName: (ctx) => const SettingsPage(),
LogOverviewPage.routeName: (ctx) => const LogOverviewPage(),
ConfigurePlatesScreen.routeName: (ctx) => const ConfigurePlatesScreen(),
ConfigureDashboardWidgetsScreen.routeName: (ctx) =>
const ConfigureDashboardWidgetsScreen(),
TrophyScreen.routeName: (ctx) => const TrophyScreen(),
},
localizationsDelegates: AppLocalizations.localizationsDelegates,
supportedLocales: AppLocalizations.supportedLocales,
return ChangeNotifierProxyProvider2<
AuthProvider,
ExercisesProvider,
RoutinesProvider
>(
create: (context) => RoutinesProvider(
WgerBaseProvider(Provider.of(context, listen: false)),
Provider.of(context, listen: false),
ref.watch(plateCalculatorProvider.notifier),
[],
),
update: (context, auth, exercises, previous) =>
previous ??
RoutinesProvider(
WgerBaseProvider(auth),
exercises,
ref.watch(plateCalculatorProvider.notifier),
[],
),
child: MaterialApp(
title: 'wger',
navigatorKey: navigatorKey,
theme: wgerLightTheme,
darkTheme: wgerDarkTheme,
highContrastTheme: wgerLightThemeHc,
highContrastDarkTheme: wgerDarkThemeHc,
themeMode: user.themeMode,
home: _getHomeScreen(auth),
routes: {
DashboardScreen.routeName: (ctx) => const DashboardScreen(),
FormScreen.routeName: (ctx) => const FormScreen(),
GalleryScreen.routeName: (ctx) => const GalleryScreen(),
GymModeScreen.routeName: (ctx) => const GymModeScreen(),
HomeTabsScreen.routeName: (ctx) => HomeTabsScreen(),
MeasurementCategoriesScreen.routeName: (ctx) =>
const MeasurementCategoriesScreen(),
MeasurementEntriesScreen.routeName: (ctx) =>
const MeasurementEntriesScreen(),
NutritionalPlansScreen.routeName: (ctx) => const NutritionalPlansScreen(),
NutritionalDiaryScreen.routeName: (ctx) => const NutritionalDiaryScreen(),
NutritionalPlanScreen.routeName: (ctx) => const NutritionalPlanScreen(),
LogMealsScreen.routeName: (ctx) => const LogMealsScreen(),
LogMealScreen.routeName: (ctx) => const LogMealScreen(),
WeightScreen.routeName: (ctx) => const WeightScreen(),
RoutineScreen.routeName: (ctx) => const RoutineScreen(),
RoutineEditScreen.routeName: (ctx) => const RoutineEditScreen(),
WorkoutLogsScreen.routeName: (ctx) => const WorkoutLogsScreen(),
RoutineListScreen.routeName: (ctx) => const RoutineListScreen(),
ExercisesScreen.routeName: (ctx) => const ExercisesScreen(),
ExerciseDetailScreen.routeName: (ctx) => const ExerciseDetailScreen(),
AddExerciseScreen.routeName: (ctx) => const AddExerciseScreen(),
AboutPage.routeName: (ctx) => const AboutPage(),
SettingsPage.routeName: (ctx) => const SettingsPage(),
LogOverviewPage.routeName: (ctx) => const LogOverviewPage(),
ConfigurePlatesScreen.routeName: (ctx) => const ConfigurePlatesScreen(),
ConfigureDashboardWidgetsScreen.routeName: (ctx) =>
const ConfigureDashboardWidgetsScreen(),
TrophyScreen.routeName: (ctx) => const TrophyScreen(),
},
localizationsDelegates: AppLocalizations.localizationsDelegates,
supportedLocales: AppLocalizations.supportedLocales,
),
);
},
),
Expand Down
2 changes: 1 addition & 1 deletion lib/models/workouts/set_config_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ class SetConfigData {
this.maxNrOfSets,
this.weight,
this.maxWeight,
this.weightUnitId = WEIGHT_UNIT_KG,
this.weightUnitId,
this.weightRounding,
this.repetitions,
this.maxRepetitions,
Expand Down
2 changes: 1 addition & 1 deletion lib/models/workouts/set_config_data.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions lib/models/workouts/slot_entry.dart
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ class SlotEntry {
int? order,
SlotEntryType? type,
required Exercise exercise,
int? weightUnitId,
this.weightUnitId,
this.weightRounding,
int? repetitionUnitId,
this.repetitionRounding,
Expand All @@ -203,7 +203,6 @@ class SlotEntry {
this.type = type ?? SlotEntryType.normal;
exerciseObj = exercise;
exerciseId = exercise.id!;
this.weightUnitId = weightUnitId ?? WEIGHT_UNIT_KG;

this.repetitionUnitId = repetitionUnitId ?? REP_UNIT_REPETITIONS_ID;
}
Expand Down
4 changes: 4 additions & 0 deletions lib/providers/plate_weights.dart
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,10 @@ class PlateCalculatorNotifier extends Notifier<PlateCalculatorState> {
}
}

bool isMetricSelected() {
return state.isMetric;
}

Future<void> toggleSelection(num x) async {
final newSelectedPlates = List.of(state.selectedPlates);
if (newSelectedPlates.contains(x)) {
Expand Down
16 changes: 12 additions & 4 deletions lib/providers/routines.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import 'package:wger/models/workouts/slot_entry.dart';
import 'package:wger/models/workouts/weight_unit.dart';
import 'package:wger/providers/base_provider.dart';
import 'package:wger/providers/exercises.dart';
import 'package:wger/providers/plate_weights.dart';

class RoutinesProvider with ChangeNotifier {
final _logger = Logger('RoutinesProvider');
Expand Down Expand Up @@ -67,13 +68,15 @@ class RoutinesProvider with ChangeNotifier {
Routine? activeRoutine;
final ExercisesProvider _exerciseProvider;
final WgerBaseProvider baseProvider;
final PlateCalculatorNotifier _plateCalculatorProvider;
List<Routine> _routines = [];
List<WeightUnit> _weightUnits = [];
List<RepetitionUnit> _repetitionUnits = [];

RoutinesProvider(
this.baseProvider,
this._exerciseProvider,
this._plateCalculatorProvider,
List<Routine> entries, {
List<WeightUnit>? weightUnits,
List<RepetitionUnit>? repetitionUnits,
Expand Down Expand Up @@ -113,7 +116,8 @@ class RoutinesProvider with ChangeNotifier {

/// Return the default weight unit (kg)
WeightUnit get defaultWeightUnit {
return _weightUnits.firstWhere((element) => element.id == WEIGHT_UNIT_KG);
final id = _plateCalculatorProvider.isMetricSelected() ? WEIGHT_UNIT_KG : WEIGHT_UNIT_LB;
return _weightUnits.firstWhere((element) => element.id == id);
}

WeightUnit findWeightUnitById(int id) => _weightUnits.firstWhere((element) => element.id == id);
Expand Down Expand Up @@ -203,9 +207,13 @@ class RoutinesProvider with ChangeNotifier {
(e) => e.id == setConfig.repetitionsUnitId,
);

setConfig.weightUnit = _weightUnits.firstWhere(
(e) => e.id == setConfig.weightUnitId,
);
if (setConfig.weightUnitId != null) {
setConfig.weightUnit = _weightUnits.firstWhere(
(e) => e.id == setConfig.weightUnitId,
);
} else {
setConfig.weightUnit = defaultWeightUnit;
}
}
}
}
Expand Down
8 changes: 8 additions & 0 deletions test/routine/routine_screen_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,26 @@ import 'dart:io';

import 'package:drift/native.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/annotations.dart';
import 'package:mockito/mockito.dart';
import 'package:provider/provider.dart';
import 'package:wger/database/exercises/exercise_database.dart';
import 'package:wger/l10n/generated/app_localizations.dart';
import 'package:wger/providers/base_provider.dart';
import 'package:wger/providers/exercises.dart';
import 'package:wger/providers/plate_weights.dart';
import 'package:wger/providers/routines.dart';
import 'package:wger/screens/routine_screen.dart';

import '../../test_data/routines.dart';
import 'routine_screen_test.mocks.dart';

class PlateNotifierMock extends Notifier<PlateCalculatorState>
with Mock
implements PlateCalculatorNotifier {}

@GenerateMocks([WgerBaseProvider])
void main() {
final mockBaseProvider = MockWgerBaseProvider();
Expand All @@ -48,6 +55,7 @@ void main() {
create: (context) => RoutinesProvider(
mockBaseProvider,
exercisesProvider,
PlateNotifierMock(),
[getTestRoutine()],
),
child: MaterialApp(
Expand Down
48 changes: 42 additions & 6 deletions test/routine/routines_provider_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:http/http.dart';
import 'package:mockito/annotations.dart';
Expand All @@ -34,13 +35,18 @@ import 'package:wger/models/workouts/routine.dart';
import 'package:wger/models/workouts/weight_unit.dart';
import 'package:wger/providers/base_provider.dart';
import 'package:wger/providers/exercises.dart';
import 'package:wger/providers/plate_weights.dart';
import 'package:wger/providers/routines.dart';

import '../../test_data/exercises.dart';
import '../../test_data/routines.dart';
import '../fixtures/fixture_reader.dart';
import 'routines_provider_test.mocks.dart';

class PlateNotifierMock extends Notifier<PlateCalculatorState>
with Mock
implements PlateCalculatorNotifier {}

@GenerateMocks([WgerBaseProvider, ExercisesProvider])
void main() {
final mockBaseProvider = MockWgerBaseProvider();
Expand Down Expand Up @@ -74,7 +80,12 @@ void main() {
);

// Load the entries
final provider = RoutinesProvider(mockBaseProvider, exercisesProvider, []);
final provider = RoutinesProvider(
mockBaseProvider,
exercisesProvider,
PlateNotifierMock(),
[],
);
final plan = await provider.fetchAndSetRoutineSparse(325397);
final plans = provider.getPlans();

Expand Down Expand Up @@ -102,7 +113,12 @@ void main() {
);

// Load the entries
final provider = RoutinesProvider(mockBaseProvider, exercisesProvider, []);
final provider = RoutinesProvider(
mockBaseProvider,
exercisesProvider,
PlateNotifierMock(),
[],
);

await provider.fetchAndSetRoutineSparse(325397);
await provider.deleteRoutine(325397);
Expand All @@ -121,7 +137,12 @@ void main() {
).thenAnswer((_) => Future.value(tRepetitionUnits['results']));

// Load the entries
final provider = RoutinesProvider(mockBaseProvider, exercisesProvider, []);
final provider = RoutinesProvider(
mockBaseProvider,
exercisesProvider,
PlateNotifierMock(),
[],
);
await provider.fetchAndSetRepetitionUnits();
final repetitionUnits = provider.repetitionUnits;

Expand All @@ -139,7 +160,12 @@ void main() {
final ExercisesProvider testExercisesProvider = ExercisesProvider(mockBaseProvider);

// Load the entries
final provider = RoutinesProvider(mockBaseProvider, testExercisesProvider, []);
final provider = RoutinesProvider(
mockBaseProvider,
testExercisesProvider,
PlateNotifierMock(),
[],
);
await provider.fetchAndSetWeightUnits();
final weightUnits = provider.weightUnits;

Expand Down Expand Up @@ -168,7 +194,12 @@ void main() {
final prefs = PreferenceHelper.asyncPref;

// Load the entries
final provider = RoutinesProvider(mockBaseProvider, exercisesProvider, []);
final provider = RoutinesProvider(
mockBaseProvider,
exercisesProvider,
PlateNotifierMock(),
[],
);
await provider.fetchAndSetUnits();
final prefsJson = jsonDecode((await prefs.getString(PREFS_WORKOUT_UNITS))!);

Expand Down Expand Up @@ -229,7 +260,12 @@ void main() {
(_) async => Future.value(testCrunches),
);

final provider = RoutinesProvider(mockBaseProvider, mockExercisesProvider, []);
final provider = RoutinesProvider(
mockBaseProvider,
mockExercisesProvider,
PlateNotifierMock(),
[],
);
provider.repetitionUnits = testRepetitionUnits;
provider.weightUnits = testWeightUnits;

Expand Down
Loading