Skip to content

Commit

Permalink
refactor(app): move out all global action handlers to where they belong
Browse files Browse the repository at this point in the history
Signed-off-by: Deep Panchal <deep302001@gmail.com>
  • Loading branch information
deepanchal committed Jul 5, 2024
1 parent 70d243b commit 730ec98
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 62 deletions.
56 changes: 0 additions & 56 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,62 +156,6 @@ impl App {
})?;
}
Action::StatusLine(ref s) => self.state.status_line.clone_from(s),
Action::FocusNext => match self.state.mode {
Mode::Book => {
if let Some(book_index) = self.state.books.state.selected() {
self.state.mode = Mode::Page;
action_tx.send(Action::SelectNextPage)?;
}
}
Mode::Page => {}
_ => {}
},
Action::FocusPrev => match self.state.mode {
Mode::Content => {}
Mode::Page => {
self.state.mode = Mode::Book;
self.state.page_content = None;
}
_ => {}
},
Action::LoadBooks => {
let books = self.dnote.get_books()?;
self.state.books = StatefulList::with_items(books);
}
Action::LoadActiveBookPages => {
if let Some(book) = self.state.get_active_book() {
let pages = self.dnote.get_pages(&book.name)?;
self.state.pages = StatefulList::with_items(pages);
}
}
Action::UpdateActiveBookPages => {
if let Some(book) = self.state.get_active_book() {
let new_pages = self.dnote.get_pages(&book.name)?;
self.state.update_pages(new_pages);
}
}
Action::LoadActivePageContent => {
if let Some(page) = self.state.get_active_page() {
let page_info = self.dnote.get_page_content(page.id)?;
self.state.page_content = Some(page_info.content);
}
}
Action::SelectNextBook => {
self.state.select_next_book();
action_tx.send(Action::LoadActiveBookPages)?;
}
Action::SelectPrevBook => {
self.state.select_prev_book();
action_tx.send(Action::LoadActiveBookPages)?;
}
Action::SelectNextPage => {
self.state.select_next_page();
action_tx.send(Action::LoadActivePageContent)?;
}
Action::SelectPrevPage => {
self.state.select_prev_page();
action_tx.send(Action::LoadActivePageContent)?;
}
Action::AddPageToActiveBook => {
if let Some(book) = self.state.get_active_book() {
tui.exit()?;
Expand Down
32 changes: 30 additions & 2 deletions src/components/books.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@ use super::{Component, Frame};
use crate::{
action::Action,
config::{Config, KeyBindings},
dnote::DnoteBook,
dnote::{Dnote, DnoteBook},
mode::Mode,
state::State,
state::{State, StatefulList},
};

#[derive(Default)]
pub struct BooksPane {
dnote: Dnote,
command_tx: Option<UnboundedSender<Action>>,
config: Config,
}
Expand All @@ -33,6 +34,13 @@ impl BooksPane {
pub fn new() -> Self {
Self::default()
}

fn send_action(&self, action: Action) -> Result<()> {
if let Some(tx) = &self.command_tx {
tx.send(action.clone())?;
}
Ok(())
}
}

impl Component for BooksPane {
Expand All @@ -57,6 +65,26 @@ impl Component for BooksPane {
match action {
Action::Tick => {}
Action::Render => {}
Action::FocusNext => {
// Change to page pane
if let Some(book_index) = state.books.state.selected() {
state.mode = Mode::Page;
self.send_action(Action::SelectNextPage)?;
}
}
Action::FocusPrev => {}
Action::LoadBooks => {
let books = self.dnote.get_books()?;
state.books = StatefulList::with_items(books);
}
Action::SelectNextBook => {
state.select_next_book();
self.send_action(Action::LoadActiveBookPages)?;
}
Action::SelectPrevBook => {
state.select_prev_book();
self.send_action(Action::LoadActiveBookPages)?;
}
_ => {}
}
Ok(None)
Expand Down
11 changes: 10 additions & 1 deletion src/components/content.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ use super::{Component, Frame};
use crate::{
action::Action,
config::{Config, KeyBindings},
dnote::DnoteBook,
dnote::{Dnote, DnoteBook},
state::State,
};

#[derive(Default)]
pub struct ContentPane {
dnote: Dnote,
command_tx: Option<UnboundedSender<Action>>,
config: Config,
}
Expand Down Expand Up @@ -49,6 +50,14 @@ impl Component for ContentPane {
match action {
Action::Tick => {}
Action::Render => {}
Action::FocusNext => {}
Action::FocusPrev => {}
Action::LoadActivePageContent => {
if let Some(page) = state.get_active_page() {
let page_info = self.dnote.get_page_content(page.id)?;
state.page_content = Some(page_info.content);
}
}
_ => {}
}
Ok(None)
Expand Down
39 changes: 36 additions & 3 deletions src/components/pages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@ use super::{Component, Frame};
use crate::{
action::Action,
config::{Config, KeyBindings},
dnote::DnoteBook,
dnote::{Dnote, DnoteBook},
mode::Mode,
state::State,
state::{State, StatefulList},
};

#[derive(Default)]
pub struct PagesPane {
dnote: Dnote,
command_tx: Option<UnboundedSender<Action>>,
config: Config,
}
Expand All @@ -29,6 +30,13 @@ impl PagesPane {
pub fn new() -> Self {
Self::default()
}

fn send_action(&self, action: Action) -> Result<()> {
if let Some(tx) = &self.command_tx {
tx.send(action.clone())?;
}
Ok(())
}
}

impl Component for PagesPane {
Expand All @@ -49,7 +57,32 @@ impl Component for PagesPane {
fn update(&mut self, action: Action, state: &mut State) -> Result<Option<Action>> {
match action {
Action::Tick => {}
Action::Render => {}
Action::FocusNext => {}
Action::FocusPrev => {
// Change to book pane
state.mode = Mode::Book;
state.page_content = None;
}
Action::LoadActiveBookPages => {
if let Some(book) = state.get_active_book() {
let pages = self.dnote.get_pages(&book.name)?;
state.pages = StatefulList::with_items(pages);
}
}
Action::UpdateActiveBookPages => {
if let Some(book) = state.get_active_book() {
let new_pages = self.dnote.get_pages(&book.name)?;
state.update_pages(new_pages);
}
}
Action::SelectNextPage => {
state.select_next_page();
self.send_action(Action::LoadActivePageContent)?;
}
Action::SelectPrevPage => {
state.select_prev_page();
self.send_action(Action::LoadActivePageContent)?;
}
_ => {}
}
Ok(None)
Expand Down

0 comments on commit 730ec98

Please sign in to comment.