Skip to content

Commit

Permalink
Optimize the Layout ViewBinding (#5830)
Browse files Browse the repository at this point in the history
  • Loading branch information
thesarangal authored Sep 18, 2024
1 parent 3e915f9 commit f751ab4
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,15 @@ package fr.free.nrw.commons.explore.media

import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import fr.free.nrw.commons.Media
import fr.free.nrw.commons.R
import fr.free.nrw.commons.category.CategoryImagesCallback
import fr.free.nrw.commons.databinding.FragmentSearchPaginatedBinding
import fr.free.nrw.commons.explore.paging.BasePagingFragment
import fr.free.nrw.commons.media.MediaDetailPagerFragment.MediaDetailProvider

abstract class PageableMediaFragment : BasePagingFragment<Media>(), MediaDetailProvider {

/**
* ViewBinding
*/
private var _binding: FragmentSearchPaginatedBinding? = null
private val binding get() = _binding!!

override val pagedListAdapter by lazy {
PagedMediaAdapter(categoryImagesCallback::onMediaClicked)
}
Expand All @@ -39,15 +30,6 @@ abstract class PageableMediaFragment : BasePagingFragment<Media>(), MediaDetailP
}
}

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
_binding = FragmentSearchPaginatedBinding.inflate(inflater, container, false)
return binding.root
}

private val simpleDataObserver =
SimpleDataObserver { categoryImagesCallback.viewPagerNotifyDataSetChanged() }

Expand All @@ -57,7 +39,6 @@ abstract class PageableMediaFragment : BasePagingFragment<Media>(), MediaDetailP
}

override fun onDestroyView() {
_binding = null
super.onDestroyView()
pagedListAdapter.unregisterAdapterDataObserver(simpleDataObserver)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,19 @@ abstract class BasePagingFragment<T> : CommonsDaggerSupportFragment(),
private val mergeAdapter by lazy { MergeAdapter(pagedListAdapter, loadingAdapter) }
private var searchResults: LiveData<PagedList<T>>? = null

private var binding : FragmentSearchPaginatedBinding? = null
protected lateinit var binding : FragmentSearchPaginatedBinding

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
binding = FragmentSearchPaginatedBinding.inflate(inflater, container, false)
return binding.root
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val binding = FragmentSearchPaginatedBinding.bind(view)
this.binding = binding

binding.paginatedSearchResultsList.apply {
layoutManager = GridLayoutManager(context, if (isPortrait) 1 else 2)
Expand All @@ -51,7 +58,7 @@ abstract class BasePagingFragment<T> : CommonsDaggerSupportFragment(),
*/
override fun onConfigurationChanged(newConfig: Configuration) {
super.onConfigurationChanged(newConfig)
binding!!.paginatedSearchResultsList.apply {
binding.paginatedSearchResultsList.apply {
layoutManager = GridLayoutManager(context, if (isPortrait) 1 else 2)
}
}
Expand All @@ -75,30 +82,30 @@ abstract class BasePagingFragment<T> : CommonsDaggerSupportFragment(),
}

override fun hideInitialLoadProgress() {
binding!!.paginatedSearchInitialLoadProgress.visibility = GONE
binding.paginatedSearchInitialLoadProgress.visibility = GONE
}

override fun showInitialLoadInProgress() {
binding!!.paginatedSearchInitialLoadProgress.visibility = VISIBLE
binding.paginatedSearchInitialLoadProgress.visibility = VISIBLE
}

override fun showSnackbar() {
ViewUtil.showShortSnackbar(binding!!.paginatedSearchResultsList, errorTextId)
ViewUtil.showShortSnackbar(binding.paginatedSearchResultsList, errorTextId)
}

fun onQueryUpdated(query: String) {
injectedPresenter.onQueryUpdated(query)
}

override fun showEmptyText(query: String) {
binding!!.contentNotFound.text = getEmptyText(query)
binding!!.contentNotFound.visibility = VISIBLE
binding.contentNotFound.text = getEmptyText(query)
binding.contentNotFound.visibility = VISIBLE
}

abstract fun getEmptyText(query: String): String

override fun hideEmptyText() {
binding!!.contentNotFound.visibility = GONE
binding.contentNotFound.visibility = GONE
}
}

Expand Down

0 comments on commit f751ab4

Please sign in to comment.