Skip to content

Commit

Permalink
Allow imports of old workouts
Browse files Browse the repository at this point in the history
  • Loading branch information
a-mabe committed Dec 2, 2024
1 parent b643859 commit 3ecff05
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 17 deletions.
46 changes: 30 additions & 16 deletions lib/pages/import_workout/import_workout.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'dart:io';

import 'package:flutter/material.dart';
import 'package:openhiit/data/timer_type.dart';
import 'package:openhiit/data/workout_type.dart';
import 'package:openhiit/providers/workout_provider.dart';
import 'package:openhiit/utils/log/log.dart';
import 'package:openhiit/pages/home/home.dart';
Expand All @@ -29,12 +30,6 @@ class ImportWorkoutState extends State<ImportWorkout> {
Widget build(BuildContext context) {
WorkoutProvider workoutProvider = Provider.of<WorkoutProvider>(context);

/// Update the database with the workout. If this is a brand new workout,
/// make its index the first in the list of workouts and push down the
/// rest of the workouts. This ensures the new workout appears at the top
/// 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(
TimerType timer, WorkoutProvider workoutProvider) async {
logger.i("Adding imported timer to database: ${timer.toString()}");
Expand Down Expand Up @@ -120,19 +115,33 @@ class ImportWorkoutState extends State<ImportWorkout> {

logger.d("Parsed list: $parsedList");

for (Map<String, dynamic> parsedTimer
for (Map<String, dynamic> parsedItem
in parsedList) {
try {
logger.d("Creating object from json");

TimerType timer =
TimerType.fromJson(parsedTimer);
TimerType? timer;
Workout? workout;

logger.d("Parsed timer: $timer");
try {
timer = TimerType.fromJson(parsedItem);
logger.d("Parsed timer: $timer");
} catch (e) {
logger.e("Error parsing TimerType: $e");
}

logger.d("settings: ${timer.timeSettings}");
if (timer == null) {
try {
workout = Workout.fromJson(parsedItem);
timer = workoutProvider.migrateToTimer(
workout, false);
logger.d("Parsed workout: $workout");
} catch (e) {
logger.e("Error parsing Workout: $e");
}
}

if (timer.name.isNotEmpty) {
if (timer != null && timer.name.isNotEmpty) {
bool importStatus = true;
do {
logger.i(
Expand All @@ -153,7 +162,7 @@ class ImportWorkoutState extends State<ImportWorkout> {
context: context,
builder: (BuildContext context) {
return CopyOrSkipDialog(
timer: timer,
timer: timer!,
onSkip: () {
Navigator.of(context).pop();
},
Expand Down Expand Up @@ -195,6 +204,11 @@ class ImportWorkoutState extends State<ImportWorkout> {
} while (!importStatus);
logger.i(
"Successfully imported ${timer.name}");
} else if (workout != null &&
workout.title.isNotEmpty) {
// Handle workout import logic here
logger.i(
"Successfully imported workout ${workout.title}");
} else {
// User canceled the file picker
}
Expand All @@ -210,7 +224,7 @@ class ImportWorkoutState extends State<ImportWorkout> {
title:
"Error reading ${file.path.split('/').last}",
message:
"File contains invalid timer configuration, skipping import.",
"File contains invalid timer or workout configuration, skipping import.",
);
},
);
Expand All @@ -219,7 +233,7 @@ class ImportWorkoutState extends State<ImportWorkout> {
}
} on Exception catch (e) {
logger.e(
"The provided file does not contain valid exported timer JSON: $e");
"The provided file does not contain valid exported timer or workout JSON: $e");

if (context.mounted) {
await showDialog(
Expand All @@ -229,7 +243,7 @@ class ImportWorkoutState extends State<ImportWorkout> {
title:
"Error reading ${file.path.split('/').last}",
message:
"File contains invalid exported timer format, skipping import.",
"File contains invalid exported timer or workout format, skipping import.",
);
},
);
Expand Down
1 change: 0 additions & 1 deletion lib/utils/import_export/local_file_util.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import 'package:file_picker/file_picker.dart';
import 'package:flutter/material.dart';
import 'package:logger/logger.dart';
import 'package:openhiit/data/timer_type.dart';
import 'package:openhiit/data/workout_type.dart';
import 'package:path_provider/path_provider.dart';
import 'package:share_plus/share_plus.dart';

Expand Down

0 comments on commit 3ecff05

Please sign in to comment.