Skip to content

Commit

Permalink
Merge pull request #674 from Stremio/fix/meta-details-mark-item-as-wa…
Browse files Browse the repository at this point in the history
…tched

Fix: meta details mark item as watched
  • Loading branch information
elpiel authored Apr 24, 2024
2 parents 6265784 + 134c578 commit b81ebad
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 37 deletions.
26 changes: 4 additions & 22 deletions src/models/ctx/update_library.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,11 +146,10 @@ pub fn update_library<E: Env + 'static>(
Msg::Action(Action::Ctx(ActionCtx::LibraryItemMarkAsWatched { id, is_watched })) => {
match library.items.get(id) {
Some(library_item) => {
Effects::msg(Msg::Internal(Internal::LibraryItemMarkAsWatched {
id: library_item.id.clone(),
is_watched: *is_watched,
}))
.unchanged()
let mut library_item = library_item.to_owned();
library_item.mark_as_watched::<E>(*is_watched);
Effects::msg(Msg::Internal(Internal::UpdateLibraryItem(library_item)))
.unchanged()
}
_ => Effects::none().unchanged(),
}
Expand Down Expand Up @@ -287,23 +286,6 @@ pub fn update_library<E: Env + 'static>(
}))
.unchanged(),
},
Msg::Internal(Internal::LibraryItemMarkAsWatched { id, is_watched }) => {
match library.items.get(id) {
Some(library_item) => {
let mut library_item = library_item.to_owned();
if *is_watched {
library_item.state.times_watched += 1;
library_item.state.last_watched = Some(E::now());
} else {
library_item.state.times_watched = 0;
}

Effects::msg(Msg::Internal(Internal::UpdateLibraryItem(library_item)))
.unchanged()
}
_ => Effects::none().unchanged(),
}
}
_ => Effects::none().unchanged(),
}
}
Expand Down
1 change: 1 addition & 0 deletions src/models/library_with_filters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,7 @@ mod test {
state: LibraryItemState {
last_watched: Some(Utc::now() - Duration::weeks(1)),
flagged_watched: 1,
times_watched: 1,
..Default::default()
},
behavior_hints: crate::types::resource::MetaItemBehaviorHints::default(),
Expand Down
9 changes: 4 additions & 5 deletions src/models/meta_details.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,10 @@ impl<E: Env + 'static> UpdateWithCtx<E> for MetaDetails {
Msg::Action(Action::MetaDetails(ActionMetaDetails::MarkAsWatched(is_watched))) => {
match &self.library_item {
Some(library_item) => {
Effects::msg(Msg::Internal(Internal::LibraryItemMarkAsWatched {
id: library_item.id.clone(),
is_watched: *is_watched,
}))
.unchanged()
let mut library_item = library_item.to_owned();
library_item.mark_as_watched::<E>(*is_watched);
Effects::msg(Msg::Internal(Internal::UpdateLibraryItem(library_item)))
.unchanged()
}
_ => Effects::none().unchanged(),
}
Expand Down
5 changes: 0 additions & 5 deletions src/runtime/msg/internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,4 @@ pub enum Internal {
),
/// When dismissed events changed
DismissedEventsChanged,
/// Marks a library item as watched or unwatched. (Overrides the current state)
LibraryItemMarkAsWatched {
id: LibraryItemId,
is_watched: bool,
},
}
16 changes: 11 additions & 5 deletions src/types/library/library_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,10 @@ impl LibraryItem {
}
}

/// Returns whether the item has been watched when either of the state fields are:
/// - `times_watched > 0`
/// or
/// - `flagged_watched == 1` (true)
/// Returns whether the item has been watched
#[inline]
pub fn watched(&self) -> bool {
self.state.times_watched > 0 || self.state.flagged_watched == 1
self.state.times_watched > 0
}

/// Pulling notifications relies on a few key things:
Expand Down Expand Up @@ -100,6 +97,15 @@ impl LibraryItem {
&& self.poster_shape == other.poster_shape
&& self.behavior_hints == other.behavior_hints
}

pub fn mark_as_watched<E: Env>(&mut self, is_watched: bool) {
if is_watched {
self.state.times_watched = self.state.times_watched.saturating_add(1);
self.state.last_watched = Some(E::now());
} else {
self.state.times_watched = 0;
}
}
}

impl<E: Env + 'static> From<(&MetaItemPreview, PhantomData<E>)> for LibraryItem {
Expand Down

0 comments on commit b81ebad

Please sign in to comment.