Skip to content

Commit a84c856

Browse files
committed
refactor
1 parent 39d9361 commit a84c856

File tree

4 files changed

+19
-45
lines changed

4 files changed

+19
-45
lines changed
Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,22 @@
11
package com.espressodev.gptmap.core.domain
22

3-
import com.espressodev.gptmap.core.data.AccountService
43
import com.espressodev.gptmap.core.data.FirestoreService
5-
import com.espressodev.gptmap.core.model.Exceptions
64
import com.espressodev.gptmap.core.mongodb.RealmSyncService
75
import kotlinx.coroutines.Dispatchers
8-
import kotlinx.coroutines.async
96
import kotlinx.coroutines.withContext
107
import javax.inject.Inject
118

129
class AddDatabaseIfUserIsNewUseCase @Inject constructor(
13-
private val accountService: AccountService,
1410
private val firestoreService: FirestoreService,
1511
private val realmSyncService: RealmSyncService
1612
) {
1713
suspend operator fun invoke(): Result<Boolean> = withContext(Dispatchers.IO) {
18-
val isUserInRealmDb = async { realmSyncService.isUserInDatabase() }.await()
19-
if (isUserInRealmDb) {
20-
return@withContext Result.success(value = true)
14+
runCatching {
15+
val isUserInRealmDb = realmSyncService.isUserInDatabase().getOrThrow()
16+
if (isUserInRealmDb) return@runCatching true
17+
val realmUser = firestoreService.getUser().toRealmUser()
18+
realmSyncService.saveUser(realmUser).getOrThrow()
19+
true
2120
}
22-
val realmUser =
23-
accountService.userId?.let { userId -> firestoreService.getUser(userId).toRealmUser() }
24-
?: throw Exceptions.FirebaseUserIsNullException()
25-
realmSyncService.saveUser(realmUser).getOrThrow()
26-
Result.success(value = true)
2721
}
2822
}

core/mongodb/src/main/kotlin/com/espressodev/gptmap/core/mongodb/RealmSyncService.kt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,11 @@ interface RealmSyncService {
1414

1515
suspend fun saveImageAnalysis(realmImageAnalysis: RealmImageAnalysis): Result<Boolean>
1616

17-
fun isUserInDatabase(): Boolean
17+
fun isUserInDatabase(): Result<Boolean>
1818

1919
fun getFavourites(): Flow<List<Favourite>>
2020

2121
fun getFavourite(id: String): Favourite
2222

2323
fun getImageAnalyses(): Flow<List<ImageAnalysis>>
24-
25-
suspend fun getImageAnalysis(id: String): Result<ImageAnalysis>
2624
}

core/mongodb/src/main/kotlin/com/espressodev/gptmap/core/mongodb/impl/RealmSyncServiceImpl.kt

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,8 @@ import com.espressodev.gptmap.core.mongodb.module.RealmModule.realm
1313
import com.espressodev.gptmap.core.mongodb.module.RealmModule.realmUser
1414
import io.realm.kotlin.UpdatePolicy
1515
import io.realm.kotlin.ext.query
16-
import kotlinx.coroutines.Dispatchers
1716
import kotlinx.coroutines.flow.Flow
1817
import kotlinx.coroutines.flow.map
19-
import kotlinx.coroutines.withContext
2018

2119
class RealmSyncServiceImpl : RealmSyncService {
2220

@@ -84,21 +82,7 @@ class RealmSyncServiceImpl : RealmSyncService {
8482
it.list.map { realmImageAnalysis -> realmImageAnalysis.toImageAnalysis() }
8583
}
8684

87-
override suspend fun getImageAnalysis(id: String): Result<ImageAnalysis> =
88-
withContext(Dispatchers.IO) {
89-
runCatching {
90-
realm.query<RealmImageAnalysis>(
91-
"userId == $0 AND imageId == $1",
92-
realmUserId,
93-
id
94-
)
95-
.find()
96-
.first()
97-
.toImageAnalysis()
98-
}
99-
}
100-
101-
override fun isUserInDatabase(): Boolean =
85+
override fun isUserInDatabase(): Result<Boolean> = runCatching {
10286
realm.query<RealmUser>("userId == $0", realmUserId).first().find() != null
103-
87+
}
10488
}

gradle/libs.versions.toml

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,20 @@ androidxComposeBom = "2023.10.01"
66
androidxComposeCompiler = "1.5.3" # Compose VC
77
androidxCoreSplashscreen = "1.0.1"
88
androidxHiltNavigationCompose = "1.1.0"
9-
androidxLifecycle = "2.6.2"
9+
androidxLifecycle = "2.7.0"
1010
androidxNavigation = "2.7.6"
1111
androidxTestExt = "1.1.5"
12-
androidxTestRunner = "1.5.0"
12+
androidxTestRunner = "1.5.2"
1313
androidxWork = "2.9.0"
14-
firebaseBom = "32.7.0"
14+
firebaseBom = "32.7.1"
1515
firebaseCrashlyticsPlugin = "2.9.9"
16-
firebase-crashlytics = "18.6.0"
17-
firebase-auth = "22.3.0"
18-
firebase-firestore = "24.10.0"
16+
firebase-crashlytics = "18.6.1"
17+
firebase-auth = "22.3.1"
18+
firebase-firestore = "24.10.1"
1919
firebase-analytics = "21.5.0"
2020
firebase-messaging = "23.4.0"
2121
gmsPlugin = "4.4.0"
22-
hilt = "2.48.1"
22+
hilt = "2.50"
2323
hiltExt = "1.1.0"
2424
kotlin = "1.9.10" # Can't update due to Realm
2525
kotlinxCoroutines = "1.7.3"
@@ -42,15 +42,15 @@ junitVersion = "4.13.2"
4242
espressoCore = "3.5.1"
4343
appcompat = "1.6.1"
4444
coilCompose = "2.5.0"
45-
generativeai = "0.1.1"
45+
generativeai = "0.1.2"
4646
lottieCompose = "6.2.0"
4747
firebaseStorage = "20.3.0"
4848
kotlin2 = "1.9.10"
4949
coreKtxVersion = "1.12.0"
5050
baseJunit = "4.13.2"
5151
espressoCoreVersion = "3.5.1"
5252
appcompatVersion = "1.6.1"
53-
material = "1.11.0"
53+
material = "1.5.4"
5454
detekt = "1.23.3"
5555
detektCompose = "0.3.8"
5656
kotlinxCollectionsImmutable = "0.3.6"
@@ -65,6 +65,7 @@ turbine = "1.0.0"
6565
# Compose
6666
androidx-activity-compose = { group = "androidx.activity", name = "activity-compose", version.ref = "androidxActivity" }
6767
androidx-compose-foundation = { group = "androidx.compose.foundation", name = "foundation" }
68+
androidx-compose-material = { group = "androidx.compose.material", name = "material", version.ref = "material" }
6869
androidx-compose-foundation-layout = { group = "androidx.compose.foundation", name = "foundation-layout" }
6970
androidx-compose-material-iconsExtended = { group = "androidx.compose.material", name = "material-icons-extended" }
7071
androidx-compose-material3 = { group = "androidx.compose.material3", name = "material3" }
@@ -171,9 +172,6 @@ kotlin-gradlePlugin = { group = "org.jetbrains.kotlin", name = "kotlin-gradle-pl
171172
ksp-gradlePlugin = { group = "com.google.devtools.ksp", name = "com.google.devtools.ksp.gradle.plugin", version.ref = "ksp" }
172173
androidx-core-ktx = { group = "androidx.core", name = "core-ktx", version.ref = "coreKtxVersion" }
173174
junit = { group = "junit", name = "junit", version.ref = "baseJunit" }
174-
androidx-espresso-core = { group = "androidx.test.espresso", name = "espresso-core", version.ref = "espressoCoreVersion" }
175-
androidx-appcompat = { group = "androidx.appcompat", name = "appcompat", version.ref = "appcompatVersion" }
176-
material = { group = "com.google.android.material", name = "material", version.ref = "material" }
177175
# End of it
178176

179177
[plugins]

0 commit comments

Comments
 (0)