Skip to content

Commit

Permalink
home: clean up hacky overlay impl
Browse files Browse the repository at this point in the history
Should have a lesser likelihood of crashing outright, hopefully.
  • Loading branch information
OxygenCobalt committed Jan 5, 2024
1 parent c4143e7 commit cdd08e7
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 24 deletions.
21 changes: 20 additions & 1 deletion app/src/main/java/org/oxycblt/auxio/MainFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import android.view.WindowInsets
import androidx.activity.OnBackPressedCallback
import androidx.core.view.ViewCompat
import androidx.core.view.isInvisible
import androidx.core.view.isVisible
import androidx.core.view.updatePadding
import androidx.fragment.app.activityViewModels
import androidx.navigation.findNavController
Expand All @@ -33,7 +34,9 @@ import com.google.android.material.R as MR
import com.google.android.material.bottomsheet.BackportBottomSheetBehavior
import com.google.android.material.shape.MaterialShapeDrawable
import com.google.android.material.transition.MaterialFadeThrough
import com.leinardi.android.speeddial.SpeedDialOverlayLayout
import dagger.hilt.android.AndroidEntryPoint
import java.lang.reflect.Field
import kotlin.math.max
import kotlin.math.min
import org.oxycblt.auxio.databinding.FragmentMainBinding
Expand All @@ -56,6 +59,7 @@ import org.oxycblt.auxio.util.context
import org.oxycblt.auxio.util.coordinatorLayoutBehavior
import org.oxycblt.auxio.util.getAttrColorCompat
import org.oxycblt.auxio.util.getDimen
import org.oxycblt.auxio.util.lazyReflectedField
import org.oxycblt.auxio.util.logD
import org.oxycblt.auxio.util.navigateSafe
import org.oxycblt.auxio.util.systemBarInsetsCompat
Expand Down Expand Up @@ -153,6 +157,9 @@ class MainFragment :
}
}

binding.mainScrim.setOnClickListener { homeModel.setSpeedDialOpen(false) }
binding.sheetScrim.setOnClickListener { homeModel.setSpeedDialOpen(false) }

// --- VIEWMODEL SETUP ---
// This has to be done here instead of the playback panel to make sure that it's prioritized
// by StateFlow over any detail fragment.
Expand All @@ -161,7 +168,7 @@ class MainFragment :
collect(detailModel.toShow.flow, ::handleShow)
collectImmediately(detailModel.editedPlaylist, detailBackCallback::invalidateEnabled)
collectImmediately(homeModel.showOuter.flow, ::handleShowOuter)
collectImmediately(homeModel.speedDialOpen, speedDialBackCallback::invalidateEnabled)
collectImmediately(homeModel.speedDialOpen, ::handleSpeedDialState)
collectImmediately(listModel.selected, selectionBackCallback::invalidateEnabled)
collectImmediately(playbackModel.song, ::updateSong)
collectImmediately(playbackModel.openPanel.flow, ::handlePanel)
Expand Down Expand Up @@ -336,6 +343,13 @@ class MainFragment :
homeModel.showOuter.consume()
}

private fun handleSpeedDialState(open: Boolean) {
requireNotNull(speedDialBackCallback) { "SpeedDialBackPressedCallback was not available" }
.invalidateEnabled(open)
requireBinding().mainScrim.isVisible = open
requireBinding().sheetScrim.isVisible = open
}

private fun updateSong(song: Song?) {
if (song != null) {
tryShowSheets()
Expand Down Expand Up @@ -519,4 +533,9 @@ class MainFragment :
isEnabled = open
}
}

private companion object {
val SPEED_DIAL_OVERLAY_ANIMATION_DURATION_FIELD: Field by
lazyReflectedField(SpeedDialOverlayLayout::class, "mAnimationDuration")
}
}
34 changes: 11 additions & 23 deletions app/src/main/java/org/oxycblt/auxio/home/HomeFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

package org.oxycblt.auxio.home

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.LayoutInflater
import android.view.MenuItem
Expand Down Expand Up @@ -124,6 +125,7 @@ class HomeFragment :

override fun getSelectionToolbar(binding: FragmentHomeBinding) = binding.homeSelectionToolbar

@SuppressLint("ClickableViewAccessibility")
override fun onBindingCreated(binding: FragmentHomeBinding, savedInstanceState: Bundle?) {
super.onBindingCreated(binding, savedInstanceState)

Expand All @@ -146,23 +148,16 @@ class HomeFragment :

// --- UI SETUP ---

// Stock bottom sheet overlay won't work with our nested UI setup, have to replicate
// it ourselves.
binding.root.rootView.apply {
// Stock bottom sheet overlay won't work with our nested UI setup, have to replicate
// it ourselves.
findViewById<View>(R.id.main_scrim).setOnClickListener {
homeModel.setSpeedDialOpen(false)
}

findViewById<View>(R.id.main_scrim).setOnTouchListener { _, event ->
handleSpeedDialBoundaryTouch(event)
}

findViewById<View>(R.id.sheet_scrim).setOnClickListener {
homeModel.setSpeedDialOpen(false)
}

findViewById<View>(R.id.sheet_scrim).setOnTouchListener { _, event ->
handleSpeedDialBoundaryTouch(event)
post {
findViewById<View>(R.id.main_scrim).setOnTouchListener { _, event ->
handleSpeedDialBoundaryTouch(event)
}
findViewById<View>(R.id.sheet_scrim).setOnTouchListener { _, event ->
handleSpeedDialBoundaryTouch(event)
}
}
}

Expand Down Expand Up @@ -616,13 +611,6 @@ class HomeFragment :
private fun updateSpeedDial(open: Boolean) {
val binding = requireBinding()

binding.root.rootView.apply {
// Stock bottom sheet overlay won't work with our nested UI setup, have to replicate
// it ourselves.
findViewById<View>(R.id.main_scrim).isClickable = open
findViewById<View>(R.id.sheet_scrim).isClickable = open
}

if (open) {
binding.homeNewPlaylistFab.open(true)
} else {
Expand Down

0 comments on commit cdd08e7

Please sign in to comment.