Skip to content

A fully functional Android app built entirely with Kotlin and Jetpack Compose

Open NewsResource URL when Card is tapped

Caren Chang1486a3e50392
diff --git a/core-ui/src/main/java/com/google/samples/apps/nowinandroid/core/ui/NewsResourceCard.kt b/core-ui/src/main/java/com/google/samples/apps/nowinandroid/core/ui/NewsResourceCard.kt
index db0236b59..a616c5419 100644
--- a/core-ui/src/main/java/com/google/samples/apps/nowinandroid/core/ui/NewsResourceCard.kt
+++ b/core-ui/src/main/java/com/google/samples/apps/nowinandroid/core/ui/NewsResourceCard.kt
@@ -17,6 +17,7 @@
package com.google.samples.apps.nowinandroid.core.ui
import androidx.compose.foundation.background
+import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
@@ -63,10 +64,15 @@ import kotlinx.datetime.Instant
fun NewsResourceCardExpanded(
newsResource: NewsResource,
isBookmarked: Boolean,
- onToggleBookmark: () -> Unit
+ onToggleBookmark: () -> Unit,
+ onClick: () -> Unit
) {
Column(
modifier = Modifier.padding(16.dp)
+ .clickable(
+ onClickLabel = stringResource(R.string.card_tap_action),
+ onClick = { onClick() }
+ )
) {
if (!newsResource.headerImageUrl.isNullOrEmpty()) {
Row {
@@ -228,7 +234,7 @@ fun BookmarkButtonBookmarkedPreview() {
fun ExpandedNewsResourcePreview() {
NiaTheme {
Surface {
- NewsResourceCardExpanded(newsResource, true, {})
+ NewsResourceCardExpanded(newsResource, true, {}, {})
}
}
}
diff --git a/core-ui/src/main/res/values/strings.xml b/core-ui/src/main/res/values/strings.xml
index ff7a987f7..b99d88d2c 100644
--- a/core-ui/src/main/res/values/strings.xml
+++ b/core-ui/src/main/res/values/strings.xml
@@ -16,4 +16,6 @@
<resources>
<string name="bookmark">Bookmark</string>
<string name="unbookmark">Unbookmark</string>
+
+ <string name="card_tap_action">Open Resource Link</string>
</resources>
diff --git a/feature-foryou/src/main/java/com/google/samples/apps/nowinandroid/feature/foryou/ForYouScreen.kt b/feature-foryou/src/main/java/com/google/samples/apps/nowinandroid/feature/foryou/ForYouScreen.kt
index 7d680f5f2..eea37b6f0 100644
--- a/feature-foryou/src/main/java/com/google/samples/apps/nowinandroid/feature/foryou/ForYouScreen.kt
+++ b/feature-foryou/src/main/java/com/google/samples/apps/nowinandroid/feature/foryou/ForYouScreen.kt
@@ -16,6 +16,8 @@
package com.google.samples.apps.nowinandroid.feature.foryou
+import android.content.Intent
+import android.net.Uri
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
@@ -34,10 +36,12 @@ import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.key
import androidx.compose.ui.Modifier
+import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.semantics.Role
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
+import androidx.core.content.ContextCompat.startActivity
import androidx.hilt.navigation.compose.hiltViewModel
import com.google.accompanist.flowlayout.FlowRow
import com.google.samples.apps.nowinandroid.core.model.data.FollowableTopic
@@ -91,12 +95,19 @@ fun ForYouScreen(
}
items(uiState.feed) { (newsResource: NewsResource, isBookmarked: Boolean) ->
+ val launchResourceIntent =
+ Intent(Intent.ACTION_VIEW, Uri.parse(newsResource.url))
+ val context = LocalContext.current
+
NewsResourceCardExpanded(
newsResource = newsResource,
isBookmarked = isBookmarked,
onToggleBookmark = {
onNewsResourcesCheckedChanged(newsResource.id, !isBookmarked)
- }
+ },
+ onClick = {
+ startActivity(context, launchResourceIntent, null)
+ },
)
}
}