Skip to content

Commit

Permalink
fix(external tracks): fix external tracks not updating correctly in t…
Browse files Browse the repository at this point in the history
…he ui sometimes

- also resize the add track button cause i've seen cases where it gets accidentally pressed
  • Loading branch information
abdallahmehiz committed Sep 2, 2024
1 parent 7157642 commit d301d38
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,7 @@ class PlayerActivity : AppCompatActivity() {
internal fun onObserverEvent(property: String) {
when (property) {
"chapter-list" -> viewModel.loadChapters()
"track-list" -> viewModel.loadTracks()
}
}

Expand Down Expand Up @@ -498,8 +499,6 @@ class PlayerActivity : AppCompatActivity() {
}
setOrientation()
viewModel.changeVideoAspect(playerPreferences.videoAspect.get())
viewModel.loadChapters()
viewModel.loadTracks()
}

MPVLib.mpvEventId.MPV_EVENT_SEEK -> {
Expand Down Expand Up @@ -691,10 +690,6 @@ class PlayerActivity : AppCompatActivity() {
return super.onKeyUp(keyCode, event)
}

companion object {
const val TAG = "mpvKt"
}

private val additionalObservedProps = mapOf(
"chapter" to MPVLib.mpvFormat.MPV_FORMAT_INT64,
"sid" to MPVLib.mpvFormat.MPV_FORMAT_STRING,
Expand All @@ -704,3 +699,5 @@ class PlayerActivity : AppCompatActivity() {
"hwdec" to MPVLib.mpvFormat.MPV_FORMAT_STRING
)
}

const val TAG = "mpvKt"
32 changes: 18 additions & 14 deletions app/src/main/java/live/mehiz/mpvkt/ui/player/PlayerViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import android.net.Uri
import android.os.Build
import android.provider.Settings
import android.util.DisplayMetrics
import android.util.Log
import android.widget.Toast
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
Expand Down Expand Up @@ -150,22 +151,29 @@ class PlayerViewModel(
MPVLib.getPropertyString("track-list/$it/type")
}

private var trackLoadingJob: Job? = null
fun loadTracks() {
viewModelScope.launch {
val tracksCount = MPVLib.getPropertyInt("track-list/count")!!
trackLoadingJob?.cancel()
trackLoadingJob = viewModelScope.launch {
val possibleTrackTypes = listOf("video", "audio", "sub")
val vidTracks = mutableListOf<Track>()
val subTracks = mutableListOf<Track>()
val audioTracks = mutableListOf(Track(-1, activity.getString(R.string.player_sheets_tracks_off), null))
for (i in 0..<tracksCount) {
val type = getTrackType(i)
if (!possibleTrackTypes.contains(type) || type == null) continue
when (type) {
"sub" -> subTracks.add(Track(getTrackMPVId(i), getTrackTitle(i), getTrackLanguage(i)))
"audio" -> audioTracks.add(Track(getTrackMPVId(i), getTrackTitle(i), getTrackLanguage(i)))
"video" -> vidTracks.add(Track(getTrackMPVId(i), getTrackTitle(i), getTrackLanguage(i)))
else -> error("Unrecognized track type")
try {
val tracksCount = MPVLib.getPropertyInt("track-list/count")!!
for (i in 0..<tracksCount) {
val type = getTrackType(i)
if (!possibleTrackTypes.contains(type) || type == null) continue
when (type) {
"sub" -> subTracks.add(Track(getTrackMPVId(i), getTrackTitle(i), getTrackLanguage(i)))
"audio" -> audioTracks.add(Track(getTrackMPVId(i), getTrackTitle(i), getTrackLanguage(i)))
"video" -> vidTracks.add(Track(getTrackMPVId(i), getTrackTitle(i), getTrackLanguage(i)))
else -> error("Unrecognized track type")
}
}
} catch (e: NullPointerException) {
Log.e(TAG, "Couldn't load tracks, probably cause mpv was destroyed")
return@launch
}
_subtitleTracks.update { subTracks }
_selectedSubtitles.update { Pair(activity.player.sid, activity.player.secondarySid) }
Expand Down Expand Up @@ -220,11 +228,7 @@ class PlayerViewModel(
} else {
url
} ?: return
val trackCount = MPVLib.getPropertyInt("track-list/count")
MPVLib.command(arrayOf("sub-add", path, "cached"))
if (trackCount == MPVLib.getPropertyInt("track-list/count")) return
_subtitleTracks.update { it.plus(Track(activity.player.sid, path, null)) }
_selectedSubtitles.update { Pair(activity.player.sid, activity.player.secondarySid) }
}

fun setSubtitle(sid: Int, secondarySid: Int) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.RowScope
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
Expand Down Expand Up @@ -65,18 +65,26 @@ fun AddTrackRow(
modifier = modifier
.fillMaxWidth()
.clickable(onClick = onClick)
.height(48.dp)
.padding(start = MaterialTheme.spacing.medium),
.height(48.dp),
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.spacedBy(MaterialTheme.spacing.smaller),
) {
Icon(
Icons.Default.Add,
null,
modifier = Modifier.size(32.dp),
)
Text(title)
Spacer(Modifier.weight(1f))
Row(
modifier = Modifier
.clickable(onClick = onClick)
.fillMaxHeight()
.weight(1f)
.padding(start = MaterialTheme.spacing.medium),
horizontalArrangement = Arrangement.spacedBy(MaterialTheme.spacing.smaller),
verticalAlignment = Alignment.CenterVertically,
) {
Icon(
Icons.Default.Add,
null,
modifier = Modifier.size(32.dp),
)
Text(title)
}
actions()
}
}
Expand Down

0 comments on commit d301d38

Please sign in to comment.