mirror of
				https://github.com/commons-app/apps-android-commons.git
				synced 2025-10-31 14:53:59 +01:00 
			
		
		
		
	Optimize the Layout ViewBinding
This commit is contained in:
		
							parent
							
								
									9393dda9a4
								
							
						
					
					
						commit
						115e5d99ef
					
				
					 2 changed files with 17 additions and 29 deletions
				
			
		|  | @ -2,24 +2,15 @@ package fr.free.nrw.commons.explore.media | ||||||
| 
 | 
 | ||||||
| import android.content.Context | import android.content.Context | ||||||
| import android.os.Bundle | import android.os.Bundle | ||||||
| import android.view.LayoutInflater |  | ||||||
| import android.view.View | import android.view.View | ||||||
| import android.view.ViewGroup |  | ||||||
| import fr.free.nrw.commons.Media | import fr.free.nrw.commons.Media | ||||||
| import fr.free.nrw.commons.R | import fr.free.nrw.commons.R | ||||||
| import fr.free.nrw.commons.category.CategoryImagesCallback | 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.explore.paging.BasePagingFragment | ||||||
| import fr.free.nrw.commons.media.MediaDetailPagerFragment.MediaDetailProvider | import fr.free.nrw.commons.media.MediaDetailPagerFragment.MediaDetailProvider | ||||||
| 
 | 
 | ||||||
| abstract class PageableMediaFragment : BasePagingFragment<Media>(), MediaDetailProvider { | abstract class PageableMediaFragment : BasePagingFragment<Media>(), MediaDetailProvider { | ||||||
| 
 | 
 | ||||||
|     /** |  | ||||||
|      * ViewBinding |  | ||||||
|      */ |  | ||||||
|     private var _binding: FragmentSearchPaginatedBinding? = null |  | ||||||
|     private val binding get() = _binding!! |  | ||||||
| 
 |  | ||||||
|     override val pagedListAdapter by lazy { |     override val pagedListAdapter by lazy { | ||||||
|         PagedMediaAdapter(categoryImagesCallback::onMediaClicked) |         PagedMediaAdapter(categoryImagesCallback::onMediaClicked) | ||||||
|     } |     } | ||||||
|  | @ -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 = |     private val simpleDataObserver = | ||||||
|         SimpleDataObserver { categoryImagesCallback.viewPagerNotifyDataSetChanged() } |         SimpleDataObserver { categoryImagesCallback.viewPagerNotifyDataSetChanged() } | ||||||
| 
 | 
 | ||||||
|  | @ -57,7 +39,6 @@ abstract class PageableMediaFragment : BasePagingFragment<Media>(), MediaDetailP | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     override fun onDestroyView() { |     override fun onDestroyView() { | ||||||
|         _binding = null |  | ||||||
|         super.onDestroyView() |         super.onDestroyView() | ||||||
|         pagedListAdapter.unregisterAdapterDataObserver(simpleDataObserver) |         pagedListAdapter.unregisterAdapterDataObserver(simpleDataObserver) | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  | @ -29,12 +29,19 @@ abstract class BasePagingFragment<T> : CommonsDaggerSupportFragment(), | ||||||
|     private val mergeAdapter by lazy { MergeAdapter(pagedListAdapter, loadingAdapter) } |     private val mergeAdapter by lazy { MergeAdapter(pagedListAdapter, loadingAdapter) } | ||||||
|     private var searchResults: LiveData<PagedList<T>>? = null |     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?) { |     override fun onViewCreated(view: View, savedInstanceState: Bundle?) { | ||||||
|         super.onViewCreated(view, savedInstanceState) |         super.onViewCreated(view, savedInstanceState) | ||||||
|         val binding = FragmentSearchPaginatedBinding.bind(view) |  | ||||||
|         this.binding = binding |  | ||||||
| 
 | 
 | ||||||
|         binding.paginatedSearchResultsList.apply { |         binding.paginatedSearchResultsList.apply { | ||||||
|             layoutManager = GridLayoutManager(context, if (isPortrait) 1 else 2) |             layoutManager = GridLayoutManager(context, if (isPortrait) 1 else 2) | ||||||
|  | @ -51,7 +58,7 @@ abstract class BasePagingFragment<T> : CommonsDaggerSupportFragment(), | ||||||
|      */ |      */ | ||||||
|     override fun onConfigurationChanged(newConfig: Configuration) { |     override fun onConfigurationChanged(newConfig: Configuration) { | ||||||
|         super.onConfigurationChanged(newConfig) |         super.onConfigurationChanged(newConfig) | ||||||
|         binding!!.paginatedSearchResultsList.apply { |         binding.paginatedSearchResultsList.apply { | ||||||
|             layoutManager = GridLayoutManager(context, if (isPortrait) 1 else 2) |             layoutManager = GridLayoutManager(context, if (isPortrait) 1 else 2) | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
|  | @ -75,15 +82,15 @@ abstract class BasePagingFragment<T> : CommonsDaggerSupportFragment(), | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     override fun hideInitialLoadProgress() { |     override fun hideInitialLoadProgress() { | ||||||
|         binding!!.paginatedSearchInitialLoadProgress.visibility = GONE |         binding.paginatedSearchInitialLoadProgress.visibility = GONE | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     override fun showInitialLoadInProgress() { |     override fun showInitialLoadInProgress() { | ||||||
|         binding!!.paginatedSearchInitialLoadProgress.visibility = VISIBLE |         binding.paginatedSearchInitialLoadProgress.visibility = VISIBLE | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     override fun showSnackbar() { |     override fun showSnackbar() { | ||||||
|         ViewUtil.showShortSnackbar(binding!!.paginatedSearchResultsList, errorTextId) |         ViewUtil.showShortSnackbar(binding.paginatedSearchResultsList, errorTextId) | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     fun onQueryUpdated(query: String) { |     fun onQueryUpdated(query: String) { | ||||||
|  | @ -91,14 +98,14 @@ abstract class BasePagingFragment<T> : CommonsDaggerSupportFragment(), | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     override fun showEmptyText(query: String) { |     override fun showEmptyText(query: String) { | ||||||
|         binding!!.contentNotFound.text = getEmptyText(query) |         binding.contentNotFound.text = getEmptyText(query) | ||||||
|         binding!!.contentNotFound.visibility = VISIBLE |         binding.contentNotFound.visibility = VISIBLE | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     abstract fun getEmptyText(query: String): String |     abstract fun getEmptyText(query: String): String | ||||||
| 
 | 
 | ||||||
|     override fun hideEmptyText() { |     override fun hideEmptyText() { | ||||||
|         binding!!.contentNotFound.visibility = GONE |         binding.contentNotFound.visibility = GONE | ||||||
|     } |     } | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Rajat Sarangal
						Rajat Sarangal