From 15def4acb8ea1fd364836ae693475b3a0919f262 Mon Sep 17 00:00:00 2001 From: Norbel AMBANUMBEN Date: Mon, 11 Nov 2024 23:24:58 +0100 Subject: [PATCH] fix: ios email error (#253) --- .../org/ooni/probe/SetupDependencies.kt | 19 +++++++++++++++++-- .../org/ooni/probe/background/RunOperation.kt | 7 +++++-- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/composeApp/src/iosMain/kotlin/org/ooni/probe/SetupDependencies.kt b/composeApp/src/iosMain/kotlin/org/ooni/probe/SetupDependencies.kt index fd9a7f46..4a8dc8b9 100644 --- a/composeApp/src/iosMain/kotlin/org/ooni/probe/SetupDependencies.kt +++ b/composeApp/src/iosMain/kotlin/org/ooni/probe/SetupDependencies.kt @@ -6,7 +6,8 @@ import androidx.datastore.core.DataStore import androidx.datastore.preferences.core.Preferences import app.cash.sqldelight.driver.native.NativeSqliteDriver import co.touchlab.kermit.Logger -import kotlinx.coroutines.GlobalScope +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.flow.MutableSharedFlow import kotlinx.coroutines.flow.first import kotlinx.coroutines.launch @@ -30,6 +31,7 @@ import platform.BackgroundTasks.BGTaskScheduler import platform.Foundation.NSBundle import platform.Foundation.NSDate import platform.Foundation.NSDocumentDirectory +import platform.Foundation.NSError import platform.Foundation.NSFileManager import platform.Foundation.NSLocale import platform.Foundation.NSLocaleLanguageDirectionRightToLeft @@ -41,7 +43,9 @@ import platform.Foundation.NSUserDomainMask import platform.Foundation.characterDirectionForLanguage import platform.Foundation.dateByAddingTimeInterval import platform.Foundation.stringWithContentsOfFile +import platform.MessageUI.MFMailComposeResult import platform.MessageUI.MFMailComposeViewController +import platform.MessageUI.MFMailComposeViewControllerDelegateProtocol import platform.UIKit.UIActivityTypeAirDrop import platform.UIKit.UIActivityTypePostToFacebook import platform.UIKit.UIActivityViewController @@ -155,6 +159,17 @@ class SetupDependencies( val email = it.toString().removePrefix("mailto:") if (canSendMail) { MFMailComposeViewController().apply { + mailComposeDelegate = object : + NSObject(), + MFMailComposeViewControllerDelegateProtocol { + override fun mailComposeController( + controller: MFMailComposeViewController, + didFinishWithResult: MFMailComposeResult, + error: NSError?, + ) { + controller.dismissViewControllerAnimated(true, null) + } + } setToRecipients(listOf(email)) extras?.forEach { (key, value) -> when (key) { @@ -205,7 +220,7 @@ class SetupDependencies( fun scheduleNextAutorun() { val getAutoRunSettings by lazy { dependencies.getAutoRunSettings } - GlobalScope.launch { + CoroutineScope(Dispatchers.Default).launch { configureAutoRun(getAutoRunSettings().first()) } } diff --git a/composeApp/src/iosMain/kotlin/org/ooni/probe/background/RunOperation.kt b/composeApp/src/iosMain/kotlin/org/ooni/probe/background/RunOperation.kt index f89b137e..a9c62fd7 100644 --- a/composeApp/src/iosMain/kotlin/org/ooni/probe/background/RunOperation.kt +++ b/composeApp/src/iosMain/kotlin/org/ooni/probe/background/RunOperation.kt @@ -1,7 +1,9 @@ package org.ooni.probe.background import co.touchlab.kermit.Logger -import kotlinx.coroutines.GlobalScope +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.SupervisorJob import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.collect import kotlinx.coroutines.launch @@ -14,7 +16,8 @@ class RunOperation( ) : NSOperation() { override fun main() { Logger.d { "Running operation" } - GlobalScope.launch { + val coroutineScope = CoroutineScope(SupervisorJob() + Dispatchers.Default) + coroutineScope.launch { runBackgroundTask(spec).collect() }.invokeOnCompletion { Logger.d { "Operation completed" }