-
Notifications
You must be signed in to change notification settings - Fork 120
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Error Handling for urlConnection #2902
base: master
Are you sure you want to change the base?
Changes from all commits
494f766
e00b968
d41ab05
ff4c85f
96871f2
60d4787
b70eb4a
a16a172
e8555fe
6b8fa5e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,6 +39,7 @@ | |
import kotlinx.coroutines.CoroutineDispatcher | ||
import kotlinx.coroutines.flow.MutableSharedFlow | ||
import kotlinx.coroutines.flow.asSharedFlow | ||
import kotlinx.coroutines.flow.catch | ||
import kotlinx.coroutines.launch | ||
|
||
private const val MIN_DOWNLOAD_ZOOM_LEVEL = 9 | ||
|
@@ -77,6 +78,7 @@ | |
val downloadProgress = MutableLiveData(0f) | ||
val bottomText = MutableLiveData<String?>(null) | ||
val downloadButtonEnabled = MutableLiveData(false) | ||
val isFailure = MutableLiveData(false) | ||
|
||
private val _navigate = MutableSharedFlow<UiState>(replay = 0) | ||
val navigate = _navigate.asSharedFlow() | ||
|
@@ -98,20 +100,30 @@ | |
isDownloadProgressVisible.value = true | ||
downloadProgress.value = 0f | ||
viewModelScope.launch(ioDispatcher) { | ||
offlineAreaRepository.downloadTiles(viewport!!).collect { (bytesDownloaded, totalBytes) -> | ||
val progressValue = | ||
if (totalBytes > 0) { | ||
(bytesDownloaded.toFloat() / totalBytes.toFloat()).coerceIn(0f, 1f) | ||
} else { | ||
0f | ||
} | ||
downloadProgress.postValue(progressValue) | ||
} | ||
offlineAreaRepository | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you also add tests for this logic as well? |
||
.downloadTiles(viewport!!) | ||
Check warning on line 104 in ground/src/main/java/com/google/android/ground/ui/offlineareas/selector/OfflineAreaSelectorViewModel.kt Codecov / codecov/patchground/src/main/java/com/google/android/ground/ui/offlineareas/selector/OfflineAreaSelectorViewModel.kt#L103-L104
|
||
.catch { | ||
isFailure.postValue(true) | ||
isDownloadProgressVisible.postValue(false) | ||
} | ||
.collect { (bytesDownloaded, totalBytes) -> | ||
updateDownloadProgress(bytesDownloaded, totalBytes) | ||
} | ||
Check warning on line 111 in ground/src/main/java/com/google/android/ground/ui/offlineareas/selector/OfflineAreaSelectorViewModel.kt Codecov / codecov/patchground/src/main/java/com/google/android/ground/ui/offlineareas/selector/OfflineAreaSelectorViewModel.kt#L106-L111
|
||
isDownloadProgressVisible.postValue(false) | ||
_navigate.emit(UiState.OfflineAreaBackToHomeScreen) | ||
} | ||
} | ||
|
||
private fun updateDownloadProgress(bytesDownloaded: Int, totalBytes: Int) { | ||
val progressValue = | ||
Check warning on line 118 in ground/src/main/java/com/google/android/ground/ui/offlineareas/selector/OfflineAreaSelectorViewModel.kt Codecov / codecov/patchground/src/main/java/com/google/android/ground/ui/offlineareas/selector/OfflineAreaSelectorViewModel.kt#L118
|
||
if (totalBytes > 0) { | ||
(bytesDownloaded.toFloat() / totalBytes.toFloat()).coerceIn(0f, 1f) | ||
Check warning on line 120 in ground/src/main/java/com/google/android/ground/ui/offlineareas/selector/OfflineAreaSelectorViewModel.kt Codecov / codecov/patchground/src/main/java/com/google/android/ground/ui/offlineareas/selector/OfflineAreaSelectorViewModel.kt#L120
|
||
} else { | ||
0f | ||
Check warning on line 122 in ground/src/main/java/com/google/android/ground/ui/offlineareas/selector/OfflineAreaSelectorViewModel.kt Codecov / codecov/patchground/src/main/java/com/google/android/ground/ui/offlineareas/selector/OfflineAreaSelectorViewModel.kt#L122
|
||
} | ||
downloadProgress.postValue(progressValue) | ||
} | ||
Check warning on line 125 in ground/src/main/java/com/google/android/ground/ui/offlineareas/selector/OfflineAreaSelectorViewModel.kt Codecov / codecov/patchground/src/main/java/com/google/android/ground/ui/offlineareas/selector/OfflineAreaSelectorViewModel.kt#L124-L125
|
||
|
||
fun onCancelClick() { | ||
viewModelScope.launch { _navigate.emit(UiState.Up) } | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we add tests for this logic?