0xf1f1
04/01/2024, 7:32 AMPeter
04/01/2024, 1:38 PM0xf1f1
04/01/2024, 2:24 PMPeter
04/01/2024, 3:07 PMPeter
04/01/2024, 3:16 PM@Singleton
class SessionRepository @Inject constructor() {
private val currentSession: Session? = null
fun set(session: Session) {
currentSession = session
}
fun get(): Session? {
return currentSession
}
}
class LoginViewModel @Inject constructor(
private val repository: SessionRepository
) : ViewModel() {
fun onLogin() {
// ...
repository.set(Session(...))
}
}
class SettingsViewModel @Inject constructor(
private val repository: SessionRepository
) : ViewModel() {
val state = MutableStateFlow<SettingsState>(null)
init {
load()
}
fun load() {
val isLoggedIn = repository.get() != null
state.value = SettingsState(isLoggedIn)
}
fun onLogout() {
// ...
repository.set(null)
}
}