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 f84daa4
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 22 deletions.
2 changes: 0 additions & 2 deletions src/deep_links/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,6 @@ impl From<(&String, &LibraryRequest)> for LibraryDeepLinks {
.as_str()
.unwrap()
),
("page", &request.page.to_string())
]),
),
_ => format!(
Expand All @@ -541,7 +540,6 @@ impl From<(&String, &LibraryRequest)> for LibraryDeepLinks {
.as_str()
.unwrap()
),
("page", &request.page.to_string())
]),
),
},
Expand Down
51 changes: 31 additions & 20 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,19 +283,9 @@ fn selectable_update<F: LibraryFilter>(
.unwrap_or_default(),
})
.collect();
let (prev_page, next_page) = match selected {
let 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
library
.items
.values()
.filter(|library_item| F::predicate(library_item, notifications))
Expand All @@ -287,15 +301,13 @@ fn selectable_update<F: LibraryFilter>(
),
..selected.request.to_owned()
},
});
(prev_page, next_page)
})
}
_ => 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 +333,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

0 comments on commit f84daa4

Please sign in to comment.