|
|
|
@ -24,42 +24,36 @@ import androidx.compose.ui.Modifier
|
|
|
|
|
import androidx.compose.ui.graphics.toArgb
|
|
|
|
|
import androidx.compose.ui.platform.LocalContext
|
|
|
|
|
import com.google.samples.apps.nowinandroid.core.domain.model.UserNewsResource
|
|
|
|
|
import com.google.samples.apps.nowinandroid.core.model.data.NewsResource
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Extension function for displaying a [List] of [NewsResourceCardExpanded] backed by a generic
|
|
|
|
|
* [List] [T].
|
|
|
|
|
* Extension function for displaying a [List] of [NewsResourceCardExpanded] backed by a list of
|
|
|
|
|
* [UserNewsResource]s.
|
|
|
|
|
*
|
|
|
|
|
* [newsResourceMapper] maps type [T] to a [NewsResource]
|
|
|
|
|
* [isBookmarkedMapper] maps type [T] to whether the [NewsResource] is bookmarked
|
|
|
|
|
* [onToggleBookmark] defines the action invoked when a user wishes to bookmark an item
|
|
|
|
|
* [onItemClick] optional parameter for action to be performed when the card is clicked. The
|
|
|
|
|
* default action launches an intent matching the card.
|
|
|
|
|
*/
|
|
|
|
|
fun <T> LazyListScope.newsResourceCardItems(
|
|
|
|
|
items: List<T>,
|
|
|
|
|
newsResourceMapper: (item: T) -> UserNewsResource, // TODO remove this?
|
|
|
|
|
isBookmarkedMapper: (item: T) -> Boolean,
|
|
|
|
|
onToggleBookmark: (item: T) -> Unit,
|
|
|
|
|
onItemClick: ((item: T) -> Unit)? = null,
|
|
|
|
|
fun LazyListScope.userNewsResourceCardItems(
|
|
|
|
|
items: List<UserNewsResource>,
|
|
|
|
|
onToggleBookmark: (item: UserNewsResource) -> Unit,
|
|
|
|
|
onItemClick: ((item: UserNewsResource) -> Unit)? = null,
|
|
|
|
|
itemModifier: Modifier = Modifier,
|
|
|
|
|
) = items(
|
|
|
|
|
items = items,
|
|
|
|
|
key = { newsResourceMapper(it).id },
|
|
|
|
|
itemContent = { item ->
|
|
|
|
|
val newsResource = newsResourceMapper(item)
|
|
|
|
|
val resourceUrl = Uri.parse(newsResource.url)
|
|
|
|
|
key = { it.id },
|
|
|
|
|
itemContent = { userNewsResource ->
|
|
|
|
|
val resourceUrl = Uri.parse(userNewsResource.url)
|
|
|
|
|
val backgroundColor = MaterialTheme.colorScheme.background.toArgb()
|
|
|
|
|
val context = LocalContext.current
|
|
|
|
|
|
|
|
|
|
NewsResourceCardExpanded(
|
|
|
|
|
userNewsResource = newsResource,
|
|
|
|
|
isBookmarked = isBookmarkedMapper(item),
|
|
|
|
|
onToggleBookmark = { onToggleBookmark(item) },
|
|
|
|
|
userNewsResource = userNewsResource,
|
|
|
|
|
isBookmarked = userNewsResource.isSaved,
|
|
|
|
|
onToggleBookmark = { onToggleBookmark(userNewsResource) },
|
|
|
|
|
onClick = {
|
|
|
|
|
when (onItemClick) {
|
|
|
|
|
null -> launchCustomChromeTab(context, resourceUrl, backgroundColor)
|
|
|
|
|
else -> onItemClick(item)
|
|
|
|
|
else -> onItemClick(userNewsResource)
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
modifier = itemModifier
|
|
|
|
|