-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #82 from subroh0508/add-common-viewmodel
Add common ViewModel
- Loading branch information
Showing
55 changed files
with
888 additions
and
233 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
...rc/androidUnitTest/kotlin/net/subroh0508/colormaster/data/extension/AuthClient.android.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package net.subroh0508.colormaster.data.extension | ||
|
||
import net.subroh0508.colormaster.network.auth.AuthClient | ||
|
||
actual suspend fun signInWithGoogle( | ||
client: AuthClient, | ||
) = client.signInWithGoogle("idToken") |
17 changes: 17 additions & 0 deletions
17
.../androidUnitTest/kotlin/net/subroh0508/colormaster/data/extension/UserDocument.android.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package net.subroh0508.colormaster.data.extension | ||
|
||
import net.subroh0508.colormaster.network.auth.AuthClient | ||
import net.subroh0508.colormaster.network.firestore.FirestoreClient | ||
import net.subroh0508.colormaster.network.firestore.document.UserDocument | ||
|
||
actual suspend fun setUserDocument( | ||
auth: AuthClient, | ||
firestore: FirestoreClient, | ||
inChargeIds: List<String>, | ||
favoriteIds: List<String>, | ||
) { | ||
auth.signInWithGoogle("idToken") | ||
val uid = auth.currentUser?.uid ?: return | ||
|
||
firestore.setUserDocument(uid, UserDocument(inChargeIds, favoriteIds)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 0 additions & 69 deletions
69
...st/kotlin/net/subroh0508/colormaster/data/spec/DefaultIdolColorsRepositorySpec.android.kt
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
core/data/src/commonMain/kotlin/net/subroh0508/colormaster/data/DefaultMyIdolsRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package net.subroh0508.colormaster.data | ||
|
||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.onStart | ||
import net.subroh0508.colormaster.data.extension.search | ||
import net.subroh0508.colormaster.model.IdolColor | ||
import net.subroh0508.colormaster.model.MyIdolsRepository | ||
import net.subroh0508.colormaster.network.auth.AuthClient | ||
import net.subroh0508.colormaster.network.firestore.FirestoreClient | ||
import net.subroh0508.colormaster.network.imasparql.ImasparqlClient | ||
|
||
internal class DefaultMyIdolsRepository( | ||
private val imasparqlClient: ImasparqlClient, | ||
private val firestoreClient: FirestoreClient, | ||
private val authClient: AuthClient, | ||
) : MyIdolsRepository { | ||
private val inChargeOfIdolsStateFlow = MutableStateFlow<List<IdolColor>>(listOf()) | ||
private val favoriteIdolsStateFlow = MutableStateFlow<List<IdolColor>>(listOf()) | ||
|
||
override fun getInChargeOfIdolsStream(lang: String): Flow<List<IdolColor>> { | ||
return inChargeOfIdolsStateFlow.onStart { | ||
inChargeOfIdolsStateFlow.value = imasparqlClient.search(getUserDocument().inCharges, lang) | ||
} | ||
} | ||
|
||
override fun getFavoriteIdolsStream(lang: String): Flow<List<IdolColor>> { | ||
return favoriteIdolsStateFlow.onStart { | ||
favoriteIdolsStateFlow.value = imasparqlClient.search(getUserDocument().favorites, lang) | ||
} | ||
} | ||
|
||
private val currentUser get() = authClient.currentUser | ||
|
||
private suspend fun getUserDocument() = firestoreClient.getUserDocument(currentUser?.uid) | ||
} |
24 changes: 24 additions & 0 deletions
24
core/data/src/commonMain/kotlin/net/subroh0508/colormaster/data/DefaultPreviewRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package net.subroh0508.colormaster.data | ||
|
||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.onStart | ||
import net.subroh0508.colormaster.data.extension.search | ||
import net.subroh0508.colormaster.model.IdolColor | ||
import net.subroh0508.colormaster.model.PreviewRepository | ||
import net.subroh0508.colormaster.network.imasparql.ImasparqlClient | ||
|
||
internal class DefaultPreviewRepository( | ||
private val imasparqlClient: ImasparqlClient, | ||
) : PreviewRepository { | ||
private val idolsStateFlow = MutableStateFlow<List<IdolColor>>(listOf()) | ||
|
||
override fun getPreviewColorsStream() = idolsStateFlow.onStart { clear() } | ||
|
||
override fun clear() { | ||
idolsStateFlow.value = listOf() | ||
} | ||
|
||
override suspend fun show(ids: List<String>, lang: String) { | ||
idolsStateFlow.value = imasparqlClient.search(ids, lang) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
core/data/src/commonMain/kotlin/net/subroh0508/colormaster/data/di/MyIdolsRepositories.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package net.subroh0508.colormaster.data.di | ||
|
||
import net.subroh0508.colormaster.data.DefaultMyIdolsRepository | ||
import net.subroh0508.colormaster.model.MyIdolsRepository | ||
import org.koin.dsl.module | ||
|
||
object MyIdolsRepositories { | ||
val Module get() = module { | ||
single<MyIdolsRepository> { DefaultMyIdolsRepository(get(), get(), get()) } | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
core/data/src/commonMain/kotlin/net/subroh0508/colormaster/data/di/PreviewRepositories.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package net.subroh0508.colormaster.data.di | ||
|
||
import net.subroh0508.colormaster.data.DefaultPreviewRepository | ||
import net.subroh0508.colormaster.model.PreviewRepository | ||
import org.koin.dsl.module | ||
|
||
object PreviewRepositories { | ||
val Module get() = module { | ||
single<PreviewRepository> { DefaultPreviewRepository(get()) } | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
core/data/src/commonMain/kotlin/net/subroh0508/colormaster/data/extension/Converter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package net.subroh0508.colormaster.data.extension | ||
|
||
import net.subroh0508.colormaster.model.IdolColor | ||
import net.subroh0508.colormaster.network.imasparql.json.IdolColorJson | ||
import net.subroh0508.colormaster.network.imasparql.serializer.Response | ||
|
||
internal fun Response<IdolColorJson>.toIdolColors() = results | ||
.bindings | ||
.mapNotNull { (idMap, nameMap, colorMap) -> | ||
val id = idMap["value"] ?: return@mapNotNull null | ||
val name = nameMap["value"] ?: return@mapNotNull null | ||
val color = colorMap["value"] ?: return@mapNotNull null | ||
|
||
val intColor = Triple( | ||
color.substring(0, 2).toInt(16), | ||
color.substring(2, 4).toInt(16), | ||
color.substring(4, 6).toInt(16), | ||
) | ||
|
||
IdolColor(id, name, intColor) | ||
} |
16 changes: 16 additions & 0 deletions
16
core/data/src/commonMain/kotlin/net/subroh0508/colormaster/data/extension/ImasparqlClient.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package net.subroh0508.colormaster.data.extension | ||
|
||
import net.subroh0508.colormaster.network.imasparql.ImasparqlClient | ||
import net.subroh0508.colormaster.network.imasparql.json.IdolColorJson | ||
import net.subroh0508.colormaster.network.imasparql.query.SearchByIdQuery | ||
|
||
internal suspend fun ImasparqlClient.search( | ||
ids: List<String>, | ||
lang: String, | ||
) = if (ids.isEmpty()) | ||
listOf() | ||
else | ||
search( | ||
SearchByIdQuery(lang, ids).build(), | ||
IdolColorJson.serializer(), | ||
).toIdolColors() |
7 changes: 7 additions & 0 deletions
7
core/data/src/commonTest/kotlin/net/subroh0508/colormaster/data/extension/AuthClient.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package net.subroh0508.colormaster.data.extension | ||
|
||
import net.subroh0508.colormaster.network.auth.AuthClient | ||
|
||
expect suspend fun signInWithGoogle( | ||
client: AuthClient, | ||
) |
11 changes: 11 additions & 0 deletions
11
core/data/src/commonTest/kotlin/net/subroh0508/colormaster/data/extension/UserDocument.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package net.subroh0508.colormaster.data.extension | ||
|
||
import net.subroh0508.colormaster.network.auth.AuthClient | ||
import net.subroh0508.colormaster.network.firestore.FirestoreClient | ||
|
||
expect suspend fun setUserDocument( | ||
auth: AuthClient, | ||
firestore: FirestoreClient, | ||
inChargeIds: List<String> = listOf(), | ||
favoriteIds: List<String> = listOf(), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
...a/src/commonTest/kotlin/net/subroh0508/colormaster/data/module/MyIdolsRepositoryModule.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package net.subroh0508.colormaster.data.module | ||
|
||
import io.ktor.client.HttpClient | ||
import net.subroh0508.colormaster.data.di.MyIdolsRepositories | ||
import net.subroh0508.colormaster.model.MyIdolsRepository | ||
import net.subroh0508.colormaster.network.auth.AuthClient | ||
import net.subroh0508.colormaster.network.firestore.FirestoreClient | ||
import net.subroh0508.colormaster.network.imasparql.di.Api | ||
import net.subroh0508.colormaster.test.fake.FakeAuthClient | ||
import net.subroh0508.colormaster.test.fake.FakeFirestoreClient | ||
import org.koin.dsl.koinApplication | ||
import org.koin.dsl.module | ||
|
||
internal fun buildMyIdolsRepository( | ||
block: () -> HttpClient, | ||
): Triple<MyIdolsRepository, AuthClient, FirestoreClient> { | ||
val authClient: AuthClient = FakeAuthClient() | ||
val firestoreClient: FirestoreClient = FakeFirestoreClient() | ||
|
||
val repository: MyIdolsRepository = koinApplication { | ||
modules( | ||
Api.Module(block()) + module { | ||
single { authClient } | ||
single { firestoreClient } | ||
} + MyIdolsRepositories.Module | ||
) | ||
}.koin.get(MyIdolsRepository::class) | ||
|
||
return Triple(repository, authClient, firestoreClient) | ||
} |
Oops, something went wrong.