diff --git a/client/core/persistence/client-core-persistence.gradle.kts b/client/core/persistence/client-core-persistence.gradle.kts index e9fb57e403..2aaadca6ba 100644 --- a/client/core/persistence/client-core-persistence.gradle.kts +++ b/client/core/persistence/client-core-persistence.gradle.kts @@ -16,6 +16,7 @@ kotlin { sourceSets { commonMain.dependencies { + implementation(project(Modules.Common.Core.infrastructure)) libs.common.apply { implementation(koinCore) implementation(coroutines) diff --git a/client/core/persistence/src/androidMain/kotlin/com/oztechan/ccc/client/core/persistence/di/ClientCorePersistenceModule.android.kt b/client/core/persistence/src/androidMain/kotlin/com/oztechan/ccc/client/core/persistence/di/ClientCorePersistenceModule.android.kt index 8267a5748c..2ef23b2967 100644 --- a/client/core/persistence/src/androidMain/kotlin/com/oztechan/ccc/client/core/persistence/di/ClientCorePersistenceModule.android.kt +++ b/client/core/persistence/src/androidMain/kotlin/com/oztechan/ccc/client/core/persistence/di/ClientCorePersistenceModule.android.kt @@ -4,19 +4,15 @@ import android.content.Context import android.content.SharedPreferences import com.oztechan.ccc.client.core.persistence.FlowPersistence import com.oztechan.ccc.client.core.persistence.FlowPersistenceImpl -import com.oztechan.ccc.client.core.persistence.Persistence -import com.oztechan.ccc.client.core.persistence.PersistenceImpl import com.oztechan.ccc.client.core.persistence.SuspendPersistence import com.oztechan.ccc.client.core.persistence.SuspendPersistenceImpl +import com.oztechan.ccc.common.core.infrastructure.di.DISPATCHER_IO import com.russhwolf.settings.ObservableSettings -import com.russhwolf.settings.Settings import com.russhwolf.settings.SharedPreferencesSettings -import com.russhwolf.settings.coroutines.FlowSettings -import com.russhwolf.settings.coroutines.SuspendSettings import com.russhwolf.settings.coroutines.toFlowSettings import com.russhwolf.settings.coroutines.toSuspendSettings -import org.koin.core.module.dsl.bind import org.koin.core.module.dsl.singleOf +import org.koin.core.qualifier.named import org.koin.dsl.module private const val KEY_APPLICATION_PREFERENCES = "application_preferences" @@ -25,17 +21,13 @@ actual val clientCorePersistenceModule = module { singleOf(::provideSharedPreferences) single { SharedPreferencesSettings(get()) } - single { get() } - @Suppress("OPT_IN_USAGE") - single { get().toSuspendSettings() } - @Suppress("OPT_IN_USAGE") - single { get().toFlowSettings() } - singleOf(::PersistenceImpl) { bind() } - @Suppress("OPT_IN_USAGE") - singleOf(::FlowPersistenceImpl) { bind() } - @Suppress("OPT_IN_USAGE") - singleOf(::SuspendPersistenceImpl) { bind() } + single { + FlowPersistenceImpl(get().toFlowSettings(get(named(DISPATCHER_IO)))) + } + single { + SuspendPersistenceImpl(get().toSuspendSettings(get(named(DISPATCHER_IO)))) + } } private fun provideSharedPreferences( diff --git a/client/core/persistence/src/commonMain/kotlin/com/oztechan/ccc/client/core/persistence/Persistence.kt b/client/core/persistence/src/commonMain/kotlin/com/oztechan/ccc/client/core/persistence/Persistence.kt deleted file mode 100644 index 77002313a1..0000000000 --- a/client/core/persistence/src/commonMain/kotlin/com/oztechan/ccc/client/core/persistence/Persistence.kt +++ /dev/null @@ -1,6 +0,0 @@ -package com.oztechan.ccc.client.core.persistence - -interface Persistence { - fun getValue(key: String, defaultValue: T): T - fun setValue(key: String, value: T) -} diff --git a/client/core/persistence/src/commonMain/kotlin/com/oztechan/ccc/client/core/persistence/PersistenceImpl.kt b/client/core/persistence/src/commonMain/kotlin/com/oztechan/ccc/client/core/persistence/PersistenceImpl.kt deleted file mode 100644 index f5f6216942..0000000000 --- a/client/core/persistence/src/commonMain/kotlin/com/oztechan/ccc/client/core/persistence/PersistenceImpl.kt +++ /dev/null @@ -1,26 +0,0 @@ -package com.oztechan.ccc.client.core.persistence - -import com.oztechan.ccc.client.core.persistence.error.UnsupportedPersistenceException -import com.russhwolf.settings.Settings - -internal class PersistenceImpl(private val settings: Settings) : Persistence { - - @Suppress("UNCHECKED_CAST") - override fun getValue(key: String, defaultValue: T): T = when (defaultValue) { - is Long -> settings.getLong(key, defaultValue) as T - is String -> settings.getString(key, defaultValue) as T - is Int -> settings.getInt(key, defaultValue) as T - is Boolean -> settings.getBoolean(key, defaultValue) as T - is Float -> settings.getFloat(key, defaultValue) as T - else -> throw UnsupportedPersistenceException() - } - - override fun setValue(key: String, value: T) = when (value) { - is Long -> settings.putLong(key, value) - is String -> settings.putString(key, value) - is Int -> settings.putInt(key, value) - is Boolean -> settings.putBoolean(key, value) - is Float -> settings.putFloat(key, value) - else -> throw UnsupportedPersistenceException() - } -} diff --git a/client/core/persistence/src/commonTest/kotlin/com/oztechan/ccc/client/core/persistence/PersistenceTest.kt b/client/core/persistence/src/commonTest/kotlin/com/oztechan/ccc/client/core/persistence/PersistenceTest.kt deleted file mode 100644 index 0c7ca6ec93..0000000000 --- a/client/core/persistence/src/commonTest/kotlin/com/oztechan/ccc/client/core/persistence/PersistenceTest.kt +++ /dev/null @@ -1,93 +0,0 @@ -package com.oztechan.ccc.client.core.persistence - -import com.oztechan.ccc.client.core.persistence.error.UnsupportedPersistenceException -import com.oztechan.ccc.client.core.persistence.fakes.Fakes.KEY -import com.oztechan.ccc.client.core.persistence.fakes.Fakes.mockBoolean -import com.oztechan.ccc.client.core.persistence.fakes.Fakes.mockFloat -import com.oztechan.ccc.client.core.persistence.fakes.Fakes.mockInt -import com.oztechan.ccc.client.core.persistence.fakes.Fakes.mockLong -import com.oztechan.ccc.client.core.persistence.fakes.Fakes.mockString -import com.russhwolf.settings.Settings -import io.mockative.Mock -import io.mockative.classOf -import io.mockative.configure -import io.mockative.every -import io.mockative.mock -import io.mockative.verify -import kotlin.test.Test -import kotlin.test.assertEquals -import kotlin.test.assertFailsWith - -internal class PersistenceTest { - private val persistence: Persistence by lazy { - PersistenceImpl(settings) - } - - @Mock - private val settings = configure(mock(classOf())) { - stubsUnitByDefault = true - } - - @Test - fun `getValue returns the same type`() { - every { settings.getFloat(KEY, mockFloat) } - .returns(mockFloat) - every { settings.getBoolean(KEY, mockBoolean) } - .returns(mockBoolean) - every { settings.getInt(KEY, mockInt) } - .returns(mockInt) - every { settings.getString(KEY, mockString) } - .returns(mockString) - every { settings.getLong(KEY, mockLong) } - .returns(mockLong) - - assertEquals(mockFloat, persistence.getValue(KEY, mockFloat)) - assertEquals(mockBoolean, persistence.getValue(KEY, mockBoolean)) - assertEquals(mockInt, persistence.getValue(KEY, mockInt)) - assertEquals(mockString, persistence.getValue(KEY, mockString)) - assertEquals(mockLong, persistence.getValue(KEY, mockLong)) - - verify { settings.getFloat(KEY, mockFloat) } - .wasInvoked() - verify { settings.getBoolean(KEY, mockBoolean) } - .wasInvoked() - verify { settings.getInt(KEY, mockInt) } - .wasInvoked() - verify { settings.getString(KEY, mockString) } - .wasInvoked() - verify { settings.getLong(KEY, mockLong) } - .wasInvoked() - } - - @Test - fun `setValue sets the same type`() { - persistence.setValue(KEY, mockFloat) - persistence.setValue(KEY, mockBoolean) - persistence.setValue(KEY, mockInt) - persistence.setValue(KEY, mockString) - persistence.setValue(KEY, mockLong) - - verify { settings.putFloat(KEY, mockFloat) } - .wasInvoked() - verify { settings.putBoolean(KEY, mockBoolean) } - .wasInvoked() - verify { settings.putInt(KEY, mockInt) } - .wasInvoked() - verify { settings.putString(KEY, mockString) } - .wasInvoked() - verify { settings.putLong(KEY, mockLong) } - .wasInvoked() - } - - @Test - fun `UnsupportedPersistenceException throw when unsupported type tried to saved or read`() { - val mockObject = object {} - - assertFailsWith(UnsupportedPersistenceException::class) { - persistence.setValue(KEY, mockObject) - } - assertFailsWith(UnsupportedPersistenceException::class) { - persistence.getValue(KEY, mockObject) - } - } -} diff --git a/client/core/persistence/src/iosMain/kotlin/com/oztechan/ccc/client/core/persistence/di/ClientCorePersistenceModule.ios.kt b/client/core/persistence/src/iosMain/kotlin/com/oztechan/ccc/client/core/persistence/di/ClientCorePersistenceModule.ios.kt index 8c33b91b86..d5e7ce4790 100644 --- a/client/core/persistence/src/iosMain/kotlin/com/oztechan/ccc/client/core/persistence/di/ClientCorePersistenceModule.ios.kt +++ b/client/core/persistence/src/iosMain/kotlin/com/oztechan/ccc/client/core/persistence/di/ClientCorePersistenceModule.ios.kt @@ -2,34 +2,27 @@ package com.oztechan.ccc.client.core.persistence.di import com.oztechan.ccc.client.core.persistence.FlowPersistence import com.oztechan.ccc.client.core.persistence.FlowPersistenceImpl -import com.oztechan.ccc.client.core.persistence.Persistence -import com.oztechan.ccc.client.core.persistence.PersistenceImpl import com.oztechan.ccc.client.core.persistence.SuspendPersistence import com.oztechan.ccc.client.core.persistence.SuspendPersistenceImpl +import com.oztechan.ccc.common.core.infrastructure.di.DISPATCHER_IO import com.russhwolf.settings.NSUserDefaultsSettings import com.russhwolf.settings.ObservableSettings -import com.russhwolf.settings.Settings -import com.russhwolf.settings.coroutines.FlowSettings -import com.russhwolf.settings.coroutines.SuspendSettings import com.russhwolf.settings.coroutines.toFlowSettings import com.russhwolf.settings.coroutines.toSuspendSettings -import org.koin.core.module.dsl.bind -import org.koin.core.module.dsl.singleOf +import org.koin.core.qualifier.named import org.koin.dsl.module actual val clientCorePersistenceModule = module { single { NSUserDefaultsSettings(get().userDefaults) } - single { get() } - @Suppress("OPT_IN_USAGE") - single { get().toSuspendSettings() } - @Suppress("OPT_IN_USAGE") - single { get().toFlowSettings() } - singleOf(::PersistenceImpl) { bind() } @Suppress("OPT_IN_USAGE") - singleOf(::FlowPersistenceImpl) { bind() } + single { + FlowPersistenceImpl(get().toFlowSettings(get(named(DISPATCHER_IO)))) + } @Suppress("OPT_IN_USAGE") - singleOf(::SuspendPersistenceImpl) { bind() } + single { + SuspendPersistenceImpl(get().toSuspendSettings(get(named(DISPATCHER_IO)))) + } }