mirror of
https://github.com/wger-project/flutter.git
synced 2026-12-03 10:33:54 +01:00
Extract routines repository to its own file
This commit is contained in:
@@ -33,7 +33,7 @@ final class NutritionNotifierProvider
|
||||
NutritionNotifier create() => NutritionNotifier();
|
||||
}
|
||||
|
||||
String _$nutritionNotifierHash() => r'cddf4167586ddc55c031d097be7af02e7e1dec33';
|
||||
String _$nutritionNotifierHash() => r'8b4b7cfe44572e929265e43dc3241c67833fec12';
|
||||
|
||||
abstract class _$NutritionNotifier extends $StreamNotifier<NutritionState> {
|
||||
Stream<NutritionState> build();
|
||||
|
||||
@@ -17,7 +17,6 @@
|
||||
*/
|
||||
|
||||
import 'package:collection/collection.dart';
|
||||
import 'package:drift/drift.dart' show OrderingTerm;
|
||||
import 'package:logging/logging.dart';
|
||||
import 'package:riverpod_annotation/riverpod_annotation.dart';
|
||||
import 'package:wger/database/powersync/database.dart';
|
||||
@@ -30,10 +29,9 @@ import 'package:wger/models/workouts/session.dart';
|
||||
import 'package:wger/models/workouts/slot.dart';
|
||||
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/helpers.dart';
|
||||
import 'package:wger/providers/wger_base.dart';
|
||||
import 'package:wger/providers/routines_repository.dart';
|
||||
import 'package:wger/providers/workout_session.dart';
|
||||
|
||||
part 'routines.g.dart';
|
||||
@@ -325,271 +323,3 @@ class RoutinesRiverpod extends _$RoutinesRiverpod {
|
||||
await repo.handleConfigServer(entry, value, type);
|
||||
}
|
||||
}
|
||||
|
||||
@Riverpod(keepAlive: true)
|
||||
RoutinesRepository routinesRepository(Ref ref) {
|
||||
final baseProvider = ref.watch(wgerBaseProvider);
|
||||
final db = ref.read(driftPowerSyncDatabase);
|
||||
return RoutinesRepository(baseProvider, db);
|
||||
}
|
||||
|
||||
class RoutinesRepository {
|
||||
RoutinesRepository(this._baseProvider, this._db);
|
||||
|
||||
final _logger = Logger('RoutinesRepository');
|
||||
final WgerBaseProvider _baseProvider;
|
||||
final DriftPowersyncDatabase _db;
|
||||
static const _routinesUrlPath = 'routine';
|
||||
static const _routinesStructureSubpath = 'structure';
|
||||
static const _routinesDateSequenceDisplaySubpath = 'date-sequence-display';
|
||||
static const _routinesDateSequenceGymSubpath = 'date-sequence-gym';
|
||||
static const _daysUrlPath = 'day';
|
||||
static const _slotsUrlPath = 'slot';
|
||||
static const _slotEntriesUrlPath = 'slot-entry';
|
||||
static const _routineConfigSets = 'sets-config';
|
||||
static const _routineConfigMaxSets = 'max-sets-config';
|
||||
static const _routineConfigWeights = 'weight-config';
|
||||
static const _routineConfigMaxWeights = 'max-weight-config';
|
||||
static const _routineConfigRepetitions = 'repetitions-config';
|
||||
static const _routineConfigMaxRepetitions = 'max-repetitions-config';
|
||||
static const _routineConfigRir = 'rir-config';
|
||||
static const _routineConfigMaxRir = 'max-rir-config';
|
||||
static const _routineConfigRestTime = 'rest-config';
|
||||
static const _routineConfigMaxRestTime = 'max-rest-config';
|
||||
|
||||
/*
|
||||
* Routines
|
||||
*/
|
||||
Future<Routine> fetchAndSetRoutineFullServer(int routineId) async {
|
||||
// Fetch structure and computed data
|
||||
final results = await Future.wait([
|
||||
_baseProvider.fetch(
|
||||
_baseProvider.makeUrl(
|
||||
_routinesUrlPath,
|
||||
objectMethod: _routinesStructureSubpath,
|
||||
id: routineId,
|
||||
),
|
||||
),
|
||||
_baseProvider.fetch(
|
||||
_baseProvider.makeUrl(
|
||||
_routinesUrlPath,
|
||||
id: routineId,
|
||||
objectMethod: _routinesDateSequenceDisplaySubpath,
|
||||
),
|
||||
),
|
||||
_baseProvider.fetch(
|
||||
_baseProvider.makeUrl(
|
||||
_routinesUrlPath,
|
||||
id: routineId,
|
||||
objectMethod: _routinesDateSequenceGymSubpath,
|
||||
),
|
||||
),
|
||||
]);
|
||||
|
||||
final routine = Routine.fromJson(results[0] as Map<String, dynamic>);
|
||||
final dayData = results[1] as List<dynamic>;
|
||||
final dayDataGym = results[2] as List<dynamic>;
|
||||
|
||||
routine.dayData = dayData.map((entry) => DayData.fromJson(entry)).toList();
|
||||
routine.dayDataGym = dayDataGym.map((entry) => DayData.fromJson(entry)).toList();
|
||||
|
||||
return routine;
|
||||
}
|
||||
|
||||
Future<Routine> addRoutineServer(Routine routine) async {
|
||||
final data = await _baseProvider.post(
|
||||
routine.toJson(),
|
||||
_baseProvider.makeUrl(_routinesUrlPath),
|
||||
);
|
||||
return Routine.fromJson(data);
|
||||
}
|
||||
|
||||
Future<Routine> editRoutineServer(Routine routine) async {
|
||||
await _baseProvider.patch(
|
||||
routine.toJson(),
|
||||
_baseProvider.makeUrl(_routinesUrlPath, id: routine.id),
|
||||
);
|
||||
return fetchAndSetRoutineFullServer(routine.id!);
|
||||
}
|
||||
|
||||
Future<void> deleteRoutineServer(int id) async {
|
||||
await _baseProvider.deleteRequest(_routinesUrlPath, id);
|
||||
}
|
||||
|
||||
/// Streams all routines from the local PowerSync-backed Drift table.
|
||||
///
|
||||
/// Sort matches Django's `Routine.Meta.ordering = ['-start', '-created']` so
|
||||
/// that REST responses and PowerSync emissions agree on the row order. The
|
||||
/// UI takes the first row as the active routine ([RoutinesState.currentRoutine])
|
||||
/// and [addRoutine]'s optimistic insertion assumes newest-first.
|
||||
Stream<List<Routine>> watchAllDrift() {
|
||||
_logger.finer('Watching all local routines');
|
||||
return (_db.select(_db.routineTable)..orderBy([
|
||||
(t) => OrderingTerm.desc(t.start),
|
||||
(t) => OrderingTerm.desc(t.created),
|
||||
]))
|
||||
.watch();
|
||||
}
|
||||
|
||||
/// Updates a routine via the local Drift table
|
||||
Future<void> editLocalDrift(Routine routine) async {
|
||||
final id = routine.id;
|
||||
if (id == null) {
|
||||
throw StateError('Cannot edit a routine without id');
|
||||
}
|
||||
_logger.finer('Updating local routine $id');
|
||||
await (_db.update(
|
||||
_db.routineTable,
|
||||
)..where((t) => t.id.equals(id))).write(routine.toCompanion());
|
||||
}
|
||||
|
||||
/// Deletes a routine via the local Drift table
|
||||
Future<void> deleteLocalDrift(int id) async {
|
||||
_logger.finer('Deleting local routine $id');
|
||||
await (_db.delete(_db.routineTable)..where((t) => t.id.equals(id))).go();
|
||||
}
|
||||
|
||||
/*
|
||||
* Days
|
||||
*/
|
||||
Future<Day> addDayServer(Day day) async {
|
||||
final data = await _baseProvider.post(
|
||||
day.toJson(),
|
||||
_baseProvider.makeUrl(_daysUrlPath),
|
||||
);
|
||||
day = Day.fromJson(data);
|
||||
|
||||
fetchAndSetRoutineFullServer(day.routineId);
|
||||
|
||||
return day;
|
||||
}
|
||||
|
||||
Future<void> editDayServer(Day day) async {
|
||||
await _baseProvider.patch(
|
||||
day.toJson(),
|
||||
_baseProvider.makeUrl(_daysUrlPath, id: day.id),
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> deleteDayServer(int dayId) async {
|
||||
await _baseProvider.deleteRequest(_daysUrlPath, dayId);
|
||||
}
|
||||
|
||||
/*
|
||||
* Slots
|
||||
*/
|
||||
Future<Slot> addSlotServer(Slot slot) async {
|
||||
final data = await _baseProvider.post(
|
||||
slot.toJson(),
|
||||
_baseProvider.makeUrl(_slotsUrlPath),
|
||||
);
|
||||
final newSlot = Slot.fromJson(data);
|
||||
return newSlot;
|
||||
}
|
||||
|
||||
Future<void> deleteSlotServer(int slotId) async {
|
||||
await _baseProvider.deleteRequest(_slotsUrlPath, slotId);
|
||||
}
|
||||
|
||||
Future<void> editSlotServer(Slot slot) async {
|
||||
await _baseProvider.patch(
|
||||
slot.toJson(),
|
||||
_baseProvider.makeUrl(_slotsUrlPath, id: slot.id),
|
||||
);
|
||||
}
|
||||
|
||||
/*
|
||||
* Slot entries
|
||||
*/
|
||||
Future<SlotEntry> addSlotEntryServer(SlotEntry entry) async {
|
||||
final data = await _baseProvider.post(
|
||||
entry.toJson(),
|
||||
_baseProvider.makeUrl(_slotEntriesUrlPath),
|
||||
);
|
||||
return SlotEntry.fromJson(data);
|
||||
}
|
||||
|
||||
Future<void> deleteSlotEntryServer(int id) async {
|
||||
await _baseProvider.deleteRequest(_slotEntriesUrlPath, id);
|
||||
}
|
||||
|
||||
Future<void> editSlotEntryServer(SlotEntry entry) async {
|
||||
await _baseProvider.patch(
|
||||
entry.toJson(),
|
||||
_baseProvider.makeUrl(_slotEntriesUrlPath, id: entry.id),
|
||||
);
|
||||
}
|
||||
|
||||
/*
|
||||
* Configs
|
||||
*/
|
||||
String _getConfigUrl(ConfigType type) {
|
||||
switch (type) {
|
||||
case ConfigType.sets:
|
||||
return _routineConfigSets;
|
||||
case ConfigType.maxSets:
|
||||
return _routineConfigMaxSets;
|
||||
case ConfigType.weight:
|
||||
return _routineConfigWeights;
|
||||
case ConfigType.maxWeight:
|
||||
return _routineConfigMaxWeights;
|
||||
case ConfigType.repetitions:
|
||||
return _routineConfigRepetitions;
|
||||
case ConfigType.maxRepetitions:
|
||||
return _routineConfigMaxRepetitions;
|
||||
case ConfigType.rir:
|
||||
return _routineConfigRir;
|
||||
case ConfigType.maxRir:
|
||||
return _routineConfigMaxRir;
|
||||
case ConfigType.rest:
|
||||
return _routineConfigRestTime;
|
||||
case ConfigType.maxRest:
|
||||
return _routineConfigMaxRestTime;
|
||||
}
|
||||
}
|
||||
|
||||
Future<BaseConfig> editConfigServer(BaseConfig config, ConfigType type) async {
|
||||
final data = await _baseProvider.patch(
|
||||
config.toJson(),
|
||||
_baseProvider.makeUrl(_getConfigUrl(type), id: config.id),
|
||||
);
|
||||
|
||||
return BaseConfig.fromJson(data);
|
||||
}
|
||||
|
||||
Future<BaseConfig> addConfigServer(BaseConfig config, ConfigType type) async {
|
||||
final data = await _baseProvider.post(
|
||||
config.toJson(),
|
||||
_baseProvider.makeUrl(_getConfigUrl(type)),
|
||||
);
|
||||
return BaseConfig.fromJson(data);
|
||||
}
|
||||
|
||||
Future<void> deleteConfigServer(int id, ConfigType type) async {
|
||||
await _baseProvider.deleteRequest(_getConfigUrl(type), id);
|
||||
}
|
||||
|
||||
Future<void> handleConfigServer(SlotEntry entry, num? value, ConfigType type) async {
|
||||
final configs = entry.getConfigsByType(type);
|
||||
final config = configs.isNotEmpty ? configs.first : null;
|
||||
// final value = input.isNotEmpty ? num.parse(input) : null;
|
||||
|
||||
if (value == null && config != null) {
|
||||
// Value removed, delete entry
|
||||
configs.removeWhere((c) => c.id! == config.id!);
|
||||
await deleteConfigServer(config.id!, type);
|
||||
} else if (config != null) {
|
||||
// Update existing value
|
||||
configs.first.value = value!;
|
||||
await editConfigServer(configs.first, type);
|
||||
} else if (value != null && config == null) {
|
||||
// Create new config
|
||||
configs.add(
|
||||
await addConfigServer(
|
||||
BaseConfig.firstIteration(value, entry.id!),
|
||||
type,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -111,7 +111,7 @@ final class RoutinesRiverpodProvider
|
||||
RoutinesRiverpod create() => RoutinesRiverpod();
|
||||
}
|
||||
|
||||
String _$routinesRiverpodHash() => r'd98b72296e7fcf0134123452c5838991d4d80a27';
|
||||
String _$routinesRiverpodHash() => r'9574d6cbb8701111e8ec1c8c60081bed56aadc3c';
|
||||
|
||||
abstract class _$RoutinesRiverpod extends $StreamNotifier<RoutinesState> {
|
||||
Stream<RoutinesState> build();
|
||||
@@ -130,45 +130,3 @@ abstract class _$RoutinesRiverpod extends $StreamNotifier<RoutinesState> {
|
||||
element.handleCreate(ref, build);
|
||||
}
|
||||
}
|
||||
|
||||
@ProviderFor(routinesRepository)
|
||||
final routinesRepositoryProvider = RoutinesRepositoryProvider._();
|
||||
|
||||
final class RoutinesRepositoryProvider
|
||||
extends $FunctionalProvider<RoutinesRepository, RoutinesRepository, RoutinesRepository>
|
||||
with $Provider<RoutinesRepository> {
|
||||
RoutinesRepositoryProvider._()
|
||||
: super(
|
||||
from: null,
|
||||
argument: null,
|
||||
retry: null,
|
||||
name: r'routinesRepositoryProvider',
|
||||
isAutoDispose: false,
|
||||
dependencies: null,
|
||||
$allTransitiveDependencies: null,
|
||||
);
|
||||
|
||||
@override
|
||||
String debugGetCreateSourceHash() => _$routinesRepositoryHash();
|
||||
|
||||
@$internal
|
||||
@override
|
||||
$ProviderElement<RoutinesRepository> $createElement(
|
||||
$ProviderPointer pointer,
|
||||
) => $ProviderElement(pointer);
|
||||
|
||||
@override
|
||||
RoutinesRepository create(Ref ref) {
|
||||
return routinesRepository(ref);
|
||||
}
|
||||
|
||||
/// {@macro riverpod.override_with_value}
|
||||
Override overrideWithValue(RoutinesRepository value) {
|
||||
return $ProviderOverride(
|
||||
origin: this,
|
||||
providerOverride: $SyncValueProvider<RoutinesRepository>(value),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
String _$routinesRepositoryHash() => r'249951c94887d0ff4882d7a88bd9dcfb25282f4e';
|
||||
|
||||
309
lib/providers/routines_repository.dart
Normal file
309
lib/providers/routines_repository.dart
Normal file
@@ -0,0 +1,309 @@
|
||||
/*
|
||||
* This file is part of wger Workout Manager <https://github.com/wger-project>.
|
||||
* Copyright (c) 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.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
import 'package:drift/drift.dart' show OrderingTerm;
|
||||
import 'package:logging/logging.dart';
|
||||
import 'package:riverpod_annotation/riverpod_annotation.dart';
|
||||
import 'package:wger/database/powersync/database.dart';
|
||||
import 'package:wger/models/workouts/base_config.dart';
|
||||
import 'package:wger/models/workouts/day.dart';
|
||||
import 'package:wger/models/workouts/day_data.dart';
|
||||
import 'package:wger/models/workouts/routine.dart';
|
||||
import 'package:wger/models/workouts/slot.dart';
|
||||
import 'package:wger/models/workouts/slot_entry.dart';
|
||||
import 'package:wger/providers/base_provider.dart';
|
||||
import 'package:wger/providers/wger_base.dart';
|
||||
|
||||
part 'routines_repository.g.dart';
|
||||
|
||||
@Riverpod(keepAlive: true)
|
||||
RoutinesRepository routinesRepository(Ref ref) {
|
||||
final baseProvider = ref.watch(wgerBaseProvider);
|
||||
final db = ref.read(driftPowerSyncDatabase);
|
||||
return RoutinesRepository(baseProvider, db);
|
||||
}
|
||||
|
||||
/// Data access for routine-related entities.
|
||||
///
|
||||
/// HTTP for everything that hasn't been migrated to PowerSync yet (creation
|
||||
/// of routines, days, slots, slot entries, configs, …). Top-level [Routine]
|
||||
/// reads as well as edit/delete go through the local PowerSync-backed Drift
|
||||
/// table — see [watchAllDrift], [editLocalDrift], [deleteLocalDrift].
|
||||
class RoutinesRepository {
|
||||
RoutinesRepository(this._baseProvider, this._db);
|
||||
|
||||
final _logger = Logger('RoutinesRepository');
|
||||
final WgerBaseProvider _baseProvider;
|
||||
final DriftPowersyncDatabase _db;
|
||||
static const _routinesUrlPath = 'routine';
|
||||
static const _routinesStructureSubpath = 'structure';
|
||||
static const _routinesDateSequenceDisplaySubpath = 'date-sequence-display';
|
||||
static const _routinesDateSequenceGymSubpath = 'date-sequence-gym';
|
||||
static const _daysUrlPath = 'day';
|
||||
static const _slotsUrlPath = 'slot';
|
||||
static const _slotEntriesUrlPath = 'slot-entry';
|
||||
static const _routineConfigSets = 'sets-config';
|
||||
static const _routineConfigMaxSets = 'max-sets-config';
|
||||
static const _routineConfigWeights = 'weight-config';
|
||||
static const _routineConfigMaxWeights = 'max-weight-config';
|
||||
static const _routineConfigRepetitions = 'repetitions-config';
|
||||
static const _routineConfigMaxRepetitions = 'max-repetitions-config';
|
||||
static const _routineConfigRir = 'rir-config';
|
||||
static const _routineConfigMaxRir = 'max-rir-config';
|
||||
static const _routineConfigRestTime = 'rest-config';
|
||||
static const _routineConfigMaxRestTime = 'max-rest-config';
|
||||
|
||||
/*
|
||||
* Routines
|
||||
*/
|
||||
Future<Routine> fetchAndSetRoutineFullServer(int routineId) async {
|
||||
// Fetch structure and computed data
|
||||
final results = await Future.wait([
|
||||
_baseProvider.fetch(
|
||||
_baseProvider.makeUrl(
|
||||
_routinesUrlPath,
|
||||
objectMethod: _routinesStructureSubpath,
|
||||
id: routineId,
|
||||
),
|
||||
),
|
||||
_baseProvider.fetch(
|
||||
_baseProvider.makeUrl(
|
||||
_routinesUrlPath,
|
||||
id: routineId,
|
||||
objectMethod: _routinesDateSequenceDisplaySubpath,
|
||||
),
|
||||
),
|
||||
_baseProvider.fetch(
|
||||
_baseProvider.makeUrl(
|
||||
_routinesUrlPath,
|
||||
id: routineId,
|
||||
objectMethod: _routinesDateSequenceGymSubpath,
|
||||
),
|
||||
),
|
||||
]);
|
||||
|
||||
final routine = Routine.fromJson(results[0] as Map<String, dynamic>);
|
||||
final dayData = results[1] as List<dynamic>;
|
||||
final dayDataGym = results[2] as List<dynamic>;
|
||||
|
||||
routine.dayData = dayData.map((entry) => DayData.fromJson(entry)).toList();
|
||||
routine.dayDataGym = dayDataGym.map((entry) => DayData.fromJson(entry)).toList();
|
||||
|
||||
return routine;
|
||||
}
|
||||
|
||||
Future<Routine> addRoutineServer(Routine routine) async {
|
||||
final data = await _baseProvider.post(
|
||||
routine.toJson(),
|
||||
_baseProvider.makeUrl(_routinesUrlPath),
|
||||
);
|
||||
return Routine.fromJson(data);
|
||||
}
|
||||
|
||||
Future<Routine> editRoutineServer(Routine routine) async {
|
||||
await _baseProvider.patch(
|
||||
routine.toJson(),
|
||||
_baseProvider.makeUrl(_routinesUrlPath, id: routine.id),
|
||||
);
|
||||
return fetchAndSetRoutineFullServer(routine.id!);
|
||||
}
|
||||
|
||||
Future<void> deleteRoutineServer(int id) async {
|
||||
await _baseProvider.deleteRequest(_routinesUrlPath, id);
|
||||
}
|
||||
|
||||
/// Streams all routines from the local PowerSync-backed Drift table.
|
||||
///
|
||||
/// Templates (`is_template = TRUE`, including public ones) are excluded at
|
||||
/// the sync-rule level (see `sync_rules.yaml`), so they never reach the
|
||||
/// local DB — no client-side filter needed here.
|
||||
///
|
||||
/// Sort matches Django's `Routine.Meta.ordering = ['-start', '-created']` so
|
||||
/// that REST responses and PowerSync emissions agree on the row order. The
|
||||
/// UI takes the first row as the active routine and the optimistic
|
||||
/// insertion in `RoutinesRiverpod.addRoutine` assumes newest-first.
|
||||
Stream<List<Routine>> watchAllDrift() {
|
||||
_logger.finer('Watching all local routines');
|
||||
return (_db.select(_db.routineTable)..orderBy([
|
||||
(t) => OrderingTerm.desc(t.start),
|
||||
(t) => OrderingTerm.desc(t.created),
|
||||
]))
|
||||
.watch();
|
||||
}
|
||||
|
||||
/// Updates a routine via the local Drift table.
|
||||
Future<void> editLocalDrift(Routine routine) async {
|
||||
final id = routine.id;
|
||||
if (id == null) {
|
||||
throw StateError('Cannot edit a routine without id');
|
||||
}
|
||||
_logger.finer('Updating local routine $id');
|
||||
await (_db.update(
|
||||
_db.routineTable,
|
||||
)..where((t) => t.id.equals(id))).write(routine.toCompanion());
|
||||
}
|
||||
|
||||
/// Deletes a routine via the local Drift table.
|
||||
Future<void> deleteLocalDrift(int id) async {
|
||||
_logger.finer('Deleting local routine $id');
|
||||
await (_db.delete(_db.routineTable)..where((t) => t.id.equals(id))).go();
|
||||
}
|
||||
|
||||
/*
|
||||
* Days
|
||||
*/
|
||||
Future<Day> addDayServer(Day day) async {
|
||||
final data = await _baseProvider.post(
|
||||
day.toJson(),
|
||||
_baseProvider.makeUrl(_daysUrlPath),
|
||||
);
|
||||
day = Day.fromJson(data);
|
||||
|
||||
fetchAndSetRoutineFullServer(day.routineId);
|
||||
|
||||
return day;
|
||||
}
|
||||
|
||||
Future<void> editDayServer(Day day) async {
|
||||
await _baseProvider.patch(
|
||||
day.toJson(),
|
||||
_baseProvider.makeUrl(_daysUrlPath, id: day.id),
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> deleteDayServer(int dayId) async {
|
||||
await _baseProvider.deleteRequest(_daysUrlPath, dayId);
|
||||
}
|
||||
|
||||
/*
|
||||
* Slots
|
||||
*/
|
||||
Future<Slot> addSlotServer(Slot slot) async {
|
||||
final data = await _baseProvider.post(
|
||||
slot.toJson(),
|
||||
_baseProvider.makeUrl(_slotsUrlPath),
|
||||
);
|
||||
final newSlot = Slot.fromJson(data);
|
||||
return newSlot;
|
||||
}
|
||||
|
||||
Future<void> deleteSlotServer(int slotId) async {
|
||||
await _baseProvider.deleteRequest(_slotsUrlPath, slotId);
|
||||
}
|
||||
|
||||
Future<void> editSlotServer(Slot slot) async {
|
||||
await _baseProvider.patch(
|
||||
slot.toJson(),
|
||||
_baseProvider.makeUrl(_slotsUrlPath, id: slot.id),
|
||||
);
|
||||
}
|
||||
|
||||
/*
|
||||
* Slot entries
|
||||
*/
|
||||
Future<SlotEntry> addSlotEntryServer(SlotEntry entry) async {
|
||||
final data = await _baseProvider.post(
|
||||
entry.toJson(),
|
||||
_baseProvider.makeUrl(_slotEntriesUrlPath),
|
||||
);
|
||||
return SlotEntry.fromJson(data);
|
||||
}
|
||||
|
||||
Future<void> deleteSlotEntryServer(int id) async {
|
||||
await _baseProvider.deleteRequest(_slotEntriesUrlPath, id);
|
||||
}
|
||||
|
||||
Future<void> editSlotEntryServer(SlotEntry entry) async {
|
||||
await _baseProvider.patch(
|
||||
entry.toJson(),
|
||||
_baseProvider.makeUrl(_slotEntriesUrlPath, id: entry.id),
|
||||
);
|
||||
}
|
||||
|
||||
/*
|
||||
* Configs
|
||||
*/
|
||||
String _getConfigUrl(ConfigType type) {
|
||||
switch (type) {
|
||||
case ConfigType.sets:
|
||||
return _routineConfigSets;
|
||||
case ConfigType.maxSets:
|
||||
return _routineConfigMaxSets;
|
||||
case ConfigType.weight:
|
||||
return _routineConfigWeights;
|
||||
case ConfigType.maxWeight:
|
||||
return _routineConfigMaxWeights;
|
||||
case ConfigType.repetitions:
|
||||
return _routineConfigRepetitions;
|
||||
case ConfigType.maxRepetitions:
|
||||
return _routineConfigMaxRepetitions;
|
||||
case ConfigType.rir:
|
||||
return _routineConfigRir;
|
||||
case ConfigType.maxRir:
|
||||
return _routineConfigMaxRir;
|
||||
case ConfigType.rest:
|
||||
return _routineConfigRestTime;
|
||||
case ConfigType.maxRest:
|
||||
return _routineConfigMaxRestTime;
|
||||
}
|
||||
}
|
||||
|
||||
Future<BaseConfig> editConfigServer(BaseConfig config, ConfigType type) async {
|
||||
final data = await _baseProvider.patch(
|
||||
config.toJson(),
|
||||
_baseProvider.makeUrl(_getConfigUrl(type), id: config.id),
|
||||
);
|
||||
|
||||
return BaseConfig.fromJson(data);
|
||||
}
|
||||
|
||||
Future<BaseConfig> addConfigServer(BaseConfig config, ConfigType type) async {
|
||||
final data = await _baseProvider.post(
|
||||
config.toJson(),
|
||||
_baseProvider.makeUrl(_getConfigUrl(type)),
|
||||
);
|
||||
return BaseConfig.fromJson(data);
|
||||
}
|
||||
|
||||
Future<void> deleteConfigServer(int id, ConfigType type) async {
|
||||
await _baseProvider.deleteRequest(_getConfigUrl(type), id);
|
||||
}
|
||||
|
||||
Future<void> handleConfigServer(SlotEntry entry, num? value, ConfigType type) async {
|
||||
final configs = entry.getConfigsByType(type);
|
||||
final config = configs.isNotEmpty ? configs.first : null;
|
||||
|
||||
if (value == null && config != null) {
|
||||
// Value removed, delete entry
|
||||
configs.removeWhere((c) => c.id! == config.id!);
|
||||
await deleteConfigServer(config.id!, type);
|
||||
} else if (config != null) {
|
||||
// Update existing value
|
||||
configs.first.value = value!;
|
||||
await editConfigServer(configs.first, type);
|
||||
} else if (value != null && config == null) {
|
||||
// Create new config
|
||||
configs.add(
|
||||
await addConfigServer(
|
||||
BaseConfig.firstIteration(value, entry.id!),
|
||||
type,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
52
lib/providers/routines_repository.g.dart
Normal file
52
lib/providers/routines_repository.g.dart
Normal file
@@ -0,0 +1,52 @@
|
||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||
|
||||
part of 'routines_repository.dart';
|
||||
|
||||
// **************************************************************************
|
||||
// RiverpodGenerator
|
||||
// **************************************************************************
|
||||
|
||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||
// ignore_for_file: type=lint, type=warning
|
||||
|
||||
@ProviderFor(routinesRepository)
|
||||
final routinesRepositoryProvider = RoutinesRepositoryProvider._();
|
||||
|
||||
final class RoutinesRepositoryProvider
|
||||
extends $FunctionalProvider<RoutinesRepository, RoutinesRepository, RoutinesRepository>
|
||||
with $Provider<RoutinesRepository> {
|
||||
RoutinesRepositoryProvider._()
|
||||
: super(
|
||||
from: null,
|
||||
argument: null,
|
||||
retry: null,
|
||||
name: r'routinesRepositoryProvider',
|
||||
isAutoDispose: false,
|
||||
dependencies: null,
|
||||
$allTransitiveDependencies: null,
|
||||
);
|
||||
|
||||
@override
|
||||
String debugGetCreateSourceHash() => _$routinesRepositoryHash();
|
||||
|
||||
@$internal
|
||||
@override
|
||||
$ProviderElement<RoutinesRepository> $createElement(
|
||||
$ProviderPointer pointer,
|
||||
) => $ProviderElement(pointer);
|
||||
|
||||
@override
|
||||
RoutinesRepository create(Ref ref) {
|
||||
return routinesRepository(ref);
|
||||
}
|
||||
|
||||
/// {@macro riverpod.override_with_value}
|
||||
Override overrideWithValue(RoutinesRepository value) {
|
||||
return $ProviderOverride(
|
||||
origin: this,
|
||||
providerOverride: $SyncValueProvider<RoutinesRepository>(value),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
String _$routinesRepositoryHash() => r'249951c94887d0ff4882d7a88bd9dcfb25282f4e';
|
||||
@@ -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) 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.
|
||||
@@ -25,7 +25,7 @@ import 'package:wger/helpers/consts.dart';
|
||||
import 'package:wger/l10n/generated/app_localizations.dart';
|
||||
import 'package:wger/models/workouts/day.dart';
|
||||
import 'package:wger/providers/network_provider.dart';
|
||||
import 'package:wger/providers/routines.dart';
|
||||
import 'package:wger/providers/routines_repository.dart';
|
||||
import 'package:wger/widgets/routines/forms/day.dart';
|
||||
|
||||
import '../../test_data/routines.dart';
|
||||
|
||||
@@ -11,7 +11,7 @@ import 'package:wger/models/workouts/day.dart' as _i3;
|
||||
import 'package:wger/models/workouts/routine.dart' as _i2;
|
||||
import 'package:wger/models/workouts/slot.dart' as _i4;
|
||||
import 'package:wger/models/workouts/slot_entry.dart' as _i5;
|
||||
import 'package:wger/providers/routines.dart' as _i7;
|
||||
import 'package:wger/providers/routines_repository.dart' as _i7;
|
||||
|
||||
// ignore_for_file: type=lint
|
||||
// ignore_for_file: avoid_redundant_argument_values
|
||||
@@ -56,14 +56,6 @@ class MockRoutinesRepository extends _i1.Mock implements _i7.RoutinesRepository
|
||||
_i1.throwOnMissingStub(this);
|
||||
}
|
||||
|
||||
@override
|
||||
_i8.Future<List<_i2.Routine>> fetchAllRoutinesSparseServer() =>
|
||||
(super.noSuchMethod(
|
||||
Invocation.method(#fetchAllRoutinesSparseServer, []),
|
||||
returnValue: _i8.Future<List<_i2.Routine>>.value(<_i2.Routine>[]),
|
||||
)
|
||||
as _i8.Future<List<_i2.Routine>>);
|
||||
|
||||
@override
|
||||
_i8.Future<_i2.Routine> fetchAndSetRoutineFullServer(int? routineId) =>
|
||||
(super.noSuchMethod(
|
||||
|
||||
@@ -25,6 +25,7 @@ import 'package:wger/l10n/generated/app_localizations.dart';
|
||||
import 'package:wger/models/workouts/slot.dart';
|
||||
import 'package:wger/models/workouts/slot_entry.dart';
|
||||
import 'package:wger/providers/routines.dart';
|
||||
import 'package:wger/providers/routines_repository.dart';
|
||||
import 'package:wger/widgets/routines/forms/slot.dart';
|
||||
|
||||
import '../../../test_data/routines.dart';
|
||||
|
||||
@@ -11,7 +11,7 @@ import 'package:wger/models/workouts/day.dart' as _i3;
|
||||
import 'package:wger/models/workouts/routine.dart' as _i2;
|
||||
import 'package:wger/models/workouts/slot.dart' as _i4;
|
||||
import 'package:wger/models/workouts/slot_entry.dart' as _i5;
|
||||
import 'package:wger/providers/routines.dart' as _i7;
|
||||
import 'package:wger/providers/routines_repository.dart' as _i7;
|
||||
|
||||
// ignore_for_file: type=lint
|
||||
// ignore_for_file: avoid_redundant_argument_values
|
||||
@@ -56,14 +56,6 @@ class MockRoutinesRepository extends _i1.Mock implements _i7.RoutinesRepository
|
||||
_i1.throwOnMissingStub(this);
|
||||
}
|
||||
|
||||
@override
|
||||
_i8.Future<List<_i2.Routine>> fetchAllRoutinesSparseServer() =>
|
||||
(super.noSuchMethod(
|
||||
Invocation.method(#fetchAllRoutinesSparseServer, []),
|
||||
returnValue: _i8.Future<List<_i2.Routine>>.value(<_i2.Routine>[]),
|
||||
)
|
||||
as _i8.Future<List<_i2.Routine>>);
|
||||
|
||||
@override
|
||||
_i8.Future<_i2.Routine> fetchAndSetRoutineFullServer(int? routineId) =>
|
||||
(super.noSuchMethod(
|
||||
|
||||
@@ -22,7 +22,7 @@ import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:mockito/annotations.dart';
|
||||
import 'package:mockito/mockito.dart';
|
||||
import 'package:wger/l10n/generated/app_localizations.dart';
|
||||
import 'package:wger/providers/routines.dart';
|
||||
import 'package:wger/providers/routines_repository.dart';
|
||||
import 'package:wger/widgets/routines/forms/day.dart';
|
||||
import 'package:wger/widgets/routines/routine_edit.dart';
|
||||
|
||||
|
||||
@@ -11,7 +11,7 @@ import 'package:wger/models/workouts/day.dart' as _i3;
|
||||
import 'package:wger/models/workouts/routine.dart' as _i2;
|
||||
import 'package:wger/models/workouts/slot.dart' as _i4;
|
||||
import 'package:wger/models/workouts/slot_entry.dart' as _i5;
|
||||
import 'package:wger/providers/routines.dart' as _i7;
|
||||
import 'package:wger/providers/routines_repository.dart' as _i7;
|
||||
|
||||
// ignore_for_file: type=lint
|
||||
// ignore_for_file: avoid_redundant_argument_values
|
||||
@@ -56,14 +56,6 @@ class MockRoutinesRepository extends _i1.Mock implements _i7.RoutinesRepository
|
||||
_i1.throwOnMissingStub(this);
|
||||
}
|
||||
|
||||
@override
|
||||
_i8.Future<List<_i2.Routine>> fetchAllRoutinesSparseServer() =>
|
||||
(super.noSuchMethod(
|
||||
Invocation.method(#fetchAllRoutinesSparseServer, []),
|
||||
returnValue: _i8.Future<List<_i2.Routine>>.value(<_i2.Routine>[]),
|
||||
)
|
||||
as _i8.Future<List<_i2.Routine>>);
|
||||
|
||||
@override
|
||||
_i8.Future<_i2.Routine> fetchAndSetRoutineFullServer(int? routineId) =>
|
||||
(super.noSuchMethod(
|
||||
|
||||
@@ -28,7 +28,7 @@ import 'package:wger/helpers/consts.dart';
|
||||
import 'package:wger/l10n/generated/app_localizations.dart';
|
||||
import 'package:wger/models/workouts/routine.dart';
|
||||
import 'package:wger/providers/network_provider.dart';
|
||||
import 'package:wger/providers/routines.dart';
|
||||
import 'package:wger/providers/routines_repository.dart';
|
||||
import 'package:wger/screens/routine_edit_screen.dart';
|
||||
import 'package:wger/screens/routine_screen.dart';
|
||||
import 'package:wger/widgets/routines/forms/routine.dart';
|
||||
|
||||
@@ -11,7 +11,7 @@ import 'package:wger/models/workouts/day.dart' as _i3;
|
||||
import 'package:wger/models/workouts/routine.dart' as _i2;
|
||||
import 'package:wger/models/workouts/slot.dart' as _i4;
|
||||
import 'package:wger/models/workouts/slot_entry.dart' as _i5;
|
||||
import 'package:wger/providers/routines.dart' as _i7;
|
||||
import 'package:wger/providers/routines_repository.dart' as _i7;
|
||||
|
||||
// ignore_for_file: type=lint
|
||||
// ignore_for_file: avoid_redundant_argument_values
|
||||
@@ -56,14 +56,6 @@ class MockRoutinesRepository extends _i1.Mock implements _i7.RoutinesRepository
|
||||
_i1.throwOnMissingStub(this);
|
||||
}
|
||||
|
||||
@override
|
||||
_i8.Future<List<_i2.Routine>> fetchAllRoutinesSparseServer() =>
|
||||
(super.noSuchMethod(
|
||||
Invocation.method(#fetchAllRoutinesSparseServer, []),
|
||||
returnValue: _i8.Future<List<_i2.Routine>>.value(<_i2.Routine>[]),
|
||||
)
|
||||
as _i8.Future<List<_i2.Routine>>);
|
||||
|
||||
@override
|
||||
_i8.Future<_i2.Routine> fetchAndSetRoutineFullServer(int? routineId) =>
|
||||
(super.noSuchMethod(
|
||||
|
||||
@@ -25,7 +25,7 @@ import 'package:mockito/mockito.dart';
|
||||
import 'package:wger/database/powersync/database.dart';
|
||||
import 'package:wger/models/workouts/routine.dart';
|
||||
import 'package:wger/providers/base_provider.dart';
|
||||
import 'package:wger/providers/routines.dart';
|
||||
import 'package:wger/providers/routines_repository.dart';
|
||||
|
||||
import '../fixtures/fixture_reader.dart';
|
||||
import 'routines_provider_repository_test.mocks.dart';
|
||||
|
||||
@@ -23,6 +23,7 @@ import 'package:mockito/mockito.dart';
|
||||
import 'package:wger/models/workouts/day.dart';
|
||||
import 'package:wger/models/workouts/routine.dart';
|
||||
import 'package:wger/providers/routines.dart';
|
||||
import 'package:wger/providers/routines_repository.dart';
|
||||
|
||||
import '../../test_data/routines.dart';
|
||||
import '../fake_connectivity.dart';
|
||||
|
||||
@@ -11,7 +11,7 @@ import 'package:wger/models/workouts/day.dart' as _i3;
|
||||
import 'package:wger/models/workouts/routine.dart' as _i2;
|
||||
import 'package:wger/models/workouts/slot.dart' as _i4;
|
||||
import 'package:wger/models/workouts/slot_entry.dart' as _i5;
|
||||
import 'package:wger/providers/routines.dart' as _i7;
|
||||
import 'package:wger/providers/routines_repository.dart' as _i7;
|
||||
|
||||
// ignore_for_file: type=lint
|
||||
// ignore_for_file: avoid_redundant_argument_values
|
||||
@@ -56,14 +56,6 @@ class MockRoutinesRepository extends _i1.Mock implements _i7.RoutinesRepository
|
||||
_i1.throwOnMissingStub(this);
|
||||
}
|
||||
|
||||
@override
|
||||
_i8.Future<List<_i2.Routine>> fetchAllRoutinesSparseServer() =>
|
||||
(super.noSuchMethod(
|
||||
Invocation.method(#fetchAllRoutinesSparseServer, []),
|
||||
returnValue: _i8.Future<List<_i2.Routine>>.value(<_i2.Routine>[]),
|
||||
)
|
||||
as _i8.Future<List<_i2.Routine>>);
|
||||
|
||||
@override
|
||||
_i8.Future<_i2.Routine> fetchAndSetRoutineFullServer(int? routineId) =>
|
||||
(super.noSuchMethod(
|
||||
|
||||
@@ -24,7 +24,7 @@ import 'package:mockito/mockito.dart';
|
||||
import 'package:wger/l10n/generated/app_localizations.dart';
|
||||
import 'package:wger/models/workouts/routine.dart';
|
||||
import 'package:wger/providers/network_provider.dart';
|
||||
import 'package:wger/providers/routines.dart';
|
||||
import 'package:wger/providers/routines_repository.dart';
|
||||
import 'package:wger/screens/form_screen.dart';
|
||||
import 'package:wger/screens/routine_list_screen.dart';
|
||||
import 'package:wger/screens/routine_screen.dart';
|
||||
|
||||
@@ -11,7 +11,7 @@ import 'package:wger/models/workouts/day.dart' as _i3;
|
||||
import 'package:wger/models/workouts/routine.dart' as _i2;
|
||||
import 'package:wger/models/workouts/slot.dart' as _i4;
|
||||
import 'package:wger/models/workouts/slot_entry.dart' as _i5;
|
||||
import 'package:wger/providers/routines.dart' as _i7;
|
||||
import 'package:wger/providers/routines_repository.dart' as _i7;
|
||||
|
||||
// ignore_for_file: type=lint
|
||||
// ignore_for_file: avoid_redundant_argument_values
|
||||
@@ -56,14 +56,6 @@ class MockRoutinesRepository extends _i1.Mock implements _i7.RoutinesRepository
|
||||
_i1.throwOnMissingStub(this);
|
||||
}
|
||||
|
||||
@override
|
||||
_i8.Future<List<_i2.Routine>> fetchAllRoutinesSparseServer() =>
|
||||
(super.noSuchMethod(
|
||||
Invocation.method(#fetchAllRoutinesSparseServer, []),
|
||||
returnValue: _i8.Future<List<_i2.Routine>>.value(<_i2.Routine>[]),
|
||||
)
|
||||
as _i8.Future<List<_i2.Routine>>);
|
||||
|
||||
@override
|
||||
_i8.Future<_i2.Routine> fetchAndSetRoutineFullServer(int? routineId) =>
|
||||
(super.noSuchMethod(
|
||||
|
||||
@@ -25,6 +25,7 @@ import 'package:wger/helpers/consts.dart';
|
||||
import 'package:wger/l10n/generated/app_localizations.dart';
|
||||
import 'package:wger/models/workouts/slot_entry.dart';
|
||||
import 'package:wger/providers/routines.dart';
|
||||
import 'package:wger/providers/routines_repository.dart';
|
||||
import 'package:wger/widgets/routines/forms/repetitions.dart';
|
||||
import 'package:wger/widgets/routines/forms/rir.dart';
|
||||
import 'package:wger/widgets/routines/forms/slot_entry.dart';
|
||||
|
||||
@@ -11,7 +11,7 @@ import 'package:wger/models/workouts/day.dart' as _i3;
|
||||
import 'package:wger/models/workouts/routine.dart' as _i2;
|
||||
import 'package:wger/models/workouts/slot.dart' as _i4;
|
||||
import 'package:wger/models/workouts/slot_entry.dart' as _i5;
|
||||
import 'package:wger/providers/routines.dart' as _i7;
|
||||
import 'package:wger/providers/routines_repository.dart' as _i7;
|
||||
|
||||
// ignore_for_file: type=lint
|
||||
// ignore_for_file: avoid_redundant_argument_values
|
||||
@@ -56,14 +56,6 @@ class MockRoutinesRepository extends _i1.Mock implements _i7.RoutinesRepository
|
||||
_i1.throwOnMissingStub(this);
|
||||
}
|
||||
|
||||
@override
|
||||
_i8.Future<List<_i2.Routine>> fetchAllRoutinesSparseServer() =>
|
||||
(super.noSuchMethod(
|
||||
Invocation.method(#fetchAllRoutinesSparseServer, []),
|
||||
returnValue: _i8.Future<List<_i2.Routine>>.value(<_i2.Routine>[]),
|
||||
)
|
||||
as _i8.Future<List<_i2.Routine>>);
|
||||
|
||||
@override
|
||||
_i8.Future<_i2.Routine> fetchAndSetRoutineFullServer(int? routineId) =>
|
||||
(super.noSuchMethod(
|
||||
|
||||
Reference in New Issue
Block a user