Skip to content

Commit

Permalink
Add Koin
Browse files Browse the repository at this point in the history
  • Loading branch information
takahirom committed Sep 3, 2022
1 parent 4540b20 commit 2d8cbe7
Show file tree
Hide file tree
Showing 10 changed files with 56 additions and 14 deletions.
1 change: 1 addition & 0 deletions app-ios-combined/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ kotlin {
projects.forEach {
api(it)
}
implementation(libs.koin)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package io.github.droidkaigi.confsched2022

import org.koin.core.context.startKoin

fun initKoin() {
startKoin {
modules(dataModule)
}
}
1 change: 1 addition & 0 deletions core-data/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ kotlin {
val iosMain by getting {
dependencies {
implementation(libs.ktorClientDarwin)
implementation(libs.koin)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -71,7 +71,7 @@ class ApiModule {
}

private val Context.dataStore by preferencesDataStore(
name = PreferenceDatastore.NAME,
name = UserDatastore.NAME,
)

@Provides
Expand All @@ -81,7 +81,7 @@ class ApiModule {
}
@Provides
@Singleton
fun providePreferenceDatastore(flowSettings: FlowSettings): PreferenceDatastore {
return PreferenceDatastore(flowSettings)
fun providePreferenceDatastore(flowSettings: FlowSettings): UserDatastore {
return UserDatastore(flowSettings)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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<DroidKaigiSchedule> = callbackFlow {
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -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<FlowSettings> {
AppleSettings(NSUserDefaults.new()!!).toFlowSettings()
}
singleOf(::UserDatastore)

singleOf(::NetworkService)
singleOf(::AuthApi)
singleOf(::SessionsApi)
singleOf(::DataSessionsRepository) bind SessionsRepository::class
}
4 changes: 4 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down Expand Up @@ -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" }
Expand Down

0 comments on commit 2d8cbe7

Please sign in to comment.