From 2e7f52f63a318e406caf10decec73821434933aa Mon Sep 17 00:00:00 2001 From: Gabriel Luong Date: Tue, 19 Oct 2021 01:37:25 -0400 Subject: [PATCH] [fenix] For https://github.com/mozilla-mobile/fenix/issues/22022 - Composify Customize Home Button and update the background color --- .../sessioncontrol/SessionControlAdapter.kt | 9 +- .../CustomizeHomeButtonViewHolder.kt | 87 +++++++++++++++++-- .../res/layout/customize_home_list_item.xml | 29 ------- .../CustomizeHomeButtonViewHolderTest.kt | 40 --------- 4 files changed, 87 insertions(+), 78 deletions(-) delete mode 100644 app/src/main/res/layout/customize_home_list_item.xml delete mode 100644 app/src/test/java/org/mozilla/fenix/home/sessioncontrol/CustomizeHomeButtonViewHolderTest.kt diff --git a/app/src/main/java/org/mozilla/fenix/home/sessioncontrol/SessionControlAdapter.kt b/app/src/main/java/org/mozilla/fenix/home/sessioncontrol/SessionControlAdapter.kt index e5ad649a34..5a00427eef 100644 --- a/app/src/main/java/org/mozilla/fenix/home/sessioncontrol/SessionControlAdapter.kt +++ b/app/src/main/java/org/mozilla/fenix/home/sessioncontrol/SessionControlAdapter.kt @@ -33,7 +33,6 @@ import org.mozilla.fenix.home.sessioncontrol.viewholders.CustomizeHomeButtonView import org.mozilla.fenix.home.sessioncontrol.viewholders.NoCollectionsMessageViewHolder import org.mozilla.fenix.home.sessioncontrol.viewholders.PrivateBrowsingDescriptionViewHolder import org.mozilla.fenix.home.sessioncontrol.viewholders.TabInCollectionViewHolder -import org.mozilla.fenix.home.topsites.TopSitePagerViewHolder import org.mozilla.fenix.home.sessioncontrol.viewholders.onboarding.ExperimentDefaultBrowserCardViewHolder import org.mozilla.fenix.home.sessioncontrol.viewholders.onboarding.OnboardingAutomaticSignInViewHolder import org.mozilla.fenix.home.sessioncontrol.viewholders.onboarding.OnboardingFinishViewHolder @@ -46,6 +45,7 @@ import org.mozilla.fenix.home.sessioncontrol.viewholders.onboarding.OnboardingTo import org.mozilla.fenix.home.sessioncontrol.viewholders.onboarding.OnboardingTrackingProtectionViewHolder import org.mozilla.fenix.home.pocket.PocketStoriesViewHolder import org.mozilla.fenix.home.tips.ButtonTipViewHolder +import org.mozilla.fenix.home.topsites.TopSitePagerViewHolder import mozilla.components.feature.tab.collections.Tab as ComponentTab sealed class AdapterItem(@LayoutRes val viewType: Int) { @@ -231,6 +231,10 @@ class SessionControlAdapter( @SuppressWarnings("ComplexMethod", "LongMethod", "ReturnCount") override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder { when (viewType) { + CustomizeHomeButtonViewHolder.LAYOUT_ID -> return CustomizeHomeButtonViewHolder( + composeView = ComposeView(parent.context), + interactor = interactor + ) PocketStoriesViewHolder.LAYOUT_ID -> return PocketStoriesViewHolder( composeView = ComposeView(parent.context), store = store, @@ -283,7 +287,6 @@ class SessionControlAdapter( view, interactor ) - CustomizeHomeButtonViewHolder.LAYOUT_ID -> CustomizeHomeButtonViewHolder(view, interactor) OnboardingFinishViewHolder.LAYOUT_ID -> OnboardingFinishViewHolder(view, interactor) OnboardingToolbarPositionPickerViewHolder.LAYOUT_ID -> OnboardingToolbarPositionPickerViewHolder( view @@ -303,6 +306,8 @@ class SessionControlAdapter( override fun onViewRecycled(holder: RecyclerView.ViewHolder) { when (holder) { + is CustomizeHomeButtonViewHolder, + is HistoryMetadataGroupViewHolder, is RecentTabViewHolder, is PocketStoriesViewHolder -> { // no op diff --git a/app/src/main/java/org/mozilla/fenix/home/sessioncontrol/viewholders/CustomizeHomeButtonViewHolder.kt b/app/src/main/java/org/mozilla/fenix/home/sessioncontrol/viewholders/CustomizeHomeButtonViewHolder.kt index 3b01a974e7..4f723448cb 100644 --- a/app/src/main/java/org/mozilla/fenix/home/sessioncontrol/viewholders/CustomizeHomeButtonViewHolder.kt +++ b/app/src/main/java/org/mozilla/fenix/home/sessioncontrol/viewholders/CustomizeHomeButtonViewHolder.kt @@ -5,25 +5,98 @@ package org.mozilla.fenix.home.sessioncontrol.viewholders import android.view.View +import androidx.compose.foundation.isSystemInDarkTheme +import androidx.compose.foundation.layout.Column +import androidx.compose.foundation.layout.Spacer +import androidx.compose.foundation.layout.fillMaxWidth +import androidx.compose.foundation.layout.height +import androidx.compose.foundation.layout.padding +import androidx.compose.material.Button +import androidx.compose.material.ButtonDefaults +import androidx.compose.material.ButtonDefaults.outlinedButtonColors +import androidx.compose.material.Text +import androidx.compose.runtime.Composable +import androidx.compose.ui.Modifier +import androidx.compose.ui.platform.ComposeView +import androidx.compose.ui.platform.ViewCompositionStrategy +import androidx.compose.ui.res.stringResource +import androidx.compose.ui.text.font.Font +import androidx.compose.ui.text.font.FontFamily +import androidx.compose.ui.tooling.preview.Preview +import androidx.compose.ui.unit.dp +import androidx.compose.ui.unit.sp import androidx.recyclerview.widget.RecyclerView +import mozilla.components.ui.colors.PhotonColors import org.mozilla.fenix.R -import org.mozilla.fenix.databinding.CustomizeHomeListItemBinding import org.mozilla.fenix.home.sessioncontrol.CustomizeHomeIteractor +import org.mozilla.fenix.theme.FirefoxTheme class CustomizeHomeButtonViewHolder( - view: View, + val composeView: ComposeView, private val interactor: CustomizeHomeIteractor -) : RecyclerView.ViewHolder(view) { +) : RecyclerView.ViewHolder(composeView) { init { - val binding = CustomizeHomeListItemBinding.bind(view) + composeView.setViewCompositionStrategy( + ViewCompositionStrategy.DisposeOnViewTreeLifecycleDestroyed + ) + composeView.setContent { + FirefoxTheme { + Column { + Spacer(modifier = Modifier.height(68.dp)) - binding.customizeHome.setOnClickListener { - interactor.openCustomizeHomePage() + CustomizeHomeButton( + onButtonClick = { interactor.openCustomizeHomePage() } + ) + } + } } } companion object { - const val LAYOUT_ID = R.layout.customize_home_list_item + val LAYOUT_ID = View.generateViewId() } } + +/** + * A "Customize homepage" button. + * + * @param onButtonClick Invoked when the user clicks on the button. + */ +@Composable +fun CustomizeHomeButton( + onButtonClick: () -> Unit +) { + val backgroundColor = when (isSystemInDarkTheme()) { + true -> PhotonColors.DarkGrey50 + false -> PhotonColors.LightGrey40 + } + + Button( + onClick = { onButtonClick() }, + modifier = Modifier + .padding(horizontal = 16.dp) + .fillMaxWidth() + .height(36.dp), + elevation = ButtonDefaults.elevation(defaultElevation = 0.dp, pressedElevation = 0.dp), + colors = outlinedButtonColors( + backgroundColor = backgroundColor, + contentColor = FirefoxTheme.colors.textPrimary + ) + ) { + Text( + text = stringResource(R.string.browser_menu_customize_home_1), + fontSize = 14.sp, + fontFamily = FontFamily(Font(R.font.metropolis_semibold)), + letterSpacing = 0.5.sp, + lineHeight = 16.sp, + maxLines = 1 + ) + } +} + +@Composable +@Preview +fun CustomizeHomeButtonPreview() { + CustomizeHomeButton(onButtonClick = {}) +} diff --git a/app/src/main/res/layout/customize_home_list_item.xml b/app/src/main/res/layout/customize_home_list_item.xml deleted file mode 100644 index b6c8658af0..0000000000 --- a/app/src/main/res/layout/customize_home_list_item.xml +++ /dev/null @@ -1,29 +0,0 @@ - - - - - -