Load sidebar entries automatically in 200-item batches.

This commit is contained in:
2026-07-20 13:21:14 +02:00
parent 7887d02e0b
commit df71f1de95
10 changed files with 94 additions and 58 deletions

View File

@@ -347,8 +347,7 @@ pub enum Message {
items: Vec<Media>,
thumbs: HashMap<String, Option<std::path::PathBuf>>,
},
LoadMorePosts,
LoadMoreMedia,
SidebarScrolled(f32),
CreatePost,
CreatePage,
@@ -886,6 +885,8 @@ pub struct BdsApp {
sidebar_media_thumbs: HashMap<String, Option<std::path::PathBuf>>,
sidebar_posts_has_more: bool,
sidebar_media_has_more: bool,
sidebar_posts_loading_more: bool,
sidebar_media_loading_more: bool,
// Sidebar filters (per sidebar_views.allium PostsView / MediaView)
post_filter: PostFilter,
@@ -1089,6 +1090,8 @@ impl BdsApp {
sidebar_media_thumbs: HashMap::new(),
sidebar_posts_has_more: false,
sidebar_media_has_more: false,
sidebar_posts_loading_more: false,
sidebar_media_loading_more: false,
post_filter: PostFilter::default(),
page_filter: PostFilter::default(),
media_filter: MediaFilter::default(),
@@ -1180,6 +1183,8 @@ impl BdsApp {
sidebar_media_thumbs: HashMap::new(),
sidebar_posts_has_more: false,
sidebar_media_has_more: false,
sidebar_posts_loading_more: false,
sidebar_media_loading_more: false,
post_filter: PostFilter::default(),
page_filter: PostFilter::default(),
media_filter: MediaFilter::default(),
@@ -2702,8 +2707,7 @@ impl BdsApp {
| Message::SidebarMediaLoaded { .. }
| Message::SidebarPostsAppended(_)
| Message::SidebarMediaAppended { .. }
| Message::LoadMorePosts
| Message::LoadMoreMedia) => self.handle_sidebar_message(message),
| Message::SidebarScrolled(_)) => self.handle_sidebar_message(message),
// ── Modal ──
Message::ShowModal(state) => {

View File

@@ -3,8 +3,12 @@ use chrono::Datelike;
impl BdsApp {
/// Number of items to load per sidebar page.
/// Matches the TypeScript app's limit of 500 for initial load.
pub(super) const SIDEBAR_PAGE_SIZE: i64 = 500;
pub(super) const SIDEBAR_PAGE_SIZE: i64 = 200;
const SIDEBAR_AUTO_LOAD_THRESHOLD: f32 = 0.9;
fn should_auto_load_more(relative_y: f32, has_more: bool, loading: bool) -> bool {
relative_y >= Self::SIDEBAR_AUTO_LOAD_THRESHOLD && has_more && !loading
}
/// Refresh only sidebar posts using current filter state (async).
pub(super) fn refresh_sidebar_posts(&mut self) -> Task<Message> {
@@ -94,9 +98,13 @@ impl BdsApp {
/// Load more posts (append to existing sidebar data).
pub(super) fn load_more_sidebar_posts(&mut self) -> Task<Message> {
if self.sidebar_posts_loading_more || !self.sidebar_posts_has_more {
return Task::none();
}
let (Some(_), Some(project)) = (&self.db, &self.active_project) else {
return Task::none();
};
self.sidebar_posts_loading_more = true;
let db_path = self.db_path.clone();
let project_id = project.id.clone();
@@ -378,9 +386,13 @@ impl BdsApp {
/// Load more media (append to existing sidebar data).
pub(super) fn load_more_sidebar_media(&mut self) -> Task<Message> {
if self.sidebar_media_loading_more || !self.sidebar_media_has_more {
return Task::none();
}
let (Some(_), Some(project)) = (&self.db, &self.active_project) else {
return Task::none();
};
self.sidebar_media_loading_more = true;
let db_path = self.db_path.clone();
let project_id = project.id.clone();
@@ -617,12 +629,14 @@ impl BdsApp {
// ── Async sidebar data ──
Message::SidebarPostsLoaded(mut posts) => {
self.sidebar_posts_loading_more = false;
self.sidebar_posts_has_more = posts.len() > Self::SIDEBAR_PAGE_SIZE as usize;
posts.truncate(Self::SIDEBAR_PAGE_SIZE as usize);
self.sidebar_posts = posts;
Task::none()
}
Message::SidebarMediaLoaded { mut items, thumbs } => {
self.sidebar_media_loading_more = false;
self.sidebar_media_has_more = items.len() > Self::SIDEBAR_PAGE_SIZE as usize;
items.truncate(Self::SIDEBAR_PAGE_SIZE as usize);
self.sidebar_media = items;
@@ -630,21 +644,60 @@ impl BdsApp {
Task::none()
}
Message::SidebarPostsAppended(mut posts) => {
self.sidebar_posts_loading_more = false;
self.sidebar_posts_has_more = posts.len() > Self::SIDEBAR_PAGE_SIZE as usize;
posts.truncate(Self::SIDEBAR_PAGE_SIZE as usize);
self.sidebar_posts.extend(posts);
Task::none()
}
Message::SidebarMediaAppended { mut items, thumbs } => {
self.sidebar_media_loading_more = false;
self.sidebar_media_has_more = items.len() > Self::SIDEBAR_PAGE_SIZE as usize;
items.truncate(Self::SIDEBAR_PAGE_SIZE as usize);
self.sidebar_media.extend(items);
self.sidebar_media_thumbs.extend(thumbs);
Task::none()
}
Message::LoadMorePosts => self.load_more_sidebar_posts(),
Message::LoadMoreMedia => self.load_more_sidebar_media(),
Message::SidebarScrolled(relative_y) => match self.sidebar_view {
SidebarView::Posts | SidebarView::Pages
if Self::should_auto_load_more(
relative_y,
self.sidebar_posts_has_more,
self.sidebar_posts_loading_more,
) =>
{
self.load_more_sidebar_posts()
}
SidebarView::Media
if Self::should_auto_load_more(
relative_y,
self.sidebar_media_has_more,
self.sidebar_media_loading_more,
) =>
{
self.load_more_sidebar_media()
}
_ => Task::none(),
},
_ => unreachable!("non-sidebar message routed to sidebar handler"),
}
}
}
#[cfg(test)]
mod tests {
use super::BdsApp;
#[test]
fn sidebar_page_size_is_200() {
assert_eq!(BdsApp::SIDEBAR_PAGE_SIZE, 200);
}
#[test]
fn sidebar_auto_loads_once_near_the_end() {
assert!(!BdsApp::should_auto_load_more(0.89, true, false));
assert!(BdsApp::should_auto_load_more(0.9, true, false));
assert!(!BdsApp::should_auto_load_more(1.0, false, false));
assert!(!BdsApp::should_auto_load_more(1.0, true, true));
}
}

View File

@@ -825,23 +825,6 @@ pub fn view(
top_items.push(make_post_item(p));
}
}
// "Load More" button
if posts_has_more {
top_items.push(Space::with_height(4.0).into());
top_items.push(
button(
text(t(locale, "sidebar.loadMore"))
.size(11)
.shaping(Shaping::Advanced),
)
.on_press(Message::LoadMorePosts)
.padding([4, 8])
.width(Length::Fill)
.style(filter_button_style)
.into(),
);
}
}
iced::widget::Column::with_children(top_items)
@@ -980,23 +963,6 @@ pub fn view(
})
.collect();
top_items.extend(items);
// "Load More" button
if media_has_more {
top_items.push(Space::with_height(4.0).into());
top_items.push(
button(
text(t(locale, "sidebar.loadMore"))
.size(11)
.shaping(Shaping::Advanced),
)
.on_press(Message::LoadMoreMedia)
.padding([4, 8])
.width(Length::Fill)
.style(filter_button_style)
.into(),
);
}
}
iced::widget::Column::with_children(top_items)
@@ -1254,15 +1220,21 @@ pub fn view(
.padding(12);
// layout.allium: sidebar width is resizable, passed as parameter
container(
scrollable(content)
.direction(scrollable::Direction::Vertical(inputs::compact_scrollbar()))
.style(inputs::scrollable_style),
)
.width(Length::Fixed(width))
.height(Length::Fill)
.style(sidebar_style)
.into()
let mut scrolling = scrollable(content)
.direction(scrollable::Direction::Vertical(inputs::compact_scrollbar()))
.style(inputs::scrollable_style);
if matches!(sidebar_view, SidebarView::Posts | SidebarView::Pages) && posts_has_more
|| sidebar_view == SidebarView::Media && media_has_more
{
scrolling =
scrolling.on_scroll(|viewport| Message::SidebarScrolled(viewport.relative_offset().y));
}
container(scrolling)
.width(Length::Fixed(width))
.height(Length::Fill)
.style(sidebar_style)
.into()
}
#[cfg(test)]