-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
647c032
commit 748b59c
Showing
8 changed files
with
81 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
app/src/main/java/dev/brahmkshatriya/echo/ui/player/PlayerBackButtonHelper.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package dev.brahmkshatriya.echo.ui.player | ||
|
||
import android.view.View | ||
import androidx.activity.BackEventCompat | ||
import androidx.activity.OnBackPressedCallback | ||
import androidx.fragment.app.Fragment | ||
import androidx.lifecycle.LifecycleOwner | ||
import androidx.lifecycle.lifecycleScope | ||
import com.google.android.material.bottomsheet.BottomSheetBehavior | ||
import dev.brahmkshatriya.echo.MainActivity | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.collectLatest | ||
import kotlinx.coroutines.launch | ||
|
||
object PlayerBackButtonHelper { | ||
|
||
var bottomSheetBehavior: BottomSheetBehavior<View>? = null | ||
val playerCollapsed = MutableStateFlow(true) | ||
private fun backPressedCallback( | ||
viewLifecycleOwner: LifecycleOwner, | ||
callback: ((Boolean) -> Unit)? | ||
): OnBackPressedCallback { | ||
val backPress = object : OnBackPressedCallback(false) { | ||
override fun handleOnBackStarted(backEvent: BackEventCompat) { | ||
bottomSheetBehavior?.startBackProgress(backEvent) | ||
} | ||
|
||
override fun handleOnBackProgressed(backEvent: BackEventCompat) { | ||
bottomSheetBehavior?.updateBackProgress(backEvent) | ||
} | ||
|
||
override fun handleOnBackPressed() { | ||
bottomSheetBehavior?.handleBackInvoked() | ||
} | ||
|
||
override fun handleOnBackCancelled() { | ||
bottomSheetBehavior?.cancelBackProgress() | ||
} | ||
} | ||
viewLifecycleOwner.lifecycleScope.launch { | ||
playerCollapsed.collectLatest { | ||
backPress.isEnabled = !it | ||
callback?.invoke(it) | ||
} | ||
} | ||
return backPress | ||
} | ||
|
||
fun addCallback(fragment: Fragment, callback: ((Boolean) -> Unit)? = null) = | ||
with(fragment) { | ||
val mainActivity = (requireActivity() as? MainActivity) | ||
?: throw IllegalArgumentException("Fragment must be attached to MainActivity") | ||
val backPressedCallback = | ||
backPressedCallback(viewLifecycleOwner, callback) | ||
mainActivity.onBackPressedDispatcher.addCallback( | ||
viewLifecycleOwner, | ||
backPressedCallback | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -53,6 +53,8 @@ interface PlayerHelper { | |
String.format("%02d:%02d", minutes, seconds % 60) | ||
} | ||
} | ||
|
||
|
||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters