Skip to content

Commit 28b9137

Browse files
committed
fix some bugs
1 parent b1d198d commit 28b9137

File tree

1 file changed

+31
-61
lines changed

1 file changed

+31
-61
lines changed

app/src/main/java/org/akanework/gramophone/MainActivity.kt

Lines changed: 31 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ class MainActivity : AppCompatActivity(), Player.Listener {
113113
)
114114
if (duration != null && !isUserTracking) {
115115
bottomSheetFullSlider.value =
116-
instance.currentPosition.toFloat() / duration.toFloat()
116+
(instance.currentPosition.toFloat() / duration.toFloat()).coerceAtMost(1f)
117117
bottomSheetFullPosition.text = position
118118
}
119119
}
@@ -143,11 +143,7 @@ class MainActivity : AppCompatActivity(), Player.Listener {
143143
}
144144
if (standardBottomSheetBehavior.state != BottomSheetBehavior.STATE_EXPANDED) {
145145
standardBottomSheetBehavior.state = BottomSheetBehavior.STATE_COLLAPSED
146-
previewPlayer.alpha = 1f
147-
previewPlayer.visibility = View.VISIBLE
148-
Handler(Looper.getMainLooper()).postDelayed({
149-
standardBottomSheetBehavior.isHideable = false
150-
}, 200)
146+
standardBottomSheetBehavior.isHideable = false
151147
}
152148
},
153149
200,
@@ -175,9 +171,7 @@ class MainActivity : AppCompatActivity(), Player.Listener {
175171
if (!standardBottomSheetBehavior.isHideable) {
176172
standardBottomSheetBehavior.isHideable = true
177173
}
178-
Handler(Looper.getMainLooper()).postDelayed({
179-
standardBottomSheetBehavior.state = BottomSheetBehavior.STATE_HIDDEN
180-
}, 200)
174+
standardBottomSheetBehavior.state = BottomSheetBehavior.STATE_HIDDEN
181175
}
182176
}
183177

@@ -327,8 +321,6 @@ class MainActivity : AppCompatActivity(), Player.Listener {
327321
standardBottomSheet.setOnClickListener {
328322
if (standardBottomSheetBehavior.state == BottomSheetBehavior.STATE_COLLAPSED) {
329323
standardBottomSheetBehavior.state = BottomSheetBehavior.STATE_EXPANDED
330-
fullPlayer.visibility = View.VISIBLE
331-
previewPlayer.visibility = View.GONE
332324
}
333325
}
334326

@@ -401,13 +393,10 @@ class MainActivity : AppCompatActivity(), Player.Listener {
401393

402394
bottomSheetFullSlideUpButton.setOnClickListener {
403395
standardBottomSheetBehavior.state = BottomSheetBehavior.STATE_COLLAPSED
404-
Handler(Looper.getMainLooper()).postDelayed(
405-
{
406-
fullPlayer.visibility = View.GONE
407-
previewPlayer.visibility = View.VISIBLE
408-
},
409-
200,
410-
)
396+
}
397+
398+
val bottomSheetBackCallback = onBackPressedDispatcher.addCallback(this) {
399+
standardBottomSheetBehavior.state = BottomSheetBehavior.STATE_COLLAPSED
411400
}
412401

413402
standardBottomSheetBehavior.addBottomSheetCallback(
@@ -416,16 +405,31 @@ class MainActivity : AppCompatActivity(), Player.Listener {
416405
bottomSheet: View,
417406
newState: Int,
418407
) {
419-
if (newState == BottomSheetBehavior.STATE_COLLAPSED
420-
&& previewPlayer.isVisible) {
421-
fullPlayer.visibility = View.GONE
422-
previewPlayer.alpha = 1f
423-
} else if (newState == BottomSheetBehavior.STATE_DRAGGING) {
424-
fullPlayer.visibility = View.VISIBLE
425-
previewPlayer.visibility = View.VISIBLE
426-
} else if (newState == BottomSheetBehavior.STATE_EXPANDED) {
427-
previewPlayer.visibility = View.GONE
408+
when (newState) {
409+
BottomSheetBehavior.STATE_COLLAPSED -> {
410+
fullPlayer.visibility = View.GONE
411+
previewPlayer.visibility = View.VISIBLE
412+
previewPlayer.alpha = 1f
413+
}
414+
415+
BottomSheetBehavior.STATE_DRAGGING,
416+
BottomSheetBehavior.STATE_SETTLING,
417+
BottomSheetBehavior.STATE_HALF_EXPANDED -> {
418+
fullPlayer.visibility = View.VISIBLE
419+
previewPlayer.visibility = View.VISIBLE
420+
}
421+
422+
BottomSheetBehavior.STATE_EXPANDED -> {
423+
previewPlayer.visibility = View.GONE
424+
fullPlayer.visibility = View.VISIBLE
425+
}
426+
427+
BottomSheetBehavior.STATE_HIDDEN -> {
428+
previewPlayer.visibility = View.GONE
429+
fullPlayer.visibility = View.GONE
430+
}
428431
}
432+
bottomSheetBackCallback.isEnabled = newState == BottomSheetBehavior.STATE_EXPANDED
429433
}
430434

431435
override fun onSlide(
@@ -451,24 +455,6 @@ class MainActivity : AppCompatActivity(), Player.Listener {
451455
Constants.PERMISSION_READ_MEDIA_AUDIO,
452456
)
453457
}
454-
onBackInvokedDispatcher.registerOnBackInvokedCallback(
455-
OnBackInvokedDispatcher.PRIORITY_DEFAULT
456-
) {
457-
if (standardBottomSheetBehavior.state == BottomSheetBehavior.STATE_EXPANDED) {
458-
standardBottomSheetBehavior.state = BottomSheetBehavior.STATE_COLLAPSED
459-
Handler(Looper.getMainLooper()).postDelayed(
460-
{
461-
fullPlayer.visibility = View.GONE
462-
previewPlayer.visibility = View.VISIBLE
463-
},
464-
200,
465-
)
466-
} else if (isTaskRoot) {
467-
moveTaskToBack(true)
468-
} else {
469-
supportFragmentManager.popBackStack()
470-
}
471-
}
472458
} else {
473459
if (ContextCompat.checkSelfPermission(
474460
this,
@@ -482,22 +468,6 @@ class MainActivity : AppCompatActivity(), Player.Listener {
482468
Constants.PERMISSION_READ_EXTERNAL_STORAGE,
483469
)
484470
}
485-
onBackPressedDispatcher.addCallback(this) {
486-
if (standardBottomSheetBehavior.state == BottomSheetBehavior.STATE_EXPANDED) {
487-
standardBottomSheetBehavior.state = BottomSheetBehavior.STATE_COLLAPSED
488-
Handler(Looper.getMainLooper()).postDelayed(
489-
{
490-
fullPlayer.visibility = View.GONE
491-
previewPlayer.visibility = View.VISIBLE
492-
},
493-
200,
494-
)
495-
} else if (isTaskRoot) {
496-
moveTaskToBack(true)
497-
} else {
498-
supportFragmentManager.popBackStack()
499-
}
500-
}
501471
}
502472
}
503473

0 commit comments

Comments
 (0)