From 003a2ce81a17bd53e280c67de8eb45fed8c24b10 Mon Sep 17 00:00:00 2001 From: "codrut.topliceanu" Date: Fri, 6 Aug 2021 16:52:31 +0300 Subject: [PATCH] For #17917: Use View binding in downloads screen --- .../fenix/library/LibrarySiteItemView.kt | 30 +++++++++-------- .../library/downloads/DownloadFragment.kt | 18 ++++++++--- .../fenix/library/downloads/DownloadView.kt | 17 +++++----- .../DownloadsListItemViewHolder.kt | 32 ++++++++++--------- 4 files changed, 56 insertions(+), 41 deletions(-) diff --git a/app/src/main/java/org/mozilla/fenix/library/LibrarySiteItemView.kt b/app/src/main/java/org/mozilla/fenix/library/LibrarySiteItemView.kt index 67ade043d..479ff64cd 100644 --- a/app/src/main/java/org/mozilla/fenix/library/LibrarySiteItemView.kt +++ b/app/src/main/java/org/mozilla/fenix/library/LibrarySiteItemView.kt @@ -12,10 +12,9 @@ import android.widget.ImageView import android.widget.TextView import androidx.constraintlayout.widget.ConstraintLayout import androidx.core.view.isVisible -import kotlinx.android.synthetic.main.library_site_item.view.* import mozilla.components.concept.menu.MenuController import mozilla.components.concept.menu.Orientation -import org.mozilla.fenix.R +import org.mozilla.fenix.databinding.LibrarySiteItemBinding import org.mozilla.fenix.ext.components import org.mozilla.fenix.ext.increaseTapArea import org.mozilla.fenix.ext.loadIntoView @@ -29,40 +28,45 @@ class LibrarySiteItemView @JvmOverloads constructor( defStyleRes: Int = 0 ) : ConstraintLayout(context, attrs, defStyleAttr, defStyleRes) { - val titleView: TextView get() = title + private val binding = LibrarySiteItemBinding.inflate( + LayoutInflater.from(context), + this, + true + ) - val urlView: TextView get() = url + val titleView: TextView get() = binding.title - val iconView: ImageView get() = favicon + val urlView: TextView get() = binding.url - val overflowView: ImageButton get() = overflow_menu + val iconView: ImageView get() = binding.favicon + + val overflowView: ImageButton get() = binding.overflowMenu init { - LayoutInflater.from(context).inflate(R.layout.library_site_item, this, true) - overflow_menu.increaseTapArea(OVERFLOW_EXTRA_DIPS) + overflowView.increaseTapArea(OVERFLOW_EXTRA_DIPS) } /** * Change visibility of parts of this view based on what type of item is being represented. */ fun displayAs(mode: ItemType) { - url.isVisible = mode == ItemType.SITE + urlView.isVisible = mode == ItemType.SITE } /** * Changes the icon to show a check mark if [isSelected] */ fun changeSelected(isSelected: Boolean) { - icon.displayedChild = if (isSelected) 1 else 0 + binding.icon.displayedChild = if (isSelected) 1 else 0 } fun loadFavicon(url: String) { - context.components.core.icons.loadIntoView(favicon, url) + context.components.core.icons.loadIntoView(iconView, url) } fun attachMenu(menuController: MenuController) { - overflow_menu.setOnClickListener { + overflowView.setOnClickListener { menuController.show( anchor = it, orientation = Orientation.DOWN @@ -89,7 +93,7 @@ class LibrarySiteItemView @JvmOverloads constructor( } } - favicon.setOnClickListener { + iconView.setOnClickListener { if (item in holder.selectedItems) { interactor.deselect(item) } else { diff --git a/app/src/main/java/org/mozilla/fenix/library/downloads/DownloadFragment.kt b/app/src/main/java/org/mozilla/fenix/library/downloads/DownloadFragment.kt index 71473e906..63f0fea0f 100644 --- a/app/src/main/java/org/mozilla/fenix/library/downloads/DownloadFragment.kt +++ b/app/src/main/java/org/mozilla/fenix/library/downloads/DownloadFragment.kt @@ -16,7 +16,6 @@ import android.view.ViewGroup import androidx.annotation.VisibleForTesting import androidx.appcompat.app.AlertDialog import androidx.lifecycle.lifecycleScope -import kotlinx.android.synthetic.main.fragment_downloads.view.* import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers.IO @@ -35,6 +34,7 @@ import org.mozilla.fenix.browser.browsingmode.BrowsingMode import org.mozilla.fenix.components.StoreProvider import org.mozilla.fenix.components.metrics.Event import org.mozilla.fenix.components.metrics.MetricController +import org.mozilla.fenix.databinding.FragmentDownloadsBinding import org.mozilla.fenix.ext.components import org.mozilla.fenix.ext.filterNotExistsOnDisk import org.mozilla.fenix.ext.requireComponents @@ -53,12 +53,15 @@ class DownloadFragment : LibraryPageFragment(), UserInteractionHan private var pendingDownloadDeletionJob: (suspend () -> Unit)? = null private lateinit var downloadsUseCases: DownloadsUseCases + private var _binding: FragmentDownloadsBinding? = null + private val binding get() = _binding!! + override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? - ): View? { - val view = inflater.inflate(R.layout.fragment_downloads, container, false) + ): View { + _binding = FragmentDownloadsBinding.inflate(inflater, container, false) val items = provideDownloads(requireComponents.core.store.state) downloadsUseCases = requireContext().components.useCases.downloadUseCases @@ -83,9 +86,14 @@ class DownloadFragment : LibraryPageFragment(), UserInteractionHan downloadInteractor = DownloadInteractor( downloadController ) - downloadView = DownloadView(view.downloadsLayout, downloadInteractor) + downloadView = DownloadView(binding.downloadsLayout, downloadInteractor) + + return binding.root + } - return view + override fun onDestroyView() { + super.onDestroyView() + _binding = null } /** diff --git a/app/src/main/java/org/mozilla/fenix/library/downloads/DownloadView.kt b/app/src/main/java/org/mozilla/fenix/library/downloads/DownloadView.kt index b122ef8f0..c060b29f5 100644 --- a/app/src/main/java/org/mozilla/fenix/library/downloads/DownloadView.kt +++ b/app/src/main/java/org/mozilla/fenix/library/downloads/DownloadView.kt @@ -5,17 +5,15 @@ package org.mozilla.fenix.library.downloads 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_downloads.* import kotlinx.android.synthetic.main.component_downloads.view.* -import kotlinx.android.synthetic.main.component_history.view.progress_bar -import kotlinx.android.synthetic.main.component_history.view.swipe_refresh import mozilla.components.support.base.feature.UserInteractionHandler import org.mozilla.fenix.R +import org.mozilla.fenix.databinding.ComponentDownloadsBinding import org.mozilla.fenix.library.LibraryPageView import org.mozilla.fenix.selection.SelectionInteractor @@ -55,8 +53,11 @@ class DownloadView( val interactor: DownloadInteractor ) : LibraryPageView(container), UserInteractionHandler { - val view: View = LayoutInflater.from(container.context) - .inflate(R.layout.component_downloads, container, true) + val binding = ComponentDownloadsBinding.inflate( + LayoutInflater.from(container.context), + container, + true + ) var mode: DownloadFragmentState.Mode = DownloadFragmentState.Mode.Normal private set @@ -65,7 +66,7 @@ class DownloadView( private val layoutManager = LinearLayoutManager(container.context) init { - view.download_list.apply { + binding.downloadList.apply { layoutManager = this@DownloadView.layoutManager adapter = downloadAdapter (itemAnimator as SimpleItemAnimator).supportsChangeAnimations = false @@ -75,8 +76,8 @@ class DownloadView( fun update(state: DownloadFragmentState) { val oldMode = mode - view.progress_bar.isVisible = state.isDeletingItems - view.swipe_refresh.isEnabled = false + binding.progressBar.isVisible = state.isDeletingItems + binding.swipeRefresh.isEnabled = false mode = state.mode downloadAdapter.updatePendingDeletionIds(state.pendingDeletionIds) diff --git a/app/src/main/java/org/mozilla/fenix/library/downloads/viewholders/DownloadsListItemViewHolder.kt b/app/src/main/java/org/mozilla/fenix/library/downloads/viewholders/DownloadsListItemViewHolder.kt index 5a80e3719..1e468c0af 100644 --- a/app/src/main/java/org/mozilla/fenix/library/downloads/viewholders/DownloadsListItemViewHolder.kt +++ b/app/src/main/java/org/mozilla/fenix/library/downloads/viewholders/DownloadsListItemViewHolder.kt @@ -7,10 +7,10 @@ package org.mozilla.fenix.library.downloads.viewholders import android.view.View import androidx.core.view.isVisible import androidx.recyclerview.widget.RecyclerView -import kotlinx.android.synthetic.main.download_list_item.view.* -import kotlinx.android.synthetic.main.library_site_item.view.* import mozilla.components.feature.downloads.toMegabyteOrKilobyteString import org.mozilla.fenix.R +import org.mozilla.fenix.databinding.DownloadListItemBinding +import org.mozilla.fenix.databinding.LibrarySiteItemBinding import org.mozilla.fenix.selection.SelectionHolder import org.mozilla.fenix.library.downloads.DownloadInteractor import org.mozilla.fenix.library.downloads.DownloadItem @@ -26,11 +26,13 @@ class DownloadsListItemViewHolder( ) : RecyclerView.ViewHolder(view) { private var item: DownloadItem? = null + private val binding = DownloadListItemBinding.bind(view) + private val librarySiteItemBinding = LibrarySiteItemBinding.bind(binding.downloadLayout) init { setupMenu() - itemView.delete_downloads_button.setOnClickListener { + binding.deleteDownloadsButton.setOnClickListener { val selected = selectionHolder.selectedItems if (selected.isEmpty()) { downloadInteractor.onDeleteAll() @@ -45,26 +47,26 @@ class DownloadsListItemViewHolder( mode: DownloadFragmentState.Mode, isPendingDeletion: Boolean = false ) { - itemView.download_layout.visibility = if (isPendingDeletion) { + binding.downloadLayout.visibility = if (isPendingDeletion) { View.GONE } else { View.VISIBLE } - itemView.download_layout.titleView.text = item.fileName - itemView.download_layout.urlView.text = item.size.toLong().toMegabyteOrKilobyteString() + binding.downloadLayout.titleView.text = item.fileName + binding.downloadLayout.urlView.text = item.size.toLong().toMegabyteOrKilobyteString() toggleTopContent(false, mode == DownloadFragmentState.Mode.Normal) - itemView.download_layout.setSelectionInteractor(item, selectionHolder, downloadInteractor) - itemView.download_layout.changeSelected(item in selectionHolder.selectedItems) + binding.downloadLayout.setSelectionInteractor(item, selectionHolder, downloadInteractor) + binding.downloadLayout.changeSelected(item in selectionHolder.selectedItems) - itemView.favicon.setImageResource(item.getIcon()) + librarySiteItemBinding.favicon.setImageResource(item.getIcon()) - itemView.overflow_menu.setImageResource(R.drawable.ic_delete) + librarySiteItemBinding.overflowMenu.setImageResource(R.drawable.ic_delete) - itemView.overflow_menu.showAndEnable() + librarySiteItemBinding.overflowMenu.showAndEnable() - itemView.overflow_menu.setOnClickListener { + librarySiteItemBinding.overflowMenu.setOnClickListener { downloadInteractor.onDeleteSome(setOf(item)) } @@ -75,10 +77,10 @@ class DownloadsListItemViewHolder( showTopContent: Boolean, isNormalMode: Boolean ) { - itemView.delete_downloads_button.isVisible = showTopContent + binding.deleteDownloadsButton.isVisible = showTopContent if (showTopContent) { - itemView.delete_downloads_button.run { + binding.deleteDownloadsButton.run { if (isNormalMode) { isEnabled = true alpha = 1f @@ -98,7 +100,7 @@ class DownloadsListItemViewHolder( downloadInteractor.onDeleteSome(setOf(item)) } } - itemView.download_layout.attachMenu(downloadMenu.menuController) + binding.downloadLayout.attachMenu(downloadMenu.menuController) } companion object {