backend for watch history

This commit is contained in:
Bnyro 2022-07-01 18:04:25 +02:00
parent c929529b55
commit 458af7d7d4
3 changed files with 50 additions and 0 deletions

View File

@ -455,6 +455,7 @@ class PlayerFragment : Fragment() {
if (!relatedStreamsEnabled) toggleComments() if (!relatedStreamsEnabled) toggleComments()
// prepare for autoplay // prepare for autoplay
initAutoPlay() initAutoPlay()
PreferenceHelper.addToWatchHistory(requireContext(), videoId!!, response)
} }
} }
} }

View File

@ -0,0 +1,11 @@
package com.github.libretube.obj
data class WatchHistoryItem(
val videoId: String?,
val title: String?,
val uploadDate: String?,
val uploader: String?,
val uploaderUrl: String?,
val thumbnailUrl: String?,
val duration: Int?
)

View File

@ -2,13 +2,18 @@ package com.github.libretube.util
import android.content.Context import android.content.Context
import android.content.SharedPreferences import android.content.SharedPreferences
import android.util.Log
import androidx.preference.PreferenceManager import androidx.preference.PreferenceManager
import com.github.libretube.obj.CustomInstance import com.github.libretube.obj.CustomInstance
import com.github.libretube.obj.Streams
import com.github.libretube.obj.WatchHistoryItem
import com.google.common.reflect.TypeToken import com.google.common.reflect.TypeToken
import com.google.gson.Gson import com.google.gson.Gson
import java.lang.reflect.Type import java.lang.reflect.Type
object PreferenceHelper { object PreferenceHelper {
private val TAG = "PreferenceHelper"
fun setString(context: Context, key: String?, value: String?) { fun setString(context: Context, key: String?, value: String?) {
val editor = getDefaultSharedPreferencesEditor(context) val editor = getDefaultSharedPreferencesEditor(context)
editor.putString(key, value) editor.putString(key, value)
@ -122,6 +127,39 @@ object PreferenceHelper {
editor.putStringSet("search_history", set).apply() editor.putStringSet("search_history", set).apply()
} }
fun addToWatchHistory(context: Context, videoId: String, streams: Streams) {
val editor = getDefaultSharedPreferencesEditor(context)
val gson = Gson()
val watchHistoryItem = WatchHistoryItem(
videoId,
streams.title,
streams.uploadDate,
streams.uploader,
streams.uploaderUrl?.replace("/channel/", ""),
streams.thumbnailUrl,
streams.duration
)
val watchHistory = getWatchHistory(context)
watchHistory += watchHistoryItem
val json = gson.toJson(watchHistory)
editor.putString("watch_history", json).apply()
}
fun getWatchHistory(context: Context): ArrayList<WatchHistoryItem> {
val settings = getDefaultSharedPreferences(context)
val gson = Gson()
val json: String = settings.getString("watch_history", "")!!
val type: Type = object : TypeToken<List<WatchHistoryItem?>?>() {}.type
return try {
gson.fromJson(json, type)
} catch (e: Exception) {
arrayListOf()
}
}
private fun getDefaultSharedPreferences(context: Context): SharedPreferences { private fun getDefaultSharedPreferences(context: Context): SharedPreferences {
return PreferenceManager.getDefaultSharedPreferences(context) return PreferenceManager.getDefaultSharedPreferences(context)
} }