diff --git a/app/src/main/java/com/github/libretube/fragments/PlayerFragment.kt b/app/src/main/java/com/github/libretube/fragments/PlayerFragment.kt index e2575af2c..b3369fec9 100644 --- a/app/src/main/java/com/github/libretube/fragments/PlayerFragment.kt +++ b/app/src/main/java/com/github/libretube/fragments/PlayerFragment.kt @@ -455,6 +455,7 @@ class PlayerFragment : Fragment() { if (!relatedStreamsEnabled) toggleComments() // prepare for autoplay initAutoPlay() + PreferenceHelper.addToWatchHistory(requireContext(), videoId!!, response) } } } diff --git a/app/src/main/java/com/github/libretube/obj/WatchHistoryItem.kt b/app/src/main/java/com/github/libretube/obj/WatchHistoryItem.kt new file mode 100644 index 000000000..074904223 --- /dev/null +++ b/app/src/main/java/com/github/libretube/obj/WatchHistoryItem.kt @@ -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? +) diff --git a/app/src/main/java/com/github/libretube/util/PreferenceHelper.kt b/app/src/main/java/com/github/libretube/util/PreferenceHelper.kt index 310a02578..2540268b9 100644 --- a/app/src/main/java/com/github/libretube/util/PreferenceHelper.kt +++ b/app/src/main/java/com/github/libretube/util/PreferenceHelper.kt @@ -2,13 +2,18 @@ package com.github.libretube.util import android.content.Context import android.content.SharedPreferences +import android.util.Log import androidx.preference.PreferenceManager 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.gson.Gson import java.lang.reflect.Type object PreferenceHelper { + private val TAG = "PreferenceHelper" + fun setString(context: Context, key: String?, value: String?) { val editor = getDefaultSharedPreferencesEditor(context) editor.putString(key, value) @@ -122,6 +127,39 @@ object PreferenceHelper { 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 { + val settings = getDefaultSharedPreferences(context) + val gson = Gson() + val json: String = settings.getString("watch_history", "")!! + val type: Type = object : TypeToken?>() {}.type + return try { + gson.fromJson(json, type) + } catch (e: Exception) { + arrayListOf() + } + } + private fun getDefaultSharedPreferences(context: Context): SharedPreferences { return PreferenceManager.getDefaultSharedPreferences(context) }