From 50d26cb40530c4e4b98014d0274eb0e043487ff4 Mon Sep 17 00:00:00 2001 From: Avently <7953703+avently@users.noreply.github.com> Date: Thu, 15 Feb 2024 22:40:17 +0700 Subject: [PATCH] sharing text in alerts --- .../simplex/common/platform/Share.android.kt | 26 +++++++++++++------ .../simplex/common/platform/UI.android.kt | 3 ++- .../chat/simplex/common/model/SimpleXAPI.kt | 1 + .../views/chat/group/GroupChatInfoView.kt | 2 +- .../common/views/helpers/AlertManager.kt | 14 +++++++++- .../simplex/common/views/helpers/Utils.kt | 1 + .../kotlin/chat/simplex/common/DesktopApp.kt | 3 ++- 7 files changed, 38 insertions(+), 12 deletions(-) diff --git a/apps/multiplatform/common/src/androidMain/kotlin/chat/simplex/common/platform/Share.android.kt b/apps/multiplatform/common/src/androidMain/kotlin/chat/simplex/common/platform/Share.android.kt index f0c5ea6941..dc8ead80ee 100644 --- a/apps/multiplatform/common/src/androidMain/kotlin/chat/simplex/common/platform/Share.android.kt +++ b/apps/multiplatform/common/src/androidMain/kotlin/chat/simplex/common/platform/Share.android.kt @@ -14,17 +14,27 @@ import chat.simplex.common.views.helpers.* import java.io.BufferedOutputStream import java.io.File import chat.simplex.res.MR +import kotlin.math.min actual fun ClipboardManager.shareText(text: String) { - val sendIntent: Intent = Intent().apply { - action = Intent.ACTION_SEND - putExtra(Intent.EXTRA_TEXT, text) - type = "text/plain" - flags = FLAG_ACTIVITY_NEW_TASK + var text = text + for (i in 10 downTo 1) { + try { + val sendIntent: Intent = Intent().apply { + action = Intent.ACTION_SEND + putExtra(Intent.EXTRA_TEXT, text) + type = "text/plain" + flags = FLAG_ACTIVITY_NEW_TASK + } + val shareIntent = Intent.createChooser(sendIntent, null) + shareIntent.addFlags(FLAG_ACTIVITY_NEW_TASK) + androidAppContext.startActivity(shareIntent) + break + } catch (e: Exception) { + Log.e(TAG, "Failed to share text: ${e.stackTraceToString()}") + text = text.substring(0, min(i * 1000, text.length)) + } } - val shareIntent = Intent.createChooser(sendIntent, null) - shareIntent.addFlags(FLAG_ACTIVITY_NEW_TASK) - androidAppContext.startActivity(shareIntent) } actual fun shareFile(text: String, fileSource: CryptoFile) { diff --git a/apps/multiplatform/common/src/androidMain/kotlin/chat/simplex/common/platform/UI.android.kt b/apps/multiplatform/common/src/androidMain/kotlin/chat/simplex/common/platform/UI.android.kt index 1e8fe94bf4..5a4ae01098 100644 --- a/apps/multiplatform/common/src/androidMain/kotlin/chat/simplex/common/platform/UI.android.kt +++ b/apps/multiplatform/common/src/androidMain/kotlin/chat/simplex/common/platform/UI.android.kt @@ -114,7 +114,8 @@ actual class GlobalExceptionsHandler: Thread.UncaughtExceptionHandler { Handler(Looper.getMainLooper()).post { AlertManager.shared.showAlertMsg( title = generalGetString(MR.strings.app_was_crashed), - text = e.stackTraceToString() + text = e.stackTraceToString(), + shareText = true ) } } 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 4025c0c564..00ff9e7a14 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 @@ -461,6 +461,7 @@ object ChatController { AlertManager.shared.showAlertMsg( title = generalGetString(MR.strings.possible_slow_function_title), text = generalGetString(MR.strings.possible_slow_function_desc).format(60, msg.resp.responseType + "\n" + Exception().stackTraceToString()), + shareText = true ) } } diff --git a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chat/group/GroupChatInfoView.kt b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chat/group/GroupChatInfoView.kt index d602d78d89..215e7a880f 100644 --- a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chat/group/GroupChatInfoView.kt +++ b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chat/group/GroupChatInfoView.kt @@ -152,7 +152,7 @@ fun leaveGroupDialog(rhId: Long?, groupInfo: GroupInfo, chatModel: ChatModel, cl text = generalGetString(MR.strings.you_will_stop_receiving_messages_from_this_group_chat_history_will_be_preserved), confirmText = generalGetString(MR.strings.leave_group_button), onConfirm = { - withBGApi { + withLongRunningApi(60_000) { chatModel.controller.leaveGroup(rhId, groupInfo.groupId) close?.invoke() } diff --git a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/helpers/AlertManager.kt b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/helpers/AlertManager.kt index a4cea68ff2..fa72ab2b3c 100644 --- a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/helpers/AlertManager.kt +++ b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/helpers/AlertManager.kt @@ -12,6 +12,7 @@ import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.focus.* import androidx.compose.ui.graphics.Color +import androidx.compose.ui.platform.LocalClipboardManager import androidx.compose.ui.platform.LocalDensity import androidx.compose.ui.text.AnnotatedString import androidx.compose.ui.text.style.TextAlign @@ -22,6 +23,7 @@ import chat.simplex.common.ui.theme.* import chat.simplex.res.MR import dev.icerock.moko.resources.StringResource import dev.icerock.moko.resources.compose.painterResource +import dev.icerock.moko.resources.compose.stringResource import kotlinx.coroutines.delay import kotlinx.coroutines.flow.MutableStateFlow @@ -189,6 +191,7 @@ class AlertManager { title: String, text: String? = null, confirmText: String = generalGetString(MR.strings.ok), hostDevice: Pair? = null, + shareText: Boolean? = null ) { showAlert { AlertDialog( @@ -202,10 +205,19 @@ class AlertManager { delay(200) focusRequester.requestFocus() } + // Can pass shareText = false to prevent showing Share button if it's needed in a specific case + val showShareButton = text != null && (shareText == true || (shareText == null && text.length > 500)) Row( Modifier.fillMaxWidth().padding(horizontal = DEFAULT_PADDING), - horizontalArrangement = Arrangement.Center + horizontalArrangement = if (showShareButton) Arrangement.SpaceBetween else Arrangement.Center ) { + val clipboard = LocalClipboardManager.current + if (showShareButton && text != null) { + TextButton(onClick = { + clipboard.shareText(text) + hideAlert() + }) { Text(stringResource(MR.strings.share_verb)) } + } TextButton( onClick = { hideAlert() diff --git a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/helpers/Utils.kt b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/helpers/Utils.kt index bfde131ed7..cfe58f25c0 100644 --- a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/helpers/Utils.kt +++ b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/helpers/Utils.kt @@ -52,6 +52,7 @@ private suspend fun wrapWithLogging(action: suspend CoroutineScope.() -> Unit, e AlertManager.shared.showAlertMsg( title = generalGetString(MR.strings.possible_slow_function_title), text = generalGetString(MR.strings.possible_slow_function_desc).format((end - start) / 1000, exception.stackTraceToString()), + shareText = true ) } } diff --git a/apps/multiplatform/common/src/desktopMain/kotlin/chat/simplex/common/DesktopApp.kt b/apps/multiplatform/common/src/desktopMain/kotlin/chat/simplex/common/DesktopApp.kt index 41e87b4a13..44073aa990 100644 --- a/apps/multiplatform/common/src/desktopMain/kotlin/chat/simplex/common/DesktopApp.kt +++ b/apps/multiplatform/common/src/desktopMain/kotlin/chat/simplex/common/DesktopApp.kt @@ -39,7 +39,8 @@ fun showApp() { WindowExceptionHandler { e -> AlertManager.shared.showAlertMsg( title = generalGetString(MR.strings.app_was_crashed), - text = e.stackTraceToString() + text = e.stackTraceToString(), + shareText = true ) Log.e(TAG, "App crashed, thread name: " + Thread.currentThread().name + ", exception: " + e.stackTraceToString()) window.dispatchEvent(WindowEvent(window, WindowEvent.WINDOW_CLOSING))