Skip to content

Commit

Permalink
Removed io dispatcher (#73)
Browse files Browse the repository at this point in the history
  • Loading branch information
JackEblan authored Oct 2, 2024
1 parent 6f9adad commit 20d49a5
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 30 deletions.
1 change: 0 additions & 1 deletion core/network/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ dependencies {
implementation(platform(libs.firebase.bom))
implementation(libs.firebase.firestore)

implementation(projects.core.common)
implementation(projects.core.model)

testImplementation(projects.core.testing)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
*/
package com.eblan.socialworkreviewer.core.network.firestore

import com.eblan.socialworkreviewer.core.common.Dispatcher
import com.eblan.socialworkreviewer.core.common.SwrDispatchers.IO
import com.eblan.socialworkreviewer.core.model.Category
import com.eblan.socialworkreviewer.core.network.firestore.CategoryDataSource.Companion.CATEGORIES_COLLECTION
import com.eblan.socialworkreviewer.core.network.mapper.toCategory
Expand All @@ -28,17 +26,14 @@ import com.google.firebase.firestore.FirebaseFirestore
import com.google.firebase.firestore.Query
import com.google.firebase.firestore.snapshots
import com.google.firebase.firestore.toObject
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.mapNotNull
import kotlinx.coroutines.tasks.await
import kotlinx.coroutines.withContext
import javax.inject.Inject

internal class DefaultCategoryDataSource @Inject constructor(
private val firestore: FirebaseFirestore,
@Dispatcher(IO) private val ioDispatcher: CoroutineDispatcher,
) : CategoryDataSource {
override fun getCategoryDocuments(): Flow<List<Category>> {
return firestore.collection(CATEGORIES_COLLECTION).orderBy(DATE, Query.Direction.ASCENDING)
Expand All @@ -54,16 +49,13 @@ internal class DefaultCategoryDataSource @Inject constructor(
}

override suspend fun getCategoryDocument(categoryDocumentId: String): Category? {
return withContext(ioDispatcher) {
val documentSnapshot =
firestore.collection(CATEGORIES_COLLECTION).document(categoryDocumentId).get()
.await()
val documentSnapshot =
firestore.collection(CATEGORIES_COLLECTION).document(categoryDocumentId).get().await()

try {
toCategory(categoryDocument = documentSnapshot.toObject<CategoryDocument>())
} catch (e: RuntimeException) {
null
}
return try {
toCategory(categoryDocument = documentSnapshot.toObject<CategoryDocument>())
} catch (e: RuntimeException) {
null
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,35 +17,27 @@
*/
package com.eblan.socialworkreviewer.core.network.firestore

import com.eblan.socialworkreviewer.core.common.Dispatcher
import com.eblan.socialworkreviewer.core.common.SwrDispatchers
import com.eblan.socialworkreviewer.core.model.Question
import com.eblan.socialworkreviewer.core.network.firestore.CategoryDataSource.Companion.CATEGORIES_COLLECTION
import com.eblan.socialworkreviewer.core.network.firestore.QuestionDataSource.Companion.QUESTIONS_COLLECTION
import com.eblan.socialworkreviewer.core.network.mapper.toQuestion
import com.eblan.socialworkreviewer.core.network.model.QuestionDocument
import com.google.firebase.firestore.FirebaseFirestore
import com.google.firebase.firestore.toObject
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.tasks.await
import kotlinx.coroutines.withContext
import javax.inject.Inject

internal class DefaultQuestionDataSource @Inject constructor(
private val firestore: FirebaseFirestore,
@Dispatcher(SwrDispatchers.IO) private val ioDispatcher: CoroutineDispatcher,
) : QuestionDataSource {
override suspend fun getQuestions(id: String): List<Question> {
return withContext(ioDispatcher) {
firestore.collection(CATEGORIES_COLLECTION).document(id)
.collection(QUESTIONS_COLLECTION).get().await()
.mapNotNull { queryDocumentSnapshot ->
try {
toQuestion(questionDocument = queryDocumentSnapshot.toObject<QuestionDocument>())
} catch (e: RuntimeException) {
null
}
return firestore.collection(CATEGORIES_COLLECTION).document(id)
.collection(QUESTIONS_COLLECTION).get().await().mapNotNull { queryDocumentSnapshot ->
try {
toQuestion(questionDocument = queryDocumentSnapshot.toObject<QuestionDocument>())
} catch (e: RuntimeException) {
null
}
}
}
}
}

0 comments on commit 20d49a5

Please sign in to comment.