From 7a207fd64177f022ab588cc31f1e829d0c6a1c44 Mon Sep 17 00:00:00 2001 From: Stanislav Dmitrenko <7953703+avently@users.noreply.github.com> Date: Tue, 9 Jan 2024 21:21:29 +0700 Subject: [PATCH] android, desktop: alerts when device was disconnected (#3483) --- .../chat/simplex/common/model/SimpleXAPI.kt | 71 ++++++++++++++++++- .../commonMain/resources/MR/base/strings.xml | 18 +++++ 2 files changed, 86 insertions(+), 3 deletions(-) diff --git a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/model/SimpleXAPI.kt b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/model/SimpleXAPI.kt index df23906b0..700bdc4a5 100644 --- a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/model/SimpleXAPI.kt +++ b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/model/SimpleXAPI.kt @@ -1880,9 +1880,34 @@ object ChatController { val disconnectedHost = chatModel.remoteHosts.firstOrNull { it.remoteHostId == r.remoteHostId_ } chatModel.remoteHostPairing.value = null if (disconnectedHost != null) { - showToast( - generalGetString(MR.strings.remote_host_was_disconnected_toast).format(disconnectedHost.hostDeviceName.ifEmpty { disconnectedHost.remoteHostId.toString() }) - ) + val deviceName = disconnectedHost.hostDeviceName.ifEmpty { disconnectedHost.remoteHostId.toString() } + when (r.rhStopReason) { + is RemoteHostStopReason.ConnectionFailed -> { + AlertManager.shared.showAlertMsg( + generalGetString(MR.strings.remote_host_was_disconnected_title), + if (r.rhStopReason.chatError is ChatError.ChatErrorRemoteHost) { + r.rhStopReason.chatError.remoteHostError.localizedString(deviceName) + } else { + generalGetString(MR.strings.remote_host_disconnected_from).format(deviceName, r.rhStopReason.chatError.string) + } + ) + } + is RemoteHostStopReason.Crashed -> { + AlertManager.shared.showAlertMsg( + generalGetString(MR.strings.remote_host_was_disconnected_title), + if (r.rhStopReason.chatError is ChatError.ChatErrorRemoteHost) { + r.rhStopReason.chatError.remoteHostError.localizedString(deviceName) + } else { + generalGetString(MR.strings.remote_host_disconnected_from).format(deviceName, r.rhStopReason.chatError.string) + } + ) + } + is RemoteHostStopReason.Disconnected -> { + if (r.rhsState is RemoteHostSessionState.Connected || r.rhsState is RemoteHostSessionState.Confirmed) { + showToast(generalGetString(MR.strings.remote_host_was_disconnected_toast).format(deviceName)) + } + } + } } if (chatModel.remoteHostId() == r.remoteHostId_) { chatModel.currentRemoteHost.value = null @@ -1913,6 +1938,27 @@ object ChatController { val sess = chatModel.remoteCtrlSession.value if (sess != null) { chatModel.remoteCtrlSession.value = null + fun showAlert(chatError: ChatError) { + AlertManager.shared.showAlertMsg( + generalGetString(MR.strings.remote_ctrl_was_disconnected_title), + if (chatError is ChatError.ChatErrorRemoteCtrl) { + chatError.remoteCtrlError.localizedString + } else { + generalGetString(MR.strings.remote_ctrl_disconnected_with_reason).format(chatError.string) + } + ) + } + when (r.rcStopReason) { + is RemoteCtrlStopReason.DiscoveryFailed -> showAlert(r.rcStopReason.chatError) + is RemoteCtrlStopReason.ConnectionFailed -> showAlert(r.rcStopReason.chatError) + is RemoteCtrlStopReason.SetupFailed -> showAlert(r.rcStopReason.chatError) + is RemoteCtrlStopReason.Disconnected -> { + /*AlertManager.shared.showAlertMsg( + generalGetString(MR.strings.remote_ctrl_was_disconnected_title), + )*/ + } + } + if (sess.sessionState is UIRemoteCtrlSessionState.Connected) { switchToLocalSession() } @@ -4973,6 +5019,15 @@ sealed class RemoteHostError { is BadVersion -> "badVersion" is Disconnected -> "disconnected" } + fun localizedString(name: String): String = when (this) { + is Missing -> generalGetString(MR.strings.remote_host_error_missing) + is Inactive -> generalGetString(MR.strings.remote_host_error_inactive) + is Busy -> generalGetString(MR.strings.remote_host_error_busy) + is Timeout -> generalGetString(MR.strings.remote_host_error_timeout) + is BadState -> generalGetString(MR.strings.remote_host_error_bad_state) + is BadVersion -> generalGetString(MR.strings.remote_host_error_bad_version) + is Disconnected -> generalGetString(MR.strings.remote_host_error_disconnected) + }.format(name) @Serializable @SerialName("missing") object Missing: RemoteHostError() @Serializable @SerialName("inactive") object Inactive: RemoteHostError() @Serializable @SerialName("busy") object Busy: RemoteHostError() @@ -4993,6 +5048,16 @@ sealed class RemoteCtrlError { is BadInvitation -> "badInvitation" is BadVersion -> "badVersion" } + val localizedString: String get() = when (this) { + is Inactive -> generalGetString(MR.strings.remote_ctrl_error_inactive) + is BadState -> generalGetString(MR.strings.remote_ctrl_error_bad_state) + is Busy -> generalGetString(MR.strings.remote_ctrl_error_busy) + is Timeout -> generalGetString(MR.strings.remote_ctrl_error_timeout) + is Disconnected -> generalGetString(MR.strings.remote_ctrl_error_disconnected) + is BadInvitation -> generalGetString(MR.strings.remote_ctrl_error_bad_invitation) + is BadVersion -> generalGetString(MR.strings.remote_ctrl_error_bad_version) + } + @Serializable @SerialName("inactive") object Inactive: RemoteCtrlError() @Serializable @SerialName("badState") object BadState: RemoteCtrlError() @Serializable @SerialName("busy") object Busy: RemoteCtrlError() diff --git a/apps/multiplatform/common/src/commonMain/resources/MR/base/strings.xml b/apps/multiplatform/common/src/commonMain/resources/MR/base/strings.xml index 2e6928c60..45c8bfe48 100644 --- a/apps/multiplatform/common/src/commonMain/resources/MR/base/strings.xml +++ b/apps/multiplatform/common/src/commonMain/resources/MR/base/strings.xml @@ -1694,6 +1694,10 @@ Disconnect Disconnect mobiles %s was disconnected]]> + Connection stopped + Connection stopped + %s with the reason: %s]]> + Disconnected with the reason: %s Disconnect desktop? Only one device can work at the same time Use from desktop in mobile app and scan QR code.]]> @@ -1728,6 +1732,20 @@ Random Open port in firewall To allow a mobile app to connect to the desktop, open this port in your firewall, if you have it enabled + %s is missing]]> + %s is inactive]]> + %s is busy]]> + %s]]> + %s is in a bad state]]> + %s has an unsupported version. Please, make sure you use the same version on both devices]]> + %s was disconnected]]> + Desktop is inactive + Connection to the desktop is in a bad state + Desktop is busy + Timeout reached while connecting to the desktop + Desktop was disconnected + Desktop has wrong invitation code + Desktop has an unsupported version. Please, make sure you use the same version on both devices Coming soon!