v79
11/20/2022, 9:05 AMinit { }
block in a ViewModel called? The init block for Screen A's ViewModel doesn't seem to be triggered when swiping back from Screen B, for instance.yschimke
11/20/2022, 9:23 AMyschimke
11/20/2022, 9:24 AMv79
11/20/2022, 9:53 AMyschimke
11/20/2022, 9:54 AMyschimke
11/20/2022, 9:55 AMv79
11/20/2022, 9:55 AMyschimke
11/20/2022, 9:57 AMyschimke
11/20/2022, 9:59 AMval lifecycleOwner = LocalLifecycleOwner.current
LaunchedEffect(Unit) {
lifecycleOwner.repeatOnLifecycle(Lifecycle.State.RESUMED) {
}
}
yschimke
11/20/2022, 10:00 AMv79
11/20/2022, 11:00 AMv79
11/20/2022, 11:01 AMinit {
refresh()
}
fun refresh() {
// fetches data from Room database, setting up LiveData objects
}
Composable function:
Theme {
val livecycleOwner = LocalLifecycleOwner.current
LaunchedEffect(Unit) {
livecycleOwner.repeatOnLifecycle(Lifecycle.State.RESUMED) {
Log.i("MainMenu comp","Lifecycle resumed, calling refresh")
viewModel.refresh()
}
}
}
There's a wee bit of flicker on the emulator but otherwise working. Thank you.v79
11/20/2022, 11:12 AMv79
11/20/2022, 11:21 AMZun
11/20/2022, 11:35 AMv79
11/20/2022, 11:37 AMZun
11/20/2022, 11:38 AMyschimke
11/20/2022, 11:39 AMyschimke
11/20/2022, 11:39 AMyschimke
11/20/2022, 11:40 AMv79
11/20/2022, 11:40 AMZun
11/20/2022, 11:40 AMv79
11/20/2022, 11:42 AM// Function gets the number of Vehicles in the database
// Then looks to see if there is an activeChargeEvent stored in the SettingsRepository. If there is, it fetches it from the ChargeEventRepository
// The composable function displays a FAB if there are vehicles and there is no charge event
// If there is a charge event, the FAB isn't shown, but the charge event details are
fun refreshView() {
viewModelScope.launch {
_vehicleCount = vehicleRepository.getVehicleCount()
Log.i("ChargeEventViewModel refresh", "_vehicleCount = $_vehicleCount")
_selectedVehicle = settingsRepository.getSetting(SettingsKey.SELECTED_VEHICLE)?.lValue
val activeChargeId =
settingsRepository.getSetting(SettingsKey.CURRENT_CHARGE_EVENT)?.lValue
Log.i("ChargeEventViewModel refresh", "activeChargeId = $activeChargeId")
if (activeChargeId != null) {
_activeChargeEvent = chargeEventRepository.getLiveChargeEventWithId(activeChargeId)
Log.i("ChargeEventViewModel refresh", "_activeChargeEvent = ${_activeChargeEvent.value}")
}
}
}
v79
11/20/2022, 11:51 AMdorche
11/20/2022, 7:02 PMColton Idle
11/21/2022, 3:57 AMColton Idle
11/21/2022, 3:58 AM