From 0ec3e7caecc907929308b469bb873fd35355f22e Mon Sep 17 00:00:00 2001 From: sdresselmann <46092447+sdresselmann@users.noreply.github.com> Date: Fri, 26 Apr 2024 14:51:12 +0200 Subject: [PATCH] #61 - task(firestore service): add converters for firestore data While this solution comes with quite a bit of boilerplate code, this seems to be the most straight forward way without any auto generation of code from mapper libraries. --- lib/core/interfaces/json_serializable.dart | 1 + lib/firebase/firestore_json.dart | 1 + lib/firebase/services/firestore_service.dart | 35 +++++++------------ lib/pages/home.dart | 25 +++++++++++-- lib/training_plan/models/training_plan.dart | 16 +++++++++ .../models/training_plan_entry.dart | 26 ++++++++++++++ .../models/training_plan_list.dart | 22 ++++++++++++ 7 files changed, 102 insertions(+), 24 deletions(-) create mode 100644 lib/core/interfaces/json_serializable.dart create mode 100644 lib/firebase/firestore_json.dart create mode 100644 lib/training_plan/models/training_plan.dart create mode 100644 lib/training_plan/models/training_plan_entry.dart create mode 100644 lib/training_plan/models/training_plan_list.dart diff --git a/lib/core/interfaces/json_serializable.dart b/lib/core/interfaces/json_serializable.dart new file mode 100644 index 0000000..757d355 --- /dev/null +++ b/lib/core/interfaces/json_serializable.dart @@ -0,0 +1 @@ +abstract class JsonSerializable {} diff --git a/lib/firebase/firestore_json.dart b/lib/firebase/firestore_json.dart new file mode 100644 index 0000000..4c9fdd0 --- /dev/null +++ b/lib/firebase/firestore_json.dart @@ -0,0 +1 @@ +typedef FirestoreJson = Map; diff --git a/lib/firebase/services/firestore_service.dart b/lib/firebase/services/firestore_service.dart index ca2a913..ec0d58d 100644 --- a/lib/firebase/services/firestore_service.dart +++ b/lib/firebase/services/firestore_service.dart @@ -1,6 +1,7 @@ import 'package:cloud_firestore/cloud_firestore.dart'; import 'package:get_it/get_it.dart'; import 'package:lifting_progress_tracker/firebase/services/firebase_service.dart'; +import 'package:lifting_progress_tracker/training_plan/models/training_plan_list.dart'; import 'package:logging/logging.dart'; class FirestoreService { @@ -42,38 +43,28 @@ class FirestoreService { .set(documentData); } - // Future?> getRawData( - // String collectionName, - // String documentId, - // ) async { - // final DocumentSnapshot> documentSnapshot = - // await _firestore.collection(collectionName).doc(documentId).get(); - // - // if (!documentSnapshot.exists) { - // _logger.severe( - // "Document with id $documentId does not exist.", - // ); - // return null; - // } - // - // return documentSnapshot.data(); - // } - - Future?> get( + Future get( String collectionName, String documentId, ) async { - final DocumentSnapshot> documentSnapshot = - await _firestore.collection(collectionName).doc(documentId).get(); + final DocumentSnapshot documentSnapshot = await _firestore + .collection(collectionName) + .doc(documentId) + .withConverter( + fromFirestore: (snapshot, _) => + TrainingPlanList.fromJson(snapshot.data()!), + toFirestore: (trainingPlanList, _) => trainingPlanList.toJson(), + ) + .get(); if (!documentSnapshot.exists) { _logger.severe( "Document with id $documentId does not exist.", ); - return null; + return throw Exception(); } - return documentSnapshot.data(); + return documentSnapshot.data()!; } // Future uploadRawData( diff --git a/lib/pages/home.dart b/lib/pages/home.dart index 63c48bb..8563881 100644 --- a/lib/pages/home.dart +++ b/lib/pages/home.dart @@ -3,6 +3,7 @@ import 'package:get_it/get_it.dart'; import 'package:lifting_progress_tracker/core/models/app_user.dart'; import 'package:lifting_progress_tracker/core/services/user_service.dart'; import 'package:lifting_progress_tracker/core/widgets/error_message.dart'; +import 'package:lifting_progress_tracker/firebase/services/firestore_service.dart'; import 'package:lifting_progress_tracker/pages/starting/widgets/welcome_message.dart'; import 'package:logging/logging.dart'; @@ -12,6 +13,7 @@ class HomePage extends StatelessWidget { @override Widget build(BuildContext context) { final UserService userService = GetIt.I.get(); + final FirestoreService firestoreService = GetIt.I.get(); return Scaffold( body: Center( @@ -23,8 +25,27 @@ class HomePage extends StatelessWidget { } if (snapshot.connectionState == ConnectionState.done) { - return WelcomeMessage( - username: snapshot.data!.email, + return Column( + children: [ + WelcomeMessage( + username: snapshot.data?.email ?? "", + ), + ElevatedButton( + onPressed: () => { + firestoreService.get( + "plan-entries", + snapshot.data?.uid ?? "", + ), + // .then( + // (value) { + // final plans = value; + // print(plans); + // }, + // ), + }, + child: const Text("press to fetch!"), + ), + ], ); } diff --git a/lib/training_plan/models/training_plan.dart b/lib/training_plan/models/training_plan.dart new file mode 100644 index 0000000..cff9780 --- /dev/null +++ b/lib/training_plan/models/training_plan.dart @@ -0,0 +1,16 @@ +import 'package:lifting_progress_tracker/firebase/firestore_json.dart'; +import 'package:lifting_progress_tracker/training_plan/models/training_plan_entry.dart'; + +class TrainingPlan { + Map planEntries; + + TrainingPlan({required this.planEntries}); + + TrainingPlan.fromJson(FirestoreJson json) + : planEntries = json.map( + (key, value) => MapEntry( + key, + TrainingPlanEntry.fromJson(value as FirestoreJson), + ), + ); +} diff --git a/lib/training_plan/models/training_plan_entry.dart b/lib/training_plan/models/training_plan_entry.dart new file mode 100644 index 0000000..bbdaa2a --- /dev/null +++ b/lib/training_plan/models/training_plan_entry.dart @@ -0,0 +1,26 @@ +import 'package:lifting_progress_tracker/firebase/firestore_json.dart'; + +class TrainingPlanEntry { + String repeats; + String exerciseName; + String weight; + + TrainingPlanEntry({ + required this.repeats, + required this.exerciseName, + required this.weight, + }); + + TrainingPlanEntry.fromJson(FirestoreJson json) + : repeats = json["repeats"] as String, + exerciseName = json["exerciseName"] as String, + weight = json["weight"] as String; + + FirestoreJson toJson() { + return { + "repeats": repeats, + "exerciseName": exerciseName, + "weight": weight, + }; + } +} diff --git a/lib/training_plan/models/training_plan_list.dart b/lib/training_plan/models/training_plan_list.dart new file mode 100644 index 0000000..84e01e2 --- /dev/null +++ b/lib/training_plan/models/training_plan_list.dart @@ -0,0 +1,22 @@ +import 'package:lifting_progress_tracker/firebase/firestore_json.dart'; +import 'package:lifting_progress_tracker/training_plan/models/training_plan.dart'; + +class TrainingPlanList { + Map trainingPlanList; + + TrainingPlanList({required this.trainingPlanList}); + + TrainingPlanList.fromJson(FirestoreJson json) + : trainingPlanList = json.map( + (key, value) => MapEntry( + key, + TrainingPlan.fromJson(value as FirestoreJson), + ), + ); + + FirestoreJson toJson() { + return { + "trainingPlanList": trainingPlanList, + }; + } +}