Skip to content

Commit

Permalink
feat(term): paginate entries and feeds
Browse files Browse the repository at this point in the history
  • Loading branch information
ymgyt committed May 3, 2024
1 parent 8664e3d commit 794f65d
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 41 deletions.
54 changes: 34 additions & 20 deletions crates/synd_term/src/application/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ pub enum EventLoopControlFlow {
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ListAction {
pub enum Populate {
Append,
Replace,
}
Expand Down Expand Up @@ -388,22 +388,29 @@ impl Application {
self.should_render = true;
}
Command::FetchSubscription { after, first } => {
self.fetch_subscription(ListAction::Append, after, first);
self.fetch_subscription(Populate::Append, after, first);
}
Command::UpdateSubscriptionState {
action,
Command::PopulateFetchedSubscription {
populate,
subscription,
request_seq,
} => {
self.in_flight.remove(request_seq);
// paginate
if subscription.feeds.page_info.has_next_page {
next = Some(Command::FetchSubscription {
after: subscription.feeds.page_info.end_cursor.clone(),
first: config::client::INITIAL_FEEDS_TO_FETCH,
});
}
self.components
.subscription
.update_subscription(action, subscription);
.update_subscription(populate, subscription);
self.should_render = true;
}
Command::ReloadSubscription => {
self.fetch_subscription(
ListAction::Replace,
Populate::Replace,
None,
config::client::INITIAL_FEEDS_TO_FETCH,
);
Expand All @@ -413,7 +420,7 @@ impl Application {
self.in_flight.remove(request_seq);
self.components.subscription.upsert_subscribed_feed(feed);
self.fetch_entries(
ListAction::Replace,
Populate::Replace,
None,
config::client::INITIAL_ENTRIES_TO_FETCH,
);
Expand All @@ -425,7 +432,7 @@ impl Application {
self.components.entries.remove_unsubscribed_entries(&url);
self.components.filter.update_categories(
&self.categories,
ListAction::Replace,
Populate::Replace,
self.components.entries.entries(),
);
self.should_render = true;
Expand All @@ -434,25 +441,32 @@ impl Application {
self.open_feed();
}
Command::FetchEntries { after, first } => {
self.fetch_entries(ListAction::Append, after, first);
self.fetch_entries(Populate::Append, after, first);
}
Command::UpdateEntriesState {
action,
Command::PopulateFetchedEntries {
populate,
payload,
request_seq,
} => {
self.in_flight.remove(request_seq);
self.components.filter.update_categories(
&self.categories,
action,
populate,
payload.entries.as_slice(),
);
self.components.entries.update_entries(action, payload);
// paginate
if payload.page_info.has_next_page {
next = Some(Command::FetchEntries {
after: payload.page_info.end_cursor.clone(),
first: config::client::INITIAL_ENTRIES_TO_FETCH,
});
}
self.components.entries.update_entries(populate, payload);
self.should_render = true;
}
Command::ReloadEntries => {
self.fetch_entries(
ListAction::Replace,
Populate::Replace,
None,
config::client::INITIAL_ENTRIES_TO_FETCH,
);
Expand Down Expand Up @@ -573,13 +587,13 @@ impl Application {
}

impl Application {
fn fetch_subscription(&mut self, action: ListAction, after: Option<String>, first: i64) {
fn fetch_subscription(&mut self, populate: Populate, after: Option<String>, first: i64) {
let client = self.client.clone();
let request_seq = self.in_flight.add(RequestId::FetchSubscription);
let fut = async move {
match client.fetch_subscription(after, Some(first)).await {
Ok(subscription) => Ok(Command::UpdateSubscriptionState {
action,
Ok(subscription) => Ok(Command::PopulateFetchedSubscription {
populate,
subscription,
request_seq,
}),
Expand Down Expand Up @@ -732,13 +746,13 @@ impl Application {

impl Application {
#[tracing::instrument(skip(self))]
fn fetch_entries(&mut self, action: ListAction, after: Option<String>, first: i64) {
fn fetch_entries(&mut self, populate: Populate, after: Option<String>, first: i64) {
let client = self.client.clone();
let request_seq = self.in_flight.add(RequestId::FetchEntries);
let fut = async move {
match client.fetch_entries(after, first).await {
Ok(payload) => Ok(Command::UpdateEntriesState {
action,
Ok(payload) => Ok(Command::PopulateFetchedEntries {
populate,
payload,
request_seq,
}),
Expand Down
16 changes: 9 additions & 7 deletions crates/synd_term/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use synd_auth::device_flow::{DeviceAccessTokenResponse, DeviceAuthorizationRespo
use synd_feed::types::{Category, FeedUrl};

use crate::{
application::{Direction, ListAction, RequestSequence},
application::{Direction, Populate, RequestSequence},
auth::{AuthenticationProvider, Credential},
client::{
mutation::subscribe_feed::SubscribeFeedInput, payload,
Expand Down Expand Up @@ -64,8 +64,8 @@ pub enum Command {
after: Option<String>,
first: i64,
},
UpdateSubscriptionState {
action: ListAction,
PopulateFetchedSubscription {
populate: Populate,
subscription: SubscriptionOutput,
request_seq: RequestSequence,
},
Expand All @@ -77,8 +77,8 @@ pub enum Command {
after: Option<String>,
first: i64,
},
UpdateEntriesState {
action: ListAction,
PopulateFetchedEntries {
populate: Populate,
payload: payload::FetchEntriesPayload,
request_seq: RequestSequence,
},
Expand Down Expand Up @@ -109,8 +109,10 @@ pub enum Command {
impl Display for Command {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Command::UpdateSubscriptionState { .. } => f.write_str("UpdateSubscription"),
Command::UpdateEntriesState { .. } => f.write_str("UpdateEntries"),
Command::PopulateFetchedSubscription { .. } => {
f.write_str("PopulateFetchedSubscription")
}
Command::PopulateFetchedEntries { .. } => f.write_str("PopulateFetchedEntries"),
Command::CompleteDevieAuthorizationFlow { .. } => {
f.write_str("CompleteDeviceAuthorizationFlow")
}
Expand Down
8 changes: 4 additions & 4 deletions crates/synd_term/src/ui/components/entries.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{
application::{Direction, IndexOutOfRange, ListAction},
application::{Direction, IndexOutOfRange, Populate},
client::payload,
types::{self, RequirementExt, TimeExt},
ui::{
Expand Down Expand Up @@ -37,10 +37,10 @@ impl Entries {
}
}

pub fn update_entries(&mut self, action: ListAction, payload: payload::FetchEntriesPayload) {
pub fn update_entries(&mut self, action: Populate, payload: payload::FetchEntriesPayload) {
match action {
ListAction::Append => self.entries.extend(payload.entries),
ListAction::Replace => self.entries = payload.entries,
Populate::Append => self.entries.extend(payload.entries),
Populate::Replace => self.entries = payload.entries,
}
self.apply_filter();
}
Expand Down
10 changes: 5 additions & 5 deletions crates/synd_term/src/ui/components/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use ratatui::{
use synd_feed::types::{Category, Requirement};

use crate::{
application::{Direction, ListAction},
application::{Direction, Populate},
command::Command,
config::{Categories, Icon},
keymap::{KeyTrie, Keymap},
Expand Down Expand Up @@ -256,7 +256,7 @@ impl Filter {
pub fn update_categories(
&mut self,
config: &Categories,
action: ListAction,
populate: Populate,
entries: &[types::Entry],
) {
let new = entries
Expand All @@ -265,8 +265,8 @@ impl Filter {
.collect::<HashSet<_>>();
let prev = self.categories.drain(..).collect::<HashSet<_>>();

let mut new_categories = match action {
ListAction::Replace => {
let mut new_categories = match populate {
Populate::Replace => {
let should_remove = prev.difference(&new);
let should_create = new.difference(&prev);

Expand All @@ -286,7 +286,7 @@ impl Filter {

new.into_iter().collect::<Vec<_>>()
}
ListAction::Append => {
Populate::Append => {
let should_create = new.difference(&prev);
for c in should_create {
self.categoris_state.insert(
Expand Down
10 changes: 5 additions & 5 deletions crates/synd_term/src/ui/components/subscription.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use ratatui::{
use synd_feed::types::{FeedType, FeedUrl};

use crate::{
application::{Direction, IndexOutOfRange, ListAction},
application::{Direction, IndexOutOfRange, Populate},
client::query::subscription::SubscriptionOutput,
types::{self, EntryMeta, Feed, RequirementExt, TimeExt},
ui::{
Expand Down Expand Up @@ -55,11 +55,11 @@ impl Subscription {
.map(|&idx| self.feeds.get(idx).unwrap())
}

pub fn update_subscription(&mut self, action: ListAction, subscription: SubscriptionOutput) {
pub fn update_subscription(&mut self, populate: Populate, subscription: SubscriptionOutput) {
let feed_metas = subscription.feeds.nodes.into_iter().map(types::Feed::from);
match action {
ListAction::Append => self.feeds.extend(feed_metas),
ListAction::Replace => self.feeds = feed_metas.collect(),
match populate {
Populate::Append => self.feeds.extend(feed_metas),
Populate::Replace => self.feeds = feed_metas.collect(),
}
self.apply_filter();
}
Expand Down

0 comments on commit 794f65d

Please sign in to comment.