From c59637f7ecb9e2b7b8bd0b57f63367b52d6b9985 Mon Sep 17 00:00:00 2001 From: Andrew Date: Mon, 17 Jun 2024 19:45:32 +0930 Subject: [PATCH] Simplify JobCancellationException --- .../newmessage/NewMessageViewModel.kt | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/org/thoughtcrime/securesms/conversation/newmessage/NewMessageViewModel.kt b/app/src/main/java/org/thoughtcrime/securesms/conversation/newmessage/NewMessageViewModel.kt index 8ec9dc1ce2..a8a76db74f 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/conversation/newmessage/NewMessageViewModel.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/conversation/newmessage/NewMessageViewModel.kt @@ -84,18 +84,22 @@ class NewMessageViewModel @Inject constructor( _state.update { it.copy(loading = false) } onPublicKey(onsNameOrPublicKey) } - } catch (e: Exception) { - // JobCancellationException is thrown if we cancel the job - // but it is internal, so this excludes other subclasses of CancellationException - // than TimeoutCancellationException - if (e is TimeoutCancellationException == e is CancellationException) { - _state.update { it.copy(loading = false, error = GetString(e) { it.toMessage() }) } + } catch (e: CancellationException) { + if (e is TimeoutCancellationException) { + // Ignore JobCancellationException, which is called when we cancel the job. + onError(e) } + } catch (e: Exception) { + onError(e) } } } } + private fun onError(e: Exception) { + _state.update { it.copy(loading = false, error = GetString(e) { it.toMessage() }) } + } + private fun onPublicKey(onsNameOrPublicKey: String) { viewModelScope.launch { _event.send(Event.Success(onsNameOrPublicKey)) } }