Skip to content

Commit

Permalink
Fix CheckBoxListener (Play and Like Buttons) & Radio Double tracks
Browse files Browse the repository at this point in the history
  • Loading branch information
brahmkshatriya committed Aug 15, 2024
1 parent 44f8c90 commit 94e8d46
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,12 @@ class PlayerSessionCallback(
val error = SessionResult(SessionResult.RESULT_ERROR_UNKNOWN)
val clientId = args.getString("clientId") ?: return@future error
val item = args.getParcel<EchoMediaItem>("item") ?: return@future error
radioFlow.value = Radio.State.Loading
val loaded = Radio.start(
context, messageFlow, throwableFlow, radioFlow, extensionList, clientId, item, 0
) ?: return@future error
context, messageFlow, throwableFlow, extensionList, clientId, item, 0
)
radioFlow.value = loaded ?: Radio.State.Empty
if (loaded == null) return@future error
val mediaItem = MediaItemUtils.build(
settings, loaded.tracks[0], loaded.clientId, loaded.playlist.toMediaItem()
)
Expand Down
16 changes: 6 additions & 10 deletions app/src/main/java/dev/brahmkshatriya/echo/playback/Radio.kt
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ class Radio(
context: Context,
messageFlow: MutableSharedFlow<SnackBar.Message>,
throwableFlow: MutableSharedFlow<Throwable>,
stateFlow: MutableStateFlow<State>,
extensionListFlow: StateFlow<List<MusicExtension>?>,
clientId: String,
item: EchoMediaItem,
Expand All @@ -74,8 +73,6 @@ class Radio(
}

else -> {
stateFlow.value = State.Loading

suspend fun <T> tryIO(block: suspend () -> T): T? =
withContext(Dispatchers.IO) {
tryWith(throwableFlow, extension.info) { block() }
Expand Down Expand Up @@ -107,10 +104,7 @@ class Radio(
)
null
}
stateFlow.value = state ?: State.Empty
return state
} else {
stateFlow.value = State.Empty
}
}
}
Expand All @@ -133,21 +127,23 @@ class Radio(
val mediaItem = player.currentMediaItem ?: return
val client = mediaItem.clientId
val item = mediaItem.context ?: mediaItem.track.toMediaItem()
stateFlow.value = State.Loading
scope.launch {
val loaded = start(
context, messageFlow, throwFlow, stateFlow, extensionList, client, item, 0
) ?: return@launch
play(loaded, 0)
val loaded = start(context, messageFlow, throwFlow, extensionList, client, item, 0)
stateFlow.value = loaded ?: State.Empty
if (loaded != null) play(loaded, 0)
}
}

private var autoStartRadio = true

init {
settings.registerOnSharedPreferenceChangeListener { pref, key ->
if (key == AUTO_START_RADIO)
autoStartRadio = pref.getBoolean(AUTO_START_RADIO, true)
}
}

private fun startRadio() {
if (!autoStartRadio) return
if (player.hasNextMediaItem() || player.currentMediaItem == null) return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,15 @@ import com.google.android.material.checkbox.MaterialCheckBox
class CheckBoxListener(private val block: (Boolean) -> Unit) :
MaterialCheckBox.OnCheckedStateChangedListener {
var enabled = true
var checked = false
private var checked = false
private fun check(isChecked: Boolean) {
if(checked == isChecked) return
checked = isChecked
if (checked == isChecked) return
block(isChecked)
}

override fun onCheckedStateChangedListener(checkBox: MaterialCheckBox, state: Int) {
if (enabled) when (state) {
MaterialCheckBox.STATE_CHECKED -> check(true)
else -> check(false)
}
val isChecked = checkBox.isChecked
if (enabled) check(isChecked)
checked = isChecked
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,6 @@ class PlayerTrackAdapter(
binding.playerControls.trackHeart.run {
viewModel.isLiked.value = item.isLiked
isChecked = item.isLiked
viewModel.likeListener.checked = item.isLiked
val client = viewModel.extensionListFlow.getExtension(clientId)?.client
val isLibrary = client is LibraryClient
isVisible = isLibrary
Expand Down

0 comments on commit 94e8d46

Please sign in to comment.