Skip to content

Commit

Permalink
Merge pull request #1070 from Infomaniak/replace-kx-vb-gallery
Browse files Browse the repository at this point in the history
Replace kotlin extension with view bindings in GalleryFragment
  • Loading branch information
FabianDevel authored Oct 23, 2023
2 parents 77b63bb + 2ef9dab commit 6a7d351
Showing 1 changed file with 23 additions and 15 deletions.
38 changes: 23 additions & 15 deletions app/src/main/java/com/infomaniak/drive/ui/menu/GalleryFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import com.infomaniak.drive.R
import com.infomaniak.drive.data.models.BulkOperationType
import com.infomaniak.drive.data.models.File
import com.infomaniak.drive.data.services.DownloadWorker
import com.infomaniak.drive.databinding.FragmentGalleryBinding
import com.infomaniak.drive.databinding.FragmentMenuGalleryBinding
import com.infomaniak.drive.databinding.MultiSelectLayoutBinding
import com.infomaniak.drive.ui.fileList.multiSelect.MultiSelectFragment
Expand All @@ -45,18 +46,18 @@ import com.infomaniak.drive.utils.getAdjustedColumnNumber
import com.infomaniak.drive.views.NoItemsLayoutView
import com.infomaniak.lib.core.utils.Utils.createRefreshTimer
import com.infomaniak.lib.core.utils.setPagination
import kotlinx.android.synthetic.main.fragment_gallery.galleryFastScroller
import kotlinx.android.synthetic.main.fragment_gallery.galleryRecyclerView
import kotlinx.android.synthetic.main.fragment_gallery.noGalleryLayout
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking

class GalleryFragment : MultiSelectFragment(MATOMO_CATEGORY), NoItemsLayoutView.INoItemsLayoutView {

private var _binding: FragmentGalleryBinding? = null
private val binding get() = _binding!! // This property is only valid between onCreateView and onDestroyView

override val noItemsIcon = R.drawable.ic_images
override val noItemsTitle = R.string.picturesNoFile
override val noItemsInitialListView: View by lazy { galleryFastScroller }
override val noItemsInitialListView: View by lazy { binding.galleryFastScroller }

private val galleryViewModel: GalleryViewModel by viewModels()
private lateinit var galleryAdapter: GalleryAdapter
Expand All @@ -70,8 +71,8 @@ class GalleryFragment : MultiSelectFragment(MATOMO_CATEGORY), NoItemsLayoutView.
createRefreshTimer { menuGalleryBinding?.swipeRefreshLayout?.isRefreshing = true }
}

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.fragment_gallery, container, false)
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
return FragmentGalleryBinding.inflate(inflater, container, false).also { _binding = it }.root
}

override fun initMultiSelectLayout(): MultiSelectLayoutBinding? = menuGalleryBinding?.multiSelectLayout
Expand Down Expand Up @@ -111,10 +112,12 @@ class GalleryFragment : MultiSelectFragment(MATOMO_CATEGORY), NoItemsLayoutView.

if (isCurrentlyInGallery) multiSelectManager.isMultiSelectAuthorized = true

galleryRecyclerView.adapter = galleryAdapter
configGalleryLayoutManager()
with(binding) {
galleryRecyclerView.adapter = galleryAdapter
configGalleryLayoutManager()

noGalleryLayout.iNoItemsLayoutView = this
noGalleryLayout.iNoItemsLayoutView = this@GalleryFragment
}

mainViewModel.observeDownloadOffline(requireContext()).observe(viewLifecycleOwner) { workInfoList ->
if (workInfoList.isEmpty()) return@observe
Expand All @@ -141,12 +144,12 @@ class GalleryFragment : MultiSelectFragment(MATOMO_CATEGORY), NoItemsLayoutView.
it?.let { (galleryFiles, isComplete) ->
stateRestorationPolicy = RecyclerView.Adapter.StateRestorationPolicy.PREVENT_WHEN_EMPTY
val galleryList = formatList(galleryFiles)
galleryRecyclerView.post { addAll(galleryList) }
binding.galleryRecyclerView.post { addAll(galleryList) }
this.isComplete = isComplete
noGalleryLayout.toggleVisibility(galleryList.isEmpty())
binding.noGalleryLayout.toggleVisibility(galleryList.isEmpty())
} ?: run {
isComplete = true
noGalleryLayout.toggleVisibility(
binding.noGalleryLayout.toggleVisibility(
noNetwork = mainViewModel.isInternetAvailable.value == false,
isVisible = galleryList.isEmpty(),
showRefreshButton = true,
Expand All @@ -160,7 +163,7 @@ class GalleryFragment : MultiSelectFragment(MATOMO_CATEGORY), NoItemsLayoutView.
}

private fun setupPagination() {
galleryRecyclerView.apply {
binding.galleryRecyclerView.apply {
paginationListener?.let(::removeOnScrollListener)
paginationListener = setPagination(
whenLoadMoreIsPossible = {
Expand All @@ -181,6 +184,11 @@ class GalleryFragment : MultiSelectFragment(MATOMO_CATEGORY), NoItemsLayoutView.
configGalleryLayoutManager()
}

override fun onDestroyView() {
super.onDestroyView()
_binding = null
}

private fun configGalleryLayoutManager() {
val numGalleryColumns = requireActivity().getAdjustedColumnNumber(150, minColumns = 3, maxColumns = 15)

Expand All @@ -195,7 +203,7 @@ class GalleryFragment : MultiSelectFragment(MATOMO_CATEGORY), NoItemsLayoutView.
}
}

galleryRecyclerView?.layoutManager = gridLayoutManager
_binding?.galleryRecyclerView?.layoutManager = gridLayoutManager
}

private fun loadMoreGallery(driveId: Int, forceDownload: Boolean = false) {
Expand Down Expand Up @@ -296,7 +304,7 @@ class GalleryFragment : MultiSelectFragment(MATOMO_CATEGORY), NoItemsLayoutView.
}

fun setScrollbarTrackOffset(offset: Int) {
galleryFastScroller?.trackMarginEnd = offset
_binding?.galleryFastScroller?.trackMarginEnd = offset
}

companion object {
Expand Down

0 comments on commit 6a7d351

Please sign in to comment.