Skip to content

Commit

Permalink
detach video params from player selected
Browse files Browse the repository at this point in the history
  • Loading branch information
unclekingpin committed Oct 10, 2023
1 parent d12b689 commit bc27157
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 35 deletions.
89 changes: 56 additions & 33 deletions src/models/player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::models::common::{
use crate::models::ctx::Ctx;
use crate::runtime::msg::{Action, ActionLoad, ActionPlayer, Event, Internal, Msg};
use crate::runtime::{Effects, Env, UpdateWithCtx};
use crate::types::addon::{AggrRequest, ExtraExt, ResourcePath, ResourceRequest};
use crate::types::addon::{AggrRequest, Descriptor, ExtraExt, ResourcePath, ResourceRequest};
use crate::types::library::{LibraryBucket, LibraryItem};
use crate::types::profile::Settings as ProfileSettings;
use crate::types::resource::{MetaItem, SeriesInfo, Stream, Subtitles, Video};
Expand Down Expand Up @@ -69,14 +69,14 @@ pub struct Selected {
/// A request to fetch the selected [`MetaItem`].
pub meta_request: Option<ResourceRequest>,
pub subtitles_path: Option<ResourcePath>,
pub video_params: Option<VideoParams>,
}

#[derive(Clone, Derivative, Serialize, Debug)]
#[derivative(Default)]
#[serde(rename_all = "camelCase")]
pub struct Player {
pub selected: Option<Selected>,
pub video_params: Option<VideoParams>,
pub meta_item: Option<ResourceLoadable<MetaItem>>,
pub subtitles: Vec<ResourceLoadable<Vec<Subtitles>>>,
pub next_video: Option<Video>,
Expand Down Expand Up @@ -164,36 +164,12 @@ impl<E: Env + 'static> UpdateWithCtx<E> for Player {
},
_ => eq_update(&mut self.meta_item, None),
};
let subtitles_effects = match &selected.subtitles_path {
Some(subtitles_path) => resources_update_with_vector_content::<E, _>(
&mut self.subtitles,
ResourcesAction::force_request(
&AggrRequest::AllOfResource(ResourcePath {
extra: subtitles_path
.extra
.to_owned()
.extend_one(
&VIDEO_HASH_EXTRA_PROP,
selected
.video_params
.as_ref()
.and_then(|params| params.hash.to_owned()),
)
.extend_one(
&VIDEO_SIZE_EXTRA_PROP,
selected
.video_params
.as_ref()
.and_then(|params| params.size)
.map(|size| size.to_string()),
),
..subtitles_path.to_owned()
}),
&ctx.profile.addons,
),
),
_ => eq_update(&mut self.subtitles, vec![]),
};
let subtitles_effects = subtitles_update::<E>(
&mut self.subtitles,
&self.selected,
&self.video_params,
&ctx.profile.addons,
);
let next_video_effects = next_video_update(
&mut self.next_video,
&self.selected,
Expand Down Expand Up @@ -283,6 +259,7 @@ impl<E: Env + 'static> UpdateWithCtx<E> for Player {
_ => Effects::none().unchanged(),
};
let selected_effects = eq_update(&mut self.selected, None);
let video_params_effects = eq_update(&mut self.video_params, None);
let meta_item_effects = eq_update(&mut self.meta_item, None);
let subtitles_effects = eq_update(&mut self.subtitles, vec![]);
let next_video_effects = eq_update(&mut self.next_video, None);
Expand All @@ -298,6 +275,7 @@ impl<E: Env + 'static> UpdateWithCtx<E> for Player {
switch_to_next_video_effects
.join(push_to_library_effects)
.join(selected_effects)
.join(video_params_effects)
.join(meta_item_effects)
.join(subtitles_effects)
.join(next_video_effects)
Expand All @@ -307,6 +285,17 @@ impl<E: Env + 'static> UpdateWithCtx<E> for Player {
.join(watched_effects)
.join(ended_effects)
}
Msg::Action(Action::Player(ActionPlayer::VideoParamsChanged { video_params })) => {
let video_params_effects =
eq_update(&mut self.video_params, video_params.to_owned());
let subtitles_effects = subtitles_update::<E>(
&mut self.subtitles,
&self.selected,
&self.video_params,
&ctx.profile.addons,
);
video_params_effects.join(subtitles_effects)
}
Msg::Action(Action::Player(ActionPlayer::TimeChanged {
time,
duration,
Expand Down Expand Up @@ -793,6 +782,41 @@ fn watched_update(
.map(|(meta_item, library_item)| library_item.state.watched_bitfield(&meta_item.videos));
eq_update(watched, next_watched)
}

fn subtitles_update<E: Env + 'static>(
subtitles: &mut Vec<ResourceLoadable<Vec<Subtitles>>>,
selected: &Option<Selected>,
video_params: &Option<VideoParams>,
addons: &[Descriptor],
) -> Effects {
match (selected, video_params) {
(
Some(Selected {
subtitles_path: Some(subtitles_path),
..
}),
Some(video_params),
) => resources_update_with_vector_content::<E, _>(
subtitles,
ResourcesAction::force_request(
&AggrRequest::AllOfResource(ResourcePath {
extra: subtitles_path
.extra
.to_owned()
.extend_one(&VIDEO_HASH_EXTRA_PROP, video_params.hash.to_owned())
.extend_one(
&VIDEO_SIZE_EXTRA_PROP,
video_params.size.as_ref().map(|size| size.to_string()),
),
..subtitles_path.to_owned()
}),
&addons,
),
),
_ => eq_update(subtitles, vec![]),
}
}

#[cfg(test)]
mod test {
use chrono::{TimeZone, Utc};
Expand Down Expand Up @@ -836,7 +860,6 @@ mod test {
}),
meta_request: None,
subtitles_path: None,
video_params: None,
};

// Test that it should update the next_streams from the next_video if Video has one stream
Expand Down
6 changes: 5 additions & 1 deletion src/runtime/msg/action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::{
library_by_type::Selected as LibraryByTypeSelected,
library_with_filters::Selected as LibraryWithFiltersSelected,
meta_details::Selected as MetaDetailsSelected,
player::Selected as PlayerSelected,
player::{Selected as PlayerSelected, VideoParams},
streaming_server::{
Settings as StreamingServerSettings,
StatisticsRequest as StreamingServerStatisticsRequest,
Expand Down Expand Up @@ -126,6 +126,10 @@ pub enum ActionLink {
#[derive(Clone, Deserialize, Debug)]
#[serde(tag = "action", content = "args")]
pub enum ActionPlayer {
#[serde(rename_all = "camelCase")]
VideoParamsChanged {
video_params: Option<VideoParams>,
},
TimeChanged {
time: u64,
duration: u64,
Expand Down
1 change: 0 additions & 1 deletion src/unit_tests/ctx/notifications/update_notifications.rs
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,6 @@ fn test_pull_notifications_and_play_in_player() {
},
}),
subtitles_path: None,
video_params: None,
}))),
});
runtime.dispatch(RuntimeAction {
Expand Down

0 comments on commit bc27157

Please sign in to comment.