Skip to content

Commit

Permalink
Add extension to handle bottomsheet configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
tevincent committed Mar 18, 2024
1 parent f6d1812 commit 31416c3
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<View> by lazy { BottomSheetBehavior.from(binding.bottomSheetFileInfos) }
private val baseConstraintSet by lazy {
ConstraintSet().apply {
clone(binding.pdfContainer)
Expand All @@ -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<View>? = null
private var isUiShown = false

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand All @@ -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() {
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ class PreviewSliderFragment : Fragment(), FileInfoActionsView.OnItemClickListene
}
}

configureBottomSheetFileInfo()
requireActivity().getBottomSheetFileBehavior(binding.bottomSheetFileInfos, !navigationArgs.hideActions)
setupWindowInsetsListener()
}

Expand Down Expand Up @@ -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())) {
Expand Down
27 changes: 27 additions & 0 deletions app/src/main/java/com/infomaniak/drive/utils/Extensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -163,6 +164,32 @@ fun Activity.setColorNavigationBar(appBar: Boolean = false) = with(window) {
}
}

fun Activity.getBottomSheetFileBehavior(bottomSheet: View, isDraggable: Boolean): BottomSheetBehavior<View> {
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) {
Expand Down

0 comments on commit 31416c3

Please sign in to comment.