chanjungskim
02/24/2023, 9:26 AM@HiltViewModel
class HomeViewModel @Inject constructor(
private val remoteConfigRepository: RemoteConfigRepository,
private val shopRepository: ShopRepository,
private val preferenceRepository: PreferenceRepository
) : ViewModel() {
private val _remoteConfig = mutableStateOf<RemoteConfig?>(null)
val remoteConfig: MutableState<RemoteConfig?>
get() = _remoteConfig
private val _managerSignInResult = mutableStateOf<ManagerLoginResponseBody?>(null)
val managerSignInResult: MutableState<ManagerLoginResponseBody?>
get() = _managerSignInResult
fun fetchRemoteConfig() = viewModelScope.launch {
val isTestMode = preferenceRepository.isTestMode.firstOrNull() ?: true
remoteConfigRepository.fetchRemoteConfig(isTest = isTestMode)
.onSuccess { remoteConfig ->
// TODO: save RemoteConfigData
_remoteConfig.value = remoteConfig
}.onFailure { exception ->
exception.printStackTrace()
}
}
fun signInAsManager(qrCode: String) = viewModelScope.launch {
shopRepository.signInAsManager(qrCode).onSuccess {
_managerSignInResult.value = it
}
}
fun signInAsManager(mobile: String, password: String) = viewModelScope.launch {
shopRepository.signInAsManager(mobile, password).onSuccess {
_managerSignInResult.value = it
}
}
}
Here's a my viewModel so far.
The manager can signIn with QR Code or id/pw.
To get QR Code, as far as I know, the QR reader receives data as keyboard input, So, Activity needs to detect the data and send it to the HomeScreen(The screen that the manager needs to sign in)
For id/pw, I can call it from Screen and pass id/pw and get the result. So, I think it's fine.
I am following this codelab and I have no idea how to send the API result to the screen because this codelab only introduces List and the public variable doesn't have MutableState wrapper. So, I am not sure if it's correct way or not.