Fixed pagination bug that caused items not to load
This commit is contained in:
committed by
Henrik Friedrichsen
parent
c1f0c5409c
commit
2be126430d
@@ -59,7 +59,7 @@ impl<I: ListItem> Scroller for ListView<I> {
|
||||
|
||||
impl<I: ListItem> ListView<I> {
|
||||
pub fn new(content: Arc<RwLock<Vec<I>>>, queue: Arc<Queue>, library: Arc<Library>) -> Self {
|
||||
Self {
|
||||
let result = Self {
|
||||
content,
|
||||
last_content_len: 0,
|
||||
selected: 0,
|
||||
@@ -72,7 +72,9 @@ impl<I: ListItem> ListView<I> {
|
||||
library,
|
||||
pagination: Pagination::default(),
|
||||
title: "".to_string(),
|
||||
}
|
||||
};
|
||||
result.try_paginate();
|
||||
result
|
||||
}
|
||||
|
||||
pub fn with_title(mut self, title: &str) -> Self {
|
||||
@@ -99,10 +101,30 @@ impl<I: ListItem> ListView<I> {
|
||||
}
|
||||
}
|
||||
|
||||
/// Return wether there are still items that aren't shown in the listview.
|
||||
///
|
||||
/// `true` if there are unloaded items
|
||||
/// `false` if all items are loaded
|
||||
fn can_paginate(&self) -> bool {
|
||||
self.get_pagination().max_content().unwrap_or(0) > self.get_pagination().loaded_content()
|
||||
}
|
||||
|
||||
/// Try to load more items into the list if neccessary.
|
||||
#[inline]
|
||||
fn try_paginate(&self) {
|
||||
// Paginate if there are more items
|
||||
// AND
|
||||
// The selected item is the current last item (keyboard scrolling)
|
||||
// OR
|
||||
// The scroller can't scroll further down (mouse scrolling)
|
||||
if self.can_paginate()
|
||||
&& (self.selected == self.content.read().unwrap().len().saturating_sub(1)
|
||||
|| !self.scroller.can_scroll_down())
|
||||
{
|
||||
self.pagination.call(&self.content, self.library.clone());
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_selected_index(&self) -> usize {
|
||||
self.selected
|
||||
}
|
||||
@@ -297,7 +319,10 @@ impl<I: ListItem> View for ListView<I> {
|
||||
Event::Mouse {
|
||||
event: MouseEvent::WheelDown,
|
||||
..
|
||||
} => self.scroller.scroll_down(3),
|
||||
} => {
|
||||
self.scroller.scroll_down(3);
|
||||
self.try_paginate();
|
||||
}
|
||||
Event::Mouse {
|
||||
event: MouseEvent::Press(MouseButton::Left),
|
||||
position,
|
||||
@@ -529,9 +554,7 @@ impl<I: ListItem + Clone> ViewExt for ListView<I> {
|
||||
MoveAmount::Integer(amount) => self.move_focus(*amount),
|
||||
}
|
||||
}
|
||||
if self.selected == last_idx && self.can_paginate() {
|
||||
self.pagination.call(&self.content, self.library.clone());
|
||||
}
|
||||
self.try_paginate();
|
||||
return Ok(CommandResult::Consumed(None));
|
||||
}
|
||||
_ => return Ok(CommandResult::Consumed(None)),
|
||||
|
||||
@@ -85,6 +85,13 @@ impl<I: ListItem + Clone> ApiResult<I> {
|
||||
|
||||
pub type Paginator<I> = Box<dyn Fn(Arc<RwLock<Vec<I>>>) + Send + Sync>;
|
||||
|
||||
/// Manages the loading of ListItems, to increase performance and decrease
|
||||
/// memory usage.
|
||||
///
|
||||
/// `loaded_content`: The amount of currently loaded items
|
||||
/// `max_content`: The maximum amount of items
|
||||
/// `callback`: TODO: document
|
||||
/// `busy`: TODO: document
|
||||
pub struct Pagination<I: ListItem> {
|
||||
loaded_content: Arc<RwLock<usize>>,
|
||||
max_content: Arc<RwLock<Option<usize>>>,
|
||||
|
||||
Reference in New Issue
Block a user