Skip to content

Commit

Permalink
feat(term): handle feed update
Browse files Browse the repository at this point in the history
  • Loading branch information
ymgyt committed Apr 13, 2024
1 parent d68aa81 commit b0c4907
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 38 deletions.
12 changes: 9 additions & 3 deletions crates/synd_term/src/application/input_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use thiserror::Error;
use crate::{
client::mutation::subscribe_feed::SubscribeFeedInput,
types::{self},
ui,
};

pub use feed::requirement as parse_requirement;
Expand All @@ -25,8 +26,11 @@ impl<'a> InputParser<'a> {
#
# <requirement> <category> <url>
#
# * The requirement must be one of \"MUST\", \"SHOULD\", \"MAY\"
# * For the category, please choose one category of the feed(for exampke, \"rust\"
# * The requirement must be one of
# * \"MUST\"
# * \"SHOULD\"
# * \"MAY\"
# * For the category, please choose one category of the feed(for example, \"rust\"
#
# with '#' will be ignored, and an empty URL aborts the subscription.
#
Expand All @@ -44,8 +48,10 @@ impl<'a> InputParser<'a> {

pub(super) fn edit_feed_prompt(feed: &types::Feed) -> String {
format!(
"{}\n{feed_url}",
"{}\n{requirement} {category} {feed_url}",
Self::SUSBSCRIBE_FEED_PROMPT,
requirement = feed.requirement.unwrap_or(ui::DEFAULT_REQUIREMNET),
category = feed.category.as_ref().unwrap_or(ui::default_category()),
feed_url = feed.url,
)
}
Expand Down
81 changes: 48 additions & 33 deletions crates/synd_term/src/application/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{pin::Pin, time::Duration};
use std::{future, pin::Pin, time::Duration};

use crossterm::event::{Event as CrosstermEvent, KeyEvent, KeyEventKind};
use futures_util::{FutureExt, Stream, StreamExt};
Expand Down Expand Up @@ -359,11 +359,8 @@ impl Application {
self.should_render = true;
}
Command::CompleteSubscribeFeed { feed, request_seq } => {
tracing::warn!("{feed:#?}");
tracing::warn!("{feed:#?}");
tracing::warn!("{feed:#?}");
self.in_flight.remove(request_seq);
self.components.subscription.add_subscribed_feed(feed);
self.components.subscription.upsert_subscribed_feed(feed);
self.fetch_entries(
ListAction::Replace,
None,
Expand Down Expand Up @@ -496,7 +493,24 @@ impl Application {
self.terminal.force_redraw();

let fut = match InputParser::new(input.as_str()).parse_feed_subscription() {
Ok(input) => async move { Ok(Command::SubscribeFeed { input }) }.boxed(),
Ok(input) => {
// Check for the duplicate subscription
if self
.components
.subscription
.is_already_subscribed(&input.url)
{
let message = format!("{} already subscribed", input.url);
future::ready(Ok(Command::HandleError {
message,
request_seq: None,
}))
.boxed()
} else {
future::ready(Ok(Command::SubscribeFeed { input })).boxed()
}
}

Err(err) => async move {
Ok(Command::HandleError {
message: err.to_string(),
Expand All @@ -514,11 +528,27 @@ impl Application {
return;
};

let _input = self
let input = self
.interactor
.open_editor(InputParser::edit_feed_prompt(feed));
// the terminal state becomes strange after editing in the editor
self.terminal.force_redraw();

let fut = match InputParser::new(input.as_str()).parse_feed_subscription() {
// Strictly, if the URL of the feed changed before and after an update
// it is not considered an edit, so it could be considered an error
// but currently we are allowing it
Ok(input) => async move { Ok(Command::SubscribeFeed { input }) }.boxed(),
Err(err) => async move {
Ok(Command::HandleError {
message: err.to_string(),
request_seq: None,
})
}
.boxed(),
};

self.jobs.futures.push(fut);
}

fn prompt_feed_unsubscription(&mut self) {
Expand All @@ -536,34 +566,19 @@ impl Application {
}

fn subscribe_feed(&mut self, input: SubscribeFeedInput) {
// Check for the duplicate subscription
if self
.components
.subscription
.is_already_subscribed(&input.url)
{
let message = format!("{} already subscribed", input.url);
let fut = std::future::ready(Ok(Command::HandleError {
message,
request_seq: None,
}))
.boxed();
self.jobs.futures.push(fut);
} else {
let client = self.client.clone();
let request_seq = self.in_flight.add(RequestId::SubscribeFeed);
let fut = async move {
match client.subscribe_feed(input).await {
Ok(feed) => Ok(Command::CompleteSubscribeFeed { feed, request_seq }),
Err(err) => Ok(Command::HandleError {
message: format!("{err}"),
request_seq: Some(request_seq),
}),
}
let client = self.client.clone();
let request_seq = self.in_flight.add(RequestId::SubscribeFeed);
let fut = async move {
match client.subscribe_feed(input).await {
Ok(feed) => Ok(Command::CompleteSubscribeFeed { feed, request_seq }),
Err(err) => Ok(Command::HandleError {
message: format!("{err}"),
request_seq: Some(request_seq),
}),
}
.boxed();
self.jobs.futures.push(fut);
}
.boxed();
self.jobs.futures.push(fut);
}

fn unsubscribe_feed(&mut self, url: String) {
Expand Down
7 changes: 5 additions & 2 deletions crates/synd_term/src/ui/components/subscription.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,11 @@ impl Subscription {
}
}

pub fn add_subscribed_feed(&mut self, feed: types::Feed) {
self.feeds.insert(0, feed);
pub fn upsert_subscribed_feed(&mut self, feed: types::Feed) {
match self.feeds.iter_mut().find(|x| x.url == feed.url) {
Some(x) => *x = feed,
None => self.feeds.insert(0, feed),
}
}

pub fn remove_unsubscribed_feed(&mut self, url: &str) {
Expand Down

0 comments on commit b0c4907

Please sign in to comment.