Skip to content

Commit

Permalink
fix(database): save position with filenames
Browse files Browse the repository at this point in the history
  • Loading branch information
abdallahmehiz committed Jul 8, 2024
1 parent c826cf9 commit a5ea30c
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 11 deletions.
19 changes: 11 additions & 8 deletions app/src/main/java/live/mehiz/mpvkt/ui/player/PlayerActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ class PlayerActivity : AppCompatActivity() {
private val subtitlesPreferences: SubtitlesPreferences by inject()
private val advancedPreferences: AdvancedPreferences by inject()

private lateinit var fileName: String

override fun onCreate(savedInstanceState: Bundle?) {
if (playerPreferences.drawOverDisplayCutout.get()) enableEdgeToEdge()
super.onCreate(savedInstanceState)
Expand Down Expand Up @@ -118,9 +120,6 @@ class PlayerActivity : AppCompatActivity() {
}
player.playbackSpeed = playerPreferences.defaultSpeed.get().toDouble()
MPVLib.setPropertyString("keep-open", "yes")
if (playerPreferences.savePositionOnQuit.get()) {
MPVLib.setPropertyString("save-position-on-quit", "yes")
}

player.addObserver(PlayerObserver(this))
}
Expand Down Expand Up @@ -151,7 +150,7 @@ class PlayerActivity : AppCompatActivity() {
}

private fun setupIntents(intent: Intent) {
viewModel.fileName = intent.getStringExtra("title") ?: ""
intent.getStringExtra("title")?.ifBlank { viewModel.mediaTitle.update { it } }
player.timePos = intent.getIntExtra("position", 0) / 1000
}

Expand Down Expand Up @@ -240,8 +239,12 @@ class PlayerActivity : AppCompatActivity() {
internal fun event(eventId: Int) {
when (eventId) {
MPVLib.mpvEventId.MPV_EVENT_FILE_LOADED -> {
fileName = intent.data!!.lastPathSegment!!.substringAfterLast('/')
viewModel.mediaTitle.update {
MPVLib.getPropertyString("media-title").ifBlank { fileName }
}
CoroutineScope(Dispatchers.IO).launch {
reuseVideoPlaybackState(MPVLib.getPropertyString("media-title"))
loadVideoPlaybackState(fileName)
if (intent.hasExtra("position")) setupIntents(intent)
}
setOrientation()
Expand All @@ -265,7 +268,7 @@ class PlayerActivity : AppCompatActivity() {
private suspend fun saveVideoPlaybackState() {
mpvKtDatabase.videoDataDao().upsert(
PlaybackStateEntity(
MPVLib.getPropertyString("media-title"),
fileName,
if (playerPreferences.savePositionOnQuit.get()) player.timePos ?: 0 else 0,
player.sid,
player.secondarySid,
Expand All @@ -274,10 +277,10 @@ class PlayerActivity : AppCompatActivity() {
)
}

private suspend fun reuseVideoPlaybackState(mediaTitle: String) {
private suspend fun loadVideoPlaybackState(mediaTitle: String) {
val state = mpvKtDatabase.videoDataDao().getVideoDataByTitle(mediaTitle)
state?.let {
player.timePos = it.lastPosition
player.timePos = if (playerPreferences.savePositionOnQuit.get()) it.lastPosition else 0
player.sid = it.sid
player.secondarySid = it.secondarySid
player.aid = it.aid
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class PlayerViewModel(
private val _currentDecoder = MutableStateFlow(getDecoderFromValue(MPVLib.getPropertyString("hwdec")))
val currentDecoder = _currentDecoder.asStateFlow()

var fileName = ""
var mediaTitle = MutableStateFlow("")

val isLoading = MutableStateFlow(true)

Expand Down Expand Up @@ -129,7 +129,6 @@ class PlayerViewModel(

fun loadTracks() {
viewModelScope.launch {
fileName = MPVLib.getPropertyString("media-title")
val tracksCount = MPVLib.getPropertyInt("track-list/count")!!
val possibleTrackTypes = listOf("video", "audio", "sub")
val vidTracks = mutableListOf<Track>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import androidx.compose.material.icons.automirrored.filled.ArrowBack
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
Expand All @@ -29,8 +31,9 @@ fun TopLeftPlayerControls(
icon = Icons.AutoMirrored.Default.ArrowBack,
onClick = { activity.finish() },
)
val mediaTitle by viewModel.mediaTitle.collectAsState()
Text(
viewModel.fileName,
mediaTitle,
maxLines = 1,
overflow = TextOverflow.Ellipsis,
color = Color.White,
Expand Down

0 comments on commit a5ea30c

Please sign in to comment.