Skip to content
This repository has been archived by the owner on Apr 17, 2021. It is now read-only.

Commit

Permalink
Closes #1482: fixes crash when in non en-us locale
Browse files Browse the repository at this point in the history
  • Loading branch information
severinrudie authored and liuche committed Dec 1, 2018
1 parent 07d006a commit 68d2c28
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import kotlinx.coroutines.Job
import mozilla.components.support.ktx.android.view.hideKeyboard
import org.mozilla.tv.firefox.R
import org.mozilla.tv.firefox.components.UrlAutoCompleteFilter
import org.mozilla.tv.firefox.components.locale.LocaleManager
import org.mozilla.tv.firefox.ext.forEachChild
import org.mozilla.tv.firefox.ext.forceExhaustive
import org.mozilla.tv.firefox.ext.isEffectivelyVisible
Expand Down Expand Up @@ -195,7 +196,7 @@ class BrowserNavigationOverlay @JvmOverloads constructor(

pinnedTileViewModel.getTileList().observe(lifeCycleOwner, Observer {
if (it != null) {
tileAdapter.setTiles(it)
tileAdapter.setTiles(it, context)
updateOverlayForCurrentState()
}
})
Expand All @@ -204,9 +205,12 @@ class BrowserNavigationOverlay @JvmOverloads constructor(

val manager = HomeTileManager(context, COL_COUNT)
manager.spanSizeLookup = object : GridLayoutManager.SpanSizeLookup() {

val localeIsEnUs = LocaleManager.getInstance().isLocaleENUS(context)

override fun getSpanSize(position: Int): Int {
return when (position) {
0 -> 2
return when {
position == 0 && localeIsEnUs -> 2
else -> 1
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,7 @@ class CustomPinnedTile(
)
}
}

fun PinnedTile.isEventTile(): Boolean =
this.url == "http://www.mozilla.org/firefox/concerts?utm_source=firetv&utm_campaign" +
"=livenation-promotion&utm_medium=referral&utm_content=firetv_tile"
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package org.mozilla.tv.firefox.pinnedtile

import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import android.content.Context
import android.graphics.Color
import android.support.v4.content.ContextCompat
import android.support.v7.util.DiffUtil
Expand All @@ -32,6 +33,9 @@ import org.mozilla.tv.firefox.ext.withRoundedCorners
import org.mozilla.tv.firefox.telemetry.TelemetryIntegration
import org.mozilla.tv.firefox.utils.FormattedDomain

const val EVENT_TILE = 0
const val NORMAL_TILE = 1

/**
* Duration of animation to show custom tile. If the duration is too short, the tile will just
* pop-in. I speculate this happens because the amount of time it takes to downsample the bitmap
Expand All @@ -55,24 +59,22 @@ class PinnedTileAdapter(
private val uiScope = CoroutineScope(Dispatchers.Main + uiLifecycleCancelJob)

override fun getItemViewType(position: Int): Int {
return when (position) {
0 -> 0
else -> 1
return when {
tiles[position].isEventTile() -> EVENT_TILE
else -> NORMAL_TILE
}
}
@SuppressWarnings("NestedBlockDepth")
@ExperimentalCoroutinesApi
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
if (LocaleManager.getInstance().isLocaleENUS(holder.itemView.context)) {
when (position) {
0 -> {
val eventHolder = holder as EventViewHolder
with(eventHolder) {
when (holder) {
is EventViewHolder -> {
with(holder) {
val item = tiles[position]

when (item) {
is BundledPinnedTile -> {
onBindEventTile(eventHolder, item)
onBindEventTile(holder, item)
setIconLayoutMarginParams(iconView, 0)
}
}
Expand All @@ -83,17 +85,16 @@ class PinnedTileAdapter(
}
}
}
else -> {
val tileHolder = holder as TileViewHolder
with(tileHolder) {
is TileViewHolder -> {
with(holder) {
val item = tiles[position]
when (item) {
is BundledPinnedTile -> {
onBindBundledHomeTile(tileHolder, item)
onBindBundledHomeTile(holder, item)
setIconLayoutMarginParams(iconView, R.dimen.bundled_home_tile_margin_value)
}
is CustomPinnedTile -> {
onBindCustomHomeTile(uiScope, tileHolder, item)
onBindCustomHomeTile(uiScope, holder, item)
setIconLayoutMarginParams(iconView, R.dimen.custom_home_tile_margin_value)
}
}.forceExhaustive
Expand All @@ -110,7 +111,7 @@ class PinnedTileAdapter(
true
}

val tvWhiteColor = ContextCompat.getColor(tileHolder.itemView.context, R.color.tv_white)
val tvWhiteColor = ContextCompat.getColor(holder.itemView.context, R.color.tv_white)
itemView.setOnFocusChangeListener { _, hasFocus ->
val backgroundResource: Int
val textColor: Int
Expand All @@ -128,51 +129,6 @@ class PinnedTileAdapter(
}
}
}
} else {
val tileHolder = holder as TileViewHolder

with(tileHolder) {
val item = tiles[position]
when (item) {
is BundledPinnedTile -> {
onBindBundledHomeTile(tileHolder, item)
setIconLayoutMarginParams(iconView, R.dimen.bundled_home_tile_margin_value)
}
is CustomPinnedTile -> {
onBindCustomHomeTile(uiScope, tileHolder, item)
setIconLayoutMarginParams(iconView, R.dimen.custom_home_tile_margin_value)
}
}.forceExhaustive

itemView.setOnClickListener {
loadUrl(item.url)
TelemetryIntegration.INSTANCE.homeTileClickEvent(it.context, item)
}

itemView.setOnLongClickListener {
onTileLongClick?.invoke()
lastLongClickedTile = item

true
}

val tvWhiteColor = ContextCompat.getColor(tileHolder.itemView.context, R.color.tv_white)
itemView.setOnFocusChangeListener { _, hasFocus ->
val backgroundResource: Int
val textColor: Int
if (hasFocus) {
backgroundResource = R.drawable.home_tile_title_focused_background
textColor = tvWhiteColor
onTileFocused?.invoke()
} else {
backgroundResource = 0
textColor = Color.BLACK
}
titleView.setBackgroundResource(backgroundResource)
titleView.setTextColor(textColor)
}
}
}
}

private fun setIconLayoutMarginParams(iconView: View, tileMarginValue: Int) {
Expand All @@ -182,7 +138,14 @@ class PinnedTileAdapter(
iconView.layoutParams = layoutMarginParams
}

fun setTiles(newTiles: List<PinnedTile>) {
fun setTiles(tileList: List<PinnedTile>, context: Context) {
fun localeIsEnUs() = LocaleManager.getInstance().isLocaleENUS(context)

val newTiles = when {
localeIsEnUs() -> tileList
else -> tileList.filter { !it.isEventTile() }
}

if (itemCount == 0) {
tiles = newTiles
notifyDataSetChanged()
Expand Down Expand Up @@ -217,7 +180,7 @@ class PinnedTileAdapter(

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
return when (viewType) {
0 -> EventViewHolder(
EVENT_TILE -> EventViewHolder(
LayoutInflater.from(parent.context).inflate(R.layout.event_tile, parent, false)
)
else -> TileViewHolder(
Expand Down

0 comments on commit 68d2c28

Please sign in to comment.