-
Notifications
You must be signed in to change notification settings - Fork 772
feat: Migrated :core:datastore module to KMP
#2737
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
Merged
Merged
Changes from 9 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
84f7ccb
Converted dataStore to KMP
revanthkumarJ af009a1
Update dataStore module for KMP conversion
revanthkumarJ e837a6f
Added Some data classes
revanthkumarJ 9018f28
Added Some data classes
revanthkumarJ b94c9ca
Updated
revanthkumarJ 4bf625c
Updated
revanthkumarJ f4d75b6
Refactored PreferenceHelper
revanthkumarJ c7bc4e4
Refactored PreferenceHelper
revanthkumarJ e321d79
updated
revanthkumarJ f60c935
deleted DataState class
revanthkumarJ 9c61d75
updated
revanthkumarJ 77ace5d
updated
revanthkumarJ 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
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
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
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
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
File renamed without changes.
30 changes: 30 additions & 0 deletions
30
core/datastore/src/commonMain/kotlin/org/mifos/mobile/core/datastore/DataState.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,30 @@ | ||
| /* | ||
| * Copyright 2025 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-mobile/blob/master/LICENSE.md | ||
| */ | ||
| package org.mifos.mobile.core.datastore | ||
|
|
||
| // Should be deleted once common module is migrated | ||
| sealed class DataState<out T> { | ||
| abstract val data: T? | ||
|
|
||
| data object Loading : DataState<Nothing>() { | ||
| override val data: Nothing? get() = null | ||
| } | ||
|
|
||
| data class Success<T>( | ||
| override val data: T, | ||
| ) : DataState<T>() | ||
|
|
||
| data class Error<T>( | ||
| val exception: Throwable, | ||
| override val data: T? = null, | ||
| ) : DataState<T>() { | ||
| val message = exception.message.toString() | ||
| } | ||
| } | ||
106 changes: 106 additions & 0 deletions
106
core/datastore/src/commonMain/kotlin/org/mifos/mobile/core/datastore/PreferenceHelper.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,106 @@ | ||
| /* | ||
| * Copyright 2025 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-mobile/blob/master/LICENSE.md | ||
| */ | ||
| package org.mifos.mobile.core.datastore | ||
|
|
||
| import kotlinx.coroutines.CoroutineDispatcher | ||
| import kotlinx.coroutines.CoroutineScope | ||
| import kotlinx.coroutines.flow.Flow | ||
| import kotlinx.coroutines.flow.SharingStarted | ||
| import kotlinx.coroutines.flow.StateFlow | ||
| import kotlinx.coroutines.flow.stateIn | ||
| import org.mifos.mobile.core.datastore.model.AppSettings | ||
| import org.mifos.mobile.core.datastore.model.AppTheme | ||
| import org.mifos.mobile.core.datastore.model.UserData | ||
|
|
||
| class PreferenceHelper( | ||
revanthkumarJ marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| 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 val settingsInfo: Flow<AppSettings> | ||
| get() = preferenceManager.settingsInfo | ||
|
|
||
| override val appTheme: StateFlow<AppTheme?> | ||
| get() = preferenceManager.appTheme.stateIn( | ||
| scope = unconfinedScope, | ||
| initialValue = null, | ||
| started = SharingStarted.Eagerly, | ||
| ) | ||
| override val token: StateFlow<String?> | ||
| get() = preferenceManager.token.stateIn( | ||
| scope = unconfinedScope, | ||
| initialValue = null, | ||
| started = SharingStarted.Eagerly, | ||
| ) | ||
|
|
||
| override val clientId: StateFlow<Long?> | ||
| get() = preferenceManager.clientId.stateIn( | ||
| scope = unconfinedScope, | ||
| initialValue = null, | ||
| started = SharingStarted.Eagerly, | ||
| ) | ||
|
|
||
| override val profileImage: String? | ||
| get() = preferenceManager.getProfileImage() | ||
|
|
||
| override suspend fun updateToken(token: String): DataState<Unit> { | ||
| return try { | ||
| val result = preferenceManager.updateToken(token) | ||
| DataState.Success(result) | ||
| } catch (e: Exception) { | ||
| DataState.Error(e) | ||
| } | ||
| } | ||
|
|
||
| override suspend fun updateTheme(theme: AppTheme): DataState<Unit> { | ||
| return try { | ||
| val result = preferenceManager.updateTheme(theme) | ||
| DataState.Success(result) | ||
| } catch (e: Exception) { | ||
| DataState.Error(e) | ||
| } | ||
| } | ||
|
|
||
| override suspend fun updateUser(user: UserData): DataState<Unit> { | ||
| return try { | ||
| val result = preferenceManager.updateUserInfo(user) | ||
| DataState.Success(result) | ||
| } catch (e: Exception) { | ||
| DataState.Error(e) | ||
| } | ||
| } | ||
|
|
||
| override suspend fun updateSettings(appSettings: AppSettings): DataState<Unit> { | ||
| return try { | ||
| val result = preferenceManager.updateSettingsInfo(appSettings) | ||
| DataState.Success(result) | ||
| } catch (e: Exception) { | ||
| DataState.Error(e) | ||
| } | ||
| } | ||
|
|
||
| override suspend fun updateProfileImage(image: String): DataState<Unit> { | ||
| return try { | ||
| val result = preferenceManager.updateProfileImage(image) | ||
| DataState.Success(result) | ||
| } catch (e: Exception) { | ||
| DataState.Error(e) | ||
| } | ||
| } | ||
|
|
||
| override suspend fun logOut() { | ||
| preferenceManager.clearInfo() | ||
| } | ||
| } | ||
144 changes: 144 additions & 0 deletions
144
...astore/src/commonMain/kotlin/org/mifos/mobile/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,144 @@ | ||
| /* | ||
| * Copyright 2025 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-mobile/blob/master/LICENSE.md | ||
| */ | ||
| @file:OptIn(ExperimentalSerializationApi::class, ExperimentalSettingsApi::class) | ||
|
|
||
| package org.mifos.mobile.core.datastore | ||
|
|
||
| import com.russhwolf.settings.ExperimentalSettingsApi | ||
| import com.russhwolf.settings.Settings | ||
| import com.russhwolf.settings.serialization.decodeValue | ||
| import com.russhwolf.settings.serialization.decodeValueOrNull | ||
| import com.russhwolf.settings.serialization.encodeValue | ||
| import kotlinx.coroutines.CoroutineDispatcher | ||
| import kotlinx.coroutines.flow.MutableStateFlow | ||
| import kotlinx.coroutines.flow.map | ||
| import kotlinx.coroutines.withContext | ||
| import kotlinx.serialization.ExperimentalSerializationApi | ||
| import org.mifos.mobile.core.datastore.model.AppSettings | ||
| import org.mifos.mobile.core.datastore.model.AppTheme | ||
| import org.mifos.mobile.core.datastore.model.UserData | ||
|
|
||
| private const val USER_DATA = "userData" | ||
| private const val APP_SETTINGS = "appSettings" | ||
|
|
||
| class UserPreferencesDataSource( | ||
| private val settings: Settings, | ||
| private val dispatcher: CoroutineDispatcher, | ||
| ) { | ||
|
|
||
| private val _userInfo = MutableStateFlow( | ||
| settings.decodeValue( | ||
| key = USER_DATA, | ||
| serializer = UserData.serializer(), | ||
| defaultValue = settings.decodeValueOrNull( | ||
| key = USER_DATA, | ||
| serializer = UserData.serializer(), | ||
| ) ?: UserData.DEFAULT, | ||
| ), | ||
| ) | ||
|
|
||
| private val _settingsInfo = MutableStateFlow( | ||
| settings.decodeValue( | ||
| key = APP_SETTINGS, | ||
| serializer = AppSettings.serializer(), | ||
| defaultValue = settings.decodeValueOrNull( | ||
| key = APP_SETTINGS, | ||
| serializer = AppSettings.serializer(), | ||
| ) ?: AppSettings.DEFAULT, | ||
| ), | ||
| ) | ||
|
|
||
| val token = _userInfo.map { | ||
| it.base64EncodedAuthenticationKey | ||
| } | ||
|
|
||
| val userInfo = _userInfo | ||
|
|
||
| val settingsInfo = _settingsInfo | ||
|
|
||
| val clientId = _userInfo.map { it.clientId } | ||
|
|
||
| val appTheme = _settingsInfo.map { it.appTheme } | ||
|
|
||
| suspend fun updateSettingsInfo(appSettings: AppSettings) { | ||
| withContext(dispatcher) { | ||
| settings.putSettingsPreference(appSettings) | ||
| _settingsInfo.value = appSettings | ||
| } | ||
| } | ||
|
|
||
| suspend fun updateUserInfo(user: UserData) { | ||
| withContext(dispatcher) { | ||
| settings.putUserPreference(user) | ||
| _userInfo.value = user | ||
| } | ||
| } | ||
|
|
||
| suspend fun updateToken(token: String) { | ||
| withContext(dispatcher) { | ||
| settings.putUserPreference( | ||
| UserData.DEFAULT.copy( | ||
| base64EncodedAuthenticationKey = token, | ||
| ), | ||
| ) | ||
| _userInfo.value = UserData.DEFAULT.copy( | ||
| base64EncodedAuthenticationKey = token, | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| suspend fun updateTheme(theme: AppTheme) { | ||
| withContext(dispatcher) { | ||
| settings.putSettingsPreference( | ||
| AppSettings.DEFAULT.copy( | ||
| appTheme = theme, | ||
| ), | ||
| ) | ||
| _settingsInfo.value = AppSettings.DEFAULT.copy( | ||
| appTheme = theme, | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| fun updateProfileImage(image: String) { | ||
| settings.putString(PROFILE_IMAGE, image) | ||
| } | ||
|
|
||
| fun getProfileImage(): String? { | ||
| return settings.getString(PROFILE_IMAGE, "").ifEmpty { null } | ||
| } | ||
|
|
||
| suspend fun clearInfo() { | ||
| withContext(dispatcher) { | ||
| settings.clear() | ||
| } | ||
| } | ||
|
|
||
| companion object { | ||
| private const val PROFILE_IMAGE = "preferences_profile_image" | ||
| } | ||
| } | ||
|
|
||
| @OptIn(ExperimentalSerializationApi::class) | ||
| private fun Settings.putUserPreference(user: UserData) { | ||
| encodeValue( | ||
| key = USER_DATA, | ||
| serializer = UserData.serializer(), | ||
| value = user, | ||
| ) | ||
| } | ||
|
|
||
| private fun Settings.putSettingsPreference(settings: AppSettings) { | ||
| encodeValue( | ||
| key = APP_SETTINGS, | ||
| serializer = AppSettings.serializer(), | ||
| value = settings, | ||
| ) | ||
| } |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.