mirror of
https://github.com/libre-tube/LibreTube.git
synced 2025-04-28 07:50:31 +05:30
remember watch position
This commit is contained in:
parent
0c6928166b
commit
9d5ed16e37
@ -371,6 +371,7 @@ class PlayerFragment : Fragment() {
|
||||
override fun onDestroy() {
|
||||
super.onDestroy()
|
||||
try {
|
||||
saveWatchPosition()
|
||||
mediaSession.isActive = false
|
||||
mediaSession.release()
|
||||
mediaSessionConnector.setPlayer(null)
|
||||
@ -384,6 +385,25 @@ class PlayerFragment : Fragment() {
|
||||
}
|
||||
}
|
||||
|
||||
// save the watch position if video isn't finished and option enabled
|
||||
private fun saveWatchPosition() {
|
||||
val watchPositionsEnabled = PreferenceHelper.getBoolean(
|
||||
requireContext(),
|
||||
"watch_positions_toggle",
|
||||
true
|
||||
)
|
||||
if (watchPositionsEnabled && exoPlayer.currentPosition != exoPlayer.duration) {
|
||||
PreferenceHelper.saveWatchPosition(
|
||||
requireContext(),
|
||||
videoId!!,
|
||||
exoPlayer.currentPosition
|
||||
)
|
||||
} else if (watchPositionsEnabled) {
|
||||
// delete watch position if video has ended
|
||||
PreferenceHelper.removeWatchPosition(requireContext(), videoId!!)
|
||||
}
|
||||
}
|
||||
|
||||
private fun checkForSegments() {
|
||||
if (!exoPlayer.isPlaying || !sponsorBlockPrefs.sponsorBlockEnabled) return
|
||||
|
||||
@ -445,6 +465,7 @@ class PlayerFragment : Fragment() {
|
||||
val position = arguments?.getLong("timeStamp")!! * 1000
|
||||
exoPlayer.seekTo(position)
|
||||
}
|
||||
seekToWatchPosition()
|
||||
exoPlayer.play()
|
||||
exoPlayerView.useController = true
|
||||
initializePlayerNotification(requireContext())
|
||||
@ -464,6 +485,14 @@ class PlayerFragment : Fragment() {
|
||||
run()
|
||||
}
|
||||
|
||||
// seek to saved watch position if available
|
||||
private fun seekToWatchPosition() {
|
||||
val watchPositions = PreferenceHelper.getWatchPositions(requireContext())
|
||||
watchPositions.forEach {
|
||||
if (it.videoId == videoId) exoPlayer.seekTo(it.position)
|
||||
}
|
||||
}
|
||||
|
||||
// the function is working recursively
|
||||
private fun initAutoPlay() {
|
||||
// save related streams for autoplay
|
||||
|
@ -0,0 +1,6 @@
|
||||
package com.github.libretube.obj
|
||||
|
||||
data class WatchPosition(
|
||||
val videoId: String,
|
||||
val position: Long
|
||||
)
|
@ -25,10 +25,11 @@ class AdvancedSettings : PreferenceFragmentCompat() {
|
||||
true
|
||||
}
|
||||
|
||||
// clear watch history
|
||||
// clear watch history and positions
|
||||
val clearWatchHistory = findPreference<Preference>("clear_watch_history")
|
||||
clearWatchHistory?.setOnPreferenceClickListener {
|
||||
PreferenceHelper.removePreference(requireContext(), "watch_history")
|
||||
PreferenceHelper.removePreference(requireContext(), "watch_positions")
|
||||
true
|
||||
}
|
||||
|
||||
|
@ -6,6 +6,7 @@ import androidx.preference.PreferenceManager
|
||||
import com.github.libretube.obj.CustomInstance
|
||||
import com.github.libretube.obj.Streams
|
||||
import com.github.libretube.obj.WatchHistoryItem
|
||||
import com.github.libretube.obj.WatchPosition
|
||||
import com.google.common.reflect.TypeToken
|
||||
import com.google.gson.Gson
|
||||
import java.lang.reflect.Type
|
||||
@ -144,11 +145,11 @@ object PreferenceHelper {
|
||||
val watchHistory = getWatchHistory(context)
|
||||
|
||||
// delete entries that have the same videoId
|
||||
var indexToRemove = Int.MAX_VALUE
|
||||
var indexToRemove: Int? = null
|
||||
watchHistory.forEachIndexed { index, item ->
|
||||
if (item.videoId == videoId) indexToRemove = index
|
||||
}
|
||||
if (indexToRemove != Int.MAX_VALUE) watchHistory.removeAt(indexToRemove)
|
||||
if (indexToRemove != null) watchHistory.removeAt(indexToRemove!!)
|
||||
|
||||
watchHistory += watchHistoryItem
|
||||
|
||||
@ -168,6 +169,55 @@ object PreferenceHelper {
|
||||
}
|
||||
}
|
||||
|
||||
fun saveWatchPosition(context: Context, videoId: String, position: Long) {
|
||||
val editor = getDefaultSharedPreferencesEditor(context)
|
||||
|
||||
val watchPositions = getWatchPositions(context)
|
||||
val watchPositionItem = WatchPosition(videoId, position)
|
||||
|
||||
var indexToRemove: Int? = null
|
||||
watchPositions.forEachIndexed { index, item ->
|
||||
if (item.videoId == videoId) indexToRemove = index
|
||||
}
|
||||
|
||||
if (indexToRemove != null) watchPositions.removeAt(indexToRemove!!)
|
||||
|
||||
watchPositions += watchPositionItem
|
||||
|
||||
val gson = Gson()
|
||||
val json = gson.toJson(watchPositions)
|
||||
editor.putString("watch_positions", json).commit()
|
||||
}
|
||||
|
||||
fun removeWatchPosition(context: Context, videoId: String) {
|
||||
val editor = getDefaultSharedPreferencesEditor(context)
|
||||
|
||||
val watchPositions = getWatchPositions(context)
|
||||
|
||||
var indexToRemove: Int? = null
|
||||
watchPositions.forEachIndexed { index, item ->
|
||||
if (item.videoId == videoId) indexToRemove = index
|
||||
}
|
||||
|
||||
if (indexToRemove != null) watchPositions.removeAt(indexToRemove!!)
|
||||
|
||||
val gson = Gson()
|
||||
val json = gson.toJson(watchPositions)
|
||||
editor.putString("watch_positions", json).commit()
|
||||
}
|
||||
|
||||
fun getWatchPositions(context: Context): ArrayList<WatchPosition> {
|
||||
val settings = getDefaultSharedPreferences(context)
|
||||
val gson = Gson()
|
||||
val json: String = settings.getString("watch_positions", "")!!
|
||||
val type: Type = object : TypeToken<List<WatchPosition?>?>() {}.type
|
||||
return try {
|
||||
gson.fromJson(json, type)
|
||||
} catch (e: Exception) {
|
||||
arrayListOf()
|
||||
}
|
||||
}
|
||||
|
||||
private fun getDefaultSharedPreferences(context: Context): SharedPreferences {
|
||||
return PreferenceManager.getDefaultSharedPreferences(context)
|
||||
}
|
||||
|
@ -208,4 +208,6 @@
|
||||
<string name="account">Account</string>
|
||||
<string name="restore">Restore</string>
|
||||
<string name="watch_history">Watch History</string>
|
||||
<string name="watch_positions">Remember position</string>
|
||||
<string name="watch_positions_summary">Remember the watch position and automatically seek to it.</string>
|
||||
</resources>
|
@ -54,6 +54,13 @@
|
||||
app:key="watch_history_toggle"
|
||||
app:title="@string/watch_history" />
|
||||
|
||||
<SwitchPreference
|
||||
android:defaultValue="true"
|
||||
android:icon="@drawable/ic_play_filled"
|
||||
app:key="watch_position_toggle"
|
||||
app:title="@string/watch_positions"
|
||||
app:summary="@string/watch_positions_summary"/>
|
||||
|
||||
<Preference
|
||||
android:icon="@drawable/ic_trash"
|
||||
app:key="clear_watch_history"
|
||||
|
Loading…
x
Reference in New Issue
Block a user