-
Notifications
You must be signed in to change notification settings - Fork 715
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4bf625c
commit f4d75b6
Showing
8 changed files
with
344 additions
and
233 deletions.
There are no files selected for viewing
30 changes: 30 additions & 0 deletions
30
core/datastore/src/commonMain/kotlin/org/mifos/mobile/core/datastore/DataState.kt
This file contains 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() | ||
} | ||
} |
105 changes: 105 additions & 0 deletions
105
core/datastore/src/commonMain/kotlin/org/mifos/mobile/core/datastore/PreferenceHelper.kt
This file contains 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,105 @@ | ||
/* | ||
* 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( | ||
private val preferenceManager: UserPreferenceDataSource, | ||
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() | ||
} | ||
} |
220 changes: 0 additions & 220 deletions
220
core/datastore/src/commonMain/kotlin/org/mifos/mobile/core/datastore/PreferencesHelper.kt
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.