For #17917: Use View binding in downloads screen

upstream-sync
codrut.topliceanu 3 years ago committed by mergify[bot]
parent 55ccfda759
commit 003a2ce81a

@ -12,10 +12,9 @@ import android.widget.ImageView
import android.widget.TextView import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.view.isVisible 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.MenuController
import mozilla.components.concept.menu.Orientation 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.components
import org.mozilla.fenix.ext.increaseTapArea import org.mozilla.fenix.ext.increaseTapArea
import org.mozilla.fenix.ext.loadIntoView import org.mozilla.fenix.ext.loadIntoView
@ -29,40 +28,45 @@ class LibrarySiteItemView @JvmOverloads constructor(
defStyleRes: Int = 0 defStyleRes: Int = 0
) : ConstraintLayout(context, attrs, defStyleAttr, defStyleRes) { ) : 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 { 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. * Change visibility of parts of this view based on what type of item is being represented.
*/ */
fun displayAs(mode: ItemType) { fun displayAs(mode: ItemType) {
url.isVisible = mode == ItemType.SITE urlView.isVisible = mode == ItemType.SITE
} }
/** /**
* Changes the icon to show a check mark if [isSelected] * Changes the icon to show a check mark if [isSelected]
*/ */
fun changeSelected(isSelected: Boolean) { fun changeSelected(isSelected: Boolean) {
icon.displayedChild = if (isSelected) 1 else 0 binding.icon.displayedChild = if (isSelected) 1 else 0
} }
fun loadFavicon(url: String) { fun loadFavicon(url: String) {
context.components.core.icons.loadIntoView(favicon, url) context.components.core.icons.loadIntoView(iconView, url)
} }
fun attachMenu(menuController: MenuController) { fun attachMenu(menuController: MenuController) {
overflow_menu.setOnClickListener { overflowView.setOnClickListener {
menuController.show( menuController.show(
anchor = it, anchor = it,
orientation = Orientation.DOWN orientation = Orientation.DOWN
@ -89,7 +93,7 @@ class LibrarySiteItemView @JvmOverloads constructor(
} }
} }
favicon.setOnClickListener { iconView.setOnClickListener {
if (item in holder.selectedItems) { if (item in holder.selectedItems) {
interactor.deselect(item) interactor.deselect(item)
} else { } else {

@ -16,7 +16,6 @@ import android.view.ViewGroup
import androidx.annotation.VisibleForTesting import androidx.annotation.VisibleForTesting
import androidx.appcompat.app.AlertDialog import androidx.appcompat.app.AlertDialog
import androidx.lifecycle.lifecycleScope import androidx.lifecycle.lifecycleScope
import kotlinx.android.synthetic.main.fragment_downloads.view.*
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Dispatchers.IO 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.StoreProvider
import org.mozilla.fenix.components.metrics.Event import org.mozilla.fenix.components.metrics.Event
import org.mozilla.fenix.components.metrics.MetricController import org.mozilla.fenix.components.metrics.MetricController
import org.mozilla.fenix.databinding.FragmentDownloadsBinding
import org.mozilla.fenix.ext.components import org.mozilla.fenix.ext.components
import org.mozilla.fenix.ext.filterNotExistsOnDisk import org.mozilla.fenix.ext.filterNotExistsOnDisk
import org.mozilla.fenix.ext.requireComponents import org.mozilla.fenix.ext.requireComponents
@ -53,12 +53,15 @@ class DownloadFragment : LibraryPageFragment<DownloadItem>(), UserInteractionHan
private var pendingDownloadDeletionJob: (suspend () -> Unit)? = null private var pendingDownloadDeletionJob: (suspend () -> Unit)? = null
private lateinit var downloadsUseCases: DownloadsUseCases private lateinit var downloadsUseCases: DownloadsUseCases
private var _binding: FragmentDownloadsBinding? = null
private val binding get() = _binding!!
override fun onCreateView( override fun onCreateView(
inflater: LayoutInflater, inflater: LayoutInflater,
container: ViewGroup?, container: ViewGroup?,
savedInstanceState: Bundle? savedInstanceState: Bundle?
): View? { ): View {
val view = inflater.inflate(R.layout.fragment_downloads, container, false) _binding = FragmentDownloadsBinding.inflate(inflater, container, false)
val items = provideDownloads(requireComponents.core.store.state) val items = provideDownloads(requireComponents.core.store.state)
downloadsUseCases = requireContext().components.useCases.downloadUseCases downloadsUseCases = requireContext().components.useCases.downloadUseCases
@ -83,9 +86,14 @@ class DownloadFragment : LibraryPageFragment<DownloadItem>(), UserInteractionHan
downloadInteractor = DownloadInteractor( downloadInteractor = DownloadInteractor(
downloadController downloadController
) )
downloadView = DownloadView(view.downloadsLayout, downloadInteractor) downloadView = DownloadView(binding.downloadsLayout, downloadInteractor)
return binding.root
}
return view override fun onDestroyView() {
super.onDestroyView()
_binding = null
} }
/** /**

@ -5,17 +5,15 @@
package org.mozilla.fenix.library.downloads package org.mozilla.fenix.library.downloads
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import androidx.core.view.isVisible import androidx.core.view.isVisible
import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.SimpleItemAnimator import androidx.recyclerview.widget.SimpleItemAnimator
import kotlinx.android.synthetic.main.component_downloads.* import kotlinx.android.synthetic.main.component_downloads.*
import kotlinx.android.synthetic.main.component_downloads.view.* 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 mozilla.components.support.base.feature.UserInteractionHandler
import org.mozilla.fenix.R import org.mozilla.fenix.R
import org.mozilla.fenix.databinding.ComponentDownloadsBinding
import org.mozilla.fenix.library.LibraryPageView import org.mozilla.fenix.library.LibraryPageView
import org.mozilla.fenix.selection.SelectionInteractor import org.mozilla.fenix.selection.SelectionInteractor
@ -55,8 +53,11 @@ class DownloadView(
val interactor: DownloadInteractor val interactor: DownloadInteractor
) : LibraryPageView(container), UserInteractionHandler { ) : LibraryPageView(container), UserInteractionHandler {
val view: View = LayoutInflater.from(container.context) val binding = ComponentDownloadsBinding.inflate(
.inflate(R.layout.component_downloads, container, true) LayoutInflater.from(container.context),
container,
true
)
var mode: DownloadFragmentState.Mode = DownloadFragmentState.Mode.Normal var mode: DownloadFragmentState.Mode = DownloadFragmentState.Mode.Normal
private set private set
@ -65,7 +66,7 @@ class DownloadView(
private val layoutManager = LinearLayoutManager(container.context) private val layoutManager = LinearLayoutManager(container.context)
init { init {
view.download_list.apply { binding.downloadList.apply {
layoutManager = this@DownloadView.layoutManager layoutManager = this@DownloadView.layoutManager
adapter = downloadAdapter adapter = downloadAdapter
(itemAnimator as SimpleItemAnimator).supportsChangeAnimations = false (itemAnimator as SimpleItemAnimator).supportsChangeAnimations = false
@ -75,8 +76,8 @@ class DownloadView(
fun update(state: DownloadFragmentState) { fun update(state: DownloadFragmentState) {
val oldMode = mode val oldMode = mode
view.progress_bar.isVisible = state.isDeletingItems binding.progressBar.isVisible = state.isDeletingItems
view.swipe_refresh.isEnabled = false binding.swipeRefresh.isEnabled = false
mode = state.mode mode = state.mode
downloadAdapter.updatePendingDeletionIds(state.pendingDeletionIds) downloadAdapter.updatePendingDeletionIds(state.pendingDeletionIds)

@ -7,10 +7,10 @@ package org.mozilla.fenix.library.downloads.viewholders
import android.view.View import android.view.View
import androidx.core.view.isVisible import androidx.core.view.isVisible
import androidx.recyclerview.widget.RecyclerView 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 mozilla.components.feature.downloads.toMegabyteOrKilobyteString
import org.mozilla.fenix.R 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.selection.SelectionHolder
import org.mozilla.fenix.library.downloads.DownloadInteractor import org.mozilla.fenix.library.downloads.DownloadInteractor
import org.mozilla.fenix.library.downloads.DownloadItem import org.mozilla.fenix.library.downloads.DownloadItem
@ -26,11 +26,13 @@ class DownloadsListItemViewHolder(
) : RecyclerView.ViewHolder(view) { ) : RecyclerView.ViewHolder(view) {
private var item: DownloadItem? = null private var item: DownloadItem? = null
private val binding = DownloadListItemBinding.bind(view)
private val librarySiteItemBinding = LibrarySiteItemBinding.bind(binding.downloadLayout)
init { init {
setupMenu() setupMenu()
itemView.delete_downloads_button.setOnClickListener { binding.deleteDownloadsButton.setOnClickListener {
val selected = selectionHolder.selectedItems val selected = selectionHolder.selectedItems
if (selected.isEmpty()) { if (selected.isEmpty()) {
downloadInteractor.onDeleteAll() downloadInteractor.onDeleteAll()
@ -45,26 +47,26 @@ class DownloadsListItemViewHolder(
mode: DownloadFragmentState.Mode, mode: DownloadFragmentState.Mode,
isPendingDeletion: Boolean = false isPendingDeletion: Boolean = false
) { ) {
itemView.download_layout.visibility = if (isPendingDeletion) { binding.downloadLayout.visibility = if (isPendingDeletion) {
View.GONE View.GONE
} else { } else {
View.VISIBLE View.VISIBLE
} }
itemView.download_layout.titleView.text = item.fileName binding.downloadLayout.titleView.text = item.fileName
itemView.download_layout.urlView.text = item.size.toLong().toMegabyteOrKilobyteString() binding.downloadLayout.urlView.text = item.size.toLong().toMegabyteOrKilobyteString()
toggleTopContent(false, mode == DownloadFragmentState.Mode.Normal) toggleTopContent(false, mode == DownloadFragmentState.Mode.Normal)
itemView.download_layout.setSelectionInteractor(item, selectionHolder, downloadInteractor) binding.downloadLayout.setSelectionInteractor(item, selectionHolder, downloadInteractor)
itemView.download_layout.changeSelected(item in selectionHolder.selectedItems) 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)) downloadInteractor.onDeleteSome(setOf(item))
} }
@ -75,10 +77,10 @@ class DownloadsListItemViewHolder(
showTopContent: Boolean, showTopContent: Boolean,
isNormalMode: Boolean isNormalMode: Boolean
) { ) {
itemView.delete_downloads_button.isVisible = showTopContent binding.deleteDownloadsButton.isVisible = showTopContent
if (showTopContent) { if (showTopContent) {
itemView.delete_downloads_button.run { binding.deleteDownloadsButton.run {
if (isNormalMode) { if (isNormalMode) {
isEnabled = true isEnabled = true
alpha = 1f alpha = 1f
@ -98,7 +100,7 @@ class DownloadsListItemViewHolder(
downloadInteractor.onDeleteSome(setOf(item)) downloadInteractor.onDeleteSome(setOf(item))
} }
} }
itemView.download_layout.attachMenu(downloadMenu.menuController) binding.downloadLayout.attachMenu(downloadMenu.menuController)
} }
companion object { companion object {

Loading…
Cancel
Save