Skip to content

Commit ed28035

Browse files
committed
πŸ› fix remember me
1 parent 498728f commit ed28035

4 files changed

Lines changed: 19 additions & 6 deletions

File tree

β€Ždata/src/main/kotlin/com/theapache64/stackzy/data/repo/AuthRepo.ktβ€Ž

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class AuthRepo @Inject constructor(
2222
companion object {
2323
private const val KEY_ENC_ACCOUNT = "enc_account"
2424
private const val KEY_IS_REMEMBER = "is_remember"
25+
private const val KEY_IS_LOGGED_IN = "is_logged_in"
2526
}
2627

2728
private val accountAdapter by lazy {
@@ -73,7 +74,7 @@ class AuthRepo @Inject constructor(
7374
/**
7475
* To logout and remove account details from preference
7576
*/
76-
fun logout() {
77+
fun clearAccount() {
7778
pref.remove(KEY_ENC_ACCOUNT)
7879
}
7980

@@ -85,5 +86,9 @@ class AuthRepo @Inject constructor(
8586

8687
fun isRemember() = pref.getBoolean(KEY_IS_REMEMBER, false)
8788

89+
fun setLoggedIn(isLoggedIn: Boolean) {
90+
pref.putBoolean(KEY_IS_LOGGED_IN, isLoggedIn)
91+
}
8892

93+
fun isLoggedIn() = pref.getBoolean(KEY_IS_LOGGED_IN, false)
8994
}

β€Žsrc/main/kotlin/com/theapache64/stackzy/ui/feature/login/LogInScreenViewModel.ktβ€Ž

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ class LogInScreenViewModel @Inject constructor(
9898
.onEach {
9999
if (it is Resource.Success) {
100100
authRepo.storeAccount(it.data, isRemember.value)
101+
authRepo.setLoggedIn(true)
101102
onLoggedIn(it.data)
102103
}
103104
}.collect { logInResponse ->

β€Žsrc/main/kotlin/com/theapache64/stackzy/ui/feature/pathway/PathwayViewModel.ktβ€Ž

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,14 @@ import kotlinx.coroutines.flow.StateFlow
88
import kotlinx.coroutines.flow.asStateFlow
99
import javax.inject.Inject
1010

11+
1112
class PathwayViewModel @Inject constructor(
1213
private val authRepo: AuthRepo,
13-
private val configRepo: ConfigRepo
14+
configRepo: ConfigRepo
1415
) {
1516

17+
private val config = configRepo.getLocalConfig()
18+
1619
companion object {
1720
private const val INFO_MADE_WITH_LOVE = "Made with ❀️"
1821
}
@@ -23,10 +26,10 @@ class PathwayViewModel @Inject constructor(
2326
private val _focusedCardInfo = MutableStateFlow(INFO_MADE_WITH_LOVE)
2427
val focusedCardInfo = _focusedCardInfo.asStateFlow()
2528

26-
private val _isBrowseByLibEnabled = MutableStateFlow(configRepo.getLocalConfig()?.isBrowseByLibEnabled ?: false)
29+
private val _isBrowseByLibEnabled = MutableStateFlow(config?.isBrowseByLibEnabled ?: false)
2730
val isBrowseByLibEnabled = _isBrowseByLibEnabled.asStateFlow()
2831

29-
private val _isPlayStoreEnabled = MutableStateFlow(configRepo.getLocalConfig()?.isPlayStoreEnabled ?: false)
32+
private val _isPlayStoreEnabled = MutableStateFlow(config?.isPlayStoreEnabled ?: false)
3033
val isPlayStoreEnabled = _isPlayStoreEnabled.asStateFlow()
3134

3235
private lateinit var onPlayStoreSelected: (Account) -> Unit
@@ -56,7 +59,11 @@ class PathwayViewModel @Inject constructor(
5659
}
5760

5861
fun onLogoutClicked() {
59-
authRepo.logout()
62+
authRepo.setLoggedIn(false)
63+
// If 'RememberMe` is disabled, clear account info as well
64+
if (!authRepo.isRemember()) {
65+
authRepo.clearAccount()
66+
}
6067
_loggedInAccount.value = null
6168
}
6269

β€Žsrc/test/kotlin/com/theapache64/stackzy/data/repo/AuthRepoTest.ktβ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ class AuthRepoTest {
6969
authRepo.storeAccount(dummyAccount, true)
7070
authRepo.getAccount().should.equal(dummyAccount)
7171

72-
authRepo.logout() // test finished, so delete account
72+
authRepo.clearAccount() // test finished, so delete account
7373
authRepo.getAccount().should.`null`
7474
}
7575
}

0 commit comments

Comments
Β (0)