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

Commit

Permalink
Cache findViewById calls in session control (#10575)
Browse files Browse the repository at this point in the history
  • Loading branch information
NotWoods authored May 12, 2020
1 parent 9a30d57 commit f7b4f1c
Show file tree
Hide file tree
Showing 13 changed files with 114 additions and 111 deletions.
13 changes: 13 additions & 0 deletions app/src/main/java/org/mozilla/fenix/ext/ViewHolder.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.mozilla.fenix.ext

import android.view.View
import androidx.recyclerview.widget.RecyclerView
import kotlinx.android.extensions.LayoutContainer

/**
* A base class for all recycler view holders supporting Android Extensions-style view access.
* This allows views to be used without an `itemView.<id>` prefix, and additionally caches them.
*/
abstract class ViewHolder(
override val containerView: View
) : RecyclerView.ViewHolder(containerView), LayoutContainer
2 changes: 1 addition & 1 deletion app/src/main/java/org/mozilla/fenix/home/HomeFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -904,7 +904,7 @@ class HomeFragment : Fragment() {
val viewHolder =
sessionControlView!!.view.findViewHolderForAdapterPosition(indexOfCollection)
val border =
(viewHolder as? CollectionViewHolder)?.view?.findViewById<View>(R.id.selected_border)
(viewHolder as? CollectionViewHolder)?.itemView?.findViewById<View>(R.id.selected_border)
val listener = object : Animator.AnimatorListener {
override fun onAnimationCancel(animation: Animator?) {
border?.visibility = View.GONE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,12 @@ import android.content.Context
import android.view.View
import androidx.core.graphics.BlendModeColorFilterCompat.createBlendModeColorFilterCompat
import androidx.core.graphics.BlendModeCompat.SRC_IN
import androidx.recyclerview.widget.RecyclerView
import kotlinx.android.extensions.LayoutContainer
import kotlinx.android.synthetic.main.collection_home_list_row.*
import kotlinx.android.synthetic.main.collection_home_list_row.view.*
import mozilla.components.browser.menu.BrowserMenuBuilder
import mozilla.components.browser.menu.item.SimpleBrowserMenuItem
import mozilla.components.feature.tab.collections.TabCollection
import org.mozilla.fenix.R
import org.mozilla.fenix.ext.ViewHolder
import org.mozilla.fenix.ext.getIconColor
import org.mozilla.fenix.ext.increaseTapArea
import org.mozilla.fenix.ext.removeAndDisable
Expand All @@ -25,11 +23,9 @@ import org.mozilla.fenix.home.sessioncontrol.CollectionInteractor
import org.mozilla.fenix.theme.ThemeManager

class CollectionViewHolder(
val view: View,
val interactor: CollectionInteractor,
override val containerView: View? = view
) :
RecyclerView.ViewHolder(view), LayoutContainer {
view: View,
val interactor: CollectionInteractor
) : ViewHolder(view) {

private lateinit var collection: TabCollection
private var expanded = false
Expand Down Expand Up @@ -71,32 +67,32 @@ class CollectionViewHolder(
}

private fun updateCollectionUI() {
view.collection_title.text = collection.title
collection_title.text = collection.title

view.isActivated = expanded
itemView.isActivated = expanded
if (expanded) {
view.collection_share_button.apply {
collection_share_button.apply {
showAndEnable()
increaseTapArea(buttonIncreaseDps)
}
view.collection_overflow_button.apply {
collection_overflow_button.apply {
showAndEnable()
increaseTapArea(buttonIncreaseDps)
}
} else {

view.collection_share_button.apply {
collection_share_button.apply {
removeAndDisable()
removeTouchDelegate()
}
view.collection_overflow_button.apply {
collection_overflow_button.apply {
removeAndDisable()
removeTouchDelegate()
}
}

view.collection_icon.colorFilter = createBlendModeColorFilterCompat(
collection.getIconColor(view.context),
collection_icon.colorFilter = createBlendModeColorFilterCompat(
collection.getIconColor(itemView.context),
SRC_IN
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@ package org.mozilla.fenix.home.sessioncontrol.viewholders

import android.view.View
import androidx.annotation.StringRes
import androidx.recyclerview.widget.RecyclerView
import kotlinx.android.synthetic.main.no_content_message.view.*
import kotlinx.android.synthetic.main.no_content_message.*
import org.mozilla.fenix.R
import org.mozilla.fenix.ext.ViewHolder

open class NoContentMessageViewHolder(private val view: View) : RecyclerView.ViewHolder(view) {
open class NoContentMessageViewHolder(view: View) : ViewHolder(view) {

fun bind(
@StringRes header: Int,
@StringRes description: Int
) {
with(view.context) {
view.no_content_header.text = getString(header)
view.no_content_description.text = getString(description)
with(itemView.context) {
no_content_header.text = getString(header)
no_content_description.text = getString(description)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,10 @@ import android.view.View
import androidx.annotation.DrawableRes
import androidx.annotation.StringRes
import androidx.core.view.isVisible
import kotlinx.android.synthetic.main.no_content_message_with_action.view.*
import kotlinx.android.synthetic.main.no_content_message_with_action.*
import org.mozilla.fenix.R

class NoContentMessageWithActionViewHolder(
private val view: View
) : NoContentMessageViewHolder(view) {
class NoContentMessageWithActionViewHolder(view: View) : NoContentMessageViewHolder(view) {

/**
* @param header ID of string resource for title text.
Expand All @@ -31,12 +29,12 @@ class NoContentMessageWithActionViewHolder(
listener: (() -> Unit)? = null
) {
super.bind(header, description)
with(view.context) {
with(itemView.context) {

if (buttonIcon != 0 || buttonText != 0) {
view.add_new_tab_button.apply {
add_new_tab_button.apply {
isVisible = true
setIcon(getDrawable(buttonIcon))
icon = getDrawable(buttonIcon)
text = getString(buttonText)
setOnClickListener {
listener?.invoke()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,20 @@ import android.view.View
import android.widget.PopupWindow
import androidx.core.view.isInvisible
import androidx.core.view.isVisible
import androidx.recyclerview.widget.RecyclerView
import kotlinx.android.synthetic.main.tab_header.view.*
import kotlinx.android.synthetic.main.tab_header.*
import mozilla.components.browser.menu.BrowserMenu
import mozilla.components.browser.menu.BrowserMenuBuilder
import mozilla.components.browser.menu.item.SimpleBrowserMenuItem
import org.mozilla.fenix.R
import org.mozilla.fenix.components.metrics.Event
import org.mozilla.fenix.ext.ViewHolder
import org.mozilla.fenix.ext.components
import org.mozilla.fenix.home.sessioncontrol.SessionControlInteractor

class TabHeaderViewHolder(
private val view: View,
view: View,
private val interactor: SessionControlInteractor
) : RecyclerView.ViewHolder(view) {
) : ViewHolder(view) {
private var isPrivate = false
private var tabsMenu: TabHeaderMenu

Expand All @@ -39,34 +39,28 @@ class TabHeaderViewHolder(
}
}

view.apply {
share_tabs_button.run {
setOnClickListener {
interactor.onShareTabs()
}
}
share_tabs_button.setOnClickListener {
interactor.onShareTabs()
}

close_tabs_button.run {
setOnClickListener {
view.context.components.analytics.metrics.track(Event.PrivateBrowsingGarbageIconTapped)
interactor.onCloseAllTabs(true)
}
}
close_tabs_button.setOnClickListener {
it.context.components.analytics.metrics.track(Event.PrivateBrowsingGarbageIconTapped)
interactor.onCloseAllTabs(true)
}

tabs_overflow_button.run {
var menu: PopupWindow? = null
setOnClickListener {
if (menu == null) {
menu = tabsMenu.menuBuilder
.build(view.context)
.show(
anchor = it,
orientation = BrowserMenu.Orientation.DOWN,
onDismiss = { menu = null }
)
} else {
menu?.dismiss()
}
tabs_overflow_button.run {
var menu: PopupWindow? = null
setOnClickListener {
if (menu == null) {
menu = tabsMenu.menuBuilder
.build(it.context)
.show(
anchor = it,
orientation = BrowserMenu.Orientation.DOWN,
onDismiss = { menu = null }
)
} else {
menu?.dismiss()
}
}
}
Expand All @@ -78,10 +72,10 @@ class TabHeaderViewHolder(

val headerTextResourceId =
if (isPrivate) R.string.tabs_header_private_tabs_title else R.string.tab_header_label
view.header_text.text = view.context.getString(headerTextResourceId)
view.share_tabs_button.isInvisible = !isPrivate || !hasTabs
view.close_tabs_button.isInvisible = !isPrivate || !hasTabs
view.tabs_overflow_button.isVisible = !isPrivate && hasTabs
header_text.text = itemView.context.getString(headerTextResourceId)
share_tabs_button.isInvisible = !isPrivate || !hasTabs
close_tabs_button.isInvisible = !isPrivate || !hasTabs
tabs_overflow_button.isVisible = !isPrivate && hasTabs
}

class TabHeaderMenu(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,12 @@ import android.graphics.Outline
import android.view.View
import android.view.ViewOutlineProvider
import androidx.appcompat.content.res.AppCompatResources
import androidx.recyclerview.widget.RecyclerView
import kotlinx.android.extensions.LayoutContainer
import kotlinx.android.synthetic.main.list_element.list_element_title
import kotlinx.android.synthetic.main.list_element.list_item_action_button
import kotlinx.android.synthetic.main.list_element.list_item_favicon
import kotlinx.android.synthetic.main.list_element.list_item_url
import kotlinx.android.synthetic.main.list_element.*
import mozilla.components.feature.tab.collections.TabCollection
import mozilla.components.support.ktx.android.content.getColorFromAttr
import mozilla.components.support.ktx.android.util.dpToFloat
import org.mozilla.fenix.R
import org.mozilla.fenix.ext.ViewHolder
import org.mozilla.fenix.ext.components
import org.mozilla.fenix.ext.increaseTapArea
import org.mozilla.fenix.ext.loadIntoView
Expand All @@ -26,11 +22,10 @@ import org.mozilla.fenix.home.sessioncontrol.CollectionInteractor
import mozilla.components.feature.tab.collections.Tab as ComponentTab

class TabInCollectionViewHolder(
val view: View,
view: View,
val interactor: CollectionInteractor,
private val differentLastItem: Boolean = false,
override val containerView: View? = view
) : RecyclerView.ViewHolder(view), LayoutContainer {
private val differentLastItem: Boolean = false
) : ViewHolder(view) {

lateinit var collection: TabCollection
private set
Expand All @@ -52,7 +47,7 @@ class TabInCollectionViewHolder(
}
}

view.setOnClickListener {
itemView.setOnClickListener {
interactor.onCollectionOpenTabClicked(tab)
}

Expand All @@ -70,16 +65,17 @@ class TabInCollectionViewHolder(
}

private fun updateTabUI() {
list_item_url.text = tab.url.toShortUrl(view.context.components.publicSuffixList)
val context = itemView.context
list_item_url.text = tab.url.toShortUrl(context.components.publicSuffixList)

list_element_title.text = tab.title
list_item_favicon.context.components.core.icons.loadIntoView(list_item_favicon, tab.url)

// If last item and we want to change UI for it
if (isLastItem && differentLastItem) {
view.background = AppCompatResources.getDrawable(view.context, R.drawable.rounded_bottom_corners)
itemView.background = AppCompatResources.getDrawable(context, R.drawable.rounded_bottom_corners)
} else {
view.setBackgroundColor(view.context.getColorFromAttr(R.attr.above))
itemView.setBackgroundColor(context.getColorFromAttr(R.attr.above))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,12 @@ import android.graphics.Outline
import android.view.View
import android.view.ViewOutlineProvider
import androidx.appcompat.content.res.AppCompatResources
import androidx.recyclerview.widget.RecyclerView
import kotlinx.android.extensions.LayoutContainer
import kotlinx.android.synthetic.main.tab_list_row.*
import mozilla.components.browser.state.state.MediaState
import mozilla.components.support.ktx.android.util.dpToFloat
import org.mozilla.fenix.R
import org.mozilla.fenix.components.metrics.Event
import org.mozilla.fenix.ext.ViewHolder
import org.mozilla.fenix.ext.components
import org.mozilla.fenix.ext.increaseTapArea
import org.mozilla.fenix.ext.loadIntoView
Expand All @@ -27,10 +26,8 @@ import org.mozilla.fenix.home.sessioncontrol.TabSessionInteractor

class TabViewHolder(
view: View,
interactor: TabSessionInteractor,
override val containerView: View? = view
) :
RecyclerView.ViewHolder(view), LayoutContainer {
interactor: TabSessionInteractor
) : ViewHolder(view) {

internal var tab: Tab? = null

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ package org.mozilla.fenix.home.sessioncontrol.viewholders
import android.view.View
import androidx.recyclerview.widget.RecyclerView
import com.google.android.flexbox.FlexboxLayoutManager
import kotlinx.android.extensions.LayoutContainer
import kotlinx.android.synthetic.main.component_top_sites.view.*
import mozilla.components.feature.top.sites.TopSite
import org.mozilla.fenix.R
Expand All @@ -16,9 +15,9 @@ import org.mozilla.fenix.home.sessioncontrol.viewholders.topsites.TopSitesAdapte

class TopSiteViewHolder(
view: View,
interactor: TopSiteInteractor,
override val containerView: View? = view
) : RecyclerView.ViewHolder(view), LayoutContainer {
interactor: TopSiteInteractor
) : RecyclerView.ViewHolder(view) {

private val topSitesAdapter = TopSitesAdapter(interactor)

init {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
package org.mozilla.fenix.home.sessioncontrol.viewholders.onboarding

import android.view.View
import androidx.appcompat.content.res.AppCompatResources
import androidx.appcompat.content.res.AppCompatResources.getDrawable
import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.snackbar.Snackbar
import kotlinx.android.synthetic.main.onboarding_automatic_signin.view.*
Expand All @@ -19,8 +19,10 @@ import org.mozilla.fenix.R
import org.mozilla.fenix.components.FenixSnackbar
import org.mozilla.fenix.ext.components

class OnboardingAutomaticSignInViewHolder(private val view: View) : RecyclerView.ViewHolder(view) {
class OnboardingAutomaticSignInViewHolder(view: View) : RecyclerView.ViewHolder(view) {

private lateinit var shareableAccount: ShareableAccount
private val headerText = view.header_text

init {
view.turn_on_sync_button.setOnClickListener {
Expand Down Expand Up @@ -57,11 +59,11 @@ class OnboardingAutomaticSignInViewHolder(private val view: View) : RecyclerView

fun bind(account: ShareableAccount) {
shareableAccount = account
view.header_text.text = view.context.getString(
headerText.text = itemView.context.getString(
R.string.onboarding_firefox_account_auto_signin_header_2, account.email
)
val icon = AppCompatResources.getDrawable(view.context, R.drawable.ic_onboarding_avatar_anonymous)
view.header_text.putCompoundDrawablesRelativeWithIntrinsicBounds(start = icon)
val icon = getDrawable(itemView.context, R.drawable.ic_onboarding_avatar_anonymous)
headerText.putCompoundDrawablesRelativeWithIntrinsicBounds(start = icon)
}

companion object {
Expand Down
Loading

0 comments on commit f7b4f1c

Please sign in to comment.