Skip to content

Commit

Permalink
Merge pull request #299 from Sharingang/fix-offline
Browse files Browse the repository at this point in the history
Fix offline mode
  • Loading branch information
betrisey authored Jun 7, 2021
2 parents 905623d + 7653956 commit 32f29e4
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 30 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,27 +24,35 @@ abstract class AbstractFirestoreStore<T : Any>(
* @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)
}

/**
* Function to retrieve all elements in the database.
* @return List of all elements in the database.
*/
open suspend fun getAll(): List<T> {
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()
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,24 +93,32 @@ class FirestoreUserStore @Inject constructor(private val firestore: FirebaseFire
}

override suspend fun getChatPartners(userId: String): List<String> {
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<Chat> {
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()
}
}

Expand Down Expand Up @@ -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<String> {
Expand Down

0 comments on commit 32f29e4

Please sign in to comment.