From 31416c303305520ca9b8127a8aefeb76fb224f14 Mon Sep 17 00:00:00 2001 From: Vincent TE Date: Mon, 18 Mar 2024 13:39:40 +0100 Subject: [PATCH] Add extension to handle bottomsheet configuration --- .../ui/fileList/preview/PreviewPDFActivity.kt | 38 ++++++++++--------- .../fileList/preview/PreviewSliderFragment.kt | 27 +------------ .../com/infomaniak/drive/utils/Extensions.kt | 27 +++++++++++++ 3 files changed, 48 insertions(+), 44 deletions(-) diff --git a/app/src/main/java/com/infomaniak/drive/ui/fileList/preview/PreviewPDFActivity.kt b/app/src/main/java/com/infomaniak/drive/ui/fileList/preview/PreviewPDFActivity.kt index 01fe80b012..daee405f2b 100644 --- a/app/src/main/java/com/infomaniak/drive/ui/fileList/preview/PreviewPDFActivity.kt +++ b/app/src/main/java/com/infomaniak/drive/ui/fileList/preview/PreviewPDFActivity.kt @@ -54,7 +54,6 @@ class PreviewPDFActivity : AppCompatActivity(), ExternalFileInfoActionsView.OnIt private val navHostFragment by lazy { supportFragmentManager.findFragmentById(R.id.hostFragment) as NavHostFragment } private val binding: ActivityPreviewPdfBinding by lazy { ActivityPreviewPdfBinding.inflate(layoutInflater) } - private val bottomSheetBehavior: BottomSheetBehavior by lazy { BottomSheetBehavior.from(binding.bottomSheetFileInfos) } private val baseConstraintSet by lazy { ConstraintSet().apply { clone(binding.pdfContainer) @@ -78,7 +77,8 @@ class PreviewPDFActivity : AppCompatActivity(), ExternalFileInfoActionsView.OnIt private val fileName: String by lazy { fileNameAndSize?.first ?: "" } private val fileSize: Long by lazy { fileNameAndSize?.second ?: 0 } - + private var bottomSheetBehavior: BottomSheetBehavior? = null + private var isUiShown = false override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) @@ -87,11 +87,15 @@ class PreviewPDFActivity : AppCompatActivity(), ExternalFileInfoActionsView.OnIt navController.navigate(R.id.previewPDFFragment) - with(binding) { - backButton.setOnClickListener { finish() } - bottomSheetFileInfos.updateWithExternalFile(getFakeFile()) - bottomSheetFileInfos.init(this@PreviewPDFActivity) - } + binding.backButton.setOnClickListener { finish() } + initBottomSheet() + + } + + private fun initBottomSheet() = with(binding) { + bottomSheetBehavior = getBottomSheetFileBehavior(bottomSheetFileInfos, true) + bottomSheetFileInfos.updateWithExternalFile(getFakeFile()) + bottomSheetFileInfos.init(this@PreviewPDFActivity) } override fun onStart() { @@ -129,18 +133,16 @@ class PreviewPDFActivity : AppCompatActivity(), ExternalFileInfoActionsView.OnIt fun toggleFullscreen() = with(bottomSheetBehavior) { TransitionManager.beginDelayedTransition(binding.pdfContainer, transition) - val shouldHide = state != BottomSheetBehavior.STATE_HIDDEN - - isHideable = shouldHide - state = if (shouldHide) { - collapsedConstraintSet.applyTo(binding.pdfContainer) - BottomSheetBehavior.STATE_HIDDEN - } else { - baseConstraintSet.applyTo(binding.pdfContainer) - BottomSheetBehavior.STATE_COLLAPSED + this?.state?.let { + state = if (isUiShown) { + collapsedConstraintSet.applyTo(binding.pdfContainer) + BottomSheetBehavior.STATE_HIDDEN + } else { + baseConstraintSet.applyTo(binding.pdfContainer) + BottomSheetBehavior.STATE_COLLAPSED + } } - - toggleSystemBar(show = !shouldHide) + isUiShown = !isUiShown } // This is necessary to be able to use the same view details we have in kDrive (file name, file type and size) diff --git a/app/src/main/java/com/infomaniak/drive/ui/fileList/preview/PreviewSliderFragment.kt b/app/src/main/java/com/infomaniak/drive/ui/fileList/preview/PreviewSliderFragment.kt index c1473d96cb..6c5bad55a2 100644 --- a/app/src/main/java/com/infomaniak/drive/ui/fileList/preview/PreviewSliderFragment.kt +++ b/app/src/main/java/com/infomaniak/drive/ui/fileList/preview/PreviewSliderFragment.kt @@ -172,7 +172,7 @@ class PreviewSliderFragment : Fragment(), FileInfoActionsView.OnItemClickListene } } - configureBottomSheetFileInfo() + requireActivity().getBottomSheetFileBehavior(binding.bottomSheetFileInfos, !navigationArgs.hideActions) setupWindowInsetsListener() } @@ -247,31 +247,6 @@ class PreviewSliderFragment : Fragment(), FileInfoActionsView.OnItemClickListene } } - private fun configureBottomSheetFileInfo() { - activity?.setColorNavigationBar(true) - bottomSheetBehavior = BottomSheetBehavior.from(binding.bottomSheetFileInfos) - bottomSheetBehavior.apply { - isHideable = true - isDraggable = !navigationArgs.hideActions - addBottomSheetCallback(object : BottomSheetBehavior.BottomSheetCallback() { - override fun onStateChanged(bottomSheet: View, newState: Int) { - when (bottomSheetBehavior.state) { - BottomSheetBehavior.STATE_HIDDEN -> { - activity?.window?.navigationBarColor = - ContextCompat.getColor(requireContext(), R.color.previewBackgroundTransparent) - activity?.window?.lightNavigationBar(false) - } - else -> { - activity?.setColorNavigationBar(true) - } - } - } - - override fun onSlide(bottomSheet: View, slideOffset: Float) = Unit - }) - } - } - private fun setupWindowInsetsListener() = with(binding) { ViewCompat.setOnApplyWindowInsetsListener(root) { _, windowInsets -> with(windowInsets.getInsets(WindowInsetsCompat.Type.systemBars())) { diff --git a/app/src/main/java/com/infomaniak/drive/utils/Extensions.kt b/app/src/main/java/com/infomaniak/drive/utils/Extensions.kt index dbc5360c78..d3dab07280 100644 --- a/app/src/main/java/com/infomaniak/drive/utils/Extensions.kt +++ b/app/src/main/java/com/infomaniak/drive/utils/Extensions.kt @@ -45,6 +45,7 @@ import androidx.navigation.fragment.findNavController import androidx.work.OneTimeWorkRequest import androidx.work.OutOfQuotaPolicy import coil.load +import com.google.android.material.bottomsheet.BottomSheetBehavior import com.google.android.material.card.MaterialCardView import com.google.android.material.shape.CornerFamily import com.google.android.material.textfield.MaterialAutoCompleteTextView @@ -163,6 +164,32 @@ fun Activity.setColorNavigationBar(appBar: Boolean = false) = with(window) { } } +fun Activity.getBottomSheetFileBehavior(bottomSheet: View, isDraggable: Boolean): BottomSheetBehavior { + setColorNavigationBar(true) + val bottomSheetBehavior = BottomSheetBehavior.from(bottomSheet) + bottomSheetBehavior.apply { + isHideable = true + this.isDraggable = isDraggable + addBottomSheetCallback(object : BottomSheetBehavior.BottomSheetCallback() { + override fun onStateChanged(bottomSheet: View, newState: Int) { + when (bottomSheetBehavior.state) { + BottomSheetBehavior.STATE_HIDDEN -> { + window?.navigationBarColor = + ContextCompat.getColor(this@getBottomSheetFileBehavior, R.color.previewBackgroundTransparent) + window?.lightNavigationBar(false) + } + else -> { + setColorNavigationBar(true) + } + } + } + + override fun onSlide(bottomSheet: View, slideOffset: Float) = Unit + }) + } + return bottomSheetBehavior +} + fun String.isValidUrl(): Boolean = Patterns.WEB_URL.matcher(this).matches() fun ItemUserBinding.setUserView(user: User, showChevron: Boolean = true, onItemClicked: (user: User) -> Unit) {