Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Migrate :core:datastore to KMP #2306

Open
wants to merge 13 commits into
base: kmp-impl
Choose a base branch
from
29 changes: 21 additions & 8 deletions core/datastore/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
* See https://github.com/openMF/android-client/blob/master/LICENSE.md
*/
plugins {
alias(libs.plugins.mifos.android.library)
alias(libs.plugins.mifos.android.library.jacoco)
alias(libs.plugins.mifos.android.hilt)
alias(libs.plugins.mifos.kmp.library)
alias(libs.plugins.kotlin.serialization)

}

android {
Expand All @@ -31,10 +31,23 @@ dependencies {
api(projects.core.common)

api(libs.converter.gson)
implementation(project(":core:common"))
implementation(project(":core:common"))
implementation(project(":core:common"))
implementation(project(":core:common"))
implementation(project(":core:common"))
implementation(project(":core:common"))

// fineract sdk dependencies
api(libs.mifos.android.sdk.arch)

// sdk client
api(libs.fineract.client)
kotlin{
sourceSets{
commonMain.dependencies {
implementation(libs.multiplatform.settings)
implementation(libs.multiplatform.settings.serialization)
implementation(libs.multiplatform.settings.coroutines)
implementation(libs.kotlinx.coroutines.core)
implementation(libs.kotlinx.serialization.core)
// implementation(projects.core.common)
}
}
}
}
97 changes: 97 additions & 0 deletions core/datastore/src/commonMain/kotlin/datastore/PrefManager.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*
* Copyright 2024 Mifos Initiative
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*
* See https://github.com/openMF/android-client/blob/master/LICENSE.md
*/
package datastore

//import android.content.Context
//import android.content.SharedPreferences
//import android.preference.PreferenceManager
//import com.mifos.core.common.BuildConfig
//import com.mifos.core.common.model.user.User
//import com.mifos.core.common.utils.Constants
//import com.mifos.core.common.utils.asServerConfig
//import com.mifos.core.model.ServerConfig
//import dagger.hilt.android.qualifiers.ApplicationContext
//import kotlinx.coroutines.flow.Flow
//import kotlinx.coroutines.flow.flow
//import org.mifos.core.sharedpreference.Key
//import org.mifos.core.sharedpreference.UserPreferences
//import org.openapitools.client.models.PostAuthenticationResponse
//import javax.inject.Inject
//
///**
// * Created by Aditya Gupta on 19/08/23.
// */
//const val USER_DETAILS = "user_details"
//const val AUTH_USERNAME = "auth_username"
//const val AUTH_PASSWORD = "auth_password"
//
//class PrefManager @Inject constructor(
// @ApplicationContext context: Context,
//) : UserPreferences<User>() {
//
// private val serverConfigKey = Key.Custom("SERVER_CONFIG_KEY")
//
// override val preference: SharedPreferences =
// PreferenceManager.getDefaultSharedPreferences(context)
//
// override fun getUser(): User {
// return gson.fromJson(preference.getString(USER_DETAILS, ""), User::class.java)
// }
//
// override fun saveUser(user: User) {
// preference.edit().putString(USER_DETAILS, gson.toJson(user)).apply()
// }
//
// // Created this to store userDetails
// fun savePostAuthenticationResponse(user: PostAuthenticationResponse) {
// preference.edit().putString(USER_DETAILS, gson.toJson(user)).apply()
// }
//
// fun setPermissionDeniedStatus(permissionDeniedStatus: String, status: Boolean) {
// preference.edit().putBoolean(permissionDeniedStatus, status).apply()
// }
//
// fun getPermissionDeniedStatus(permissionDeniedStatus: String): Boolean {
// return preference.getBoolean(permissionDeniedStatus, true)
// }
//
// var userStatus: Boolean
// get() = preference.getBoolean(Constants.SERVICE_STATUS, false)
// set(status) {
// preference.edit().putBoolean(Constants.SERVICE_STATUS, status).apply()
// }
//
// var usernamePassword: Pair<String, String>
// get() = Pair(
// preference.getString(AUTH_USERNAME, "")!!,
// preference.getString(AUTH_PASSWORD, "")!!,
// )
// set(value) {
// preference.edit().putString(AUTH_USERNAME, value.first).apply()
// preference.edit().putString(AUTH_PASSWORD, value.second).apply()
// }
//
// val getServerConfig: com.mifos.core.model.ServerConfig =
// preference.getString(serverConfigKey.value, null)?.let {
// gson.fromJson(it, com.mifos.core.model.ServerConfig::class.java)
// } ?: BuildConfig.DEMO_SERVER_CONFIG.asServerConfig()
//
// fun updateServerConfig(config: com.mifos.core.model.ServerConfig?) {
// this.put(serverConfigKey, config)
// }
//
// fun getStringValue(key: String): Flow<String?> = flow {
// emit(preference.getString(key, ""))
// }
//
// fun setStringValue(key: String, value: String) {
// preference.edit().putString(key, value).apply()
// }
//}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Copyright 2024 Mifos Initiative
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*
* See https://github.com/openMF/mobile-wallet/blob/master/LICENSE.md
*/
@file:OptIn(ExperimentalSerializationApi::class, ExperimentalSettingsApi::class)

package org.mifos.core.datastore

import com.russhwolf.settings.serialization.decodeValue
import com.russhwolf.settings.serialization.decodeValueOrNull
import kotlinx.coroutines.withContext
import com.russhwolf.settings.ExperimentalSettingsApi
import com.russhwolf.settings.Settings
import com.russhwolf.settings.serialization.encodeValue
import datastore.model.UserData
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.serialization.ExperimentalSerializationApi
private const val USER_DATA = "userData"
class UserPreferencesDataSource(
private val settings: Settings,
private val dispatcher: CoroutineDispatcher,
) {
@OptIn(ExperimentalSerializationApi::class, ExperimentalSettingsApi::class)
private val _userInfo = MutableStateFlow(
settings.decodeValue(
key = USER_DATA,
serializer = UserData.serializer(),
defaultValue = settings.decodeValueOrNull(
key = USER_DATA,
serializer = UserData.serializer(),
) ?: UserData.DEFAULT,
),
)
val userInfo = _userInfo
suspend fun updateUserInfo(user: UserData) {
withContext(dispatcher) {
settings.putUserPreference(user)
_userInfo.value = user
}
}
suspend fun clearInfo() {
withContext(dispatcher) {
settings.clear()
}
}
}
@OptIn(ExperimentalSerializationApi::class, ExperimentalSettingsApi::class)
private fun Settings.putUserPreference(user: UserData) {
encodeValue(
key = USER_DATA,
serializer = UserData.serializer(),
value = user,
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Copyright 2024 Mifos Initiative
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*
* See https://github.com/openMF/mobile-wallet/blob/master/LICENSE.md
*/
package org.mifos.core.datastore


import datastore.model.UserData
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.StateFlow
interface UserPreferencesRepository {
val userInfo: Flow<UserData>
suspend fun updateUser(user: UserData): Result<Unit>
suspend fun logOut(): Unit
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright 2024 Mifos Initiative
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*
* See https://github.com/openMF/mobile-wallet/blob/master/LICENSE.md
*/
package org.mifos.core.datastore


import datastore.model.UserData
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.Flow
class UserPreferencesRepositoryImpl(
private val preferenceManager: UserPreferencesDataSource,
private val ioDispatcher: CoroutineDispatcher,
unconfinedDispatcher: CoroutineDispatcher,
) : UserPreferencesRepository {
private val unconfinedScope = CoroutineScope(unconfinedDispatcher)
override val userInfo: Flow<UserData>
get() = preferenceManager.userInfo

override suspend fun updateUser(user: UserData): Result<Unit> {
return try {
val result = preferenceManager.updateUserInfo(user)
Result.success(result)
} catch (e: Exception) {
Result.failure(e)
}
}

override suspend fun logOut() {
preferenceManager.clearInfo()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright 2024 Mifos Initiative
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*
* See https://github.com/openMF/mobile-wallet/blob/master/LICENSE.md
*/
package org.mifos.core.datastore.di

import com.russhwolf.settings.Settings
import org.koin.core.qualifier.named
import org.koin.dsl.module
import org.mifos.core.datastore.UserPreferencesDataSource
import org.mifos.core.datastore.UserPreferencesRepository
import org.mifos.core.datastore.UserPreferencesRepositoryImpl

val PreferencesModule = module {
factory<Settings> { Settings() }
factory {
UserPreferencesDataSource(
settings = get(),
dispatcher = get(named(MifosDispatchers.IO.name)),
)
}
single<UserPreferencesRepository> {
UserPreferencesRepositoryImpl(
preferenceManager = get(),
ioDispatcher = get(named(MifosDispatchers.IO.name)),
unconfinedDispatcher = get(named(MifosDispatchers.Unconfined.name)),
)
}
}
enum class MifosDispatchers {
Default,
IO,
Unconfined,
}
19 changes: 19 additions & 0 deletions core/datastore/src/commonMain/kotlin/datastore/model/User.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package datastore.model

import kotlinx.serialization.Serializable
@Serializable
data class UserData(
val userId: Long,
val userName: String,
val clientId: Long,
val isAuthenticated: Boolean
) {
companion object {
val DEFAULT = UserData(
userId = -1,
userName = "",
clientId = -1,
isAuthenticated = false,
)
}
}
Loading
Loading