From ab07964bf93e1a4d46e35189e106f9eea0cb36b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20B=C3=A9trisey?= Date: Mon, 7 Jun 2021 16:41:38 +0200 Subject: [PATCH 1/2] Fix offline mode --- .../firestore/AbstractFirestoreStore.kt | 30 +++++++----- .../database/firestore/FirestoreUserStore.kt | 48 ++++++++++++------- 2 files changed, 49 insertions(+), 29 deletions(-) diff --git a/app/src/main/java/com/example/sharingang/database/firestore/AbstractFirestoreStore.kt b/app/src/main/java/com/example/sharingang/database/firestore/AbstractFirestoreStore.kt index f43954ff..1a55df42 100644 --- a/app/src/main/java/com/example/sharingang/database/firestore/AbstractFirestoreStore.kt +++ b/app/src/main/java/com/example/sharingang/database/firestore/AbstractFirestoreStore.kt @@ -24,15 +24,19 @@ abstract class AbstractFirestoreStore( * @return The item corresponding to the id. */ open suspend fun get(id: String): T? { - val document = firestore.collection(collectionName) - .document(id) - .get() - .await() + return try { + val document = firestore.collection(collectionName) + .document(id) + .get() + .await() - if (document == null) { - Log.d(tag, "No $collectionName with ID = $id") + if (document == null) { + Log.d(tag, "No $collectionName with ID = $id") + } + document?.toObject(typeClass) + } catch (_: Exception) { + null } - return document?.toObject(typeClass) } /** @@ -40,11 +44,15 @@ abstract class AbstractFirestoreStore( * @return List of all elements in the database. */ open suspend fun getAll(): List { - val result = firestore.collection(collectionName) - .get() - .await() + return try { + val result = firestore.collection(collectionName) + .get() + .await() - return result.map { it.toObject(typeClass) } + result.map { it.toObject(typeClass) } + } catch (_: Exception) { + emptyList() + } } /** diff --git a/app/src/main/java/com/example/sharingang/database/firestore/FirestoreUserStore.kt b/app/src/main/java/com/example/sharingang/database/firestore/FirestoreUserStore.kt index 39e6c73d..d0ab0d1c 100644 --- a/app/src/main/java/com/example/sharingang/database/firestore/FirestoreUserStore.kt +++ b/app/src/main/java/com/example/sharingang/database/firestore/FirestoreUserStore.kt @@ -93,24 +93,32 @@ class FirestoreUserStore @Inject constructor(private val firestore: FirebaseFire } override suspend fun getChatPartners(userId: String): List { - val chatPartners = - getUserDocument(userId).collection(DatabaseFields.DBFIELD_MESSAGEPARTNERS).get() - .await() - return chatPartners.documents.map { it.id } + return try { + val chatPartners = + getUserDocument(userId).collection(DatabaseFields.DBFIELD_MESSAGEPARTNERS).get() + .await() + chatPartners.documents.map { it.id } + } catch (_: Exception) { + emptyList() + } } override suspend fun getMessages(userId: String, with: String): List { - val documents = getUserDocument(userId).collection(DatabaseFields.DBFIELD_CHATS) - .document(with).collection(DatabaseFields.DBFIELD_MESSAGES) - .orderBy(DatabaseFields.DBFIELD_DATE) - .get().await().documents - return documents.map { - Chat( - it.getString(DatabaseFields.DBFIELD_FROM), - it.getString(DatabaseFields.DBFIELD_TO), - it.getString(DatabaseFields.DBFIELD_MESSAGE)!!, - it.getDate(DatabaseFields.DBFIELD_DATE)!! - ) + return try { + val documents = getUserDocument(userId).collection(DatabaseFields.DBFIELD_CHATS) + .document(with).collection(DatabaseFields.DBFIELD_MESSAGES) + .orderBy(DatabaseFields.DBFIELD_DATE) + .get().await().documents + return documents.map { + Chat( + it.getString(DatabaseFields.DBFIELD_FROM), + it.getString(DatabaseFields.DBFIELD_TO), + it.getString(DatabaseFields.DBFIELD_MESSAGE)!!, + it.getDate(DatabaseFields.DBFIELD_DATE)!! + ) + } + } catch (_: Exception) { + emptyList() } } @@ -178,9 +186,13 @@ class FirestoreUserStore @Inject constructor(private val firestore: FirebaseFire } override suspend fun hasBeenBlocked(userId: String, by: String): Boolean { - val blocker = getUserDocument(by) - return blocker.collection(DatabaseFields.DBFIELD_BLOCKS) - .document(userId).get().await().getBoolean(DatabaseFields.DBFIELD_ISBLOCKED) ?: false + return try { + val blocker = getUserDocument(by) + blocker.collection(DatabaseFields.DBFIELD_BLOCKS) + .document(userId).get().await().getBoolean(DatabaseFields.DBFIELD_ISBLOCKED) ?: false + } catch (_: Exception) { + true + } } override suspend fun getBlockedUsers(userId: String): List { From 765395600df6ed69eaec68f45e79cead9cc50e53 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20B=C3=A9trisey?= Date: Mon, 7 Jun 2021 17:05:59 +0200 Subject: [PATCH 2/2] Update Firebase SDK --- app/build.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/build.gradle b/app/build.gradle index 3a8d42f8..3ceefad1 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -72,7 +72,7 @@ dependencies { implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.3.1' implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0' - implementation platform('com.google.firebase:firebase-bom:28.0.1') + implementation platform('com.google.firebase:firebase-bom:28.1.0') implementation 'com.google.firebase:firebase-firestore-ktx' implementation 'com.google.firebase:firebase-auth-ktx' implementation 'com.google.firebase:firebase-storage-ktx'