Skip to content

Commit

Permalink
Merge pull request #173 from a-mabe/provider-add-workout
Browse files Browse the repository at this point in the history
Optimize DatabaseManager functions and use provider to add/delete
  • Loading branch information
a-mabe authored Oct 2, 2024
2 parents 3256b5e + 2648838 commit 4d7199d
Show file tree
Hide file tree
Showing 17 changed files with 236 additions and 299 deletions.
72 changes: 72 additions & 0 deletions lib/models/workout_type.dart
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,30 @@ class Workout {
};
}

Workout.fromMap(Map<String, dynamic> map) {
id = map['id'];
title = map['title'];
numExercises = map['numExercises'];
exercises = map['exercises'];
getReadyTime = map['getReadyTime'];
workTime = map['exerciseTime'];
restTime = map['restTime'];
halfTime = map['halfTime'];
breakTime = map['breakTime'];
warmupTime = map['warmupTime'];
cooldownTime = map['cooldownTime'];
iterations = map['iterations'];
halfwayMark = map['halfwayMark'];
workSound = map['workSound'];
restSound = map['restSound'];
halfwaySound = map['halfwaySound'];
completeSound = map['completeSound'];
countdownSound = map['countdownSound'];
colorInt = map['colorInt'];
workoutIndex = map['workoutIndex'];
showMinutes = map['showMinutes'];
}

Map<String, dynamic> toJson() {
return {
'id': id,
Expand Down Expand Up @@ -274,6 +298,54 @@ class Workout {
);
}

Workout copyWith({
String? id,
String? title,
int? numExercises,
String? exercises,
int? getReadyTime,
int? workTime,
int? restTime,
int? halfTime,
int? breakTime,
int? warmupTime,
int? cooldownTime,
int? iterations,
int? halfwayMark,
String? workSound,
String? restSound,
String? halfwaySound,
String? completeSound,
String? countdownSound,
int? colorInt,
int? workoutIndex,
int? showMinutes,
}) {
return Workout(
id ?? this.id,
title ?? this.title,
numExercises ?? this.numExercises,
exercises ?? this.exercises,
getReadyTime ?? this.getReadyTime,
workTime ?? this.workTime,
restTime ?? this.restTime,
halfTime ?? this.halfTime,
breakTime ?? this.breakTime,
warmupTime ?? this.warmupTime,
cooldownTime ?? this.cooldownTime,
iterations ?? this.iterations,
halfwayMark ?? this.halfwayMark,
workSound ?? this.workSound,
restSound ?? this.restSound,
halfwaySound ?? this.halfwaySound,
completeSound ?? this.completeSound,
countdownSound ?? this.countdownSound,
colorInt ?? this.colorInt,
workoutIndex ?? this.workoutIndex,
showMinutes ?? this.showMinutes,
);
}

/// Implement toString to print information about
/// each Workout more easily.
///
Expand Down
2 changes: 1 addition & 1 deletion lib/pages/create_timer/create_timer.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'package:flutter/material.dart';
import 'package:openhiit/widgets/form_widgets/create_form.dart';
import '../../models/workout_type.dart';
import '../set_timings.dart/set_timings.dart';
import '../set_timings/set_timings.dart';
import '../../widgets/form_widgets/submit_button.dart';

class CreateTimer extends StatefulWidget {
Expand Down
9 changes: 1 addition & 8 deletions lib/pages/home/home.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import 'package:openhiit/widgets/home/timer_list_tile.dart';
import 'package:openhiit/widgets/loader.dart';
import 'package:provider/provider.dart';
import 'package:share_plus/share_plus.dart';
import 'package:sqflite/sqflite.dart';
import 'package:url_launcher/url_launcher.dart';

// Global flag to indicate if exporting is in progress
Expand Down Expand Up @@ -77,13 +76,7 @@ class _MyHomePageState extends State<MyHomePage> {
}
});

// Initialize the database and update the workout order in the database.
Database database = await DatabaseManager().initDB();

for (var i = 0; i < reorderableWorkoutList.length; i++) {
// Update the workout order in the database.
await DatabaseManager().updateList(reorderableWorkoutList[i], database);
}
DatabaseManager().updateWorkouts(reorderableWorkoutList);
}
// ---

