refactor: Update MediaTrackPreferences handling for improved null safety and initialization

This commit is contained in:
2026-04-22 19:39:24 +02:00
parent 899e2f08b0
commit 65f2bd811b
5 changed files with 14 additions and 8 deletions

View File

@@ -264,9 +264,9 @@ class PlayerManager @Inject constructor(
_playbackState.update { it.copy(error = null) }
}
private suspend fun applyTrackPreferences() {
private fun applyTrackPreferences() {
val context = currentMediaContext ?: return
val preferences = context.preferences ?: return
val preferences = context.preferences
val currentTrackState = _tracks.value

View File

@@ -5,6 +5,6 @@ import hu.bbara.purefin.core.player.preference.MediaTrackPreferences
data class MediaContext(
val mediaId: String,
val preferences: MediaTrackPreferences?,
val preferences: MediaTrackPreferences,
val mediaSegments: List<MediaSegment>
)

View File

@@ -13,7 +13,13 @@ data class MediaTrackPreferences(
val mediaId: String,
val audioPreference: AudioTrackProperties? = null,
val subtitlePreference: SubtitleTrackProperties? = null
)
) {
companion object {
fun empty(mediaId: String): MediaTrackPreferences {
return MediaTrackPreferences(mediaId = mediaId)
}
}
}
@Serializable
data class AudioTrackProperties(

View File

@@ -10,8 +10,8 @@ class TrackPreferencesRepository @Inject constructor(
) {
val preferences: Flow<TrackPreferences> = trackPreferencesDataStore.data
fun getMediaPreferences(mediaId: String): Flow<MediaTrackPreferences?> {
return preferences.map { it.mediaPreferences[mediaId] }
fun getMediaPreferences(mediaId: String): Flow<MediaTrackPreferences> {
return preferences.map { it.mediaPreferences[mediaId] ?: MediaTrackPreferences.empty(mediaId = mediaId) }
}
suspend fun saveAudioPreference(

View File

@@ -17,7 +17,7 @@ import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.firstOrNull
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
import kotlinx.serialization.InternalSerializationApi
@@ -157,7 +157,7 @@ class PlayerViewModel @Inject constructor(
val (mediaItem, resumePositionMs) = result
val preferenceKey = episodeSeriesLookup.preferenceKeyFor(uuid)
val preferences = trackPreferencesRepository.getMediaPreferences(preferenceKey).firstOrNull()
val preferences = trackPreferencesRepository.getMediaPreferences(preferenceKey).first()
val mediaSegments = playableMediaRepository.getMediaSegments(uuid)
val mediaContext = MediaContext(