Skip to content

Commit

Permalink
Add message to StatusBar
Browse files Browse the repository at this point in the history
  • Loading branch information
akiomik committed Jan 11, 2024
1 parent 82aa9b6 commit 0c94074
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 17 deletions.
1 change: 1 addition & 0 deletions src/action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,5 @@ pub enum Action {
SendTextNote(String),
Key(KeyEvent),
MetadataUpdated(Metadata),
SystemMessage(String),
}
5 changes: 4 additions & 1 deletion src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ impl App {
let fps = FpsCounter::default();
let config = Config::new()?;
let pubkey = Keys::from_sk_str(config.privatekey.as_str())?.public_key();
let status_bar = StatusBar::new(pubkey, None);
let status_bar = StatusBar::new(pubkey, None, None, true);
let mode = Mode::Home;
Ok(Self {
tick_rate,
Expand Down Expand Up @@ -155,16 +155,19 @@ impl App {
let event = EventBuilder::new_reaction(id, pubkey, "+").to_event(&keys)?;
log::info!("Send reaction: {event:?}");
event_tx.send(event)?;
action_tx.send(Action::SystemMessage(format!("[Liked] {id}")))?;
}
Action::SendRepost((id, pubkey)) => {
let event = EventBuilder::repost(id, pubkey).to_event(&keys)?;
log::info!("Send repost: {event:?}");
event_tx.send(event)?;
action_tx.send(Action::SystemMessage(format!("[Reposted] {id}")))?;
}
Action::SendTextNote(ref content) => {
let event = EventBuilder::new_text_note(content, []).to_event(&keys)?;
log::info!("Send text note: {event:?}");
event_tx.send(event)?;
action_tx.send(Action::SystemMessage(format!("[Posted] {content}")))?;
}
_ => {}
}
Expand Down
54 changes: 38 additions & 16 deletions src/components/status_bar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,23 @@ use crate::widgets::PublicKey;
pub struct StatusBar {
pubkey: XOnlyPublicKey,
metadata: Option<Metadata>,
message: Option<String>,
is_loading: bool,
}

impl StatusBar {
pub fn new(pubkey: XOnlyPublicKey, metadata: Option<Metadata>) -> Self {
Self { pubkey, metadata }
pub fn new(
pubkey: XOnlyPublicKey,
metadata: Option<Metadata>,
message: Option<String>,
is_loading: bool,
) -> Self {
Self {
pubkey,
metadata,
message,
is_loading,
}
}

pub fn set_metadata(&mut self, metadata: Option<Metadata>) {
Expand All @@ -36,17 +48,23 @@ impl StatusBar {

impl Component for StatusBar {
fn update(&mut self, action: Action) -> Result<Option<Action>> {
if let Action::ReceiveEvent(ev) = action {
match ev.kind {
Kind::Metadata if ev.pubkey == self.pubkey => {
let maybe_metadata = Metadata::from_json(ev.content);
if let Ok(metadata) = maybe_metadata {
self.set_metadata(Some(metadata));
match action {
Action::ReceiveEvent(ev) => {
self.is_loading = false;

match ev.kind {
Kind::Metadata if ev.pubkey == self.pubkey => {
let maybe_metadata = Metadata::from_json(ev.content);
if let Ok(metadata) = maybe_metadata {
self.set_metadata(Some(metadata));
}
}
}
_ => {}
};
}
_ => {}
};
}
Action::SystemMessage(message) => self.message = Some(message),
_ => {}
};

Ok(None)
}
Expand All @@ -57,19 +75,23 @@ impl Component for StatusBar {
[
Constraint::Min(0),
Constraint::Length(1),
// Constraint::Length(1),
Constraint::Length(1),
],
)
.split(area);
f.render_widget(Clear, layout[1]);
// f.render_widget(Clear, layout[2]);
f.render_widget(Clear, layout[2]);

let name = Span::styled(self.name(), Style::default().fg(Color::Gray).italic());
let status_line = Paragraph::new(name).style(Style::default().bg(Color::Black));
f.render_widget(status_line, layout[1]);

// TODO
// f.render_widget(notification_line, layout[1]);
let message_line = if self.is_loading {
Paragraph::new("Loading...")
} else {
Paragraph::new(self.message.clone().unwrap_or_default())
};
f.render_widget(message_line, layout[2]);

Ok(())
}
Expand Down

0 comments on commit 0c94074

Please sign in to comment.