Skip to content

Commit

Permalink
Fix radio and login stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
brahmkshatriya committed Jan 13, 2025
1 parent 655f6de commit 6984158
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.merge
import kotlinx.coroutines.launch
import kotlinx.coroutines.plus
import kotlinx.coroutines.withContext
Expand Down Expand Up @@ -90,6 +89,7 @@ class ExtensionLoader(
}

private suspend fun Extension<*>.setLoginUser(trigger: Boolean = false) {
println("Setting login user for $name")
val user = userDao.getCurrentUser(id)
get<LoginClient, Unit>(throwableFlow) {
withTimeout(TIMEOUT) { onSetLoginUser(user?.toUser()) }
Expand All @@ -109,6 +109,9 @@ class ExtensionLoader(
lyricsListFlow.getExtension(clientId)?.setLoginUser()
extensionListFlow.getExtension(clientId)?.setLoginUser(true)
}
private val combined = extensionListFlow
.combine(trackerListFlow) { a, b -> a.orEmpty() + b.orEmpty() }
.combine(lyricsListFlow) { a, b -> a + b.orEmpty() }

private var initialized = false
fun initialize() {
Expand All @@ -123,25 +126,24 @@ class ExtensionLoader(
}
}
launch {
val combined = merge(
trackerListFlow, lyricsListFlow, extensionListFlow, extensionFlow
)
userDao.observeCurrentUser()
.combine(combined) { it, _ -> it }
.combine(combined) { it, ext -> it to ext }
.distinctUntilChanged()
.collect { it.map { launch { it.setUser() } } }
.collect { (users, extensions) ->
val ext = extensions.map { it.id }.toSet()
val extToRemove = userMap.keys - ext
extToRemove.forEach { userMap.remove(it) }
users.filter { it.clientId in ext }.map { launch { it.setUser() } }
}
}

//Inject other extensions
launch {
val combined = merge(
extensionFlow.map { listOfNotNull(it) }, trackerListFlow, lyricsListFlow
)
combined.collect { list ->
val trackerExtensions = trackerListFlow.value.orEmpty()
val lyricsExtensions = lyricsListFlow.value.orEmpty()
val musicExtensions = extensionListFlow.value.orEmpty()
list?.forEach { extension ->
list.forEach { extension ->
extension.get<TrackerExtensionsProvider, Unit>(throwableFlow) {
inject(extension.name, requiredTrackerExtensions, trackerExtensions) {
setTrackerExtensions(it)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,11 @@ class EffectsListener(
class Effects(sessionId: Int) {
private val equalizer = runCatching { Equalizer(0, sessionId) }.getOrNull()
private val gain = runCatching { LoudnessEnhancer(sessionId) }.getOrNull()
private fun applyBassBoost(strength: Int) {
private fun applyBassBoost(strength: Int) = runCatching {
if (strength == 0) {
equalizer?.setEnabled(false)
gain?.setEnabled(false)
return
return@runCatching
}
gain?.setEnabled(true)
equalizer?.setEnabled(true)
Expand All @@ -89,8 +89,10 @@ class EffectsListener(
}

fun release() {
equalizer?.release()
gain?.release()
runCatching {
equalizer?.release()
gain?.release()
}
}

fun applySettings(settings: SharedPreferences) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,16 +145,12 @@ class Radio(
private suspend fun startRadio() {
if (!autoStartRadio) return
val hasNext = withContext(Dispatchers.Main) {
player.hasNextMediaItem() || player.currentMediaItem != null
player.hasNextMediaItem() || player.currentMediaItem == null
}
if (hasNext) return
when (val state = stateFlow.value) {
is State.Loading -> {}
is State.Empty -> {
if (stateFlow.value != State.Empty) return
loadPlaylist()
}

is State.Empty -> loadPlaylist()
is State.Loaded -> {
val toBePlayed = state.played + 1
if (toBePlayed == state.tracks.size) loadPlaylist()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,12 @@ import dev.brahmkshatriya.echo.ui.player.PlayerColors.Companion.getColorsFrom
import dev.brahmkshatriya.echo.ui.player.PlayerTrackAdapter.Listener
import dev.brahmkshatriya.echo.ui.player.PlayerTrackAdapter.ViewHolder
import dev.brahmkshatriya.echo.ui.settings.LookFragment
import dev.brahmkshatriya.echo.utils.ui.PlayerItemSpan
import dev.brahmkshatriya.echo.utils.ui.animateVisibility
import dev.brahmkshatriya.echo.utils.image.load
import dev.brahmkshatriya.echo.utils.image.loadBlurred
import dev.brahmkshatriya.echo.utils.image.loadWithBitmap
import dev.brahmkshatriya.echo.utils.image.loadWithThumb
import dev.brahmkshatriya.echo.utils.ui.PlayerItemSpan
import dev.brahmkshatriya.echo.utils.ui.animateVisibility
import dev.brahmkshatriya.echo.utils.ui.toTimeString
import dev.brahmkshatriya.echo.viewmodels.UiViewModel
import dev.brahmkshatriya.echo.viewmodels.UiViewModel.Companion.applyInsets
Expand Down Expand Up @@ -177,7 +177,7 @@ class DefaultViewHolder(
override fun onPlayer(player: Player?) {
actualPlayer = player
player?.addListener(playerListener)
applyPlayerVideo()
applyVideo()
}

private var item: MediaItem? = null
Expand All @@ -197,22 +197,19 @@ class DefaultViewHolder(

private fun applyVideo() {
val background = item?.background
if (oldBg == background) {
applyPlayerVideo()
return
}
oldBg = background
if (background == null || background == oldBg || !showBackground())
return applyPlayerVideo()

cleanUp()
oldBg = background
actualPlayer?.addListener(playerListener)
binding.bgVideo.run {
if (background != null && showBackground()) {
resizeMode = RESIZE_MODE_ZOOM
val bP = getPlayer(context, cache, background)
players.add(bP)
backgroundPlayer = bP
player = backgroundPlayer
applyVideoVisibility(true)
} else applyPlayerVideo()
resizeMode = RESIZE_MODE_ZOOM
val bP = getPlayer(context, cache, background)
players.add(bP)
backgroundPlayer = bP
player = backgroundPlayer
applyVideoVisibility(true)
}
}

Expand All @@ -232,6 +229,7 @@ class DefaultViewHolder(
backgroundPlayer?.release()
players.remove(backgroundPlayer)
backgroundPlayer = null
oldBg = null
}

private var offset = 0f
Expand Down

0 comments on commit 6984158

Please sign in to comment.