PageViewModel: fix activity state on error

This commit is contained in:
dece 2022-01-09 22:06:09 +01:00
parent ee83d1c8fd
commit f6fc5c2f69
2 changed files with 7 additions and 7 deletions

View file

@ -130,6 +130,7 @@ class MainActivity : AppCompatActivity(), ContentAdapter.ContentAdapterListen {
if (!event.serverDetails.isNullOrEmpty()) if (!event.serverDetails.isNullOrEmpty())
message += "\n\n" + "Server details: ${event.serverDetails}" message += "\n\n" + "Server details: ${event.serverDetails}"
alert(message, title = event.short) alert(message, title = event.short)
updateState(PageViewModel.State.IDLE)
} }
} }
event.handled = true event.handled = true

View file

@ -74,11 +74,11 @@ class PageViewModel : ViewModel() {
Log.i(TAG, "sendRequest: got ${response.code} with meta \"${response.meta}\"") Log.i(TAG, "sendRequest: got ${response.code} with meta \"${response.meta}\"")
when (response.code.getCategory()) { when (response.code.getCategory()) {
Response.Code.Category.SUCCESS -> Response.Code.Category.SUCCESS ->
handleRequestSuccess(response, uri) handleSuccessResponse(response, uri)
Response.Code.Category.REDIRECT -> Response.Code.Category.REDIRECT ->
handleRedirect(response, redirects = redirects + 1) handleRedirectResponse(response, redirects = redirects + 1)
Response.Code.Category.SERVER_ERROR, Response.Code.Category.CLIENT_ERROR -> Response.Code.Category.SERVER_ERROR, Response.Code.Category.CLIENT_ERROR ->
handleError(response) handleErrorResponse(response)
else -> else ->
signalError("Can't handle code ${response.code}.") signalError("Can't handle code ${response.code}.")
} }
@ -87,10 +87,9 @@ class PageViewModel : ViewModel() {
private fun signalError(message: String) { private fun signalError(message: String) {
event.postValue(FailureEvent("Error", message)) event.postValue(FailureEvent("Error", message))
state.postValue(State.IDLE)
} }
private suspend fun handleRequestSuccess(response: Response, uri: Uri) { private suspend fun handleSuccessResponse(response: Response, uri: Uri) {
state.postValue(State.RECEIVING) state.postValue(State.RECEIVING)
linesList.clear() linesList.clear()
@ -135,11 +134,11 @@ class PageViewModel : ViewModel() {
state.postValue(State.IDLE) state.postValue(State.IDLE)
} }
private fun handleRedirect(response: Response, redirects: Int) { private fun handleRedirectResponse(response: Response, redirects: Int) {
event.postValue(RedirectEvent(response.meta, redirects)) event.postValue(RedirectEvent(response.meta, redirects))
} }
private fun handleError(response: Response) { private fun handleErrorResponse(response: Response) {
val briefMessage = when (response.code) { val briefMessage = when (response.code) {
Response.Code.TEMPORARY_FAILURE -> "40 Temporary failure" Response.Code.TEMPORARY_FAILURE -> "40 Temporary failure"
Response.Code.SERVER_UNAVAILABLE -> "41 Server unavailable" Response.Code.SERVER_UNAVAILABLE -> "41 Server unavailable"