Skip to content

Commit

Permalink
#61 - task(firestore service): add converters for firestore data
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
sdresselmann committed Apr 26, 2024
1 parent 8b32e1b commit 0ec3e7c
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 24 deletions.
1 change: 1 addition & 0 deletions lib/core/interfaces/json_serializable.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
abstract class JsonSerializable {}
1 change: 1 addition & 0 deletions lib/firebase/firestore_json.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
typedef FirestoreJson = Map<String, dynamic>;
35 changes: 13 additions & 22 deletions lib/firebase/services/firestore_service.dart
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -42,38 +43,28 @@ class FirestoreService {
.set(documentData);
}

// Future<Map<String, dynamic>?> getRawData(
// String collectionName,
// String documentId,
// ) async {
// final DocumentSnapshot<Map<String, dynamic>> 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<Map<String, dynamic>?> get(
Future<TrainingPlanList> get(
String collectionName,
String documentId,
) async {
final DocumentSnapshot<Map<String, dynamic>> documentSnapshot =
await _firestore.collection(collectionName).doc(documentId).get();
final DocumentSnapshot<TrainingPlanList> 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<void> uploadRawData(
Expand Down
25 changes: 23 additions & 2 deletions lib/pages/home.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -12,6 +13,7 @@ class HomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
final UserService userService = GetIt.I.get<UserService>();
final FirestoreService firestoreService = GetIt.I.get<FirestoreService>();

return Scaffold(
body: Center(
Expand All @@ -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!"),
),
],
);
}

Expand Down
16 changes: 16 additions & 0 deletions lib/training_plan/models/training_plan.dart
Original file line number Diff line number Diff line change
@@ -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<String, TrainingPlanEntry> planEntries;

TrainingPlan({required this.planEntries});

TrainingPlan.fromJson(FirestoreJson json)
: planEntries = json.map(
(key, value) => MapEntry(
key,
TrainingPlanEntry.fromJson(value as FirestoreJson),
),
);
}
26 changes: 26 additions & 0 deletions lib/training_plan/models/training_plan_entry.dart
Original file line number Diff line number Diff line change
@@ -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,
};
}
}
22 changes: 22 additions & 0 deletions lib/training_plan/models/training_plan_list.dart
Original file line number Diff line number Diff line change
@@ -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<String, TrainingPlan> 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,
};
}
}

0 comments on commit 0ec3e7c

Please sign in to comment.