diff --git a/lib/DOM/exercise_db.dart b/lib/DOM/exercise_db.dart index 587e1ff..79480ec 100644 --- a/lib/DOM/exercise_db.dart +++ b/lib/DOM/exercise_db.dart @@ -17,8 +17,8 @@ class ExDB { static final List _equipment = []; static final List _exercises = []; - //todo can we make this async? static addExercises(List exercises) { + //todo this should kick off a lambda function so we can improve our DB. for (var exercise in exercises) { _exercises.addIfDNE(exercise); _names.addIfDNE(exercise.name); diff --git a/lib/importing/ex_import_matching_page.dart b/lib/importing/ex_import_matching_page.dart index 329ce98..d5a2908 100644 --- a/lib/importing/ex_import_matching_page.dart +++ b/lib/importing/ex_import_matching_page.dart @@ -9,8 +9,6 @@ import 'package:open_fitness_tracker/importing/ex_match_listview.dart'; import 'package:open_fitness_tracker/navigation/routes.dart'; import 'package:open_fitness_tracker/utils/utils.dart'; -//TODO make a delete button on the exercisematch - class ImportInspectionPage extends StatefulWidget { const ImportInspectionPage({super.key, required this.newTrainingSessions}); final List newTrainingSessions; @@ -47,13 +45,10 @@ class _ImportInspectionPageState extends State { title: Text( "Imported ${widget.newTrainingSessions.length} sessions &\n ${newExs.length} different exercises"), ), - body: SafeArea( - minimum: const EdgeInsets.symmetric(horizontal: 5), - child: MatchExercisesScrollView( - exerciseMatches: matchPairs, - allImportedExercises: newExs, - confirmSelections: _confirmSelections), - ), + body: MatchExercisesScrollView( + exerciseMatches: matchPairs, + allImportedExercises: newExs, + confirmSelections: _confirmSelections), ); } @@ -106,44 +101,87 @@ class _ImportInspectionPageState extends State { return exerciseMatches; } - void _confirmSelections() { - List confirmedMatches = - matchPairs.where((match) => match.isConfirmed).toList(); - - for (var match in confirmedMatches) { - var matchedEx = match.matchedExercise; - if (matchedEx != null) { - Exercise exToUpdate = ExDB.exercises - .firstWhere((e) => e.name == matchedEx.name, orElse: () => matchedEx); - - exToUpdate.alternateNames ??= []; + void _confirmSelections() async { + Strings exNamestoRm = []; - if (match.preferForeignExerciseName) { - exToUpdate.alternateNames!.addIfDNE(exToUpdate.name); - exToUpdate.name = match.foreignExercise.name; - } else { - exToUpdate.alternateNames!.addIfDNE(match.foreignExercise.name); + bool firstTimeNoMatch = true; + for (var match in matchPairs) { + if (match.bDiscard) { + exNamestoRm.add(match.foreignExercise.name); + continue; + } + if (match.matchedExercise == null) { + if (firstTimeNoMatch) { + firstTimeNoMatch = false; + bool? result = await confirmCreatingNewExercisesDialog(context); + if (result == null || !result) { + return; + } } + //otherwise lets put em in the DB! + ExDB.addExercises([match.foreignExercise]); + continue; + } + //otherwise we're mapping the foreign ex to our ex. + Exercise exToUpdate = ExDB.exercises.firstWhere( + (e) => e.name == match.matchedExercise!.name, + orElse: () => match.matchedExercise!); + + // ignore: prefer_conditional_assignment + if (exToUpdate.alternateNames == null) { + exToUpdate.alternateNames = []; + } - ExDB.addExercises([exToUpdate]); + if (match.preferForeignExerciseName) { + exToUpdate.alternateNames!.addIfDNE(exToUpdate.name); + exToUpdate.name = match.foreignExercise.name; + } else { + exToUpdate.alternateNames!.addIfDNE(match.foreignExercise.name); } - } - List unmatchedExercises = []; + ExDB.addExercises([exToUpdate]); + //TODO now we gotta update the flipping training data with these mapped exercises and drop the exercises the user wanted to rm + } - for (var importedEx in newExs) { - bool found = false; - for (var match in confirmedMatches) { - if (match.foreignExercise.name == importedEx.name) { - found = true; - break; - } - } - if (!found) { - unmatchedExercises.add(importedEx); - } + if (mounted) { + //i think this should be fine, what's a dialog going to do? + context.push(routeNames.History.text); + } else { + //todo error handling? + throw Exception("todo help me"); } + } - context.push(routeNames.History.text); + Future confirmCreatingNewExercisesDialog( + BuildContext context, + ) async { + return showDialog( + context: context, + barrierDismissible: false, + builder: (BuildContext context) { + return AlertDialog( + title: const Text("beep boop"), + content: const Text( + "Create new exercises from any unnassigned exercises?", + textAlign: TextAlign.center, + textScaler: TextScaler.linear(1.25), + ), + actions: [ + TextButton( + child: const Text('No'), + onPressed: () { + Navigator.of(context).pop(false); + }, + ), + TextButton( + child: const Text('Yes'), + onPressed: () { + Navigator.of(context).pop(true); + }, + ), + ], + ); + }, + ); } } diff --git a/lib/importing/ex_match_listview.dart b/lib/importing/ex_match_listview.dart index 8c00865..6afa2e2 100644 --- a/lib/importing/ex_match_listview.dart +++ b/lib/importing/ex_match_listview.dart @@ -9,12 +9,14 @@ class ExerciseMatch { Exercise? matchedExercise; bool isConfirmed; bool preferForeignExerciseName; + bool bDiscard; ExerciseMatch({ required this.foreignExercise, this.matchedExercise, this.isConfirmed = false, this.preferForeignExerciseName = false, + this.bDiscard = false, }); } @@ -58,7 +60,7 @@ class _MatchExercisesScrollViewState extends State { child: ListView.builder( itemCount: widget.exerciseMatches.length, itemBuilder: (context, index) { - return _buildExerciseMatchBox(index); + return _buildExerciseMatchTile(index); }, ), ), @@ -72,10 +74,9 @@ class _MatchExercisesScrollViewState extends State { ); } - Widget _buildExerciseMatchBox(int index) { + Widget _buildExerciseMatchTile(int index) { ExerciseMatch exerciseMatch = widget.exerciseMatches[index]; - // Determine background color based on the state Color matchedExerciseBackgroundColor; if (exerciseMatch.matchedExercise == null) { matchedExerciseBackgroundColor = Colors.red.shade300; @@ -87,95 +88,92 @@ class _MatchExercisesScrollViewState extends State { return Padding( padding: const EdgeInsets.symmetric(vertical: 4.0, horizontal: 8.0), - child: Stack( - children: [ - Container( - padding: const EdgeInsets.all(8.0), - decoration: BoxDecoration( - color: Colors.white, - border: Border.all(color: Colors.grey.shade300), - borderRadius: BorderRadius.circular(8.0), - boxShadow: [ - BoxShadow( - color: Colors.grey.shade200, - offset: const Offset(0, 2), - blurRadius: 4.0, + child: Container( + padding: const EdgeInsets.all(8.0), + decoration: BoxDecoration( + color: Colors.white, + border: Border.all(color: Colors.grey.shade300), + borderRadius: BorderRadius.circular(8.0), + boxShadow: [ + BoxShadow( + color: Colors.grey.shade200, + offset: const Offset(0, 2), + blurRadius: 4.0, + ), + ], + ), + child: Row( + children: [ + Expanded( + child: Opacity( + opacity: exerciseMatch.bDiscard ? .33 : 1, + child: Column( + children: [ + ExerciseTile(exercise: exerciseMatch.foreignExercise), + const SizedBox(height: 8.0), + _selectableExTile( + index, exerciseMatch, matchedExerciseBackgroundColor), + ], ), - ], + ), ), - child: Row( - children: [ - Expanded( - child: Column( - children: [ - ExerciseTile(exercise: exerciseMatch.foreignExercise), - const SizedBox(height: 8.0), - _selectableExTile( - index, exerciseMatch, matchedExerciseBackgroundColor), - ], + SizedBox( + width: 80, + child: Column( + mainAxisAlignment: MainAxisAlignment.center, + children: [ + const Text('Accept?'), + Switch( + value: (exerciseMatch.bDiscard) ? false : exerciseMatch.isConfirmed, + onChanged: (bool value) { + if (exerciseMatch.bDiscard) return; + setState(() { + exerciseMatch.isConfirmed = value; + }); + }, ), - ), - SizedBox( - width: 80, - child: Column( - mainAxisAlignment: MainAxisAlignment.center, - children: [ - const Text('Accept?'), - Switch( - value: exerciseMatch.isConfirmed, - onChanged: (bool value) { - setState(() { - exerciseMatch.isConfirmed = value; - }); - }, - ), - const Center( - child: Text( - 'Use my name?', - textAlign: TextAlign.center, - ), - ), - Switch( - value: exerciseMatch.preferForeignExerciseName, - onChanged: (bool value) { - setState(() { - exerciseMatch.preferForeignExerciseName = value; - }); - }, - ), - IconButton( - icon: const Icon(Icons.delete), - onPressed: () { - setState(() { - widget.exerciseMatches.removeAt(index); - }); - }, - ), - ], + const Center( + child: Text( + 'Use my name?', + textAlign: TextAlign.center, + ), ), - ), - ], + Switch( + value: (exerciseMatch.bDiscard) + ? false + : exerciseMatch.preferForeignExerciseName, + onChanged: (bool value) { + if (exerciseMatch.bDiscard) return; + setState(() { + exerciseMatch.preferForeignExerciseName = value; + }); + }, + ), + const Center( + child: Text( + 'Discard?', + textAlign: TextAlign.center, + ), + ), + Switch( + value: exerciseMatch.bDiscard, + onChanged: (bool value) { + setState(() { + exerciseMatch.bDiscard = value; + }); + }, + ), + ], + ), ), - ), - ], + ], + ), ), ); } Widget _selectableExTile( int index, ExerciseMatch exerciseMatch, Color matchedExerciseBackgroundColor) { - // return GestureDetector( - // onTap: () { - // _addNewExercise(index, exerciseMatch.foreignExercise, (Exercise? userMatchedEx) { - // if (userMatchedEx != null) { - // setState(() { - // exerciseMatch.matchedExercise = userMatchedEx; - // exerciseMatch.isConfirmed = true; - // }); - // } - // }); - // }, - // child: return Stack( children: [ (exerciseMatch.matchedExercise != null) @@ -217,7 +215,6 @@ class _MatchExercisesScrollViewState extends State { ), ), ], - // ), ); } diff --git a/lib/utils/utils.dart b/lib/utils/utils.dart index 2af1b8e..cff2ab4 100644 --- a/lib/utils/utils.dart +++ b/lib/utils/utils.dart @@ -6,6 +6,7 @@ import 'dart:async'; import 'package:open_fitness_tracker/DOM/exercise_metadata.dart'; typedef Exercises = List; +typedef Strings = List; class GenericScrollBehavior extends MaterialScrollBehavior { @override