diff --git a/lib/DOM/exercise_metadata.dart b/lib/DOM/exercise_metadata.dart index 71488d8..beb41fe 100644 --- a/lib/DOM/exercise_metadata.dart +++ b/lib/DOM/exercise_metadata.dart @@ -10,6 +10,7 @@ dart run build_runner watch --delete-conflicting-outputs @JsonSerializable() class Exercise { String? id; + //^does id matter? is it another thing to track? there *should not* be name dupes. String name; List? alternateNames = []; Force? force; diff --git a/lib/cloud_io/firestore_sync.dart b/lib/cloud_io/firestore_sync.dart index 134bd67..d0fd35d 100644 --- a/lib/cloud_io/firestore_sync.dart +++ b/lib/cloud_io/firestore_sync.dart @@ -213,7 +213,7 @@ class TrainingHistoryCubit extends Cubit { .add(session.toJson()); }); // Reload the training history to update the state - await loadUserTrainingHistory(useCache: false); + await loadUserTrainingHistory(useCache: true); } catch (e) { emit(TrainingHistoryError(e.toString())); } @@ -252,6 +252,9 @@ class TrainingHistoryCubit extends Cubit { "Sign in. Make sure to verify your email if not signing in with Google Sign In, etc...")); return; } + //todo + //idk too powerful atm..0 need to have this. + emit(TrainingHistoryError("sorry john, I can't let you do that.")); try { await CloudStorage._retryWithExponentialBackoff(() async { CollectionReference historyCollection = CloudStorage.firestore @@ -313,44 +316,47 @@ class ExercisesCubit extends Cubit { emit(ExercisesLoading()); try { await CloudStorage._retryWithExponentialBackoff(() async { - QuerySnapshot globalExsSnapshot = await CloudStorage.firestore - .collection(CloudStorage._globalExercisesKey) - .get(GetOptions(source: useCache ? Source.cache : Source.server)); - + List exercises = []; List names = []; List categories = []; List muscles = []; List equipment = []; - List exercises = []; + + QuerySnapshot globalExsSnapshot = await CloudStorage.firestore + .collection(CloudStorage._globalExercisesKey) + .get(GetOptions(source: useCache ? Source.cache : Source.server)); + QuerySnapshot usrAddedExsSnapshot = await CloudStorage.firestore + .collection('users') + .doc(CloudStorage.firebaseAuth.currentUser!.uid) + .collection(CloudStorage._userAddedExercisesKey) + .get(GetOptions(source: useCache ? Source.cache : Source.serverAndCache)); + QuerySnapshot usrRemovedExsSnapshot = await CloudStorage.firestore + .collection('users') + .doc(CloudStorage.firebaseAuth.currentUser!.uid) + .collection(CloudStorage._userRemovedExercisesKey) + .get(GetOptions(source: useCache ? Source.cache : Source.serverAndCache)); for (var doc in globalExsSnapshot.docs) { - Exercise exercise = Exercise.fromJson(doc.data() as Map); - exercises.add(exercise); - if (!names.contains(exercise.name)) { - names.add(exercise.name); - } - if (exercise.category != null && !categories.contains(exercise.category)) { - categories.add(exercise.category!); - } - if (exercise.equipment != null && !equipment.contains(exercise.equipment)) { - equipment.add(exercise.equipment!); - } - for (var muscle in exercise.primaryMuscles) { - if (!muscles.contains(muscle)) { - muscles.add(muscle); - } - } + exercises.add(Exercise.fromJson(doc.data() as Map)); + } + for (var doc in usrAddedExsSnapshot.docs) { + exercises.add(Exercise.fromJson(doc.data() as Map)); + } + for (var doc in usrRemovedExsSnapshot.docs) { + final ex2remove = Exercise.fromJson(doc.data() as Map); + exercises.removeWhere((Exercise ex) => (ex.name == ex2remove.name)); + } + + for (final exercise in exercises) { + names.addIfDNE(exercise.name); + categories.addIfDNE(exercise.category); + equipment.addIfDNE(exercise.equipment); + muscles.addAllIfDNE(exercise.primaryMuscles); if (exercise.secondaryMuscles != null) { - for (var muscle in exercise.secondaryMuscles!) { - if (!muscles.contains(muscle)) { - muscles.add(muscle); - } - } + muscles.addAllIfDNE(exercise.secondaryMuscles!); } } - // TODO: Handle usrRemovedExsSnapshot and usrAddedExsSnapshot - // After processing, emit the loaded state emit(ExercisesLoaded( exercises: exercises, @@ -386,15 +392,33 @@ class ExercisesCubit extends Cubit { } }); // Optionally, reload the exercises - await loadExercises(useCache: false); + await loadExercises(useCache: true); } catch (e) { emit(ExercisesError(e.toString())); } } Future addExercises(List exercisesToAdd) async { - // Implement the method - emit(ExercisesError("addExercises method not implemented")); + if (!CloudStorage.isUserEmailVerified()) { + emit(ExercisesError( + "Sign in. Make sure to verify your email if not signing in with Google Sign In, etc...")); + return; + } + try { + await CloudStorage._retryWithExponentialBackoff(() async { + for (var ex in exercisesToAdd) { + await CloudStorage.firestore + .collection('users') + .doc(CloudStorage.firebaseAuth.currentUser!.uid) + .collection(CloudStorage._userAddedExercisesKey) + .add(ex.toJson()); + } + }); + + await loadExercises(useCache: true); + } catch (e) { + emit(ExercisesError(e.toString())); + } } } //TODO check for useCache: true diff --git a/lib/history/history_page.dart b/lib/history/history_page.dart index af3885e..d7d266b 100644 --- a/lib/history/history_page.dart +++ b/lib/history/history_page.dart @@ -83,7 +83,7 @@ class HistoryPage extends StatelessWidget { ); } else if (result == 'refresh training history') { cubit.loadUserTrainingHistory(useCache: false); - } else if (result == 'delete history') { + } else if (result == 'delete ENTIRE history') { cubit.deleteEntireTrainingHistory(); } },