Skip to content

Commit

Permalink
refactor!: implement LoadNextPage for LibraryWithFilters
Browse files Browse the repository at this point in the history
  • Loading branch information
tymmesyde committed Feb 27, 2024
1 parent fde8815 commit 9cc62ac
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 59 deletions.
34 changes: 14 additions & 20 deletions src/deep_links/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -519,30 +519,24 @@ impl From<(&String, &LibraryRequest)> for LibraryDeepLinks {
"stremio:///{}/{}?{}",
root,
utf8_percent_encode(r#type, URI_COMPONENT_ENCODE_SET),
query_params_encode(&[
(
"sort",
serde_json::to_value(&request.sort)
.unwrap()
.as_str()
.unwrap()
),
("page", &request.page.to_string())
]),
query_params_encode(&[(
"sort",
serde_json::to_value(&request.sort)
.unwrap()
.as_str()
.unwrap()
)]),
),
_ => format!(
"stremio:///{}?{}",
root,
query_params_encode(&[
(
"sort",
serde_json::to_value(&request.sort)
.unwrap()
.as_str()
.unwrap()
),
("page", &request.page.to_string())
]),
query_params_encode(&[(
"sort",
serde_json::to_value(&request.sort)
.unwrap()
.as_str()
.unwrap()
)]),
),
},
}
Expand Down
83 changes: 46 additions & 37 deletions src/models/library_with_filters.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::{iter, marker::PhantomData, num::NonZeroUsize};

use boolinator::Boolinator;
use derivative::Derivative;
use derive_more::Deref;
use itertools::Itertools;
Expand All @@ -14,7 +13,7 @@ use crate::{
ctx::Ctx,
},
runtime::{
msg::{Action, ActionLoad, Internal, Msg},
msg::{Action, ActionLibraryWithFilters, ActionLoad, Internal, Msg},
Effects, Env, UpdateWithCtx,
},
types::{
Expand Down Expand Up @@ -106,7 +105,6 @@ pub struct SelectablePage {
pub struct Selectable {
pub types: Vec<SelectableType>,
pub sorts: Vec<SelectableSort>,
pub prev_page: Option<SelectablePage>,
pub next_page: Option<SelectablePage>,
}

Expand Down Expand Up @@ -175,6 +173,32 @@ impl<E: Env + 'static, F: LibraryFilter> UpdateWithCtx<E> for LibraryWithFilters
.join(selectable_effects)
.join(catalog_effects)
}
Msg::Action(Action::LibraryWithFilters(ActionLibraryWithFilters::LoadNextPage)) => {
match self.selectable.next_page.as_ref() {
Some(next_page) => {
let next_selected = Some(Selected {
request: next_page.request.to_owned(),
});
let selected_effects = eq_update(&mut self.selected, next_selected);
let selectable_effects = selectable_update::<F>(
&mut self.selectable,
&self.selected,
&ctx.library,
&ctx.notifications,
);
let catalog_effects = catalog_update::<F>(
&mut self.catalog,
&self.selected,
&ctx.library,
&ctx.notifications,
);
selected_effects
.join(selectable_effects)
.join(catalog_effects)
}
_ => Effects::none().unchanged(),
}
}
Msg::Internal(Internal::LibraryChanged(_)) => {
let selectable_effects = selectable_update::<F>(
&mut self.selectable,
Expand Down Expand Up @@ -259,43 +283,29 @@ fn selectable_update<F: LibraryFilter>(
.unwrap_or_default(),
})
.collect();
let (prev_page, next_page) = match selected {
Some(selected) => {
let prev_page = (selected.request.page.get() > 1)
.as_option()
.map(|_| SelectablePage {
request: LibraryRequest {
page: LibraryRequestPage(
NonZeroUsize::new(selected.request.page.get() - 1).unwrap(),
),
..selected.request.to_owned()
},
});
let next_page = library
.items
.values()
.filter(|library_item| F::predicate(library_item, notifications))
.filter(|library_item| match &selected.request.r#type {
Some(r#type) => library_item.r#type == *r#type,
None => true,
})
.nth(selected.request.page.get() * CATALOG_PAGE_SIZE)
.map(|_| SelectablePage {
request: LibraryRequest {
page: LibraryRequestPage(
NonZeroUsize::new(selected.request.page.get() + 1).unwrap(),
),
..selected.request.to_owned()
},
});
(prev_page, next_page)
}
let next_page = match selected {
Some(selected) => library
.items
.values()
.filter(|library_item| F::predicate(library_item, notifications))
.filter(|library_item| match &selected.request.r#type {
Some(r#type) => library_item.r#type == *r#type,
None => true,
})
.nth(selected.request.page.get() * CATALOG_PAGE_SIZE)
.map(|_| SelectablePage {
request: LibraryRequest {
page: LibraryRequestPage(
NonZeroUsize::new(selected.request.page.get() + 1).unwrap(),
),
..selected.request.to_owned()
},
}),
_ => Default::default(),
};
let next_selectable = Selectable {
types: selectable_types,
sorts: selectable_sorts,
prev_page,
next_page,
};
eq_update(selectable, next_selectable)
Expand All @@ -321,8 +331,7 @@ fn catalog_update<F: LibraryFilter>(
Sort::TimesWatched => b.state.times_watched.cmp(&a.state.times_watched),
Sort::Name => a.name.to_lowercase().cmp(&b.name.to_lowercase()),
})
.skip((selected.request.page.get() - 1) * CATALOG_PAGE_SIZE)
.take(CATALOG_PAGE_SIZE)
.take(selected.request.page.get() * CATALOG_PAGE_SIZE)
.cloned()
.collect(),
_ => vec![],
Expand Down
7 changes: 7 additions & 0 deletions src/runtime/msg/action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,12 @@ pub enum ActionLibraryByType {
LoadNextPage(usize),
}

#[derive(Clone, Deserialize, Debug)]
#[serde(tag = "action", content = "args")]
pub enum ActionLibraryWithFilters {
LoadNextPage,
}

#[derive(Clone, Deserialize, Debug)]
#[serde(tag = "action", content = "args")]
pub enum ActionMetaDetails {
Expand Down Expand Up @@ -199,6 +205,7 @@ pub enum Action {
CatalogWithFilters(ActionCatalogWithFilters),
CatalogsWithExtra(ActionCatalogsWithExtra),
LibraryByType(ActionLibraryByType),
LibraryWithFilters(ActionLibraryWithFilters),
MetaDetails(ActionMetaDetails),
StreamingServer(ActionStreamingServer),
Player(ActionPlayer),
Expand Down
4 changes: 2 additions & 2 deletions src/unit_tests/deep_links/library_deep_links.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ fn library_deep_links_request_type() {
let ldl = LibraryDeepLinks::try_from((&root, &request)).unwrap();
assert_eq!(
ldl.library,
"stremio:///library/movie?sort=lastwatched&page=1".to_string()
"stremio:///library/movie?sort=lastwatched".to_string()
);
}

Expand All @@ -35,6 +35,6 @@ fn library_deep_links_request_no_type() {
let ldl = LibraryDeepLinks::try_from((&root, &request)).unwrap();
assert_eq!(
ldl.library,
"stremio:///library?sort=lastwatched&page=1".to_string()
"stremio:///library?sort=lastwatched".to_string()
);
}

0 comments on commit 9cc62ac

Please sign in to comment.