Skip to content

Commit

Permalink
feat(android): add share action
Browse files Browse the repository at this point in the history
  • Loading branch information
msfjarvis committed May 19, 2024
1 parent b56de40 commit b12b5c8
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

- Swiping a post from left to right now offers a share action

## [1.47.0] - 2024-05-14

### Changed
Expand Down
14 changes: 14 additions & 0 deletions android/src/main/kotlin/dev/msfjarvis/claw/android/ui/ext.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package dev.msfjarvis.claw.android.ui

import android.content.Context
import android.content.ContextWrapper
import android.content.Intent
import androidx.activity.ComponentActivity
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
Expand All @@ -29,6 +30,7 @@ fun Context.getActivity(): ComponentActivity? {

@Composable
fun rememberPostActions(
context: Context,
urlLauncher: UrlLauncher,
navController: NavController,
viewModel: ClawViewModel,
Expand Down Expand Up @@ -56,6 +58,18 @@ fun rememberPostActions(
viewModel.toggleSave(post)
}

override fun share(post: UIPost) {
val sendIntent: Intent =
Intent().apply {
action = Intent.ACTION_SEND
putExtra(Intent.EXTRA_TEXT, post.url.ifEmpty { post.commentsUrl })
putExtra(Intent.EXTRA_TITLE, post.title)
type = "text/plain"
}
val shareIntent = Intent.createChooser(sendIntent, null)
context.startActivity(shareIntent)
}

override suspend fun getComments(postId: String): UIPost {
return viewModel.getPostComments(postId)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package dev.msfjarvis.claw.android.ui.lists

import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.filled.Reply
import androidx.compose.material.icons.filled.Share
import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
Expand All @@ -31,7 +32,13 @@ fun LobstersListItem(
background = MaterialTheme.colorScheme.tertiary,
onSwipe = { postActions.viewCommentsPage(item) },
)
SwipeableActionsBox(endActions = listOf(commentsAction)) {
val shareAction =
SwipeAction(
icon = rememberVectorPainter(Icons.Filled.Share),
background = MaterialTheme.colorScheme.tertiary,
onSwipe = { postActions.share(item) },
)
SwipeableActionsBox(startActions = listOf(shareAction), endActions = listOf(commentsAction)) {
LobstersCard(post = item, postActions = postActions, refresh = refresh, modifier = modifier)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -91,16 +91,16 @@ fun LobstersPostsScreen(
modifier: Modifier = Modifier,
viewModel: ClawViewModel = injectedViewModel(),
) {
val context = LocalContext.current
val hottestListState = rememberLazyListState()
val newestListState = rememberLazyListState()
val savedListState = rememberLazyListState()
val navController = rememberNavController()
val coroutineScope = rememberCoroutineScope()
val snackbarHostState = remember { SnackbarHostState() }
val postActions = rememberPostActions(urlLauncher, navController, viewModel)
val postActions = rememberPostActions(context, urlLauncher, navController, viewModel)
val backStackEntry by navController.currentBackStackEntryAsState()
val currentDestination = backStackEntry?.destination?.route
val context = LocalContext.current

val hottestPosts = viewModel.hottestPosts.collectAsLazyPagingItems()
val newestPosts = viewModel.newestPosts.collectAsLazyPagingItems()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.material3.Scaffold
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.navigation.NavType
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
Expand All @@ -35,7 +36,7 @@ fun SearchScreen(
viewModel: ClawViewModel = injectedViewModel(),
) {
val navController = rememberNavController()
val postActions = rememberPostActions(urlLauncher, navController, viewModel)
val postActions = rememberPostActions(LocalContext.current, urlLauncher, navController, viewModel)
val listState = rememberLazyListState()
Scaffold(modifier = modifier) { paddingValues ->
NavHost(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,8 @@ val TEST_POST_ACTIONS =

override fun toggleSave(post: UIPost) {}

override fun share(post: UIPost) {}

override suspend fun getComments(postId: String): UIPost {
return UIPost(
shortId = "ooga",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ interface PostActions {

fun toggleSave(post: UIPost)

fun share(post: UIPost)

suspend fun getComments(postId: String): UIPost

suspend fun getLinkMetadata(url: String): LinkMetadata
Expand Down

0 comments on commit b12b5c8

Please sign in to comment.