Skip to content

Commit

Permalink
wow importing is a good bit o work
Browse files Browse the repository at this point in the history
  • Loading branch information
Desync-o-tron committed Sep 26, 2024
1 parent b7c0457 commit b497479
Show file tree
Hide file tree
Showing 4 changed files with 159 additions and 123 deletions.
2 changes: 1 addition & 1 deletion lib/DOM/exercise_db.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ class ExDB {
static final List<String> _equipment = [];
static final List<Exercise> _exercises = [];

//todo can we make this async?
static addExercises(List<Exercise> 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);
Expand Down
118 changes: 78 additions & 40 deletions lib/importing/ex_import_matching_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<TrainingSession> newTrainingSessions;
Expand Down Expand Up @@ -47,13 +45,10 @@ class _ImportInspectionPageState extends State<ImportInspectionPage> {
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),
);
}

Expand Down Expand Up @@ -106,44 +101,87 @@ class _ImportInspectionPageState extends State<ImportInspectionPage> {
return exerciseMatches;
}

void _confirmSelections() {
List<ExerciseMatch> 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<Exercise> 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<bool?> confirmCreatingNewExercisesDialog(
BuildContext context,
) async {
return showDialog<bool>(
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: <Widget>[
TextButton(
child: const Text('No'),
onPressed: () {
Navigator.of(context).pop(false);
},
),
TextButton(
child: const Text('Yes'),
onPressed: () {
Navigator.of(context).pop(true);
},
),
],
);
},
);
}
}
161 changes: 79 additions & 82 deletions lib/importing/ex_match_listview.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
}

Expand Down Expand Up @@ -58,7 +60,7 @@ class _MatchExercisesScrollViewState extends State<MatchExercisesScrollView> {
child: ListView.builder(
itemCount: widget.exerciseMatches.length,
itemBuilder: (context, index) {
return _buildExerciseMatchBox(index);
return _buildExerciseMatchTile(index);
},
),
),
Expand All @@ -72,10 +74,9 @@ class _MatchExercisesScrollViewState extends State<MatchExercisesScrollView> {
);
}

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;
Expand All @@ -87,95 +88,92 @@ class _MatchExercisesScrollViewState extends State<MatchExercisesScrollView> {

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)
Expand Down Expand Up @@ -217,7 +215,6 @@ class _MatchExercisesScrollViewState extends State<MatchExercisesScrollView> {
),
),
],
// ),
);
}

Expand Down
1 change: 1 addition & 0 deletions lib/utils/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import 'dart:async';
import 'package:open_fitness_tracker/DOM/exercise_metadata.dart';

typedef Exercises = List<Exercise>;
typedef Strings = List<String>;

class GenericScrollBehavior extends MaterialScrollBehavior {
@override
Expand Down

0 comments on commit b497479

Please sign in to comment.