Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ipad share #336

Merged
merged 2 commits into from
Dec 9, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 32 additions & 30 deletions composeApp/src/iosMain/kotlin/org/ooni/probe/SetupDependencies.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import org.ooni.probe.shared.PlatformInfo
import platform.BackgroundTasks.BGProcessingTask
import platform.BackgroundTasks.BGProcessingTaskRequest
import platform.BackgroundTasks.BGTaskScheduler
import platform.CoreGraphics.CGRectMake
import platform.Foundation.NSBundle
import platform.Foundation.NSDate
import platform.Foundation.NSDocumentDirectory
Expand All @@ -53,8 +54,12 @@ import platform.UIKit.UIActivityViewController
import platform.UIKit.UIApplication
import platform.UIKit.UIDevice
import platform.UIKit.UIDeviceBatteryState
import platform.UIKit.UIModalPresentationOverCurrentContext
import platform.UIKit.UIPasteboard
import platform.UIKit.UIUserInterfaceIdiomPad
import platform.UIKit.UIViewController
import platform.UIKit.UI_USER_INTERFACE_IDIOM
import platform.UIKit.popoverPresentationController
import platform.darwin.NSObject
import platform.darwin.NSObjectMeta

Expand Down Expand Up @@ -178,12 +183,8 @@ class SetupDependencies(
setToRecipients(listOf(action.to))
setSubject(action.subject)
setMessageBody(action.body, isHTML = false)
}.let {
findCurrentViewController()?.presentViewController(
it,
true,
null,
)
}.let { mailComposer ->
presentViewController(mailComposer)
}
return true
} else {
Expand All @@ -193,6 +194,29 @@ class SetupDependencies(
}
}

private fun presentViewController(uiViewController: UIViewController): Boolean {
return findCurrentViewController()?.let { viewController ->

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
uiViewController.popoverPresentationController?.sourceView = viewController.view

uiViewController.modalPresentationStyle = UIModalPresentationOverCurrentContext
uiViewController.popoverPresentationController?.sourceRect =
CGRectMake(0.0, 0.0, 200.0, 200.0)
}

viewController.presentViewController(
uiViewController,
true,
null,
)
true
} ?: run {
Logger.e { "Cannot find current view controller" }
false
}
}

fun registerTaskHandlers() {
Logger.d { "Registering task handlers" }
BGTaskScheduler.sharedScheduler.registerForTaskWithIdentifier(
Expand Down Expand Up @@ -277,20 +301,8 @@ class SetupDependencies(
activityItems = listOf(share.text),
applicationActivities = null,
)
activityViewController.excludedActivityTypes =
listOf(UIActivityTypeAirDrop, UIActivityTypePostToFacebook)

findCurrentViewController()?.let {
it.presentViewController(
activityViewController,
true,
null,
)
return true
} ?: run {
Logger.e { "Cannot share text: ${share.text}" }
return false
}
return presentViewController(activityViewController)
}

private fun shareFile(share: PlatformAction.FileSharing): Boolean {
Expand All @@ -304,17 +316,7 @@ class SetupDependencies(
activityViewController.excludedActivityTypes =
listOf(UIActivityTypeAirDrop, UIActivityTypePostToFacebook)

findCurrentViewController()?.let {
it.presentViewController(
activityViewController,
true,
null,
)
return true
} ?: run {
Logger.e { "Cannot share file: $filePath" }
return false
}
return presentViewController(activityViewController)
}

private fun openUrl(openUrl: PlatformAction.OpenUrl): Boolean {
Expand Down
Loading