Simplify JobCancellationException

This commit is contained in:
Andrew 2024-06-17 19:45:32 +09:30
parent af99390643
commit c59637f7ec

View File

@ -84,18 +84,22 @@ class NewMessageViewModel @Inject constructor(
_state.update { it.copy(loading = false) } _state.update { it.copy(loading = false) }
onPublicKey(onsNameOrPublicKey) onPublicKey(onsNameOrPublicKey)
} }
} catch (e: Exception) { } catch (e: CancellationException) {
// JobCancellationException is thrown if we cancel the job if (e is TimeoutCancellationException) {
// but it is internal, so this excludes other subclasses of CancellationException // Ignore JobCancellationException, which is called when we cancel the job.
// than TimeoutCancellationException onError(e)
if (e is TimeoutCancellationException == e is CancellationException) {
_state.update { it.copy(loading = false, error = GetString(e) { it.toMessage() }) }
} }
} 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) { private fun onPublicKey(onsNameOrPublicKey: String) {
viewModelScope.launch { _event.send(Event.Success(onsNameOrPublicKey)) } viewModelScope.launch { _event.send(Event.Success(onsNameOrPublicKey)) }
} }