From e837a6ff4b23a87517226a365e4be4f2f8732361 Mon Sep 17 00:00:00 2001 From: revanthkumarJ Date: Sat, 11 Jan 2025 16:43:59 +0530 Subject: [PATCH] Added Some data classes --- .../core/datastore/PreferencesHelper.kt | 60 ++++++++++++++++--- .../core/datastore/model/AppSettings.kt | 9 +++ .../mifos/mobile/core/datastore/model/User.kt | 6 ++ .../mobile/core/datastore/model/UserData.kt | 7 +++ 4 files changed, 74 insertions(+), 8 deletions(-) create mode 100644 core/datastore/src/commonMain/kotlin/org/mifos/mobile/core/datastore/model/AppSettings.kt create mode 100644 core/datastore/src/commonMain/kotlin/org/mifos/mobile/core/datastore/model/User.kt create mode 100644 core/datastore/src/commonMain/kotlin/org/mifos/mobile/core/datastore/model/UserData.kt diff --git a/core/datastore/src/commonMain/kotlin/org/mifos/mobile/core/datastore/PreferencesHelper.kt b/core/datastore/src/commonMain/kotlin/org/mifos/mobile/core/datastore/PreferencesHelper.kt index b5b953af7..5d0ffc364 100644 --- a/core/datastore/src/commonMain/kotlin/org/mifos/mobile/core/datastore/PreferencesHelper.kt +++ b/core/datastore/src/commonMain/kotlin/org/mifos/mobile/core/datastore/PreferencesHelper.kt @@ -16,8 +16,11 @@ import com.russhwolf.settings.set import kotlinx.coroutines.channels.awaitClose import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.callbackFlow +import org.mifos.mobile.core.datastore.model.AppSettings import org.mifos.mobile.core.datastore.model.AppTheme import org.mifos.mobile.core.datastore.model.MifosAppLanguage +import org.mifos.mobile.core.datastore.model.User +import org.mifos.mobile.core.datastore.model.UserData class PreferencesHelper(private val settings: Settings) { @@ -96,7 +99,7 @@ class PreferencesHelper(private val settings: Settings) { private val token: String? get() = getString(TOKEN, "") - val isAuthenticated: Boolean + var isAuthenticated: Boolean = false get() = !token.isNullOrEmpty() var userId: Long? @@ -105,7 +108,7 @@ class PreferencesHelper(private val settings: Settings) { putLong(USER_ID, value) } - var username: String? + var userName: String? get() = getString(USER_NAME, "") set(value) { putString(USER_NAME, value) @@ -129,12 +132,6 @@ class PreferencesHelper(private val settings: Settings) { putLong(CLIENT_ID, value) } - var userName: String? - get() = getString(USER_NAME, "") - set(value) { - putString(USER_NAME, value) - } - var clientName: String? get() = getString(CLIENT_NAME, "") set(value) { @@ -201,6 +198,53 @@ class PreferencesHelper(private val settings: Settings) { putBoolean(DEFAULT_SYSTEM_LANGUAGE, value) } + fun getUserData(): UserData { + return UserData( + clientId = clientId, + userName = userName, + isAuthenticated = isAuthenticated + + ) + } + + fun saveUserData(userData: UserData) { + clientId = userData.clientId + userName = userData.userName + isAuthenticated=userData.isAuthenticated + } + + fun getUser(): User { + return User( + userId = userId, + userName = userName, + ) + } + + fun saveUser(user: User) { + userId = user.userId + userName = user.userName + + } + + fun getAppSettings(): AppSettings { + return AppSettings( + tenant = tenant, + baseUrl = baseUrl, + passcode = passcode, + appTheme = appTheme, + language = language + ) + } + + fun saveAppSettings(appSettings: AppSettings) { + putString(TENANT, appSettings.tenant) + putString(BASE_URL, appSettings.baseUrl) + putString(PASSCODE, appSettings.passcode) + putInt(APPLICATION_THEME, appSettings.appTheme) + putString(LANGUAGE_TYPE, appSettings.language) + } + + companion object { private const val USER_ID = "preferences_user_id" private const val TOKEN = "preferences_token" diff --git a/core/datastore/src/commonMain/kotlin/org/mifos/mobile/core/datastore/model/AppSettings.kt b/core/datastore/src/commonMain/kotlin/org/mifos/mobile/core/datastore/model/AppSettings.kt new file mode 100644 index 000000000..27018aab3 --- /dev/null +++ b/core/datastore/src/commonMain/kotlin/org/mifos/mobile/core/datastore/model/AppSettings.kt @@ -0,0 +1,9 @@ +package org.mifos.mobile.core.datastore.model + +data class AppSettings( + val tenant: String?, + val baseUrl: String?, + val passcode: String?, + val appTheme: Int?, + val language: String?, +) diff --git a/core/datastore/src/commonMain/kotlin/org/mifos/mobile/core/datastore/model/User.kt b/core/datastore/src/commonMain/kotlin/org/mifos/mobile/core/datastore/model/User.kt new file mode 100644 index 000000000..30b179cd7 --- /dev/null +++ b/core/datastore/src/commonMain/kotlin/org/mifos/mobile/core/datastore/model/User.kt @@ -0,0 +1,6 @@ +package org.mifos.mobile.core.datastore.model + +data class User( + val userName: String?, + val userId: Long?, +) diff --git a/core/datastore/src/commonMain/kotlin/org/mifos/mobile/core/datastore/model/UserData.kt b/core/datastore/src/commonMain/kotlin/org/mifos/mobile/core/datastore/model/UserData.kt new file mode 100644 index 000000000..025cc26ff --- /dev/null +++ b/core/datastore/src/commonMain/kotlin/org/mifos/mobile/core/datastore/model/UserData.kt @@ -0,0 +1,7 @@ +package org.mifos.mobile.core.datastore.model + +data class UserData( + val isAuthenticated: Boolean, + val userName: String?, + val clientId: Long?, +) \ No newline at end of file