-
Notifications
You must be signed in to change notification settings - Fork 639
feat: Migrate :core:datastore
module to KMP
#2291
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
Closed
Closed
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
5be65aa
datastore-migration-kmpl
kapmaurya 1a1e717
commit message
kapmaurya c307045
datastore-migration-kmpl
kapmaurya 8d09261
datastore-migration-kmpl
kapmaurya 8d330e5
commit message
kapmaurya 8e864ad
datastore-migration-kmpl
kapmaurya e4f4fff
datastore-migration-kmpl
kapmaurya 1c268ca
datastore-migration-kmpl
kapmaurya 84cf597
Merge remote-tracking branch 'android-client/Datastore-migrate-kmpl' …
kapmaurya e7fed67
datastore-migration-kmpl
kapmaurya b1ed2cd
datastore-migration-kmpl
kapmaurya 8cf47c1
datastore-migration-kmpl
kapmaurya c7acb3a
datastore-migration-kmpl
kapmaurya 90e9a3c
datastore-migration-kmpl
kapmaurya File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
---|---|---|
|
@@ -8,9 +8,10 @@ | |
* 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) | ||
//id(libs.plugins.kotlin.parcelize.get().pluginId) | ||
// id("kotlinx-serialization") | ||
alias(libs.plugins.kotlin.serialization) | ||
} | ||
|
||
android { | ||
|
@@ -19,22 +20,20 @@ android { | |
defaultConfig { | ||
consumerProguardFiles("consumer-proguard-rules.pro") | ||
} | ||
testOptions { | ||
unitTests { | ||
isReturnDefaultValues = true | ||
} | ||
} | ||
|
||
} | ||
|
||
dependencies { | ||
api(projects.core.model) | ||
api(projects.core.common) | ||
|
||
api(libs.converter.gson) | ||
|
||
// 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) | ||
} | ||
} | ||
} | ||
} | ||
Comment on lines
26
to
39
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why it's inside the |
This file contains hidden or 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,95 @@ | ||
/* | ||
* 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 com.mifos.core.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: ServerConfig = | ||
// preference.getString(serverConfigKey.value, null)?.let { | ||
// gson.fromJson(it, ServerConfig::class.java) | ||
// } ?: BuildConfig.DEMO_SERVER_CONFIG.asServerConfig() | ||
// | ||
// fun updateServerConfig(config: ServerConfig?) { | ||
// this.put(serverConfigKey, config) | ||
// } | ||
// | ||
|
||
// | ||
// fun setStringValue(key: String, value: String) { | ||
// preference.edit().putString(key, value).apply() | ||
// } | ||
//} |
62 changes: 62 additions & 0 deletions
62
core/datastore/src/commonMain/kotlin/org.mifos.core.datastore/UserPreferencesDataSource.kt
This file contains hidden or 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,62 @@ | ||
///* | ||
// * 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 kotlinx.coroutines.CoroutineDispatcher | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.serialization.ExperimentalSerializationApi | ||
import org.mifos.core.datastore.model.UserData | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Update this class accordingly for this project |
||
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, | ||
) | ||
} |
21 changes: 21 additions & 0 deletions
21
core/datastore/src/commonMain/kotlin/org.mifos.core.datastore/UserPreferencesRepository.kt
This file contains hidden or 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 @@ | ||
/* | ||
* 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 kotlinx.coroutines.flow.Flow | ||
import org.mifos.core.datastore.model.UserData | ||
|
||
|
||
interface UserPreferencesRepository { | ||
val userInfo: Flow<UserData> | ||
|
||
suspend fun updateUser(user: UserData): Result<Unit> | ||
suspend fun logOut(): Unit | ||
} |
37 changes: 37 additions & 0 deletions
37
...datastore/src/commonMain/kotlin/org.mifos.core.datastore/UserPreferencesRepositoryImpl.kt
This file contains hidden or 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,37 @@ | ||
/* | ||
* 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 kotlinx.coroutines.CoroutineDispatcher | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.flow.Flow | ||
import org.mifos.core.datastore.model.UserData | ||
|
||
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() | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
core/datastore/src/commonMain/kotlin/org.mifos.core.datastore/di/PreferenceModule.kt
This file contains hidden or 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,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.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() } | ||
// Use the IO dispatcher name - MifosDispatchers.IO.name | ||
factory { UserPreferencesDataSource(get(), 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
19
core/datastore/src/commonMain/kotlin/org.mifos.core.datastore/model/User.kt
This file contains hidden or 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,19 @@ | ||
package org.mifos.core.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, | ||
) | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove //id(libs.plugins.kotlin.parcelize.get().pluginId)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
okk