Expand Down
16 changes: 5 additions & 11 deletions lib/pages/import_workout/import_workout.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import 'package:openhiit/pages/home/home.dart';
import 'package:openhiit/utils/database/database_manager.dart';
import 'package:openhiit/pages/import_workout/widgets/file_error.dart';
import 'package:openhiit/widgets/loader.dart';
import 'package:sqflite_common_ffi/sqflite_ffi.dart';
import 'package:uuid/uuid.dart';
import 'widgets/copy_or_skip.dart';
import '../../models/workout_type.dart';
Expand Down Expand Up @@ -37,8 +36,7 @@ class ImportWorkoutState extends State<ImportWorkout> {
/// of the list of workouts on the home page. If this is an existing workout
/// that was edited, keep its index where it is.
///
Future<bool> importWorkoutUpdateDatabase(
Database database, Workout workoutArgument) async {
Future<bool> importWorkoutUpdateDatabase(Workout workoutArgument) async {
/// Grab the list of existing workouts so we can bump down the
/// index of each in order to make room for this new workout to be at
/// the top of the list.
Expand All @@ -47,8 +45,7 @@ class ImportWorkoutState extends State<ImportWorkout> {
logger.i(
"Adding imported workout to database: ${workoutArgument.toString()}");

List<Workout> workouts =
await DatabaseManager().lists(DatabaseManager().initDB());
List<Workout> workouts = await DatabaseManager().getWorkouts();

logger.i("Grabbed existing workouts: ${workouts.length}");

Expand All @@ -59,10 +56,10 @@ class ImportWorkoutState extends State<ImportWorkout> {
for (var i = 0; i < workouts.length; i++) {
if (i == 0) {
workouts[i].workoutIndex = 0;
await DatabaseManager().insertList(workouts[i], database);
await DatabaseManager().insertWorkout(workouts[i]);
} else {
workouts[i].workoutIndex = workouts[i].workoutIndex + 1;
await DatabaseManager().updateList(workouts[i], database);
await DatabaseManager().updateWorkout(workouts[i]);
}
}

Expand Down Expand Up @@ -152,12 +149,9 @@ class ImportWorkoutState extends State<ImportWorkout> {
"Attempting to import ${workout.title}");

try {
Database database =
await DatabaseManager().initDB();

importStatus =
await importWorkoutUpdateDatabase(
database, workout);
workout);
} on Exception catch (e) {
logger.e(
"Database conflict on import: $e");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import 'package:flutter/material.dart';
import 'package:logger/logger.dart';
import 'dart:convert';
import '../../models/workout_type.dart';
import '../set_timings.dart/set_timings.dart';
import '../set_timings/set_timings.dart';
import '../../widgets/form_widgets/submit_button.dart';

var logger = Logger(
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:openhiit/pages/home/home.dart';
import 'package:openhiit/providers/workout_provider.dart';
import 'package:provider/provider.dart';
import 'package:soundpool/soundpool.dart';
import 'package:sqflite/sqflite.dart';
import 'package:uuid/uuid.dart';
import '../../models/workout_type.dart';
import '../../utils/database/database_manager.dart';
Expand All @@ -26,9 +27,7 @@ class _SetSoundsState extends State<SetSounds> {
/// is successfully added to the DB, push to the home screen.
///
void submitWorkout(Workout workoutArgument) async {
Database database = await DatabaseManager().initDB();

await updateDatabase(database, workoutArgument).then((value) => pushHome());
await saveWorkout(workoutArgument).then((value) => pushHome());
}

/// Update the database with the workout. If this is a brand new workout,
Expand All @@ -37,38 +36,20 @@ class _SetSoundsState extends State<SetSounds> {
/// of the list of workouts on the home page. If this is an existing workout
/// that was edited, keep its index where it is.
///
Future updateDatabase(database, Workout workoutArgument) async {
/// If the workout does not have an ID, that means this is a brand new
/// workout. Grab the list of existing workouts so we can bump down the
/// index of each in order to make room for this new workout to be at
/// the top of the list.
///
if (workoutArgument.id == "") {
List<Workout> workouts =
await DatabaseManager().lists(DatabaseManager().initDB());
Future saveWorkout(Workout workoutArgument) async {
WorkoutProvider workoutProvider =
Provider.of<WorkoutProvider>(context, listen: false);
DatabaseManager databaseManager = DatabaseManager();

// Give the new workout an ID
if (workoutArgument.id == "") {
workoutArgument.id = const Uuid().v1();

// Insert the new workout into the top (beginning) of the list
workouts.insert(0, workoutArgument);

// Increase the index of all old workouts by 1.
for (var i = 0; i < workouts.length; i++) {
if (i == 0) {
await DatabaseManager().insertList(workouts[i], database);
} else {
workouts[i].workoutIndex = workouts[i].workoutIndex + 1;
await DatabaseManager().updateList(workouts[i], database);
}
}
}

/// If the workout already has an ID, that means this is an existing
/// workout that was edited. Simply update the workout in the DB.
///
else {
await DatabaseManager().updateList(workoutArgument, database);
workoutProvider.updateWorkoutIndices(1);
await workoutProvider.addWorkout(workoutArgument).then((value) {
workoutProvider.sort((d) => d.workoutIndex, true);
databaseManager.updateWorkouts(workoutProvider.workouts);
});
} else {
await workoutProvider.updateWorkout(workoutArgument);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import 'package:flutter/material.dart';
import 'package:logger/logger.dart';
import 'package:openhiit/pages/set_timings.dart/constants/set_timings_constants.dart';
import 'package:openhiit/pages/set_timings/constants/set_timings_constants.dart';
import 'widgets/time_input_trailing.dart';
import '../../models/workout_type.dart';
import '../../widgets/form_widgets/submit_button.dart';
import 'widgets/time_list_item.dart';
import '../set_sounds.dart/set_sounds.dart';
import '../set_sounds/set_sounds.dart';

var logger = Logger(
printer: PrettyPrinter(methodCount: 0),
Expand Down Expand Up @@ -89,7 +89,7 @@ class _SetTimingsState extends State<SetTimings> {
addListeners();

logger.i(
"Loading for workout object for creation/editing: ${workout.toString()}");
"Loading workout object for creation/editing: ${workout.toString()}");

Map<String, ValueNotifier<int>> notifierMap = {
"Work": ValueNotifier(workout.workTime),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import 'package:flutter/material.dart';
import 'package:openhiit/pages/set_timings.dart/utils/set_timings_utils.dart';
import 'package:openhiit/pages/set_timings/utils/set_timings_utils.dart';

import '../../../widgets/form_widgets/number_input.dart';

Expand Down
39 changes: 11 additions & 28 deletions lib/pages/view_workout/view_workout.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import 'dart:convert';
import 'dart:io';
import 'package:openhiit/pages/home/home.dart';
import 'package:openhiit/providers/workout_provider.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:provider/provider.dart';
import 'package:uuid/uuid.dart';
import 'package:flutter/material.dart';
import '../../utils/functions.dart';
import 'widgets/start_button.dart';
import 'package:sqflite/sqflite.dart';
import '../../widgets/card_item_animated.dart';
import '../../utils/database/database_manager.dart';
import 'widgets/view_workout_appbar.dart';
Expand Down Expand Up @@ -42,27 +43,13 @@ class ViewWorkoutState extends State<ViewWorkout> {
/// Returns:
/// - A Future representing the completion of the delete operation.
Future deleteList(workoutArgument) async {
// Initialize the database.
Future<Database> database = DatabaseManager().initDB();
WorkoutProvider workoutProvider =
Provider.of<WorkoutProvider>(context, listen: false);
DatabaseManager databaseManager = DatabaseManager();

// Delete the specified workout list from the database.
await DatabaseManager()
.deleteList(workoutArgument.id, database)
.then((value) async {
// Retrieve the updated list of workouts from the database.
List<Workout> workouts =
await DatabaseManager().lists(DatabaseManager().initDB());

// Sort the workouts based on their workout indices.
workouts.sort((a, b) => a.workoutIndex.compareTo(b.workoutIndex));

// Update the workout indices of remaining lists after the deleted list.
for (int i = workoutArgument.workoutIndex; i < workouts.length; i++) {
workouts[i].workoutIndex = i;
await DatabaseManager()
.updateList(workouts[i], await DatabaseManager().initDB());
}
});
workoutProvider.deleteWorkout(workoutArgument);
workoutProvider.updateWorkoutIndices(0);
databaseManager.updateWorkouts(workoutProvider.workouts);
}

@override
Expand Down Expand Up @@ -163,8 +150,7 @@ class ViewWorkoutState extends State<ViewWorkout> {
/// It duplicates the current workout and updates the list and the database accordingly.
/// Fetch the list of workouts from the database.
List<Workout> workouts =
await DatabaseManager().lists(DatabaseManager().initDB());
List<Workout> workouts = await DatabaseManager().getWorkouts();

/// Increment the workoutIndex of each workout in the list.
for (Workout workout in workouts) {
Expand Down Expand Up @@ -199,15 +185,12 @@ class ViewWorkoutState extends State<ViewWorkout> {
/// Insert the duplicate workout at the beginning of the list.
workouts.insert(0, duplicateWorkout);

/// Initialize the database.
Database database = await DatabaseManager().initDB();

/// Insert the duplicate workout into the database.
await DatabaseManager().insertList(duplicateWorkout, database);
await DatabaseManager().insertWorkout(duplicateWorkout);

/// Update the workoutIndex of each workout in the database.
for (Workout workout in workouts) {
await DatabaseManager().updateList(workout, database);
await DatabaseManager().updateWorkout(workout);
}

/// Navigate back to the main screen to show that the workout has been copied.
Expand Down
Loading

0 comments on commit 4d7199d

Please sign in to comment.