Skip to content

Commit

Permalink
🐛 fix remember me
Browse files Browse the repository at this point in the history
  • Loading branch information
theapache64 committed Jan 31, 2022
1 parent 498728f commit ed28035
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}

Expand All @@ -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)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 ❤️"
}
Expand All @@ -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
Expand Down Expand Up @@ -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
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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`
}
}

0 comments on commit ed28035

Please # to comment.