From 4601114aeb7e246924efb5f86d424a1243b6e7bd Mon Sep 17 00:00:00 2001 From: Luke Knoble <35696371+Desync-o-tron@users.noreply.github.com> Date: Tue, 8 Oct 2024 17:54:22 -0400 Subject: [PATCH] finally got the import function tested and working --- lib/DOM/history_importing_logic.dart | 81 ++++++++++------------------ lib/DOM/training_metadata.dart | 2 +- lib/utils/utils.dart | 1 + test/import_strong_csv_test.dart | 10 ++++ 4 files changed, 40 insertions(+), 54 deletions(-) diff --git a/lib/DOM/history_importing_logic.dart b/lib/DOM/history_importing_logic.dart index 9c2f587..c5edbe2 100644 --- a/lib/DOM/history_importing_logic.dart +++ b/lib/DOM/history_importing_logic.dart @@ -4,13 +4,14 @@ import 'package:flutter/foundation.dart'; import 'package:intl/intl.dart'; import 'training_metadata.dart'; import 'exercise_metadata.dart'; +import 'package:open_fitness_tracker/utils/utils.dart'; List importStrongCsv(String filepathORfileStr, Units units, [bool forTesting = false]) { final List rows; if (kIsWeb || forTesting) { rows = filepathORfileStr.split("\n"); - if (rows.last.isEmpty) { + if (rows.last.isEmpty || rows.last.isWhitespace) { rows.removeLast(); } } else { @@ -22,7 +23,6 @@ List importStrongCsv(String filepathORfileStr, Units units, List sessions = []; Exercise exercise = Exercise(name: "temp"); TrainingSession session = TrainingSession(name: "temp", date: DateTime.now()); - SetsOfAnExercise setsOfExercise = SetsOfAnExercise(exercise)..sets = []; bool firstRun = true; for (int i = 0; i < rows.length; i++) { @@ -44,8 +44,7 @@ List importStrongCsv(String filepathORfileStr, Units units, if (firstRun) { firstRun = false; - exercise = Exercise(name: exerciseName, notes: notes); - setsOfExercise = SetsOfAnExercise(exercise)..sets = []; + session = TrainingSession( name: workoutName, dateOfLastEdit: date, @@ -55,11 +54,8 @@ List importStrongCsv(String filepathORfileStr, Units units, ); } - bool newExercise = false; - if (exerciseName != exercise.name) { - newExercise = true; - } bool newSession = isNewSession(session, duration, workoutName, date); + bool newExercise = (exerciseName != exercise.name || newSession); bool lastSetEver = (i == rows.length - 1); //we have to save & renew the session FIRST, as it's going to get stuff added to it. @@ -73,11 +69,6 @@ List importStrongCsv(String filepathORfileStr, Units units, notes: workoutNotes, ); } - if (newExercise) { - session.trainingData.add(setsOfExercise); - setsOfExercise = SetsOfAnExercise(exercise); - } - //todo implement notes history. exercise = Exercise(name: exerciseName, notes: notes); final set = Set.full( @@ -92,50 +83,27 @@ List importStrongCsv(String filepathORfileStr, Units units, ); if (newExercise) { - setsOfExercise.sets = [set]; + session.trainingData.add(SetsOfAnExercise(exercise)..sets = [set]); } else { - setsOfExercise.sets.add(set); + session.trainingData.last.sets.add(set); } if (lastSetEver) { - session.trainingData.add(setsOfExercise); sessions.add(session); } - - ///// - //// - // if (!newExercise) { - // setsOfExercise.sets.add(set); - // } - // if ((newExercise) || (i == rows.length - 1)) { - // session.trainingData.add(setsOfExercise); - // setsOfExercise = SetsOfAnExercise(exercise)..sets = [set]; - // if ((i == rows.length - 1) && !isNewSession(session, duration, workoutName, date)) { - // session.trainingData.add(setsOfExercise); - // } - // } - - // // if (isNewSession(session, duration, workoutName, date) || (i == rows.length - 1)) { - // // sessions.add(session); - // // } - // if (isNewSession(session, duration, workoutName, date)) { - // sessions.add(session); - // session = TrainingSession( - // name: workoutName, - // dateOfLastEdit: date, - // date: date, - // duration: duration, - // notes: workoutNotes, - // trainingData: [setsOfExercise], - // ); - // } - // if (i == rows.length - 1) { - // sessions.add(session); - // } } //TODO lets run through all the sessions and update the setMetrics. // setMetrics: ['reps', 'weight', 'distance', 'time'], + for (var sesh in sessions) { + for (var soe in sesh.trainingData) { + if (soe.ex.name.contains("Shrug (")) { + void x; + } + setupSetMetrics(soe); + void x; + } + } return sessions; } @@ -148,14 +116,21 @@ bool isNewSession( } void setupSetMetrics(SetsOfAnExercise setsOfExercise) { + /* + so going into this.. + ex always has setmetrics set.. though the default might not be right. + the set's members are based on the ex's setmetrics, so they cant be trusted either. + we just need to see what values are set. + */ + //set the ex's setmetrics right first List setMetrics = []; - //strong is going to give a value of 0 instead of null for things. - for (var set in setsOfExercise.sets) { - if (set.reps! > 0) setMetrics.add("reps"); - if (set.weight! > 0) setMetrics.add("weight"); - if (set.distance! > 0) setMetrics.add("distance"); - if (set.time! > 0) setMetrics.add("time"); + for (Set set in setsOfExercise.sets) { + if (set.reps != null && set.reps! > 0) setMetrics.addIfDNE("reps"); + if (set.weight != null && set.weight! > 0) setMetrics.addIfDNE("weight"); + if (set.distance != null && set.distance! > 0) setMetrics.addIfDNE("distance"); + if (set.time != null && set.time! > 0) setMetrics.addIfDNE("time"); } + //then get the set right for (var set in setsOfExercise.sets) { if (!setMetrics.contains('reps')) set.reps = null; if (!setMetrics.contains('weight')) set.weight = null; diff --git a/lib/DOM/training_metadata.dart b/lib/DOM/training_metadata.dart index 6841768..86a615d 100644 --- a/lib/DOM/training_metadata.dart +++ b/lib/DOM/training_metadata.dart @@ -147,7 +147,7 @@ class Set { this.distanceUnits, this.completed = false, }) { - Set(ex); + id = DateTime.now().toIso8601String(); } factory Set.fromJson(Map json) => _$SetFromJson(json); diff --git a/lib/utils/utils.dart b/lib/utils/utils.dart index 94fbe05..0bda2ed 100644 --- a/lib/utils/utils.dart +++ b/lib/utils/utils.dart @@ -101,6 +101,7 @@ extension StringExtension on String { String toTitleCase() => replaceAll(RegExp(' +'), ' ').split(' ').map((str) => str.toCaps()).join(' '); String capTheFirstLetter() => this[0].toUpperCase() + substring(1); + bool get isWhitespace => trim().isEmpty; } extension ListExtension on List { diff --git a/test/import_strong_csv_test.dart b/test/import_strong_csv_test.dart index 00ad992..56f173c 100644 --- a/test/import_strong_csv_test.dart +++ b/test/import_strong_csv_test.dart @@ -86,5 +86,15 @@ Date,Workout Name,Duration,Exercise Name,Set Order,Weight,Reps,Distance,Seconds, expect(sessions[1].trainingData[1].ex.name, "Cycling"); expect(sessions[1].trainingData[1].sets.length, 2); }); + + test('import with nothing', () { + const csvData = ''' +Date,Workout Name,Duration,Exercise Name,Set Order,Weight,Reps,Distance,Seconds,Notes,Workout Notes,RPE + '''; + + final sessions = importStrongCsv(csvData, Units(), true); + + expect(sessions.length, 0); + }); }); }