From 2d8cbe77bba3331d3a225ff3c192079b4f043743 Mon Sep 17 00:00:00 2001 From: takahirom Date: Sat, 3 Sep 2022 15:17:52 +0900 Subject: [PATCH] Add Koin --- app-ios-combined/build.gradle.kts | 1 + .../droidkaigi/confsched2022/KmpHelper.kt | 9 +++++++ core-data/build.gradle.kts | 1 + .../confsched2022/data/di/ApiModule.kt | 12 ++++----- .../data/sessions/di/SessionDataModule.kt | 6 ++--- ...referenceDatastore.kt => UserDatastore.kt} | 2 +- .../confsched2022/data/auth/AuthApi.kt | 4 +-- .../data/sessions/DataSessionsRepository.kt | 4 +-- .../droidkaigi/confsched2022/KoinModules.kt | 27 +++++++++++++++++++ gradle/libs.versions.toml | 4 +++ 10 files changed, 56 insertions(+), 14 deletions(-) create mode 100644 app-ios-combined/src/iosMain/kotlin/io/github/droidkaigi/confsched2022/KmpHelper.kt rename core-data/src/commonMain/kotlin/io/github/droidkaigi/confsched2022/data/{PreferenceDatastore.kt => UserDatastore.kt} (97%) create mode 100644 core-data/src/iosMain/kotlin/io/github/droidkaigi/confsched2022/KoinModules.kt diff --git a/app-ios-combined/build.gradle.kts b/app-ios-combined/build.gradle.kts index d84386d61..70a114fbe 100644 --- a/app-ios-combined/build.gradle.kts +++ b/app-ios-combined/build.gradle.kts @@ -32,6 +32,7 @@ kotlin { projects.forEach { api(it) } + implementation(libs.koin) } } } diff --git a/app-ios-combined/src/iosMain/kotlin/io/github/droidkaigi/confsched2022/KmpHelper.kt b/app-ios-combined/src/iosMain/kotlin/io/github/droidkaigi/confsched2022/KmpHelper.kt new file mode 100644 index 000000000..bb497a64b --- /dev/null +++ b/app-ios-combined/src/iosMain/kotlin/io/github/droidkaigi/confsched2022/KmpHelper.kt @@ -0,0 +1,9 @@ +package io.github.droidkaigi.confsched2022 + +import org.koin.core.context.startKoin + +fun initKoin() { + startKoin { + modules(dataModule) + } +} \ No newline at end of file diff --git a/core-data/build.gradle.kts b/core-data/build.gradle.kts index 6869a386d..ded0e77e5 100644 --- a/core-data/build.gradle.kts +++ b/core-data/build.gradle.kts @@ -30,6 +30,7 @@ kotlin { val iosMain by getting { dependencies { implementation(libs.ktorClientDarwin) + implementation(libs.koin) } } } diff --git a/core-data/src/androidMain/kotlin/io/github/droidkaigi/confsched2022/data/di/ApiModule.kt b/core-data/src/androidMain/kotlin/io/github/droidkaigi/confsched2022/data/di/ApiModule.kt index 5c80653f3..2ddaea7b1 100644 --- a/core-data/src/androidMain/kotlin/io/github/droidkaigi/confsched2022/data/di/ApiModule.kt +++ b/core-data/src/androidMain/kotlin/io/github/droidkaigi/confsched2022/data/di/ApiModule.kt @@ -10,7 +10,7 @@ import dagger.Provides import dagger.hilt.InstallIn import dagger.hilt.components.SingletonComponent import io.github.droidkaigi.confsched2022.data.NetworkService -import io.github.droidkaigi.confsched2022.data.PreferenceDatastore +import io.github.droidkaigi.confsched2022.data.UserDatastore import io.github.droidkaigi.confsched2022.data.auth.AuthApi import io.github.droidkaigi.confsched2022.data.auth.Authenticator import io.github.droidkaigi.confsched2022.data.auth.AuthenticatorImpl @@ -36,10 +36,10 @@ class ApiModule { @Singleton fun provideAuthApi( httpClient: HttpClient, - preferenceDatastore: PreferenceDatastore, + userDatastore: UserDatastore, authenticator: Authenticator ): AuthApi { - return AuthApi(httpClient, preferenceDatastore, authenticator) + return AuthApi(httpClient, userDatastore, authenticator) } @Provides @@ -71,7 +71,7 @@ class ApiModule { } private val Context.dataStore by preferencesDataStore( - name = PreferenceDatastore.NAME, + name = UserDatastore.NAME, ) @Provides @@ -81,7 +81,7 @@ class ApiModule { } @Provides @Singleton - fun providePreferenceDatastore(flowSettings: FlowSettings): PreferenceDatastore { - return PreferenceDatastore(flowSettings) + fun providePreferenceDatastore(flowSettings: FlowSettings): UserDatastore { + return UserDatastore(flowSettings) } } diff --git a/core-data/src/androidMain/kotlin/io/github/droidkaigi/confsched2022/data/sessions/di/SessionDataModule.kt b/core-data/src/androidMain/kotlin/io/github/droidkaigi/confsched2022/data/sessions/di/SessionDataModule.kt index e0fddce2a..d0b2ea1f9 100644 --- a/core-data/src/androidMain/kotlin/io/github/droidkaigi/confsched2022/data/sessions/di/SessionDataModule.kt +++ b/core-data/src/androidMain/kotlin/io/github/droidkaigi/confsched2022/data/sessions/di/SessionDataModule.kt @@ -5,7 +5,7 @@ import dagger.Provides import dagger.hilt.InstallIn import dagger.hilt.components.SingletonComponent import io.github.droidkaigi.confsched2022.data.NetworkService -import io.github.droidkaigi.confsched2022.data.PreferenceDatastore +import io.github.droidkaigi.confsched2022.data.UserDatastore import io.github.droidkaigi.confsched2022.data.sessions.DataSessionsRepository import io.github.droidkaigi.confsched2022.data.sessions.SessionsApi import io.github.droidkaigi.confsched2022.model.SessionsRepository @@ -19,12 +19,12 @@ class SessionDataModule { @Singleton fun provideSessionsRepository( networkService: NetworkService, - preferenceDatastore: PreferenceDatastore + userDatastore: UserDatastore ): SessionsRepository { val sessionsApi = SessionsApi(networkService) return DataSessionsRepository( sessionsApi = sessionsApi, - favoriteSessionsDataStore = preferenceDatastore + favoriteSessionsDataStore = userDatastore ) } // @Provides diff --git a/core-data/src/commonMain/kotlin/io/github/droidkaigi/confsched2022/data/PreferenceDatastore.kt b/core-data/src/commonMain/kotlin/io/github/droidkaigi/confsched2022/data/UserDatastore.kt similarity index 97% rename from core-data/src/commonMain/kotlin/io/github/droidkaigi/confsched2022/data/PreferenceDatastore.kt rename to core-data/src/commonMain/kotlin/io/github/droidkaigi/confsched2022/data/UserDatastore.kt index 66fd2b397..48c9f0aa2 100644 --- a/core-data/src/commonMain/kotlin/io/github/droidkaigi/confsched2022/data/PreferenceDatastore.kt +++ b/core-data/src/commonMain/kotlin/io/github/droidkaigi/confsched2022/data/UserDatastore.kt @@ -8,7 +8,7 @@ import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.flow.map -class PreferenceDatastore(private val flowSettings: FlowSettings) { +class UserDatastore(private val flowSettings: FlowSettings) { suspend fun addFavorite(sessionId: String) { val favoriteSet = flowSettings.getString(KEY, "").toFavoriteSet().toMutableSet() diff --git a/core-data/src/commonMain/kotlin/io/github/droidkaigi/confsched2022/data/auth/AuthApi.kt b/core-data/src/commonMain/kotlin/io/github/droidkaigi/confsched2022/data/auth/AuthApi.kt index 72d7dbfd3..8b71f6a27 100644 --- a/core-data/src/commonMain/kotlin/io/github/droidkaigi/confsched2022/data/auth/AuthApi.kt +++ b/core-data/src/commonMain/kotlin/io/github/droidkaigi/confsched2022/data/auth/AuthApi.kt @@ -1,6 +1,6 @@ package io.github.droidkaigi.confsched2022.data.auth -import io.github.droidkaigi.confsched2022.data.PreferenceDatastore +import io.github.droidkaigi.confsched2022.data.UserDatastore import io.ktor.client.HttpClient import io.ktor.client.plugins.ResponseException import io.ktor.client.request.header @@ -14,7 +14,7 @@ import kotlinx.coroutines.flow.first class AuthApi( private val httpClient: HttpClient, - private val userDataStore: PreferenceDatastore, + private val userDataStore: UserDatastore, private val authenticator: Authenticator ) { suspend fun authIfNeeded() { diff --git a/core-data/src/commonMain/kotlin/io/github/droidkaigi/confsched2022/data/sessions/DataSessionsRepository.kt b/core-data/src/commonMain/kotlin/io/github/droidkaigi/confsched2022/data/sessions/DataSessionsRepository.kt index 2bd03203b..01902eb98 100644 --- a/core-data/src/commonMain/kotlin/io/github/droidkaigi/confsched2022/data/sessions/DataSessionsRepository.kt +++ b/core-data/src/commonMain/kotlin/io/github/droidkaigi/confsched2022/data/sessions/DataSessionsRepository.kt @@ -1,6 +1,6 @@ package io.github.droidkaigi.confsched2022.data.sessions -import io.github.droidkaigi.confsched2022.data.PreferenceDatastore +import io.github.droidkaigi.confsched2022.data.UserDatastore import io.github.droidkaigi.confsched2022.model.DroidKaigiSchedule import io.github.droidkaigi.confsched2022.model.SessionsRepository import io.github.droidkaigi.confsched2022.model.Timetable @@ -12,7 +12,7 @@ import kotlinx.coroutines.flow.callbackFlow class DataSessionsRepository( val sessionsApi: SessionsApi, - val favoriteSessionsDataStore: PreferenceDatastore + val favoriteSessionsDataStore: UserDatastore ) : SessionsRepository { override fun droidKaigiScheduleFlow(): Flow = callbackFlow { try { diff --git a/core-data/src/iosMain/kotlin/io/github/droidkaigi/confsched2022/KoinModules.kt b/core-data/src/iosMain/kotlin/io/github/droidkaigi/confsched2022/KoinModules.kt new file mode 100644 index 000000000..d51e1a55d --- /dev/null +++ b/core-data/src/iosMain/kotlin/io/github/droidkaigi/confsched2022/KoinModules.kt @@ -0,0 +1,27 @@ +package io.github.droidkaigi.confsched2022 + +import com.russhwolf.settings.AppleSettings +import com.russhwolf.settings.coroutines.FlowSettings +import com.russhwolf.settings.coroutines.toFlowSettings +import io.github.droidkaigi.confsched2022.data.NetworkService +import io.github.droidkaigi.confsched2022.data.UserDatastore +import io.github.droidkaigi.confsched2022.data.auth.AuthApi +import io.github.droidkaigi.confsched2022.data.sessions.DataSessionsRepository +import io.github.droidkaigi.confsched2022.data.sessions.SessionsApi +import io.github.droidkaigi.confsched2022.model.SessionsRepository +import org.koin.core.module.dsl.singleOf +import org.koin.dsl.bind +import org.koin.dsl.module +import platform.Foundation.NSUserDefaults + +val dataModule = module { + singleOf { + AppleSettings(NSUserDefaults.new()!!).toFlowSettings() + } + singleOf(::UserDatastore) + + singleOf(::NetworkService) + singleOf(::AuthApi) + singleOf(::SessionsApi) + singleOf(::DataSessionsRepository) bind SessionsRepository::class +} diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 76aeaf300..d850c9c0e 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -41,6 +41,7 @@ firebaseAuth = "21.0.8" androidxStartup = "1.1.1" kluent = "1.68" coilCompose="2.2.0" +koin = "3.2.0" [libraries] androidGradlePlugin = { group = "com.android.tools.build", name = "gradle", version.ref = "androidGradlePlugin" } @@ -104,6 +105,9 @@ firebaseCommon = { module = "com.google.firebase:firebase-common", version.ref = firebaseAuth = { module = "com.google.firebase:firebase-auth", version.ref = "firebaseAuth" } coilCompose = { module = "io.coil-kt:coil-compose", version.ref = "coilCompose" } +# iOS +koin = { module = "io.insert-koin:koin-core", version.ref = "koin" } + # test junit = { module = "junit:junit", version.ref = "junit" }