Merge pull request #465 from android/caren/custom_chrome_tab

Use Chrome Custom tab for opening articles
caren/test_showing_followed_topics
Caren 2 years ago committed by GitHub
commit 7d42831ee9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -28,6 +28,7 @@ dependencies {
implementation(project(":core:model"))
implementation(project(":core:domain"))
implementation(libs.androidx.browser)
implementation(libs.androidx.core.ktx)
implementation(libs.coil.kt)
implementation(libs.coil.kt.compose)

@ -16,22 +16,26 @@
package com.google.samples.apps.nowinandroid.core.ui
import android.content.Intent
import android.content.Context
import android.net.Uri
import androidx.annotation.ColorInt
import androidx.browser.customtabs.CustomTabColorSchemeParams
import androidx.browser.customtabs.CustomTabsIntent
import androidx.compose.foundation.lazy.LazyListScope
import androidx.compose.foundation.lazy.grid.GridCells
import androidx.compose.foundation.lazy.grid.LazyGridScope
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
import androidx.compose.foundation.lazy.grid.items
import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.graphics.toArgb
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.tooling.preview.Devices
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.core.content.ContextCompat
import com.google.samples.apps.nowinandroid.core.designsystem.theme.NiaTheme
import com.google.samples.apps.nowinandroid.core.domain.model.SaveableNewsResource
import com.google.samples.apps.nowinandroid.core.model.data.previewNewsResources
@ -51,13 +55,13 @@ fun LazyGridScope.newsFeed(
val resourceUrl by remember {
mutableStateOf(Uri.parse(saveableNewsResource.newsResource.url))
}
val launchResourceIntent = Intent(Intent.ACTION_VIEW, resourceUrl)
val context = LocalContext.current
val backgroundColor = MaterialTheme.colorScheme.background.toArgb()
NewsResourceCardExpanded(
newsResource = saveableNewsResource.newsResource,
isBookmarked = saveableNewsResource.isSaved,
onClick = { ContextCompat.startActivity(context, launchResourceIntent, null) },
onClick = { launchCustomChromeTab(context, resourceUrl, backgroundColor) },
onToggleBookmark = {
onNewsResourcesCheckedChanged(
saveableNewsResource.newsResource.id,
@ -70,6 +74,16 @@ fun LazyGridScope.newsFeed(
}
}
fun launchCustomChromeTab(context: Context, uri: Uri, @ColorInt toolbarColor: Int) {
val customTabBarColor = CustomTabColorSchemeParams.Builder()
.setToolbarColor(toolbarColor).build()
val customTabsIntent = CustomTabsIntent.Builder()
.setDefaultColorSchemeParams(customTabBarColor)
.build()
customTabsIntent.launchUrl(context, uri)
}
/**
* A sealed hierarchy describing the state of the feed of news resources.
*/

@ -16,13 +16,13 @@
package com.google.samples.apps.nowinandroid.core.ui
import android.content.Intent
import android.net.Uri
import androidx.compose.foundation.lazy.LazyListScope
import androidx.compose.foundation.lazy.items
import androidx.compose.material3.MaterialTheme
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.toArgb
import androidx.compose.ui.platform.LocalContext
import androidx.core.content.ContextCompat
import com.google.samples.apps.nowinandroid.core.model.data.NewsResource
/**
@ -47,8 +47,8 @@ fun <T> LazyListScope.newsResourceCardItems(
key = { newsResourceMapper(it).id },
itemContent = { item ->
val newsResource = newsResourceMapper(item)
val launchResourceIntent =
Intent(Intent.ACTION_VIEW, Uri.parse(newsResource.url))
val resourceUrl = Uri.parse(newsResource.url)
val backgroundColor = MaterialTheme.colorScheme.background.toArgb()
val context = LocalContext.current
NewsResourceCardExpanded(
@ -57,7 +57,7 @@ fun <T> LazyListScope.newsResourceCardItems(
onToggleBookmark = { onToggleBookmark(item) },
onClick = {
when (onItemClick) {
null -> ContextCompat.startActivity(context, launchResourceIntent, null)
null -> launchCustomChromeTab(context, resourceUrl, backgroundColor)
else -> onItemClick(item)
}
},

@ -4,6 +4,7 @@ androidDesugarJdkLibs = "1.2.0"
androidGradlePlugin = "7.3.1"
androidxActivity = "1.6.1"
androidxAppCompat = "1.5.1"
androidxBrowser = "1.4.0"
androidxComposeBom = "2022.11.00"
androidxComposeCompiler = "1.3.2"
androidxComposeRuntimeTracing = "1.0.0-alpha01"
@ -54,6 +55,7 @@ android-desugarJdkLibs = { group = "com.android.tools", name = "desugar_jdk_libs
androidx-activity-compose = { group = "androidx.activity", name = "activity-compose", version.ref = "androidxActivity" }
androidx-appcompat = { group = "androidx.appcompat", name = "appcompat", version.ref = "androidxAppCompat" }
androidx-benchmark-macro = { group = "androidx.benchmark", name = "benchmark-macro-junit4", version.ref = "androidxMacroBenchmark" }
androidx-browser = { group = "androidx.browser", name = "browser", version.ref = "androidxBrowser" }
androidx-compose-bom = { group = "androidx.compose", name = "compose-bom", version.ref = "androidxComposeBom" }
androidx-compose-foundation = { group = "androidx.compose.foundation", name = "foundation" }
androidx-compose-foundation-layout = { group = "androidx.compose.foundation", name = "foundation-layout" }

Loading…
Cancel
Save