From 7c5f0b1f0b02fc280bb06871c5bf6e8f9a28e3d1 Mon Sep 17 00:00:00 2001 From: paulcoding810 <41385034+longnghia@users.noreply.github.com> Date: Fri, 27 Dec 2024 11:42:37 +0700 Subject: [PATCH] Create openInBrowser extension --- .../paulcoding/hviewer/extensions/Activity.kt | 10 ++++++++ .../hviewer/ui/page/post/PostPage.kt | 6 ++--- .../hviewer/ui/page/posts/PostCard.kt | 23 ++++++++++++++----- 3 files changed, 29 insertions(+), 10 deletions(-) create mode 100644 app/src/main/java/com/paulcoding/hviewer/extensions/Activity.kt diff --git a/app/src/main/java/com/paulcoding/hviewer/extensions/Activity.kt b/app/src/main/java/com/paulcoding/hviewer/extensions/Activity.kt new file mode 100644 index 0000000..6d0a116 --- /dev/null +++ b/app/src/main/java/com/paulcoding/hviewer/extensions/Activity.kt @@ -0,0 +1,10 @@ +package com.paulcoding.hviewer.extensions + +import android.app.Activity +import android.content.Intent +import android.net.Uri + +fun Activity.openInBrowser(url: String) { + val intent = Intent(Intent.ACTION_VIEW, Uri.parse(url)) + startActivity(intent) +} \ No newline at end of file diff --git a/app/src/main/java/com/paulcoding/hviewer/ui/page/post/PostPage.kt b/app/src/main/java/com/paulcoding/hviewer/ui/page/post/PostPage.kt index 3272e34..7d4f4ae 100644 --- a/app/src/main/java/com/paulcoding/hviewer/ui/page/post/PostPage.kt +++ b/app/src/main/java/com/paulcoding/hviewer/ui/page/post/PostPage.kt @@ -1,8 +1,6 @@ package com.paulcoding.hviewer.ui.page.post import android.annotation.SuppressLint -import android.content.Intent -import android.net.Uri import android.widget.Toast import androidx.compose.animation.AnimatedVisibility import androidx.compose.foundation.gestures.detectTapGestures @@ -40,6 +38,7 @@ import com.paulcoding.hviewer.MainActivity import com.paulcoding.hviewer.MainApp.Companion.appContext import com.paulcoding.hviewer.extensions.isScrolledToEnd import com.paulcoding.hviewer.extensions.isScrollingUp +import com.paulcoding.hviewer.extensions.openInBrowser import com.paulcoding.hviewer.helper.makeToast import com.paulcoding.hviewer.ui.component.HBackIcon import com.paulcoding.hviewer.ui.component.HGoTop @@ -120,8 +119,7 @@ fun PostPage(appViewModel: AppViewModel, navToWebView: (String) -> Unit, goBack: DropdownMenuItem( onClick = { showMenu.value = false - val intent = Intent(Intent.ACTION_VIEW, Uri.parse(image)) - context.startActivity(intent) + context.openInBrowser(image) }, text = { Text("Open in browser") diff --git a/app/src/main/java/com/paulcoding/hviewer/ui/page/posts/PostCard.kt b/app/src/main/java/com/paulcoding/hviewer/ui/page/posts/PostCard.kt index b583b71..96fbeb3 100644 --- a/app/src/main/java/com/paulcoding/hviewer/ui/page/posts/PostCard.kt +++ b/app/src/main/java/com/paulcoding/hviewer/ui/page/posts/PostCard.kt @@ -28,9 +28,12 @@ import androidx.compose.runtime.setValue import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.Color +import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.text.style.TextDecoration import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.sp +import com.paulcoding.hviewer.MainActivity +import com.paulcoding.hviewer.extensions.openInBrowser import com.paulcoding.hviewer.model.PostItem import com.paulcoding.hviewer.model.Tag import com.paulcoding.hviewer.ui.component.HFavoriteIcon @@ -69,6 +72,7 @@ fun PostCard( ) { var isBottomSheetVisible by remember { mutableStateOf(false) } val bottomSheetState = rememberModalBottomSheetState() + val context = LocalContext.current as MainActivity LaunchedEffect(isBottomSheetVisible) { if (isBottomSheetVisible) { @@ -123,12 +127,19 @@ fun PostCard( SelectionContainer { Text(text = name, fontSize = 20.sp) } - Text( - text = url, - textDecoration = TextDecoration.Underline, - fontSize = 12.sp, - color = Color.Blue - ) + TextButton( + onClick = { + isBottomSheetVisible = false + context.openInBrowser(url) + }, + ) { + Text( + text = url, + textDecoration = TextDecoration.Underline, + fontSize = 12.sp, + color = Color.Blue + ) + } if (size != null) { Text(text = "Size: $size") }