library_with_filters pagination refactored
@@ -8,7 +8,6 @@ use boolinator::Boolinator;
use derivative::Derivative;
use itertools::Itertools;
use serde::{Deserialize, Serialize};
-use std::cmp;
use std::iter;
use std::marker::PhantomData;
use strum::IntoEnumIterator;
@@ -263,8 +262,7 @@ fn catalog_update<F: LibraryFilter>(
library: &LibraryBucket,
) -> Effects {
let next_catalog = match selected {
- Some(selected) => {
- let library_items = library
+ Some(selected) => library
.items
.values()
.filter(|library_item| F::predicate(library_item))
@@ -277,21 +275,14 @@ 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()),
})
- .cloned()
- .collect::<Vec<_>>();
- let library_items_len = library_items.len();
- let initial_page_element = if selected.request.page > 0 {
- cmp::min(
- (selected.request.page - 1) * CATALOG_PAGE_SIZE,
- library_items_len,
- )
+ .skip(if selected.request.page > 0 {
+ (selected.request.page - 1) * CATALOG_PAGE_SIZE
} else {
0
- };
- let final_page_element =
- cmp::min(initial_page_element + CATALOG_PAGE_SIZE, library_items_len);
- library_items[initial_page_element..final_page_element].to_vec()
- }
+ })
+ .take(CATALOG_PAGE_SIZE)
+ .cloned()
+ .collect(),
_ => vec![],
};
eq_update(catalog, next_catalog)
