Skip to content

Commit

Permalink
refactor: move the logic to update_library
Browse files Browse the repository at this point in the history
  • Loading branch information
kKaskak committed Feb 12, 2024
1 parent dc446cb commit e154210
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 26 deletions.
2 changes: 1 addition & 1 deletion src/models/ctx/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
mod update_events;
use update_events::*;

mod update_library;
pub mod update_library;
use update_library::*;

mod update_notifications;
Expand Down
33 changes: 31 additions & 2 deletions src/models/ctx/update_library.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use std::{collections::HashMap, marker::PhantomData};

use stremio_watched_bitfield::WatchedBitField;

use futures::{
future::{self, Either},
FutureExt, TryFutureExt,
Expand All @@ -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,
Expand All @@ -21,6 +26,7 @@ use crate::{
LibraryItemsResponse, SuccessResponse,
},
library::{LibraryBucket, LibraryBucketRef, LibraryItem},
resource::MetaItem,
profile::{AuthKey, Profile},
},
};
Expand Down Expand Up @@ -279,7 +285,10 @@ pub fn update_library<E: Env + 'static>(
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<WatchedField> ? 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 {
Expand Down Expand Up @@ -487,3 +496,23 @@ fn plan_sync_with_api<E: Env + 'static>(library: &LibraryBucket, auth_key: &Auth
)
.into()
}

pub fn watched_update(
watched: &mut Option<WatchedBitField>,
meta_items: &[ResourceLoadable<MetaItem>],
library_item: &Option<LibraryItem>,
) -> 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)
}
24 changes: 2 additions & 22 deletions src/models/meta_details.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -531,24 +531,4 @@ fn library_item_update<E: Env + 'static>(
_ => None,
};
eq_update(library_item, next_library_item)
}

fn watched_update(
watched: &mut Option<WatchedBitField>,
meta_items: &[ResourceLoadable<MetaItem>],
library_item: &Option<LibraryItem>,
) -> 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)
}
}
2 changes: 1 addition & 1 deletion src/runtime/msg/internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
}

0 comments on commit e154210

Please sign in to comment.