Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add mark season as watched for videos #772

Open
wants to merge 3 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions src/models/meta_details.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,33 @@ impl<E: Env + 'static> UpdateWithCtx<E> for MetaDetails {
}
_ => Effects::none().unchanged(),
},
Msg::Action(Action::MetaDetails(ActionMetaDetails::MarkSeasonAsWatched(
season,
is_watched,
))) => match (&self.library_item, &self.watched) {
(Some(library_item), Some(watched)) => {
// Find videos of given season from the first ready meta item loadable
let videos = self
.meta_items
.iter()
.find(|meta_item| matches!(&meta_item.content, Some(Loadable::Ready(_))))
.and_then(|meta_item| meta_item.content.as_ref())
.and_then(|meta_item| meta_item.ready())
.map(|meta_item| meta_item.videos_by_season(*season));

match videos {
Some(videos) => {
let mut library_item = library_item.to_owned();
library_item.mark_videos_as_watched::<E>(watched, videos, *is_watched);

Effects::msg(Msg::Internal(Internal::UpdateLibraryItem(library_item)))
.unchanged()
}
None => Effects::none().unchanged(),
}
}
_ => Effects::none().unchanged(),
},
Msg::Internal(Internal::ResourceRequestResult(request, result))
if request.path.resource == META_RESOURCE_NAME =>
{
Expand Down
31 changes: 31 additions & 0 deletions src/models/player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -675,6 +675,37 @@ impl<E: Env + 'static> UpdateWithCtx<E> for Player {
_ => Effects::none().unchanged(),
}
}
Msg::Action(Action::Player(ActionPlayer::MarkSeasonAsWatched(season, is_watched))) => {
match (&self.library_item, &self.watched) {
(Some(library_item), Some(watched)) => {
// Find videos of given season from the meta item loadable
let videos = self
.meta_item
.as_ref()
.and_then(|meta_item| meta_item.content.as_ref())
.and_then(|meta_item| meta_item.ready())
.map(|meta_item| meta_item.videos_by_season(*season));

match videos {
Some(videos) => {
let mut library_item = library_item.to_owned();
library_item.mark_videos_as_watched::<E>(
watched,
videos,
*is_watched,
);

Effects::msg(Msg::Internal(Internal::UpdateLibraryItem(
library_item,
)))
.unchanged()
}
None => Effects::none().unchanged(),
}
}
_ => Effects::none().unchanged(),
}
}
Msg::Internal(Internal::LibraryChanged(_)) => {
let library_item_effects = library_item_update::<E>(
&mut self.library_item,
Expand Down
4 changes: 4 additions & 0 deletions src/runtime/msg/action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ pub enum ActionMetaDetails {
///
/// [`LibraryItem`]: crate::types::library::LibraryItem
MarkVideoAsWatched(Video, bool),
/// Mark all videos from given season as watched
MarkSeasonAsWatched(u32, bool),
}

#[derive(Clone, Deserialize, Debug)]
Expand Down Expand Up @@ -199,6 +201,8 @@ pub enum ActionPlayer {
///
/// [`LibraryItem`]: crate::types::library::LibraryItem
MarkVideoAsWatched(Video, bool),
/// Mark all videos from given season as watched
MarkSeasonAsWatched(u32, bool),
}

#[derive(Clone, Deserialize, Debug)]
Expand Down
28 changes: 28 additions & 0 deletions src/types/library/library_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,34 @@ impl LibraryItem {
};
}
}

pub fn mark_videos_as_watched<E: Env>(
&mut self,
watched: &WatchedBitField,
videos: Vec<&Video>,
is_watched: bool,
) {
let mut watched = watched.to_owned();

for video in &videos {
watched.set_video(&video.id, is_watched);
}

self.state.watched = Some(watched.into());

if is_watched {
self.state.last_watched = match (
&self.state.last_watched,
videos.last().and_then(|last_video| last_video.released),
) {
(Some(last_watched), Some(released)) if last_watched < &released => {
Some(released.to_owned())
}
(None, released) => released.to_owned(),
(last_watched, _) => last_watched.to_owned(),
};
}
}
}

impl<E: Env + 'static> From<(&MetaItemPreview, PhantomData<E>)> for LibraryItem {
Expand Down
13 changes: 13 additions & 0 deletions src/types/resource/meta_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,19 @@ impl MetaItem {
Either::Right(self.videos.iter().rev())
}
}

/// Returns a vector of videos for a given season
pub fn videos_by_season(&self, season: u32) -> Vec<&Video> {
self.videos
.iter()
.filter(|video| {
video
.series_info
.as_ref()
.is_some_and(|series_info| series_info.season == season)
})
.collect_vec()
}
}

#[derive(Default, Clone, PartialEq, Eq, Serialize, Deserialize, Debug)]
Expand Down
Loading