Skip to content
This repository has been archived by the owner on Feb 20, 2023. It is now read-only.

Commit

Permalink
For #8643 and #7606 - illustrations are now connected to their radio …
Browse files Browse the repository at this point in the history
…buttons
  • Loading branch information
BranescuMihai committed May 6, 2020
1 parent cde31cf commit 6f97e75
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,12 @@ class OnboardingThemePickerViewHolder(view: View) : RecyclerView.ViewHolder(view
}

radioLightTheme.addToRadioGroup(radioDarkTheme)
radioDarkTheme.addToRadioGroup(radioLightTheme)

radioLightTheme.addToRadioGroup(radioFollowDeviceTheme)
radioLightTheme.addIllustration(view.theme_light_image)

radioDarkTheme.addToRadioGroup(radioLightTheme)
radioDarkTheme.addToRadioGroup(radioFollowDeviceTheme)
radioDarkTheme.addIllustration(view.theme_dark_image)

radioFollowDeviceTheme.addToRadioGroup(radioDarkTheme)
radioFollowDeviceTheme.addToRadioGroup(radioLightTheme)
Expand All @@ -60,12 +62,10 @@ class OnboardingThemePickerViewHolder(view: View) : RecyclerView.ViewHolder(view
}

radioLightTheme.onClickListener {
setLightIllustrationSelected()
setNewTheme(AppCompatDelegate.MODE_NIGHT_NO)
}

radioDarkTheme.onClickListener {
setDarkIllustrationSelected()
view.context.components.analytics.metrics.track(
Event.DarkThemeSelected(
Event.DarkThemeSelected.Source.ONBOARDING
Expand All @@ -75,7 +75,6 @@ class OnboardingThemePickerViewHolder(view: View) : RecyclerView.ViewHolder(view
}

radioFollowDeviceTheme.onClickListener {
setNoIllustrationSelected()
if (SDK_INT >= Build.VERSION_CODES.P) {
setNewTheme(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM)
} else {
Expand All @@ -84,40 +83,21 @@ class OnboardingThemePickerViewHolder(view: View) : RecyclerView.ViewHolder(view
}

with(view.context.settings()) {
val radio: OnboardingRadioButton
when {
val radio: OnboardingRadioButton = when {
shouldUseLightTheme -> {
radio = radioLightTheme
setLightIllustrationSelected()
radioLightTheme
}
shouldUseDarkTheme -> {
radio = radioDarkTheme
setDarkIllustrationSelected()
radioDarkTheme
}
else -> {
radio = radioFollowDeviceTheme
setNoIllustrationSelected()
radioFollowDeviceTheme
}
}
radio.isChecked = true
radio.updateRadioValue(true)
}
}

private fun setNoIllustrationSelected() {
itemView.theme_dark_image.isSelected = false
itemView.theme_light_image.isSelected = false
}

private fun setDarkIllustrationSelected() {
itemView.theme_dark_image.isSelected = true
itemView.theme_light_image.isSelected = false
}

private fun setLightIllustrationSelected() {
itemView.theme_dark_image.isSelected = false
itemView.theme_light_image.isSelected = true
}

private fun setNewTheme(mode: Int) {
if (AppCompatDelegate.getDefaultNightMode() == mode) return
AppCompatDelegate.setDefaultNightMode(mode)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,19 @@ class OnboardingToolbarPositionPickerViewHolder(view: View) : RecyclerView.ViewH
val radio: OnboardingRadioButton

radioTopToolbar.addToRadioGroup(radioBottomToolbar)
radioTopToolbar.addIllustration(view.toolbar_top_image)

radioBottomToolbar.addToRadioGroup(radioTopToolbar)
radioBottomToolbar.addIllustration(view.toolbar_bottom_image)

if (view.context.settings().shouldUseBottomToolbar) {
radio = radioBottomToolbar
setBottomIllustrationSelected()
radio = if (view.context.settings().shouldUseBottomToolbar) {
radioBottomToolbar
} else {
radio = radioTopToolbar
setTopIllustrationSelected()
radioTopToolbar
}
radio.isChecked = true
radio.updateRadioValue(true)

radioBottomToolbar.onClickListener {
setBottomIllustrationSelected()
itemView.context.asActivity()?.recreate()
}

Expand All @@ -44,7 +44,6 @@ class OnboardingToolbarPositionPickerViewHolder(view: View) : RecyclerView.ViewH
}

radioTopToolbar.onClickListener {
setTopIllustrationSelected()
itemView.context.asActivity()?.recreate()
}

Expand All @@ -53,16 +52,6 @@ class OnboardingToolbarPositionPickerViewHolder(view: View) : RecyclerView.ViewH
}
}

private fun setTopIllustrationSelected() {
itemView.toolbar_top_image.isSelected = true
itemView.toolbar_bottom_image.isSelected = false
}

private fun setBottomIllustrationSelected() {
itemView.toolbar_top_image.isSelected = false
itemView.toolbar_bottom_image.isSelected = true
}

companion object {
const val LAYOUT_ID = R.layout.onboarding_toolbar_position_picker
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package org.mozilla.fenix.onboarding

import android.content.Context
import android.util.AttributeSet
import android.widget.ImageButton
import androidx.appcompat.widget.AppCompatRadioButton
import androidx.core.content.edit
import androidx.core.content.withStyledAttributes
Expand All @@ -14,6 +15,7 @@ import org.mozilla.fenix.ext.settings

class OnboardingRadioButton(context: Context, attrs: AttributeSet) : AppCompatRadioButton(context, attrs) {
private val radioGroups = mutableListOf<OnboardingRadioButton>()
private var illustration: ImageButton? = null
private var clickListener: (() -> Unit)? = null
var key: Int = 0

Expand All @@ -31,6 +33,10 @@ class OnboardingRadioButton(context: Context, attrs: AttributeSet) : AppCompatRa
radioGroups.add(radioButton)
}

fun addIllustration(illustration: ImageButton) {
this.illustration = illustration
}

fun onClickListener(listener: () -> Unit) {
clickListener = listener
}
Expand All @@ -43,8 +49,11 @@ class OnboardingRadioButton(context: Context, attrs: AttributeSet) : AppCompatRa
}
}

private fun updateRadioValue(isChecked: Boolean) {
fun updateRadioValue(isChecked: Boolean) {
this.isChecked = isChecked
illustration?.let {
it.isSelected = isChecked
}
context.settings().preferences.edit {
putBoolean(context.getString(key), isChecked)
}
Expand Down

0 comments on commit 6f97e75

Please sign in to comment.