Browse Source

Refactor the audio jump method in the view model

The jump code was already duplicated in two places
and will be needed elsewhere in the future
merge-requests/2329/head
Nicolas Pomepuy 10 months ago
parent
commit
99ad4a94ca
  1. 23
      application/television/src/main/java/org/videolan/television/ui/audioplayer/AudioPlayerActivity.kt
  2. 32
      application/vlc-android/src/org/videolan/vlc/gui/audio/AudioPlayer.kt
  3. 28
      application/vlc-android/src/org/videolan/vlc/viewmodels/PlaylistModel.kt

23
application/television/src/main/java/org/videolan/television/ui/audioplayer/AudioPlayerActivity.kt

@ -392,7 +392,7 @@ class AudioPlayerActivity : BaseTvActivity(),KeycodeListener, PlaybackService.Ca
binding.playbackSpeedQuickAction.isFocusable = !bookmarkListDelegate.visible
}
bookmarkListDelegate.seekListener = { forward, long ->
jump(forward, long)
model.jump(forward, long, this)
}
bookmarkListDelegate.markerContainer = binding.bookmarkMarkerContainer
}
@ -400,27 +400,6 @@ class AudioPlayerActivity : BaseTvActivity(),KeycodeListener, PlaybackService.Ca
}
}
/**
* Jump backward or forward, with a long or small delay
* depending on the audio control setting chosen by the user
*
* @param forward is the jump forward?
* @param long has it been triggered by a long tap?
*/
private fun jump(forward:Boolean, long:Boolean) {
model.service?.let { service ->
val jumpDelay = if (long) Settings.audioLongJumpDelay else Settings.audioJumpDelay
val delay = if (forward) jumpDelay * 1000 else -(jumpDelay * 1000)
var position = service.getTime() + delay
if (position < 0) position = 0
if (position > service.length) position = service.length
service.seek(position, service.length.toDouble(), true, fast = false)
service.playlistManager.player.updateProgress(position)
if (service.playlistManager.player.lastPosition == 0.0f && (forward || service.getTime() > 0))
UiTools.snacker(this, getString(org.videolan.vlc.R.string.unseekable_stream))
}
}
private fun setShuffleMode(shuffle: Boolean) {
shuffling = shuffle
val medias = model.medias?.toMutableList() ?: return

32
application/vlc-android/src/org/videolan/vlc/gui/audio/AudioPlayer.kt

@ -708,20 +708,20 @@ class AudioPlayer : Fragment(), PlaylistAdapter.IPlayer, TextWatcher, IAudioPlay
}
fun onJumpBack(@Suppress("UNUSED_PARAMETER") view: View) {
jump(forward = false, long = false)
playlistModel.jump(forward = false, long = false, requireActivity())
}
fun onJumpBackLong(@Suppress("UNUSED_PARAMETER") view: View):Boolean {
jump(forward = false, long = true)
playlistModel.jump(forward = false, long = true, requireActivity())
return true
}
fun onJumpForward(@Suppress("UNUSED_PARAMETER") view: View) {
jump(forward = true, long = false)
playlistModel.jump(forward = true, long = false, requireActivity())
}
fun onJumpForwardLong(@Suppress("UNUSED_PARAMETER") view: View):Boolean {
jump(forward = true, long = true)
playlistModel.jump(forward = true, long = true, requireActivity())
return true
}
@ -739,28 +739,6 @@ class AudioPlayer : Fragment(), PlaylistAdapter.IPlayer, TextWatcher, IAudioPlay
}
}
/**
* Jump backward or forward, with a long or small delay
* depending on the audio control setting chosen by the user
*
* @param forward is the jump forward?
* @param long has it been triggered by a long tap?
*/
private fun jump(forward:Boolean, long:Boolean) {
playlistModel.service ?.let { service ->
val jumpDelay = if (long) Settings.audioLongJumpDelay else Settings.audioJumpDelay
var delay = if (forward) jumpDelay * 1000 else -(jumpDelay * 1000)
if (LocaleUtil.isRtl()) delay = -delay
var position = service.getTime() + delay
if (position < 0) position = 0
if (position > service.length) position = service.length
service.seek(position, service.length.toDouble(), true, fast = false)
service.playlistManager.player.updateProgress(position)
if (service.playlistManager.player.lastPosition == 0.0f && (forward || service.getTime() > 0))
UiTools.snacker(requireActivity(), getString(R.string.unseekable_stream))
}
}
fun onPlayPauseClick(@Suppress("UNUSED_PARAMETER") view: View?) {
if (playlistModel.service?.isPausable == false) {
UiTools.snackerConfirm(requireActivity(), getString(R.string.stop_unpaubale), true) {
@ -843,7 +821,7 @@ class AudioPlayer : Fragment(), PlaylistAdapter.IPlayer, TextWatcher, IAudioPlay
}
}
bookmarkListDelegate.seekListener = { forward, long ->
jump(forward , long)
playlistModel.jump(forward , long, requireActivity())
}
bookmarkListDelegate.markerContainer = binding.bookmarkMarkerContainer
}

28
application/vlc-android/src/org/videolan/vlc/viewmodels/PlaylistModel.kt

@ -20,6 +20,7 @@
package org.videolan.vlc.viewmodels
import android.app.Activity
import android.support.v4.media.session.PlaybackStateCompat
import androidx.annotation.MainThread
import androidx.fragment.app.Fragment
@ -38,10 +39,14 @@ import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.withContext
import org.videolan.medialibrary.Tools
import org.videolan.medialibrary.interfaces.media.MediaWrapper
import org.videolan.tools.Settings
import org.videolan.tools.livedata.LiveDataset
import org.videolan.vlc.PlaybackService
import org.videolan.vlc.R
import org.videolan.vlc.gui.helpers.UiTools
import org.videolan.vlc.media.PlaylistManager
import org.videolan.vlc.util.EmptyPBSCallback
import org.videolan.vlc.util.LocaleUtil
import org.videolan.vlc.util.PlaylistFilterDelegate
class PlaylistModel : ViewModel(), PlaybackService.Callback by EmptyPBSCallback {
@ -84,6 +89,29 @@ class PlaylistModel : ViewModel(), PlaybackService.Callback by EmptyPBSCallback
update()
}
/**
* Jump backward or forward, with a long or small delay
* depending on the audio control setting chosen by the user
*
* @param forward is the jump forward?
* @param long has it been triggered by a long tap?
* @param activity the activity used to display the snackbar if needed
*/
fun jump(forward:Boolean, long:Boolean, activity: Activity) {
service ?.let { service ->
val jumpDelay = if (long) Settings.audioLongJumpDelay else Settings.audioJumpDelay
var delay = if (forward) jumpDelay * 1000 else -(jumpDelay * 1000)
if (LocaleUtil.isRtl()) delay = -delay
var position = service.getTime() + delay
if (position < 0) position = 0
if (position > service.length) position = service.length
service.seek(position, service.length.toDouble(), true, fast = false)
service.playlistManager.player.updateProgress(position)
if (service.playlistManager.player.lastPosition == 0.0f && (forward || service.getTime() > 0))
UiTools.snacker(activity, activity.getString(R.string.unseekable_stream))
}
}
override fun update() {
service?.run {
if (filtering) {

Loading…
Cancel
Save