From ed280353cc732d2f5fcab56a40e1d8cbef57226c Mon Sep 17 00:00:00 2001 From: theapache64 Date: Tue, 1 Feb 2022 01:26:49 +0530 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix=20remember=20me?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/theapache64/stackzy/data/repo/AuthRepo.kt | 7 ++++++- .../ui/feature/login/LogInScreenViewModel.kt | 1 + .../ui/feature/pathway/PathwayViewModel.kt | 15 +++++++++++---- .../theapache64/stackzy/data/repo/AuthRepoTest.kt | 2 +- 4 files changed, 19 insertions(+), 6 deletions(-) diff --git a/data/src/main/kotlin/com/theapache64/stackzy/data/repo/AuthRepo.kt b/data/src/main/kotlin/com/theapache64/stackzy/data/repo/AuthRepo.kt index 6791728..c8ae7b7 100644 --- a/data/src/main/kotlin/com/theapache64/stackzy/data/repo/AuthRepo.kt +++ b/data/src/main/kotlin/com/theapache64/stackzy/data/repo/AuthRepo.kt @@ -22,6 +22,7 @@ class AuthRepo @Inject constructor( companion object { private const val KEY_ENC_ACCOUNT = "enc_account" private const val KEY_IS_REMEMBER = "is_remember" + private const val KEY_IS_LOGGED_IN = "is_logged_in" } private val accountAdapter by lazy { @@ -73,7 +74,7 @@ class AuthRepo @Inject constructor( /** * To logout and remove account details from preference */ - fun logout() { + fun clearAccount() { pref.remove(KEY_ENC_ACCOUNT) } @@ -85,5 +86,9 @@ class AuthRepo @Inject constructor( fun isRemember() = pref.getBoolean(KEY_IS_REMEMBER, false) + fun setLoggedIn(isLoggedIn: Boolean) { + pref.putBoolean(KEY_IS_LOGGED_IN, isLoggedIn) + } + fun isLoggedIn() = pref.getBoolean(KEY_IS_LOGGED_IN, false) } \ No newline at end of file diff --git a/src/main/kotlin/com/theapache64/stackzy/ui/feature/login/LogInScreenViewModel.kt b/src/main/kotlin/com/theapache64/stackzy/ui/feature/login/LogInScreenViewModel.kt index 49c6617..07eb9e8 100644 --- a/src/main/kotlin/com/theapache64/stackzy/ui/feature/login/LogInScreenViewModel.kt +++ b/src/main/kotlin/com/theapache64/stackzy/ui/feature/login/LogInScreenViewModel.kt @@ -98,6 +98,7 @@ class LogInScreenViewModel @Inject constructor( .onEach { if (it is Resource.Success) { authRepo.storeAccount(it.data, isRemember.value) + authRepo.setLoggedIn(true) onLoggedIn(it.data) } }.collect { logInResponse -> diff --git a/src/main/kotlin/com/theapache64/stackzy/ui/feature/pathway/PathwayViewModel.kt b/src/main/kotlin/com/theapache64/stackzy/ui/feature/pathway/PathwayViewModel.kt index d9f2bab..90734e0 100644 --- a/src/main/kotlin/com/theapache64/stackzy/ui/feature/pathway/PathwayViewModel.kt +++ b/src/main/kotlin/com/theapache64/stackzy/ui/feature/pathway/PathwayViewModel.kt @@ -8,11 +8,14 @@ import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.flow.asStateFlow import javax.inject.Inject + class PathwayViewModel @Inject constructor( private val authRepo: AuthRepo, - private val configRepo: ConfigRepo + configRepo: ConfigRepo ) { + private val config = configRepo.getLocalConfig() + companion object { private const val INFO_MADE_WITH_LOVE = "Made with ❤️" } @@ -23,10 +26,10 @@ class PathwayViewModel @Inject constructor( private val _focusedCardInfo = MutableStateFlow(INFO_MADE_WITH_LOVE) val focusedCardInfo = _focusedCardInfo.asStateFlow() - private val _isBrowseByLibEnabled = MutableStateFlow(configRepo.getLocalConfig()?.isBrowseByLibEnabled ?: false) + private val _isBrowseByLibEnabled = MutableStateFlow(config?.isBrowseByLibEnabled ?: false) val isBrowseByLibEnabled = _isBrowseByLibEnabled.asStateFlow() - private val _isPlayStoreEnabled = MutableStateFlow(configRepo.getLocalConfig()?.isPlayStoreEnabled ?: false) + private val _isPlayStoreEnabled = MutableStateFlow(config?.isPlayStoreEnabled ?: false) val isPlayStoreEnabled = _isPlayStoreEnabled.asStateFlow() private lateinit var onPlayStoreSelected: (Account) -> Unit @@ -56,7 +59,11 @@ class PathwayViewModel @Inject constructor( } fun onLogoutClicked() { - authRepo.logout() + authRepo.setLoggedIn(false) + // If 'RememberMe` is disabled, clear account info as well + if (!authRepo.isRemember()) { + authRepo.clearAccount() + } _loggedInAccount.value = null } diff --git a/src/test/kotlin/com/theapache64/stackzy/data/repo/AuthRepoTest.kt b/src/test/kotlin/com/theapache64/stackzy/data/repo/AuthRepoTest.kt index 2696cf0..752c5e3 100644 --- a/src/test/kotlin/com/theapache64/stackzy/data/repo/AuthRepoTest.kt +++ b/src/test/kotlin/com/theapache64/stackzy/data/repo/AuthRepoTest.kt @@ -69,7 +69,7 @@ class AuthRepoTest { authRepo.storeAccount(dummyAccount, true) authRepo.getAccount().should.equal(dummyAccount) - authRepo.logout() // test finished, so delete account + authRepo.clearAccount() // test finished, so delete account authRepo.getAccount().should.`null` } } \ No newline at end of file