From f4f6b18cbe35f784e3798158882eee27b8d2cee6 Mon Sep 17 00:00:00 2001 From: "codrut.topliceanu" Date: Tue, 3 Aug 2021 16:28:22 +0300 Subject: [PATCH] [fenix] For https://github.com/mozilla-mobile/fenix/issues/17917: Use View binding in history screens --- .../fenix/library/history/HistoryAdapter.kt | 6 ++- .../fenix/library/history/HistoryFragment.kt | 12 +++-- .../fenix/library/history/HistoryView.kt | 42 ++++++++--------- .../viewholders/HistoryListItemViewHolder.kt | 46 +++++++++---------- app/src/main/res/layout/history_list_item.xml | 4 +- 5 files changed, 57 insertions(+), 53 deletions(-) diff --git a/app/src/main/java/org/mozilla/fenix/library/history/HistoryAdapter.kt b/app/src/main/java/org/mozilla/fenix/library/history/HistoryAdapter.kt index 32b43b2210..7b111cda5a 100644 --- a/app/src/main/java/org/mozilla/fenix/library/history/HistoryAdapter.kt +++ b/app/src/main/java/org/mozilla/fenix/library/history/HistoryAdapter.kt @@ -28,8 +28,9 @@ enum class HistoryItemTimeGroup { } } -class HistoryAdapter(private val historyInteractor: HistoryInteractor) : - PagedListAdapter(historyDiffCallback), +class HistoryAdapter( + private val historyInteractor: HistoryInteractor, +) : PagedListAdapter(historyDiffCallback), SelectionHolder { private var mode: HistoryFragmentState.Mode = HistoryFragmentState.Mode.Normal @@ -41,6 +42,7 @@ class HistoryAdapter(private val historyInteractor: HistoryInteractor) : override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): HistoryListItemViewHolder { val view = LayoutInflater.from(parent.context).inflate(viewType, parent, false) + return HistoryListItemViewHolder(view, historyInteractor, this) } diff --git a/app/src/main/java/org/mozilla/fenix/library/history/HistoryFragment.kt b/app/src/main/java/org/mozilla/fenix/library/history/HistoryFragment.kt index 4181a14c85..7cbba31ded 100644 --- a/app/src/main/java/org/mozilla/fenix/library/history/HistoryFragment.kt +++ b/app/src/main/java/org/mozilla/fenix/library/history/HistoryFragment.kt @@ -20,7 +20,6 @@ import androidx.lifecycle.Observer import androidx.lifecycle.lifecycleScope import androidx.navigation.NavDirections import androidx.navigation.fragment.findNavController -import kotlinx.android.synthetic.main.fragment_history.view.* import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.Dispatchers.Main @@ -43,6 +42,7 @@ import org.mozilla.fenix.components.FenixSnackbar import org.mozilla.fenix.components.StoreProvider import org.mozilla.fenix.components.history.createSynchronousPagedHistoryProvider import org.mozilla.fenix.components.metrics.Event +import org.mozilla.fenix.databinding.FragmentHistoryBinding import org.mozilla.fenix.ext.components import org.mozilla.fenix.ext.nav import org.mozilla.fenix.ext.requireComponents @@ -62,13 +62,16 @@ class HistoryFragment : LibraryPageFragment(), UserInteractionHandl private var _historyView: HistoryView? = null protected val historyView: HistoryView get() = _historyView!! + private var _binding: FragmentHistoryBinding? = null + private val binding get() = _binding!! override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? - ): View? { - val view = inflater.inflate(R.layout.fragment_history, container, false) + ): View { + _binding = FragmentHistoryBinding.inflate(inflater, container, false) + val view = binding.root historyStore = StoreProvider.get(this) { HistoryFragmentStore( HistoryFragmentState( @@ -102,7 +105,7 @@ class HistoryFragment : LibraryPageFragment(), UserInteractionHandl historyController ) _historyView = HistoryView( - view.historyLayout, + binding.historyLayout, historyInteractor ) @@ -254,6 +257,7 @@ class HistoryFragment : LibraryPageFragment(), UserInteractionHandl override fun onDestroyView() { super.onDestroyView() _historyView = null + _binding = null } private fun openItem(item: HistoryItem) { diff --git a/app/src/main/java/org/mozilla/fenix/library/history/HistoryView.kt b/app/src/main/java/org/mozilla/fenix/library/history/HistoryView.kt index b13f4a8133..ad4fdf8392 100644 --- a/app/src/main/java/org/mozilla/fenix/library/history/HistoryView.kt +++ b/app/src/main/java/org/mozilla/fenix/library/history/HistoryView.kt @@ -5,16 +5,13 @@ package org.mozilla.fenix.library.history import android.view.LayoutInflater -import android.view.View import android.view.ViewGroup import androidx.core.view.isVisible import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.SimpleItemAnimator -import kotlinx.android.synthetic.main.component_history.* -import kotlinx.android.synthetic.main.component_history.view.* -import kotlinx.android.synthetic.main.recently_closed_nav_item.* import mozilla.components.support.base.feature.UserInteractionHandler import org.mozilla.fenix.R +import org.mozilla.fenix.databinding.ComponentHistoryBinding import org.mozilla.fenix.ext.components import org.mozilla.fenix.library.LibraryPageView import org.mozilla.fenix.selection.SelectionInteractor @@ -94,8 +91,9 @@ class HistoryView( val interactor: HistoryInteractor ) : LibraryPageView(container), UserInteractionHandler { - val view: View = LayoutInflater.from(container.context) - .inflate(R.layout.component_history, container, true) + val binding = ComponentHistoryBinding.inflate( + LayoutInflater.from(container.context), container, true + ) var mode: HistoryFragmentState.Mode = HistoryFragmentState.Mode.Normal private set @@ -104,7 +102,7 @@ class HistoryView( private val layoutManager = LinearLayoutManager(container.context) init { - view.history_list.apply { + binding.historyList.apply { layoutManager = this@HistoryView.layoutManager adapter = historyAdapter (itemAnimator as SimpleItemAnimator).supportsChangeAnimations = false @@ -112,19 +110,19 @@ class HistoryView( val primaryTextColor = ThemeManager.resolveAttribute(R.attr.primaryText, context) - view.swipe_refresh.setColorSchemeColors(primaryTextColor) - view.swipe_refresh.setOnRefreshListener { + binding.swipeRefresh.setColorSchemeColors(primaryTextColor) + binding.swipeRefresh.setOnRefreshListener { interactor.onRequestSync() - view.history_list.scrollToPosition(0) + binding.historyList.scrollToPosition(0) } } fun update(state: HistoryFragmentState) { val oldMode = mode - view.progress_bar.isVisible = state.isDeletingItems - view.swipe_refresh.isRefreshing = state.mode === HistoryFragmentState.Mode.Syncing - view.swipe_refresh.isEnabled = + binding.progressBar.isVisible = state.isDeletingItems + binding.swipeRefresh.isRefreshing = state.mode === HistoryFragmentState.Mode.Syncing + binding.swipeRefresh.isEnabled = state.mode === HistoryFragmentState.Mode.Normal || state.mode === HistoryFragmentState.Mode.Syncing mode = state.mode @@ -164,24 +162,24 @@ class HistoryView( } fun updateEmptyState(userHasHistory: Boolean) { - history_list.isVisible = userHasHistory - history_empty_view.isVisible = !userHasHistory - recently_closed_nav_empty.apply { - setOnClickListener { + binding.historyList.isVisible = userHasHistory + binding.historyEmptyView.isVisible = !userHasHistory + with(binding.recentlyClosedNavEmpty) { + recentlyClosedNav.setOnClickListener { interactor.onRecentlyClosedClicked() } - val numRecentTabs = view.context.components.core.store.state.closedTabs.size - recently_closed_tabs_description.text = String.format( - view.context.getString( + val numRecentTabs = recentlyClosedNav.context.components.core.store.state.closedTabs.size + recentlyClosedTabsDescription.text = String.format( + context.getString( if (numRecentTabs == 1) R.string.recently_closed_tab else R.string.recently_closed_tabs ), numRecentTabs ) - isVisible = !userHasHistory + recentlyClosedNav.isVisible = !userHasHistory } if (!userHasHistory) { - history_empty_view.announceForAccessibility(context.getString(R.string.history_empty_message)) + binding.historyEmptyView.announceForAccessibility(context.getString(R.string.history_empty_message)) } } diff --git a/app/src/main/java/org/mozilla/fenix/library/history/viewholders/HistoryListItemViewHolder.kt b/app/src/main/java/org/mozilla/fenix/library/history/viewholders/HistoryListItemViewHolder.kt index e7810ac1bb..9de385d57b 100644 --- a/app/src/main/java/org/mozilla/fenix/library/history/viewholders/HistoryListItemViewHolder.kt +++ b/app/src/main/java/org/mozilla/fenix/library/history/viewholders/HistoryListItemViewHolder.kt @@ -5,13 +5,10 @@ package org.mozilla.fenix.library.history.viewholders import android.view.View -import androidx.constraintlayout.widget.ConstraintLayout import androidx.core.view.isVisible import androidx.recyclerview.widget.RecyclerView -import kotlinx.android.synthetic.main.history_list_item.view.* -import kotlinx.android.synthetic.main.library_site_item.view.* -import kotlinx.android.synthetic.main.recently_closed_nav_item.view.* import org.mozilla.fenix.R +import org.mozilla.fenix.databinding.HistoryListItemBinding import org.mozilla.fenix.ext.components import org.mozilla.fenix.ext.hideAndDisable import org.mozilla.fenix.ext.showAndEnable @@ -30,11 +27,12 @@ class HistoryListItemViewHolder( ) : RecyclerView.ViewHolder(view) { private var item: HistoryItem? = null + private val binding = HistoryListItemBinding.bind(view) init { setupMenu() - itemView.delete_button.setOnClickListener { + binding.deleteButton.setOnClickListener { val selected = selectionHolder.selectedItems if (selected.isEmpty()) { historyInteractor.onDeleteAll() @@ -43,7 +41,7 @@ class HistoryListItemViewHolder( } } - itemView.findViewById(R.id.recently_closed_nav).setOnClickListener { + binding.recentlyClosedNavEmpty.recentlyClosedNav.setOnClickListener { historyInteractor.onRecentlyClosedClicked() } } @@ -56,30 +54,30 @@ class HistoryListItemViewHolder( isPendingDeletion: Boolean = false ) { if (isPendingDeletion) { - itemView.history_layout.visibility = View.GONE + binding.historyLayout.visibility = View.GONE } else { - itemView.history_layout.visibility = View.VISIBLE + binding.historyLayout.visibility = View.VISIBLE } - itemView.history_layout.titleView.text = item.title - itemView.history_layout.urlView.text = item.url + binding.historyLayout.titleView.text = item.title + binding.historyLayout.urlView.text = item.url toggleTopContent(showDeleteButton, mode === HistoryFragmentState.Mode.Normal) val headerText = timeGroup?.humanReadable(itemView.context) toggleHeader(headerText) - itemView.history_layout.setSelectionInteractor(item, selectionHolder, historyInteractor) - itemView.history_layout.changeSelected(item in selectionHolder.selectedItems) + binding.historyLayout.setSelectionInteractor(item, selectionHolder, historyInteractor) + binding.historyLayout.changeSelected(item in selectionHolder.selectedItems) if (this.item?.url != item.url) { - itemView.history_layout.loadFavicon(item.url) + binding.historyLayout.loadFavicon(item.url) } if (mode is HistoryFragmentState.Mode.Editing) { - itemView.overflow_menu.hideAndDisable() + binding.historyLayout.overflowView.hideAndDisable() } else { - itemView.overflow_menu.showAndEnable() + binding.historyLayout.overflowView.showAndEnable() } this.item = item @@ -87,10 +85,10 @@ class HistoryListItemViewHolder( private fun toggleHeader(headerText: String?) { if (headerText != null) { - itemView.header_title.visibility = View.VISIBLE - itemView.header_title.text = headerText + binding.headerTitle.visibility = View.VISIBLE + binding.headerTitle.text = headerText } else { - itemView.header_title.visibility = View.GONE + binding.headerTitle.visibility = View.GONE } } @@ -98,11 +96,11 @@ class HistoryListItemViewHolder( showTopContent: Boolean, isNormalMode: Boolean ) { - itemView.delete_button.isVisible = showTopContent - itemView.findViewById(R.id.recently_closed_nav).isVisible = showTopContent + binding.deleteButton.isVisible = showTopContent + binding.recentlyClosedNavEmpty.recentlyClosedNav.isVisible = showTopContent if (showTopContent) { - itemView.delete_button.run { + binding.deleteButton.run { if (isNormalMode) { isEnabled = true alpha = 1f @@ -112,14 +110,14 @@ class HistoryListItemViewHolder( } } val numRecentTabs = itemView.context.components.core.store.state.closedTabs.size - itemView.recently_closed_tabs_description.text = String.format( + binding.recentlyClosedNavEmpty.recentlyClosedTabsDescription.text = String.format( itemView.context.getString( if (numRecentTabs == 1) R.string.recently_closed_tab else R.string.recently_closed_tabs ), numRecentTabs ) - itemView.findViewById(R.id.recently_closed_nav).run { + binding.recentlyClosedNavEmpty.recentlyClosedNav.run { if (isNormalMode) { isEnabled = true alpha = 1f @@ -143,7 +141,7 @@ class HistoryListItemViewHolder( } } - itemView.history_layout.attachMenu(historyMenu.menuController) + binding.historyLayout.attachMenu(historyMenu.menuController) } companion object { diff --git a/app/src/main/res/layout/history_list_item.xml b/app/src/main/res/layout/history_list_item.xml index e0dcfd4919..bc8bac5300 100644 --- a/app/src/main/res/layout/history_list_item.xml +++ b/app/src/main/res/layout/history_list_item.xml @@ -18,7 +18,9 @@ android:visibility="gone" tools:visibility="visible" /> - +