For #27457 - Refactor Pocket category colors

fork
Noah Bond 2 years ago committed by mergify[bot]
parent 9a52bbcc3d
commit 0609475ede

@ -4,6 +4,7 @@
package org.mozilla.fenix.home.pocket package org.mozilla.fenix.home.pocket
import android.content.res.Configuration
import android.view.View import android.view.View
import androidx.compose.foundation.isSystemInDarkTheme import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.Column
@ -12,7 +13,6 @@ import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height import androidx.compose.foundation.layout.height
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.ComposeView import androidx.compose.ui.platform.ComposeView
import androidx.compose.ui.res.stringResource import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.tooling.preview.Preview
@ -25,7 +25,6 @@ import org.mozilla.fenix.components.components
import org.mozilla.fenix.compose.ComposeViewHolder import org.mozilla.fenix.compose.ComposeViewHolder
import org.mozilla.fenix.compose.home.HomeSectionHeader import org.mozilla.fenix.compose.home.HomeSectionHeader
import org.mozilla.fenix.theme.FirefoxTheme import org.mozilla.fenix.theme.FirefoxTheme
import org.mozilla.fenix.theme.Theme
import org.mozilla.fenix.wallpapers.WallpaperState import org.mozilla.fenix.wallpapers.WallpaperState
internal const val POCKET_CATEGORIES_SELECTED_AT_A_TIME_COUNT = 8 internal const val POCKET_CATEGORIES_SELECTED_AT_A_TIME_COUNT = 8
@ -61,10 +60,8 @@ class PocketCategoriesViewHolder(
val wallpaperState = components.appStore val wallpaperState = components.appStore
.observeAsComposableState { state -> state.wallpaperState }.value ?: WallpaperState.default .observeAsComposableState { state -> state.wallpaperState }.value ?: WallpaperState.default
var selectedBackgroundColor: Color? = null var (selectedBackgroundColor, unselectedBackgroundColor, selectedTextColor, unselectedTextColor) =
var unselectedBackgroundColor: Color? = null PocketStoriesCategoryColors.buildColors()
var selectedTextColor: Color? = null
var unselectedTextColor: Color? = null
wallpaperState.composeRunIfWallpaperCardColorsAreAvailable { cardColorLight, cardColorDark -> wallpaperState.composeRunIfWallpaperCardColorsAreAvailable { cardColorLight, cardColorDark ->
if (isSystemInDarkTheme()) { if (isSystemInDarkTheme()) {
selectedBackgroundColor = cardColorDark selectedBackgroundColor = cardColorDark
@ -79,16 +76,20 @@ class PocketCategoriesViewHolder(
} }
} }
val categoryColors = PocketStoriesCategoryColors(
selectedTextColor = selectedTextColor,
unselectedTextColor = unselectedTextColor,
selectedBackgroundColor = selectedBackgroundColor,
unselectedBackgroundColor = unselectedBackgroundColor,
)
// See the detailed comment in PocketStoriesViewHolder for reasoning behind this change. // See the detailed comment in PocketStoriesViewHolder for reasoning behind this change.
if (!homeScreenReady) return if (!homeScreenReady) return
Column { Column {
Spacer(Modifier.height(24.dp)) Spacer(Modifier.height(24.dp))
PocketTopics( PocketTopics(
selectedBackgroundColor = selectedBackgroundColor, categoryColors = categoryColors,
unselectedBackgroundColor = unselectedBackgroundColor,
selectedTextColor = selectedTextColor,
unselectedTextColor = unselectedTextColor,
categories = categories ?: emptyList(), categories = categories ?: emptyList(),
categoriesSelections = categoriesSelections ?: emptyList(), categoriesSelections = categoriesSelections ?: emptyList(),
onCategoryClick = interactor::onCategoryClicked, onCategoryClick = interactor::onCategoryClicked,
@ -103,12 +104,9 @@ class PocketCategoriesViewHolder(
@Composable @Composable
private fun PocketTopics( private fun PocketTopics(
selectedTextColor: Color? = null,
unselectedTextColor: Color? = null,
selectedBackgroundColor: Color? = null,
unselectedBackgroundColor: Color? = null,
categories: List<PocketRecommendedStoriesCategory> = emptyList(), categories: List<PocketRecommendedStoriesCategory> = emptyList(),
categoriesSelections: List<PocketRecommendedStoriesSelectedCategory> = emptyList(), categoriesSelections: List<PocketRecommendedStoriesSelectedCategory> = emptyList(),
categoryColors: PocketStoriesCategoryColors = PocketStoriesCategoryColors.buildColors(),
onCategoryClick: (PocketRecommendedStoriesCategory) -> Unit, onCategoryClick: (PocketRecommendedStoriesCategory) -> Unit,
) { ) {
Column { Column {
@ -121,21 +119,18 @@ private fun PocketTopics(
PocketStoriesCategories( PocketStoriesCategories(
categories = categories, categories = categories,
selections = categoriesSelections, selections = categoriesSelections,
selectedTextColor = selectedTextColor, modifier = Modifier.fillMaxWidth(),
unselectedTextColor = unselectedTextColor, categoryColors = categoryColors,
selectedBackgroundColor = selectedBackgroundColor,
unselectedBackgroundColor = unselectedBackgroundColor,
onCategoryClick = onCategoryClick, onCategoryClick = onCategoryClick,
modifier = Modifier
.fillMaxWidth(),
) )
} }
} }
@Composable @Composable
@Preview @Preview(uiMode = Configuration.UI_MODE_NIGHT_YES)
@Preview(uiMode = Configuration.UI_MODE_NIGHT_NO)
private fun PocketCategoriesViewHolderPreview() { private fun PocketCategoriesViewHolderPreview() {
FirefoxTheme(theme = Theme.getTheme()) { FirefoxTheme {
PocketTopics( PocketTopics(
categories = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor" categories = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor"
.split(" ") .split(" ")

@ -419,12 +419,9 @@ private fun Rect.getIntersectPercentage(realSize: IntSize, other: Rect): Float {
* *
* @param categories The categories needed to be displayed. * @param categories The categories needed to be displayed.
* @param selections List of categories currently selected. * @param selections List of categories currently selected.
* @param selectedTextColor Text [Color] when the category is selected.
* @param unselectedTextColor Text [Color] when the category is not selected.
* @param selectedBackgroundColor Background [Color] when the category is selected.
* @param unselectedBackgroundColor Background [Color] when the category is not selected.
* @param onCategoryClick Callback for when the user taps a category.
* @param modifier [Modifier] to be applied to the layout. * @param modifier [Modifier] to be applied to the layout.
* @param categoryColors The color set defined by [PocketStoriesCategoryColors] used to style Pocket categories.
* @param onCategoryClick Callback for when the user taps a category.
*/ */
@OptIn(ExperimentalComposeUiApi::class) @OptIn(ExperimentalComposeUiApi::class)
@Suppress("LongParameterList") @Suppress("LongParameterList")
@ -432,12 +429,9 @@ private fun Rect.getIntersectPercentage(realSize: IntSize, other: Rect): Float {
fun PocketStoriesCategories( fun PocketStoriesCategories(
categories: List<PocketRecommendedStoriesCategory>, categories: List<PocketRecommendedStoriesCategory>,
selections: List<PocketRecommendedStoriesSelectedCategory>, selections: List<PocketRecommendedStoriesSelectedCategory>,
selectedTextColor: Color? = null,
unselectedTextColor: Color? = null,
selectedBackgroundColor: Color? = null,
unselectedBackgroundColor: Color? = null,
onCategoryClick: (PocketRecommendedStoriesCategory) -> Unit,
modifier: Modifier = Modifier, modifier: Modifier = Modifier,
categoryColors: PocketStoriesCategoryColors = PocketStoriesCategoryColors.buildColors(),
onCategoryClick: (PocketRecommendedStoriesCategory) -> Unit,
) { ) {
Box( Box(
modifier = modifier.semantics { modifier = modifier.semantics {
@ -453,10 +447,10 @@ fun PocketStoriesCategories(
SelectableChip( SelectableChip(
text = category.name, text = category.name,
isSelected = selections.map { it.name }.contains(category.name), isSelected = selections.map { it.name }.contains(category.name),
selectedTextColor = selectedTextColor, selectedTextColor = categoryColors.selectedTextColor,
unselectedTextColor = unselectedTextColor, unselectedTextColor = categoryColors.unselectedTextColor,
selectedBackgroundColor = selectedBackgroundColor, selectedBackgroundColor = categoryColors.selectedBackgroundColor,
unselectedBackgroundColor = unselectedBackgroundColor, unselectedBackgroundColor = categoryColors.unselectedBackgroundColor,
) { ) {
onCategoryClick(category) onCategoryClick(category)
} }
@ -465,6 +459,40 @@ fun PocketStoriesCategories(
} }
} }
/**
* Wrapper for the color parameters of [PocketStoriesCategories].
*
* @param selectedTextColor Text [Color] when the category is selected.
* @param unselectedTextColor Text [Color] when the category is not selected.
* @param selectedBackgroundColor Background [Color] when the category is selected.
* @param unselectedBackgroundColor Background [Color] when the category is not selected.
*/
data class PocketStoriesCategoryColors(
val selectedBackgroundColor: Color,
val unselectedBackgroundColor: Color,
val selectedTextColor: Color,
val unselectedTextColor: Color,
) {
companion object {
/**
* Builder function used to construct an instance of [PocketStoriesCategoryColors].
*/
@Composable
fun buildColors(
selectedBackgroundColor: Color = FirefoxTheme.colors.textActionPrimary,
unselectedBackgroundColor: Color = FirefoxTheme.colors.textActionTertiary,
selectedTextColor: Color = FirefoxTheme.colors.actionPrimary,
unselectedTextColor: Color = FirefoxTheme.colors.actionTertiary,
) = PocketStoriesCategoryColors(
selectedTextColor = selectedTextColor,
unselectedTextColor = unselectedTextColor,
selectedBackgroundColor = selectedBackgroundColor,
unselectedBackgroundColor = unselectedBackgroundColor,
)
}
}
/** /**
* Pocket feature section title. * Pocket feature section title.
* Shows a default text about Pocket and offers a external link to learn more. * Shows a default text about Pocket and offers a external link to learn more.

Loading…
Cancel
Save