From d1a6d81debd17d80c0b9a923d14768f15c447e8e Mon Sep 17 00:00:00 2001 From: JackEblan Date: Thu, 3 Oct 2024 04:29:59 +0800 Subject: [PATCH] Removed io dispatcher --- core/network/build.gradle.kts | 1 - .../firestore/DefaultCategoryDataSource.kt | 20 +++++------------ .../firestore/DefaultQuestionDataSource.kt | 22 ++++++------------- 3 files changed, 13 insertions(+), 30 deletions(-) diff --git a/core/network/build.gradle.kts b/core/network/build.gradle.kts index 77a5db17..26b3525b 100644 --- a/core/network/build.gradle.kts +++ b/core/network/build.gradle.kts @@ -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) diff --git a/core/network/src/main/kotlin/com/eblan/socialworkreviewer/core/network/firestore/DefaultCategoryDataSource.kt b/core/network/src/main/kotlin/com/eblan/socialworkreviewer/core/network/firestore/DefaultCategoryDataSource.kt index 23c7251b..380de7b7 100644 --- a/core/network/src/main/kotlin/com/eblan/socialworkreviewer/core/network/firestore/DefaultCategoryDataSource.kt +++ b/core/network/src/main/kotlin/com/eblan/socialworkreviewer/core/network/firestore/DefaultCategoryDataSource.kt @@ -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 @@ -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> { return firestore.collection(CATEGORIES_COLLECTION).orderBy(DATE, Query.Direction.ASCENDING) @@ -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()) - } catch (e: RuntimeException) { - null - } + return try { + toCategory(categoryDocument = documentSnapshot.toObject()) + } catch (e: RuntimeException) { + null } } } diff --git a/core/network/src/main/kotlin/com/eblan/socialworkreviewer/core/network/firestore/DefaultQuestionDataSource.kt b/core/network/src/main/kotlin/com/eblan/socialworkreviewer/core/network/firestore/DefaultQuestionDataSource.kt index 71fcb7e6..95fc6de7 100644 --- a/core/network/src/main/kotlin/com/eblan/socialworkreviewer/core/network/firestore/DefaultQuestionDataSource.kt +++ b/core/network/src/main/kotlin/com/eblan/socialworkreviewer/core/network/firestore/DefaultQuestionDataSource.kt @@ -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 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 @@ -26,26 +24,20 @@ 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 { - return withContext(ioDispatcher) { - firestore.collection(CATEGORIES_COLLECTION).document(id) - .collection(QUESTIONS_COLLECTION).get().await() - .mapNotNull { queryDocumentSnapshot -> - try { - toQuestion(questionDocument = queryDocumentSnapshot.toObject()) - } catch (e: RuntimeException) { - null - } + return firestore.collection(CATEGORIES_COLLECTION).document(id) + .collection(QUESTIONS_COLLECTION).get().await().mapNotNull { queryDocumentSnapshot -> + try { + toQuestion(questionDocument = queryDocumentSnapshot.toObject()) + } catch (e: RuntimeException) { + null } - } + } } }