From d43ccff93ddc446847319366c2cdafab2e6204dd Mon Sep 17 00:00:00 2001 From: Cameron Porter Date: Sat, 1 Jul 2023 12:48:54 -0500 Subject: [PATCH] Add ability to share posts. --- app/src/main/java/com/jerboa/Utils.kt | 13 ++++++++ .../components/community/CommunityActivity.kt | 4 +++ .../jerboa/ui/components/home/HomeActivity.kt | 4 +++ .../person/PersonProfileActivity.kt | 4 +++ .../jerboa/ui/components/post/PostActivity.kt | 4 +++ .../jerboa/ui/components/post/PostListing.kt | 31 +++++++++++++++++++ .../jerboa/ui/components/post/PostListings.kt | 3 ++ app/src/main/res/values/strings.xml | 2 ++ 8 files changed, 65 insertions(+) diff --git a/app/src/main/java/com/jerboa/Utils.kt b/app/src/main/java/com/jerboa/Utils.kt index b927a24d3..99da2ec24 100644 --- a/app/src/main/java/com/jerboa/Utils.kt +++ b/app/src/main/java/com/jerboa/Utils.kt @@ -361,6 +361,19 @@ fun looksLikeUserUrl(url: String): Pair? { return null } +/** + * Open a sharesheet for the given URL. + */ +fun shareLink(url: String, ctx: Context) { + val intent = Intent().apply { + action = Intent.ACTION_SEND + putExtra(Intent.EXTRA_TEXT, url) + type = "text/plain" + } + val shareIntent = Intent.createChooser(intent, null) + ctx.startActivity(shareIntent) +} + fun openLink(url: String, navController: NavController, useCustomTab: Boolean, usePrivateTab: Boolean) { val parsedUrl = parseUrl(url) ?: return diff --git a/app/src/main/java/com/jerboa/ui/components/community/CommunityActivity.kt b/app/src/main/java/com/jerboa/ui/components/community/CommunityActivity.kt index d5a7fd62c..1427bd498 100644 --- a/app/src/main/java/com/jerboa/ui/components/community/CommunityActivity.kt +++ b/app/src/main/java/com/jerboa/ui/components/community/CommunityActivity.kt @@ -48,6 +48,7 @@ import com.jerboa.db.AccountViewModel import com.jerboa.db.AppSettingsViewModel import com.jerboa.newVote import com.jerboa.scrollToTop +import com.jerboa.shareLink import com.jerboa.ui.components.common.ApiEmptyText import com.jerboa.ui.components.common.ApiErrorText import com.jerboa.ui.components.common.ConsumeReturn @@ -342,6 +343,9 @@ fun CommunityActivity( ) } }, + onShareClick = { url -> + shareLink(url, ctx) + }, isScrolledToEnd = { when (val communityRes = communityViewModel.communityRes) { is ApiState.Success -> { diff --git a/app/src/main/java/com/jerboa/ui/components/home/HomeActivity.kt b/app/src/main/java/com/jerboa/ui/components/home/HomeActivity.kt index 7ecca2960..5955b891c 100644 --- a/app/src/main/java/com/jerboa/ui/components/home/HomeActivity.kt +++ b/app/src/main/java/com/jerboa/ui/components/home/HomeActivity.kt @@ -57,6 +57,7 @@ import com.jerboa.fetchInitialData import com.jerboa.loginFirstToast import com.jerboa.newVote import com.jerboa.scrollToTop +import com.jerboa.shareLink import com.jerboa.ui.components.common.ApiEmptyText import com.jerboa.ui.components.common.ApiErrorText import com.jerboa.ui.components.common.ConsumeReturn @@ -323,6 +324,9 @@ fun MainPostListingsContent( onPersonClick = { personId -> navController.toProfile(id = personId) }, + onShareClick = { url -> + shareLink(url, ctx) + }, isScrolledToEnd = { homeViewModel.nextPage() homeViewModel.appendPosts( diff --git a/app/src/main/java/com/jerboa/ui/components/person/PersonProfileActivity.kt b/app/src/main/java/com/jerboa/ui/components/person/PersonProfileActivity.kt index 46ab30bc5..38e4e916f 100644 --- a/app/src/main/java/com/jerboa/ui/components/person/PersonProfileActivity.kt +++ b/app/src/main/java/com/jerboa/ui/components/person/PersonProfileActivity.kt @@ -49,6 +49,7 @@ import com.jerboa.isScrolledToEnd import com.jerboa.newVote import com.jerboa.pagerTabIndicatorOffset2 import com.jerboa.scrollToTop +import com.jerboa.shareLink import com.jerboa.ui.components.comment.CommentNodes import com.jerboa.ui.components.comment.edit.CommentEditReturn import com.jerboa.ui.components.comment.reply.CommentReplyReturn @@ -482,6 +483,9 @@ fun UserTabs( ) } }, + onShareClick = { url -> + shareLink(url, ctx) + }, isScrolledToEnd = { personProfileViewModel.nextPage() personProfileViewModel.appendData( diff --git a/app/src/main/java/com/jerboa/ui/components/post/PostActivity.kt b/app/src/main/java/com/jerboa/ui/components/post/PostActivity.kt index aadac9987..392819b72 100644 --- a/app/src/main/java/com/jerboa/ui/components/post/PostActivity.kt +++ b/app/src/main/java/com/jerboa/ui/components/post/PostActivity.kt @@ -76,6 +76,7 @@ import com.jerboa.isModerator import com.jerboa.newVote import com.jerboa.scrollToNextParentComment import com.jerboa.scrollToPreviousParentComment +import com.jerboa.shareLink import com.jerboa.ui.components.comment.ShowCommentContextButtons import com.jerboa.ui.components.comment.commentNodeItems import com.jerboa.ui.components.comment.edit.CommentEditReturn @@ -410,6 +411,9 @@ fun PostActivity( ) } }, + onShareClick = { url -> + shareLink(url, ctx) + }, showReply = true, // Do nothing isModerator = isModerator( postView.creator, diff --git a/app/src/main/java/com/jerboa/ui/components/post/PostListing.kt b/app/src/main/java/com/jerboa/ui/components/post/PostListing.kt index 7a3015a71..2d25ed374 100644 --- a/app/src/main/java/com/jerboa/ui/components/post/PostListing.kt +++ b/app/src/main/java/com/jerboa/ui/components/post/PostListing.kt @@ -31,6 +31,7 @@ import androidx.compose.material.icons.outlined.MoreVert import androidx.compose.material.icons.outlined.Person import androidx.compose.material.icons.outlined.PushPin import androidx.compose.material.icons.outlined.Restore +import androidx.compose.material.icons.outlined.Share import androidx.compose.material.icons.outlined.Textsms import androidx.compose.material3.AlertDialog import androidx.compose.material3.Card @@ -509,6 +510,7 @@ fun PostFooterLine( onPersonClick: (personId: Int) -> Unit, onBlockCreatorClick: (person: Person) -> Unit, onBlockCommunityClick: (community: Community) -> Unit, + onShareClick: (url: String) -> Unit, onViewSourceClick: () -> Unit, modifier: Modifier = Modifier, showReply: Boolean = false, @@ -550,6 +552,10 @@ fun PostFooterLine( showMoreOptions = false onBlockCreatorClick(postView.creator) }, + onShareClick = { url -> + showMoreOptions = false + onShareClick(url) + }, onViewSourceClick = { showMoreOptions = false onViewSourceClick() @@ -711,6 +717,7 @@ fun PostFooterLinePreview() { onDeletePostClick = {}, onBlockCreatorClick = {}, onBlockCommunityClick = {}, + onShareClick = {}, onViewSourceClick = {}, enableDownVotes = true, viewSource = false, @@ -736,6 +743,7 @@ fun PreviewPostListingCard() { onPersonClick = {}, onBlockCommunityClick = {}, onBlockCreatorClick = {}, + onShareClick = {}, isModerator = true, fullBody = false, account = null, @@ -766,6 +774,7 @@ fun PreviewLinkPostListing() { onPersonClick = {}, onBlockCommunityClick = {}, onBlockCreatorClick = {}, + onShareClick = {}, isModerator = false, fullBody = false, account = null, @@ -796,6 +805,7 @@ fun PreviewImagePostListingCard() { onPersonClick = {}, onBlockCommunityClick = {}, onBlockCreatorClick = {}, + onShareClick = {}, isModerator = false, fullBody = false, account = null, @@ -826,6 +836,7 @@ fun PreviewImagePostListingSmallCard() { onPersonClick = {}, onBlockCommunityClick = {}, onBlockCreatorClick = {}, + onShareClick = {}, isModerator = false, fullBody = false, account = null, @@ -856,6 +867,7 @@ fun PreviewLinkNoThumbnailPostListing() { onPersonClick = {}, onBlockCommunityClick = {}, onBlockCreatorClick = {}, + onShareClick = {}, isModerator = true, fullBody = false, account = null, @@ -884,6 +896,7 @@ fun PostListing( onPersonClick: (personId: Int) -> Unit, onBlockCommunityClick: (community: Community) -> Unit, onBlockCreatorClick: (person: Person) -> Unit, + onShareClick: (url: String) -> Unit, showReply: Boolean = false, isModerator: Boolean, showCommunityName: Boolean = true, @@ -937,6 +950,7 @@ fun PostListing( onPersonClick = onPersonClick, onBlockCommunityClick = onBlockCommunityClick, onBlockCreatorClick = onBlockCreatorClick, + onShareClick = onShareClick, onViewSourceClick = { viewSource = !viewSource }, @@ -981,6 +995,7 @@ fun PostListing( onPersonClick = onPersonClick, onBlockCommunityClick = onBlockCommunityClick, onBlockCreatorClick = onBlockCreatorClick, + onShareClick = onShareClick, onViewSourceClick = { viewSource = !viewSource }, @@ -1327,6 +1342,7 @@ fun PostListingCard( onPersonClick: (personId: Int) -> Unit, onBlockCommunityClick: (community: Community) -> Unit, onBlockCreatorClick: (person: Person) -> Unit, + onShareClick: (url: String) -> Unit, onViewSourceClick: () -> Unit, viewSource: Boolean, showReply: Boolean = false, @@ -1390,6 +1406,7 @@ fun PostListingCard( onBlockCommunityClick = onBlockCommunityClick, onBlockCreatorClick = onBlockCreatorClick, onViewSourceClick = onViewSourceClick, + onShareClick = onShareClick, showReply = showReply, account = account, modifier = Modifier.padding(horizontal = MEDIUM_PADDING), @@ -1446,6 +1463,7 @@ fun PostOptionsDialog( onReportClick: () -> Unit, onBlockCreatorClick: () -> Unit, onBlockCommunityClick: () -> Unit, + onShareClick: (shareUrl: String) -> Unit, onViewSourceClick: () -> Unit, isCreator: Boolean, viewSource: Boolean, @@ -1569,6 +1587,18 @@ fun PostOptionsDialog( onClick = onViewSourceClick, ) } + postView.post.url?.also { url -> + IconAndTextDrawerItem( + text = stringResource(R.string.post_listing_share_link), + icon = Icons.Outlined.Share, + onClick = { onShareClick(url) }, + ) + } + IconAndTextDrawerItem( + text = stringResource(R.string.post_listing_share_post), + icon = Icons.Outlined.Share, + onClick = { onShareClick(postView.post.ap_id) }, + ) if (!isCreator) { IconAndTextDrawerItem( text = stringResource(R.string.post_listing_report_post), @@ -1626,6 +1656,7 @@ fun PostOptionsDialogPreview() { onEditPostClick = {}, onDeletePostClick = {}, onBlockCommunityClick = {}, + onShareClick = {}, onBlockCreatorClick = {}, onViewSourceClick = {}, viewSource = true, diff --git a/app/src/main/java/com/jerboa/ui/components/post/PostListings.kt b/app/src/main/java/com/jerboa/ui/components/post/PostListings.kt index f22aef448..28d27e33b 100644 --- a/app/src/main/java/com/jerboa/ui/components/post/PostListings.kt +++ b/app/src/main/java/com/jerboa/ui/components/post/PostListings.kt @@ -42,6 +42,7 @@ fun PostListings( onPersonClick: (personId: Int) -> Unit, onBlockCommunityClick: (community: Community) -> Unit, onBlockCreatorClick: (person: Person) -> Unit, + onShareClick: (url: String) -> Unit, isScrolledToEnd: () -> Unit, account: Account?, showCommunityName: Boolean = true, @@ -86,6 +87,7 @@ fun PostListings( onPersonClick = onPersonClick, onBlockCommunityClick = onBlockCommunityClick, onBlockCreatorClick = onBlockCreatorClick, + onShareClick = onShareClick, isModerator = false, showCommunityName = showCommunityName, fullBody = false, @@ -133,6 +135,7 @@ fun PreviewPostListings() { onPersonClick = {}, onBlockCommunityClick = {}, onBlockCreatorClick = {}, + onShareClick = {}, isScrolledToEnd = {}, account = null, listState = rememberLazyListState(), diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index a3dfaf408..8fdde318f 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -262,6 +262,8 @@ Text Copied Report Post Restore + Share Link + Share Post Reply to message "from " Back