From cccfcb5a3c13db2b78d98cbd952bb0d0dcf36e60 Mon Sep 17 00:00:00 2001 From: Rafael <1024481@stud.hs-mannheim.de> Date: Sun, 5 May 2024 10:26:49 +0200 Subject: [PATCH] constants.dart --- lib/constants.dart | 11 +++++++++++ lib/main.dart | 3 ++- lib/pages/user_data_page.dart | 5 +++-- lib/services/auth/auth_service.dart | 10 ++++++---- lib/services/chat/chat_service.dart | 16 ++++++++++------ 5 files changed, 32 insertions(+), 13 deletions(-) create mode 100644 lib/constants.dart diff --git a/lib/constants.dart b/lib/constants.dart new file mode 100644 index 0000000..150b94c --- /dev/null +++ b/lib/constants.dart @@ -0,0 +1,11 @@ +class Constants { + Constants._(); + + /// Title of the app + static const String appTitle = "Cofounderella"; + + static const String dbCollectionUsers = 'Users'; + static const String dbCollectionLanguages = 'languages'; + static const String dbCollectionChatRooms = 'chat_rooms'; + static const String dbCollectionMessages = 'messages'; +} diff --git a/lib/main.dart b/lib/main.dart index 99db943..d3c4d0a 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,3 +1,4 @@ +import 'package:cofounderella/constants.dart'; import 'package:cofounderella/services/auth/auth_gate.dart'; import 'package:cofounderella/themes/theme_provider.dart'; import 'package:flutter/material.dart'; @@ -28,7 +29,7 @@ class MyApp extends StatelessWidget { Widget build(BuildContext context) { return MaterialApp( debugShowCheckedModeBanner: false, - title: 'Cofounderella', + title: Constants.appTitle, theme: Provider.of(context).themeData, home: const AuthGate(), ); diff --git a/lib/pages/user_data_page.dart b/lib/pages/user_data_page.dart index 28cb9b9..bcb28f7 100644 --- a/lib/pages/user_data_page.dart +++ b/lib/pages/user_data_page.dart @@ -2,6 +2,7 @@ import 'dart:convert'; import 'package:cloud_firestore/cloud_firestore.dart'; import 'package:cofounderella/components/my_button.dart'; import 'package:cofounderella/components/my_textfield.dart'; +import 'package:cofounderella/constants.dart'; import 'package:cofounderella/models/language.dart'; import 'package:cofounderella/models/language_setting.dart'; import 'package:flutter/material.dart'; @@ -41,9 +42,9 @@ class _UserDataPageState extends State { // Get reference to the Firebase collection CollectionReference languagesRef = _firestore - .collection('Users') + .collection(Constants.dbCollectionUsers) .doc(currentUserID) - .collection('languages'); + .collection(Constants.dbCollectionLanguages); // Loop through each Language object and save it to the collection for (int i = 0; i < _selectedLanguages.length; i++) { diff --git a/lib/services/auth/auth_service.dart b/lib/services/auth/auth_service.dart index 6067130..5ebc3ba 100644 --- a/lib/services/auth/auth_service.dart +++ b/lib/services/auth/auth_service.dart @@ -1,4 +1,5 @@ import 'package:cloud_firestore/cloud_firestore.dart'; +import 'package:cofounderella/constants.dart'; import 'package:firebase_auth/firebase_auth.dart'; class AuthService { @@ -36,7 +37,10 @@ class AuthService { ); // save user info in a document - _firestore.collection("Users").doc(userCredential.user!.uid).set( + _firestore + .collection(Constants.dbCollectionUsers) + .doc(userCredential.user!.uid) + .set( { 'uid': userCredential.user!.uid, 'email': email, @@ -49,10 +53,8 @@ class AuthService { } } -// sign out + // sign out Future signOut() async { return await _auth.signOut(); } - -// errors } diff --git a/lib/services/chat/chat_service.dart b/lib/services/chat/chat_service.dart index 7668ae8..b0fe0f2 100644 --- a/lib/services/chat/chat_service.dart +++ b/lib/services/chat/chat_service.dart @@ -1,4 +1,5 @@ import 'package:cloud_firestore/cloud_firestore.dart'; +import 'package:cofounderella/constants.dart'; import 'package:cofounderella/models/message.dart'; import 'package:firebase_auth/firebase_auth.dart'; @@ -9,7 +10,10 @@ class ChatService { // get user stream Stream>> getUsersStream() { - return _firestore.collection("Users").snapshots().map((snapshot) { + return _firestore + .collection(Constants.dbCollectionUsers) + .snapshots() + .map((snapshot) { return snapshot.docs.map((doc) { // iterate each user final user = doc.data(); @@ -41,11 +45,11 @@ class ChatService { ids.sort(); // sort to ensure the chatroomID is the same for any 2 users String chatRoomID = ids.join('_'); - // add new message to database + // add new message to database await _firestore - .collection("chat_rooms") + .collection(Constants.dbCollectionChatRooms) .doc(chatRoomID) - .collection("messages") + .collection(Constants.dbCollectionMessages) .add(newMessage.toMap()); } @@ -58,9 +62,9 @@ class ChatService { String chatRoomID = ids.join('_'); return _firestore - .collection("chat_rooms") + .collection(Constants.dbCollectionChatRooms) .doc(chatRoomID) - .collection("messages") + .collection(Constants.dbCollectionMessages) .orderBy("timestamp", descending: false) .snapshots(); }