Skip to content

Commit

Permalink
✨ Autoload feed list (#138)
Browse files Browse the repository at this point in the history
  • Loading branch information
Schmiddiii committed Jun 9, 2023
1 parent 3ba6a65 commit 955da78
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 41 deletions.
55 changes: 16 additions & 39 deletions data/resources/ui/feed_list.ui
Expand Up @@ -9,51 +9,28 @@
<property name="hexpand">True</property>
<property name="halign">GTK_ALIGN_FILL</property>
<child>
<object class="GtkScrolledWindow">
<object class="GtkScrolledWindow" id="scrolled_window">
<property name="hscrollbar-policy">GTK_POLICY_NEVER</property>
<signal name="edge-reached" handler="edge_reached" swapped="true"/>

<child>
<object class="AdwClamp">
<object class="AdwClampScrollable">
<child>
<object class="GtkBox">
<property name="orientation">vertical</property>
<object class="GtkListView" id="feed_list">
<style>
<class name="card"/>
<class name="separators"/>
<class name="feed-list"/>
</style>
<property name="margin-start">5</property>
<property name="margin-end">5</property>
<property name="margin-top">5</property>
<property name="margin-bottom">5</property>

<property name="vexpand">True</property>
<property name="valign">GTK_ALIGN_FILL</property>
<property name="valign">GTK_ALIGN_START</property>
<property name="hexpand">True</property>
<property name="halign">GTK_ALIGN_FILL</property>
<child>
<object class="GtkListView" id="feed_list">
<style>
<class name="card"/>
<class name="separators"/>
<class name="feed-list"/>
</style>
<property name="margin-start">5</property>
<property name="margin-end">5</property>
<property name="margin-top">5</property>
<property name="margin-bottom">5</property>

<property name="vexpand">True</property>
<property name="valign">GTK_ALIGN_START</property>
<property name="hexpand">True</property>
<property name="halign">GTK_ALIGN_FILL</property>
<!-- Fix issue where highlighting the first/last item in the list would cut off the highlighting -->
<property name="overflow">visible</property>
</object>
</child>
<child>
<object class="GtkButton" id="load_more">
<binding name="visible">
<lookup name="more-available" type="TFFeedList">
</lookup>
</binding>
<property name="label" translatable="yes">Load more</property>
<property name="vexpand">False</property>
<property name="valign">GTK_ALIGN_FILL</property>
<property name="hexpand">True</property>
<property name="halign">GTK_ALIGN_FILL</property>
<property name="action-name">feed.more</property>
</object>
</child>
</object>
</child>
</object>
Expand Down
36 changes: 34 additions & 2 deletions src/gui/feed/feed_list.rs
Expand Up @@ -22,11 +22,15 @@ use std::cmp::min;

use gdk::{
gio::{SimpleAction, SimpleActionGroup},
glib,
glib::clone,
prelude::{ActionMapExt, ListModelExt, ObjectExt, ToValue},
subclass::prelude::ObjectSubclassIsExt,
};
use gtk::traits::WidgetExt;
use gtk::{
traits::{AdjustmentExt, WidgetExt},
Adjustment,
};
use tf_join::AnyVideo;
use tf_playlist::PlaylistManager;

Expand Down Expand Up @@ -64,6 +68,20 @@ impl FeedList {
actions.add_action(&action_more);
}

fn setup_autoload(&self) {
let adj = self.imp().scrolled_window.vadjustment();
adj.connect_changed(clone!(@weak self as s => move |adj| {
s.load_if_screen_not_filled(adj);
}));
}

fn load_if_screen_not_filled(&self, adj: &Adjustment) {
if self.property("more-available") && adj.upper() <= adj.page_size() {
// The screen is not yet filled.
let _ = self.activate_action("feed.more", None);
}
}

pub fn set_items(&self, new_items: Vec<VideoObject>) {
let imp = self.imp();
let items = &imp.items;
Expand Down Expand Up @@ -142,6 +160,7 @@ pub mod imp {
use gtk::glib;
use gtk::prelude::*;
use gtk::subclass::prelude::*;
use gtk::PositionType;
use gtk::SignalListItemFactory;
use gtk::Widget;

Expand All @@ -159,7 +178,7 @@ pub mod imp {
#[template_child]
pub(super) feed_list: TemplateChild<gtk::ListView>,
#[template_child]
load_more: TemplateChild<gtk::Button>,
pub(super) scrolled_window: TemplateChild<gtk::ScrolledWindow>,

pub(super) items: RefCell<Vec<VideoObject>>,
pub(super) model: RefCell<ListStore>,
Expand Down Expand Up @@ -205,6 +224,18 @@ pub mod imp {

video_object.play();
});

self.instance().setup_autoload();
}
}

#[gtk::template_callbacks]
impl FeedList {
#[template_callback]
fn edge_reached(&self, pos: PositionType) {
if pos == PositionType::Bottom {
let _ = WidgetExt::activate_action(&self.instance(), "feed.more", None);
}
}
}

Expand All @@ -216,6 +247,7 @@ pub mod imp {

fn class_init(klass: &mut Self::Class) {
Self::bind_template(klass);
Self::bind_template_callbacks(klass);
}

fn instance_init(obj: &InitializingObject<Self>) {
Expand Down

0 comments on commit 955da78

Please sign in to comment.