From e1542100b79e3cedd7286e9ef80dd62f8f1a756f Mon Sep 17 00:00:00 2001 From: kKaskak <117831817+kKaskak@users.noreply.github.com> Date: Mon, 12 Feb 2024 10:26:31 +0100 Subject: [PATCH] refactor: move the logic to update_library --- src/models/ctx/mod.rs | 2 +- src/models/ctx/update_library.rs | 33 ++++++++++++++++++++++++++++++-- src/models/meta_details.rs | 24 ++--------------------- src/runtime/msg/internal.rs | 2 +- 4 files changed, 35 insertions(+), 26 deletions(-) diff --git a/src/models/ctx/mod.rs b/src/models/ctx/mod.rs index 7bae221e8..60514dd66 100644 --- a/src/models/ctx/mod.rs +++ b/src/models/ctx/mod.rs @@ -1,7 +1,7 @@ mod update_events; use update_events::*; -mod update_library; +pub mod update_library; use update_library::*; mod update_notifications; diff --git a/src/models/ctx/update_library.rs b/src/models/ctx/update_library.rs index d084b82ec..5d01ea2be 100644 --- a/src/models/ctx/update_library.rs +++ b/src/models/ctx/update_library.rs @@ -1,5 +1,7 @@ use std::{collections::HashMap, marker::PhantomData}; +use stremio_watched_bitfield::WatchedBitField; + use futures::{ future::{self, Either}, FutureExt, TryFutureExt, @@ -10,7 +12,10 @@ use crate::{ LIBRARY_COLLECTION_NAME, LIBRARY_RECENT_COUNT, LIBRARY_RECENT_STORAGE_KEY, LIBRARY_STORAGE_KEY, }, - models::ctx::{CtxError, CtxStatus, OtherError}, + models::{ + common::{Loadable, ResourceLoadable, eq_update}, + ctx::{CtxError, CtxStatus, OtherError} +}, runtime::{ msg::{Action, ActionCtx, CtxAuthResponse, Event, Internal, Msg}, Effect, EffectFuture, Effects, Env, EnvFutureExt, @@ -21,6 +26,7 @@ use crate::{ LibraryItemsResponse, SuccessResponse, }, library::{LibraryBucket, LibraryBucketRef, LibraryItem}, + resource::MetaItem, profile::{AuthKey, Profile}, }, }; @@ -279,7 +285,10 @@ pub fn update_library( match library.items.get(id) { Some(library_item) => { let mut library_item = library_item.to_owned(); - library_item.state.watched = *is_watched; + // println!("{:?}", library_item); + + // watched is a Option ? how do i change it to true / false + // library_item.state.watched = *is_watched; Effects::msg(Msg::Internal(Internal::UpdateLibraryItem(library_item))) .join(Effects::msg(Msg::Event(Event::LibraryItemMarkedAsWatched { @@ -487,3 +496,23 @@ fn plan_sync_with_api(library: &LibraryBucket, auth_key: &Auth ) .into() } + +pub fn watched_update( + watched: &mut Option, + meta_items: &[ResourceLoadable], + library_item: &Option, +) -> Effects { + let next_watched = meta_items + .iter() + .find_map(|meta_item| match &meta_item.content { + Some(Loadable::Ready(meta_item)) => Some(meta_item), + _ => None, + }) + .and_then(|meta_item| { + library_item + .as_ref() + .map(|library_item| (meta_item, library_item)) + }) + .map(|(meta_item, library_item)| library_item.state.watched_bitfield(&meta_item.videos)); + eq_update(watched, next_watched) +} \ No newline at end of file diff --git a/src/models/meta_details.rs b/src/models/meta_details.rs index 202c6e0c3..aec7ca225 100644 --- a/src/models/meta_details.rs +++ b/src/models/meta_details.rs @@ -11,7 +11,7 @@ use crate::{ eq_update, resources_update, resources_update_with_vector_content, Loadable, ResourceLoadable, ResourcesAction, }, - ctx::Ctx, + ctx::{Ctx, update_library::watched_update}, }, runtime::{ msg::{Action, ActionLoad, ActionMetaDetails, Internal, Msg}, @@ -531,24 +531,4 @@ fn library_item_update( _ => None, }; eq_update(library_item, next_library_item) -} - -fn watched_update( - watched: &mut Option, - meta_items: &[ResourceLoadable], - library_item: &Option, -) -> Effects { - let next_watched = meta_items - .iter() - .find_map(|meta_item| match &meta_item.content { - Some(Loadable::Ready(meta_item)) => Some(meta_item), - _ => None, - }) - .and_then(|meta_item| { - library_item - .as_ref() - .map(|library_item| (meta_item, library_item)) - }) - .map(|(meta_item, library_item)| library_item.state.watched_bitfield(&meta_item.videos)); - eq_update(watched, next_watched) -} +} \ No newline at end of file diff --git a/src/runtime/msg/internal.rs b/src/runtime/msg/internal.rs index 1b90593bb..e92e339fc 100644 --- a/src/runtime/msg/internal.rs +++ b/src/runtime/msg/internal.rs @@ -146,6 +146,6 @@ pub enum Internal { ), /// When dismissed events changed DismissedEventsChanged, - /// Marks a library item as watched or unwatched. (Overrides the current state) + /// Marks a library item as watched or unwatched. Overrides the current state. LibraryItemMarkAsWatched { id: String, is_watched: bool }, }