Fix leave request detail refresh navigation
This commit is contained in:
@@ -87,6 +87,21 @@ private fun DriverUiState.withApiError(throwable: Throwable): DriverUiState {
|
||||
)
|
||||
}
|
||||
|
||||
internal fun DriverUiState.withLoadedLeaveRequests(
|
||||
requests: List<DriverLeaveRequestDto>,
|
||||
navigateToList: Boolean,
|
||||
): DriverUiState {
|
||||
val refreshedSelectedRequest = selectedLeaveRequest?.let { selected ->
|
||||
requests.firstOrNull { request -> request.id == selected.id } ?: selected
|
||||
}
|
||||
return copy(
|
||||
screen = if (navigateToList) DriverScreen.LeaveRequests else screen,
|
||||
leaveRequests = requests,
|
||||
selectedLeaveRequest = refreshedSelectedRequest,
|
||||
error = null,
|
||||
)
|
||||
}
|
||||
|
||||
class DriverViewModel(application: Application) : AndroidViewModel(application) {
|
||||
private val appPreferencesStore = AppPreferencesStore(application)
|
||||
private val repository = DriverRepository(application)
|
||||
@@ -346,27 +361,20 @@ class DriverViewModel(application: Application) : AndroidViewModel(application)
|
||||
|
||||
fun openLeaveRequests() {
|
||||
if (!DriverLeaveRequestUiRules.isFeatureVisible(_state.value.leaveRequestsConfig)) return
|
||||
loadLeaveRequests(showLoading = true)
|
||||
loadLeaveRequests(showLoading = true, navigateToList = true)
|
||||
}
|
||||
|
||||
fun refreshLeaveRequests() {
|
||||
if (!DriverLeaveRequestUiRules.isFeatureVisible(_state.value.leaveRequestsConfig)) return
|
||||
loadLeaveRequests(showLoading = false)
|
||||
loadLeaveRequests(showLoading = false, navigateToList = false)
|
||||
}
|
||||
|
||||
private fun loadLeaveRequests(showLoading: Boolean) {
|
||||
private fun loadLeaveRequests(showLoading: Boolean, navigateToList: Boolean) {
|
||||
viewModelScope.launch {
|
||||
_state.update { it.copy(loading = showLoading, refreshing = !showLoading, error = null, feedback = null) }
|
||||
runCatching { repository.leaveRequests() }
|
||||
.onSuccess { requests ->
|
||||
_state.update {
|
||||
it.copy(
|
||||
screen = DriverScreen.LeaveRequests,
|
||||
leaveRequests = requests,
|
||||
selectedLeaveRequest = requests.firstOrNull { request -> request.id == it.selectedLeaveRequest?.id } ?: it.selectedLeaveRequest,
|
||||
error = null,
|
||||
)
|
||||
}
|
||||
_state.update { it.withLoadedLeaveRequests(requests, navigateToList) }
|
||||
}
|
||||
.onFailure { throwable -> _state.update { it.withApiError(throwable) } }
|
||||
_state.update { it.copy(loading = false, refreshing = false) }
|
||||
|
||||
Reference in New Issue
Block a user