From 75f52d99601bd7c96bcdc61122e4a174c2aeea95 Mon Sep 17 00:00:00 2001 From: msasikanth Date: Fri, 15 Sep 2023 17:23:44 +0530 Subject: [PATCH] Fix app crashing when sharing post/feed on an iPad I tried placing the popover anchor to be based on the share icon root offset. But for some reason the offset from onGloballyPositioned is different from the bounds of the viewcontroller view. I am lazy to debug it and resolve it fully, so showing it in bottom center for the screen instead similar to how the sheet is shown on mobile. fixes #96 --- .../rss/reader/feeds/ui/FeedListItem.kt | 4 ++-- .../rss/reader/feeds/ui/ShareIconButton.kt | 17 +++++++++++++++++ .../home/ui/PostOptionShareIconButton.kt | 18 ++++++++++++++++++ 3 files changed, 37 insertions(+), 2 deletions(-) diff --git a/shared/src/commonMain/kotlin/dev/sasikanth/rss/reader/feeds/ui/FeedListItem.kt b/shared/src/commonMain/kotlin/dev/sasikanth/rss/reader/feeds/ui/FeedListItem.kt index 1af52d4f9..5cbd46c48 100644 --- a/shared/src/commonMain/kotlin/dev/sasikanth/rss/reader/feeds/ui/FeedListItem.kt +++ b/shared/src/commonMain/kotlin/dev/sasikanth/rss/reader/feeds/ui/FeedListItem.kt @@ -274,8 +274,8 @@ private fun FeedLabelInput( @Composable internal expect fun ShareIconButton(content: () -> String) @Composable -internal fun ShareIconButtonInternal(onClick: () -> Unit) { - IconButton(onClick = onClick) { +internal fun ShareIconButtonInternal(modifier: Modifier = Modifier, onClick: () -> Unit) { + IconButton(modifier = modifier, onClick = onClick) { Icon( imageVector = TwineIcons.Share, contentDescription = null, diff --git a/shared/src/iosMain/kotlin/dev/sasikanth/rss/reader/feeds/ui/ShareIconButton.kt b/shared/src/iosMain/kotlin/dev/sasikanth/rss/reader/feeds/ui/ShareIconButton.kt index d3800a36b..3a15bb453 100644 --- a/shared/src/iosMain/kotlin/dev/sasikanth/rss/reader/feeds/ui/ShareIconButton.kt +++ b/shared/src/iosMain/kotlin/dev/sasikanth/rss/reader/feeds/ui/ShareIconButton.kt @@ -16,9 +16,17 @@ package dev.sasikanth.rss.reader.feeds.ui import androidx.compose.runtime.Composable +import androidx.compose.runtime.getValue +import androidx.compose.runtime.setValue import androidx.compose.ui.interop.LocalUIViewController +import kotlinx.cinterop.ExperimentalForeignApi +import platform.CoreGraphics.CGRectGetMaxY +import platform.CoreGraphics.CGRectGetMidX +import platform.CoreGraphics.CGRectMake import platform.UIKit.UIActivityViewController +import platform.UIKit.popoverPresentationController +@OptIn(ExperimentalForeignApi::class) @Composable internal actual fun ShareIconButton(content: () -> String) { val viewController = LocalUIViewController.current @@ -26,6 +34,15 @@ internal actual fun ShareIconButton(content: () -> String) { ShareIconButtonInternal { val items = listOf(content()) val activityController = UIActivityViewController(items, null) + activityController.popoverPresentationController?.setSourceView(viewController.view) + activityController.popoverPresentationController?.setSourceRect( + CGRectMake( + x = CGRectGetMidX(viewController.view.bounds), + y = CGRectGetMaxY(viewController.view.bounds), + width = 0.0, + height = 0.0 + ) + ) viewController.presentViewController(activityController, true, null) } } diff --git a/shared/src/iosMain/kotlin/dev/sasikanth/rss/reader/home/ui/PostOptionShareIconButton.kt b/shared/src/iosMain/kotlin/dev/sasikanth/rss/reader/home/ui/PostOptionShareIconButton.kt index 07b9c01b1..6ce4e03e1 100644 --- a/shared/src/iosMain/kotlin/dev/sasikanth/rss/reader/home/ui/PostOptionShareIconButton.kt +++ b/shared/src/iosMain/kotlin/dev/sasikanth/rss/reader/home/ui/PostOptionShareIconButton.kt @@ -16,21 +16,39 @@ package dev.sasikanth.rss.reader.home.ui import androidx.compose.runtime.Composable +import androidx.compose.runtime.getValue +import androidx.compose.runtime.setValue import androidx.compose.ui.interop.LocalUIViewController import dev.sasikanth.rss.reader.resources.icons.Share import dev.sasikanth.rss.reader.resources.icons.TwineIcons import dev.sasikanth.rss.reader.resources.strings.LocalStrings +import kotlinx.cinterop.ExperimentalForeignApi +import platform.CoreGraphics.CGRectGetMaxY +import platform.CoreGraphics.CGRectGetMidX +import platform.CoreGraphics.CGRectMake import platform.UIKit.UIActivityViewController +import platform.UIKit.popoverPresentationController +@OptIn(ExperimentalForeignApi::class) @Composable internal actual fun PostOptionShareIconButton(postLink: String) { val viewController = LocalUIViewController.current + PostOptionIconButton( icon = TwineIcons.Share, contentDescription = LocalStrings.current.share, onClick = { val items = listOf(postLink) val activityController = UIActivityViewController(items, null) + activityController.popoverPresentationController?.setSourceView(viewController.view) + activityController.popoverPresentationController?.setSourceRect( + CGRectMake( + x = CGRectGetMidX(viewController.view.bounds), + y = CGRectGetMaxY(viewController.view.bounds), + width = 0.0, + height = 0.0 + ) + ) viewController.presentViewController(activityController, true, null) } )