Skip to content

Commit

Permalink
Merge pull request #763 from Stremio/feat/stremio-core-web-metadetail…
Browse files Browse the repository at this point in the history
…s-suggested-stream

feat(stremio-core-web): MetaDetails - add last used stream flag to Streams
  • Loading branch information
elpiel authored Jan 20, 2025
2 parents 5a6b217 + be5e384 commit 93e9853
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 22 deletions.
46 changes: 25 additions & 21 deletions src/models/meta_details.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@ pub struct MetaDetails {
pub meta_items: Vec<ResourceLoadable<MetaItem>>,
pub meta_streams: Vec<ResourceLoadable<Vec<Stream>>>,
pub streams: Vec<ResourceLoadable<Vec<Stream>>>,
pub suggested_stream: Option<ResourceLoadable<Option<Stream>>>,
/// A Stream from addon responses, based on your already watched streams on the device
/// (i.e. [`StreamsBucket`]), which could be played if binge watching or continuing to watch.
///
/// Finds a proper stream for the binge watching group and matching stream source.
pub last_used_stream: Option<ResourceLoadable<Option<Stream>>>,
pub library_item: Option<LibraryItem>,
#[serde(skip_serializing)]
pub watched: Option<WatchedBitField>,
Expand All @@ -64,8 +68,8 @@ impl<E: Env + 'static> UpdateWithCtx<E> for MetaDetails {
meta_streams_update(&mut self.meta_streams, &self.selected, &self.meta_items);
let streams_effects =
streams_update::<E>(&mut self.streams, &self.selected, &ctx.profile);
let suggested_stream_effects = suggested_stream_update(
&mut self.suggested_stream,
let last_used_stream_effects = last_used_stream_update(
&mut self.last_used_stream,
&self.selected,
&self.meta_items,
&self.meta_streams,
Expand All @@ -80,14 +84,14 @@ impl<E: Env + 'static> UpdateWithCtx<E> for MetaDetails {
);
let watched_effects =
watched_update(&mut self.watched, &self.meta_items, &self.library_item);
let libraty_item_sync_effects = library_item_sync(&self.library_item, &ctx.profile);
libraty_item_sync_effects
let library_item_sync_effects = library_item_sync(&self.library_item, &ctx.profile);
library_item_sync_effects
.join(selected_effects)
.join(selected_override_effects)
.join(meta_items_effects)
.join(meta_streams_effects)
.join(streams_effects)
.join(suggested_stream_effects)
.join(last_used_stream_effects)
.join(library_item_effects)
.join(watched_effects)
}
Expand All @@ -97,13 +101,13 @@ impl<E: Env + 'static> UpdateWithCtx<E> for MetaDetails {
let meta_streams_effects = eq_update(&mut self.meta_streams, vec![]);
let streams_effects = eq_update(&mut self.streams, vec![]);
let library_item_effects = eq_update(&mut self.library_item, None);
let suggested_stream_effects = eq_update(&mut self.suggested_stream, None);
let last_used_stream_effects = eq_update(&mut self.last_used_stream, None);
let watched_effects = eq_update(&mut self.watched, None);
selected_effects
.join(meta_items_effects)
.join(meta_streams_effects)
.join(streams_effects)
.join(suggested_stream_effects)
.join(last_used_stream_effects)
.join(library_item_effects)
.join(watched_effects)
}
Expand Down Expand Up @@ -147,8 +151,8 @@ impl<E: Env + 'static> UpdateWithCtx<E> for MetaDetails {
};
let meta_streams_effects =
meta_streams_update(&mut self.meta_streams, &self.selected, &self.meta_items);
let suggested_stream_effects = suggested_stream_update(
&mut self.suggested_stream,
let last_used_stream_effects = last_used_stream_update(
&mut self.last_used_stream,
&self.selected,
&self.meta_items,
&self.meta_streams,
Expand All @@ -167,7 +171,7 @@ impl<E: Env + 'static> UpdateWithCtx<E> for MetaDetails {
.join(meta_items_effects)
.join(meta_streams_effects)
.join(streams_effects)
.join(suggested_stream_effects)
.join(last_used_stream_effects)
.join(library_item_effects)
.join(watched_effects)
}
Expand All @@ -178,15 +182,15 @@ impl<E: Env + 'static> UpdateWithCtx<E> for MetaDetails {
&mut self.streams,
ResourcesAction::ResourceRequestResult { request, result },
);
let suggested_stream_effects = suggested_stream_update(
&mut self.suggested_stream,
let last_used_stream_effects = last_used_stream_update(
&mut self.last_used_stream,
&self.selected,
&self.meta_items,
&self.meta_streams,
&self.streams,
&ctx.streams,
);
streams_effects.join(suggested_stream_effects)
streams_effects.join(last_used_stream_effects)
}
Msg::Internal(Internal::LibraryChanged(_)) => {
let library_item_effects = library_item_update::<E>(
Expand All @@ -206,8 +210,8 @@ impl<E: Env + 'static> UpdateWithCtx<E> for MetaDetails {
meta_streams_update(&mut self.meta_streams, &self.selected, &self.meta_items);
let streams_effects =
streams_update::<E>(&mut self.streams, &self.selected, &ctx.profile);
let suggested_stream_effects = suggested_stream_update(
&mut self.suggested_stream,
let last_used_stream_effects = last_used_stream_update(
&mut self.last_used_stream,
&self.selected,
&self.meta_items,
&self.meta_streams,
Expand All @@ -225,7 +229,7 @@ impl<E: Env + 'static> UpdateWithCtx<E> for MetaDetails {
meta_items_effects
.join(meta_streams_effects)
.join(streams_effects)
.join(suggested_stream_effects)
.join(last_used_stream_effects)
.join(library_item_effects)
.join(watched_effects)
}
Expand Down Expand Up @@ -430,16 +434,16 @@ fn streams_update<E: Env + 'static>(
/// One note, why we cannot return `StreamItem.stream` directly if it's for the same episode,
/// is that user might have played a stream from an addon which he no longer has due to some constrains (ie p2p addon),
/// that's why we have to try to find it first and verify that's it's still available.
fn suggested_stream_update(
suggested_stream: &mut Option<ResourceLoadable<Option<Stream>>>,
fn last_used_stream_update(
last_used_stream: &mut Option<ResourceLoadable<Option<Stream>>>,
selected: &Option<Selected>,
meta_items: &[ResourceLoadable<MetaItem>],
meta_streams: &[ResourceLoadable<Vec<Stream>>],
streams: &[ResourceLoadable<Vec<Stream>>],
stream_bucket: &StreamsBucket,
) -> Effects {
let all_streams = [meta_streams, streams].concat();
let next_suggested_stream = match selected {
let next_last_used_stream = match selected {
Some(Selected {
stream_path: Some(stream_path),
..
Expand Down Expand Up @@ -493,7 +497,7 @@ fn suggested_stream_update(
}),
_ => None,
};
eq_update(suggested_stream, next_suggested_stream)
eq_update(last_used_stream, next_last_used_stream)
}

fn library_item_update<E: Env + 'static>(
Expand Down
17 changes: 16 additions & 1 deletion stremio-core-web/src/model/serialize_meta_details.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,14 @@ mod model {
pub struct Stream<'a> {
#[serde(flatten)]
pub stream: &'a stremio_core::types::resource::Stream,
// Watch progress percentage
/// Watch progress percentage
pub progress: Option<f64>,
pub deep_links: StreamDeepLinks,
/// Whether or not this is a stream the user has already played
/// Only 1 stream can be the last used one!
///
/// Find out more about how we select it from the StreamsBucket in the core's model.
pub last_used: Option<bool>,
}
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
Expand Down Expand Up @@ -188,6 +193,7 @@ pub fn serialize_meta_details<E: Env + 'static>(
&ctx.profile.settings,
))
.into_web_deep_links(),
last_used: None,
})
.collect::<Vec<_>>(),
in_library: ctx
Expand Down Expand Up @@ -276,6 +282,15 @@ pub fn serialize_meta_details<E: Env + 'static>(
},
)
.into_web_deep_links(),
last_used: meta_details.last_used_stream.as_ref().and_then(
|resource| match resource.content.as_ref() {
Some(Loadable::Ready(Some(suggested_stream))) => {
Some(suggested_stream == stream)
}
Some(Loadable::Ready(None)) => Some(false),
_ => None,
},
),
})
.collect::<Vec<_>>(),
),
Expand Down

0 comments on commit 93e9853

Please sign in to comment.