Initial commit
This commit is contained in:
116
lib/providers/trainings_provider.dart
Normal file
116
lib/providers/trainings_provider.dart
Normal file
@@ -0,0 +1,116 @@
|
||||
import 'dart:convert';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:drift/drift.dart';
|
||||
import 'package:trainhub_flutter/database/database.dart';
|
||||
import 'package:trainhub_flutter/models/training_models.dart';
|
||||
|
||||
class TrainingsProvider extends ChangeNotifier {
|
||||
final AppDatabase database;
|
||||
|
||||
List<TrainingPlanModel> _plans = [];
|
||||
List<Exercise> _exercises = [];
|
||||
|
||||
List<TrainingPlanModel> get plans => _plans;
|
||||
List<Exercise> get exercises => _exercises;
|
||||
|
||||
TrainingsProvider(this.database) {
|
||||
_loadData();
|
||||
}
|
||||
|
||||
Future<void> _loadData() async {
|
||||
final dbPlans = await database.select(database.trainingPlans).get();
|
||||
_plans = dbPlans.map((p) {
|
||||
final sectionsJson = p.sections != null && p.sections!.isNotEmpty
|
||||
? jsonDecode(p.sections!)
|
||||
: [];
|
||||
// Manual mapping or just pass to model
|
||||
return TrainingPlanModel.fromJson({
|
||||
'id': p.id,
|
||||
'name': p.name,
|
||||
'sections': sectionsJson,
|
||||
});
|
||||
}).toList();
|
||||
|
||||
_exercises = await database.select(database.exercises).get();
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
Future<TrainingPlanModel> createPlan(String name) async {
|
||||
final id = DateTime.now().toIso8601String();
|
||||
await database
|
||||
.into(database.trainingPlans)
|
||||
.insert(
|
||||
TrainingPlansCompanion.insert(
|
||||
id: id,
|
||||
name: name,
|
||||
sections: const Value('[]'),
|
||||
),
|
||||
);
|
||||
await _loadData();
|
||||
return _plans.firstWhere((p) => p.id == id);
|
||||
}
|
||||
|
||||
Future<void> updatePlan(TrainingPlanModel plan) async {
|
||||
await (database.update(
|
||||
database.trainingPlans,
|
||||
)..where((t) => t.id.equals(plan.id))).write(
|
||||
TrainingPlansCompanion(
|
||||
name: Value(plan.name),
|
||||
sections: Value(
|
||||
jsonEncode(plan.sections.map((s) => s.toJson()).toList()),
|
||||
),
|
||||
),
|
||||
);
|
||||
await _loadData();
|
||||
}
|
||||
|
||||
Future<void> deletePlan(String id) async {
|
||||
await (database.delete(
|
||||
database.trainingPlans,
|
||||
)..where((t) => t.id.equals(id))).go();
|
||||
await _loadData();
|
||||
}
|
||||
|
||||
Future<Exercise> addExercise(
|
||||
String name,
|
||||
String instructions,
|
||||
String tags,
|
||||
String videoUrl,
|
||||
) async {
|
||||
final id = DateTime.now().toIso8601String();
|
||||
await database
|
||||
.into(database.exercises)
|
||||
.insert(
|
||||
ExercisesCompanion.insert(
|
||||
id: id,
|
||||
name: name,
|
||||
instructions: Value(instructions),
|
||||
tags: Value(tags), // Storing as JSON string
|
||||
videoUrl: Value(videoUrl),
|
||||
),
|
||||
);
|
||||
await _loadData();
|
||||
return _exercises.firstWhere((e) => e.id == id);
|
||||
}
|
||||
|
||||
Future<void> updateExercise(Exercise exercise) async {
|
||||
await (database.update(
|
||||
database.exercises,
|
||||
)..where((t) => t.id.equals(exercise.id))).write(
|
||||
ExercisesCompanion(
|
||||
name: Value(exercise.name),
|
||||
instructions: Value(exercise.instructions),
|
||||
tags: Value(exercise.tags),
|
||||
videoUrl: Value(exercise.videoUrl),
|
||||
),
|
||||
);
|
||||
await _loadData();
|
||||
}
|
||||
|
||||
Future<void> deleteExercise(String id) async {
|
||||
await (database.delete(
|
||||
database.exercises,
|
||||
)..where((t) => t.id.equals(id))).go();
|
||||
await _loadData();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user