fixed watch history

This commit is contained in:
2026-10-05 20:38:56 -07:00
parent 1c3ffacccd
commit 494d8e6835
4 changed files with 31 additions and 2 deletions

View File

@@ -48,6 +48,7 @@ class MainActivity : ComponentActivity() {
val watchLaterItems by libraryManager.watchLater.collectAsState()
val favoriteItems by libraryManager.favorites.collectAsState()
val historyItems by libraryManager.history.collectAsState()
val sidebarFocusRequester = remember { FocusRequester() }
val contentFocusRequester = remember { FocusRequester() }
@@ -64,6 +65,7 @@ class MainActivity : ComponentActivity() {
if (playingItem == null) {
Sidebar(
currentDestination = currentDestination,
hasHistory = historyItems.isNotEmpty(),
hasWatchLater = watchLaterItems.isNotEmpty(),
hasFavorites = favoriteItems.isNotEmpty(),
onNavigate = { dest ->
@@ -98,6 +100,7 @@ class MainActivity : ComponentActivity() {
mode = currentDestination,
onOpenDetail = { item -> activeDetailItem = item },
onPlay = { item ->
libraryManager.addHistory(item)
playingItem = item
playingEpisode = null
},
@@ -118,6 +121,14 @@ class MainActivity : ComponentActivity() {
}
)
}
NavDestination.HISTORY -> {
LibraryScreen(
title = "Watch History",
items = historyItems,
onOpenDetail = { item -> activeDetailItem = item },
emptyMessage = "Nothing watched yet. Press play on a title and it shows up here."
)
}
NavDestination.WATCH_LATER -> {
LibraryScreen(
title = "Watch Later",
@@ -147,6 +158,7 @@ class MainActivity : ComponentActivity() {
onToggleWatchLater = { libraryManager.toggle("wl", it) },
onToggleFavorite = { libraryManager.toggle("fav", it) },
onPlay = { itm, ep ->
libraryManager.addHistory(itm)
playingItem = itm
playingEpisode = ep
},

View File

@@ -30,10 +30,21 @@ class LibraryManager private constructor(context: Context) {
private val _favorites = MutableStateFlow<List<MediaItem>>(emptyList())
val favorites: StateFlow<List<MediaItem>> = _favorites.asStateFlow()
private val _history = MutableStateFlow<List<MediaItem>>(emptyList())
val history: StateFlow<List<MediaItem>> = _history.asStateFlow()
init {
load()
}
fun addHistory(item: MediaItem) {
val current = _history.value.toMutableList()
current.removeAll { it.id == item.id && it.mediaType == item.mediaType }
current.add(0, item)
_history.value = current.take(100)
save()
}
private fun load() {
val raw = prefs.getString("vyla.lib", null)
if (raw.isNullOrEmpty()) return
@@ -41,6 +52,7 @@ class LibraryManager private constructor(context: Context) {
val root = JSONObject(raw)
_watchLater.value = parseList(root.optJSONArray("wl"))
_favorites.value = parseList(root.optJSONArray("fav"))
_history.value = parseList(root.optJSONArray("hist"))
} catch (e: Exception) {
e.printStackTrace()
}
@@ -51,6 +63,7 @@ class LibraryManager private constructor(context: Context) {
val root = JSONObject()
root.put("wl", serializeList(_watchLater.value))
root.put("fav", serializeList(_favorites.value))
root.put("hist", serializeList(_history.value))
prefs.edit().putString("vyla.lib", root.toString()).apply()
} catch (e: Exception) {
e.printStackTrace()

View File

@@ -51,6 +51,7 @@ enum class NavDestination(val key: String, val title: String, val icon: ImageVec
MOVIES("movie", "Movies", Icons.Default.Movie),
TV_SHOWS("tv", "TV Shows", Icons.Default.Tv),
ANIME("anime", "Anime", Icons.Default.AutoAwesome),
HISTORY("history", "History", Icons.Default.History),
WATCH_LATER("wl", "Watch Later", Icons.Default.Schedule),
FAVORITES("fav", "Favorites", Icons.Default.Favorite)
}
@@ -59,6 +60,7 @@ enum class NavDestination(val key: String, val title: String, val icon: ImageVec
@OptIn(ExperimentalComposeUiApi::class)
fun Sidebar(
currentDestination: NavDestination,
hasHistory: Boolean,
hasWatchLater: Boolean,
hasFavorites: Boolean,
onNavigate: (NavDestination) -> Unit,
@@ -148,6 +150,7 @@ fun Sidebar(
) {
NavDestination.values().forEach { destination ->
val shouldShow = when (destination) {
NavDestination.HISTORY -> hasHistory || currentDestination == NavDestination.HISTORY
NavDestination.WATCH_LATER -> hasWatchLater || currentDestination == NavDestination.WATCH_LATER
NavDestination.FAVORITES -> hasFavorites || currentDestination == NavDestination.FAVORITES
else -> true

View File

@@ -34,7 +34,8 @@ fun LibraryScreen(
title: String,
items: List<MediaItem>,
onOpenDetail: (MediaItem) -> Unit,
modifier: Modifier = Modifier
modifier: Modifier = Modifier,
emptyMessage: String = "Nothing here yet. Open a title, press the ⋮ button and add it."
) {
var ambientUrl by remember(items) { mutableStateOf(items.firstOrNull()?.ambientBackdropUrl) }
Box(modifier = modifier.fillMaxSize().background(VylaBackground)) {
@@ -86,7 +87,7 @@ fun LibraryScreen(
contentAlignment = androidx.compose.ui.Alignment.TopStart
) {
Text(
text = "Nothing here yet. Open a title, press the ⋮ button and add it.",
text = emptyMessage,
color = VylaTextMuted,
fontSize = 17.sp
)