Skip to content

Commit

Permalink
chore: refactor porfolio subscreens to pages
Browse files Browse the repository at this point in the history
  • Loading branch information
MishkaRogachev committed Oct 10, 2024
1 parent 4dc9f90 commit 1a1ae91
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 35 deletions.
54 changes: 33 additions & 21 deletions src/tui/screens/porfolio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,17 @@ enum ManageOption {
DeleteAccount,
}

pub trait PorfolioPage: AppScreen {
fn on_networks_change(&mut self);
}

pub struct Screen {
command_tx: mpsc::Sender<AppCommand>,
session: Session,
crypto: Arc<Mutex<Crypto>>,

mode_switch: controls::MultiSwitch,
mode: Option<Box<dyn AppScreen + Send>>,
page_switch: controls::MultiSwitch,
page: Option<Box<dyn PorfolioPage + Send>>,
quit_button: controls::Button,
receive_button: controls::Button,
send_button: controls::Button,
Expand All @@ -43,7 +47,7 @@ impl Screen {
crypto.load_active_networks().expect("Failed to load active networks");
let crypto = Arc::new(Mutex::new(crypto));

let mode_switch = controls::MultiSwitch::new(vec![
let page_switch = controls::MultiSwitch::new(vec![
controls::Button::new("Accounts", Some('a')),
controls::Button::new("Transactions", Some('t')),
controls::Button::new("Charts", Some('c')).disable(),
Expand All @@ -63,22 +67,28 @@ impl Screen {
let manage_button = controls::MenuButton::new(
"Manage", Some('m'), manage_options).keep_above();

let mode: Option<Box<dyn AppScreen + Send>> = Some(Box::new(
super::porfolio_accounts::Screen::new(session.clone(), crypto.clone())));
let page: Option<Box<dyn PorfolioPage + Send>> = Some(Box::new(
super::porfolio_accounts::Page::new(session.clone(), crypto.clone())));

Self {
command_tx,
session,
crypto,
mode_switch,
mode,
page_switch,
page,
quit_button,
receive_button,
send_button,
manage_button,
popup: None,
}
}

pub fn on_networks_change(&mut self) {
if let Some(page) = &mut self.page {
page.on_networks_change();
}
}
}

#[async_trait::async_trait]
Expand All @@ -88,6 +98,8 @@ impl AppScreen for Screen {
if let Ok(ok) = popup.handle_event(event).await {
if ok {
self.popup = None;
// TODO: check if networks have changed indeed
self.on_networks_change();
return Ok(true);
}
}
Expand Down Expand Up @@ -119,26 +131,26 @@ impl AppScreen for Screen {
return Ok(false);
}

if let Some(index) = self.mode_switch.handle_event(&event) {
if let Some(index) = self.page_switch.handle_event(&event) {
// TODO: switch to enum
match index {
0 => {
self.mode = Some(Box::new(
super::porfolio_accounts::Screen::new(self.session.clone(), self.crypto.clone())
self.page = Some(Box::new(
super::porfolio_accounts::Page::new(self.session.clone(), self.crypto.clone())
));
},
1 => {
self.mode = Some(Box::new(
super::porfolio_transactions::Screen::new(self.session.clone(), self.crypto.clone())
self.page = Some(Box::new(
super::porfolio_transactions::Page::new(self.session.clone(), self.crypto.clone())
));
},
_ => {} // TODO: implement modes
_ => {} // TODO: other pages
}
return Ok(false);
}

if let Some(mode) = &mut self.mode {
if let Ok(ok) = mode.handle_event(event.clone()).await {
if let Some(page) = &mut self.page {
if let Ok(ok) = page.handle_event(event.clone()).await {
if ok {
return Ok(true);
}
Expand Down Expand Up @@ -169,8 +181,8 @@ impl AppScreen for Screen {
popup.update().await;
}

if let Some(mode) = &mut self.mode {
mode.update().await;
if let Some(page) = &mut self.page {
page.update().await;
}
}

Expand All @@ -179,15 +191,15 @@ impl AppScreen for Screen {
.direction(Direction::Vertical)
.constraints([
Constraint::Length(controls::SWITCH_HEIGHT),
Constraint::Fill(0), // Fill height for mode
Constraint::Fill(0), // Fill height for page
Constraint::Length(controls::BUTTON_HEIGHT),
])
.split(area);

self.mode_switch.render(frame, content_layout[0]);
self.page_switch.render(frame, content_layout[0]);

if let Some(mode) = &mut self.mode {
mode.render(frame, content_layout[1]);
if let Some(page) = &mut self.page {
page.render(frame, content_layout[1]);
}

let buttons_row = Layout::default()
Expand Down
12 changes: 9 additions & 3 deletions src/tui/screens/porfolio_accounts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const SUMMARY_TEXT: &str = "Summary balance";

const UPDATE_INTERVAL: tokio::time::Duration = tokio::time::Duration::from_secs(10);

pub struct Screen {
pub struct Page {
crypto: Arc<Mutex<Crypto>>,
last_update: Option<tokio::time::Instant>,

Expand All @@ -24,7 +24,7 @@ pub struct Screen {
scroll: controls::Scroll,
}

impl Screen {
impl Page {
pub fn new(session: Session, crypto: Arc<Mutex<Crypto>>) -> Self {
let accounts = vec![account::AccountDisplay::new(session.account)];
let busy = controls::Busy::new("Loading..");
Expand Down Expand Up @@ -65,7 +65,7 @@ impl Screen {
}

#[async_trait::async_trait]
impl AppScreen for Screen {
impl AppScreen for Page {
async fn handle_event(&mut self, event: Event) -> anyhow::Result<bool> {
self.scroll.handle_event(&event);
Ok(false)
Expand Down Expand Up @@ -129,3 +129,9 @@ impl AppScreen for Screen {
self.scroll.render(frame, content_layout[1]);
}
}

impl super::porfolio::PorfolioPage for Page {
fn on_networks_change(&mut self) {
self.last_update = None;
}
}
39 changes: 28 additions & 11 deletions src/tui/screens/porfolio_transactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,27 @@ const TITLE_TEXT: &str = "Transaction history:";

const TRANSACTION_CNT_PER_PAGE: usize = 10;

pub struct Screen {
pub struct Page {
session: Session,
crypto: Arc<Mutex<Crypto>>,
update: bool,

transactions: Vec<transaction::TransactionDisplay>,
busy: controls::Busy,
scroll: controls::Scroll,
}

impl Screen {
impl Page {
pub fn new(session: Session, crypto: Arc<Mutex<Crypto>>) -> Self {
let transactions = session.db.get_transactions(
session.account, 0, TRANSACTION_CNT_PER_PAGE).expect(
"Failed to get transactions"
);

let transactions = transactions.into_iter().map(|tx| {
transaction::TransactionDisplay::new(tx, transaction::TransactionDisplayType::Incoming)
}).collect();
let transactions = Vec::new();

let busy = controls::Busy::new("Loading..");
let scroll = controls::Scroll::new();

Self {
session,
crypto,
update: true,
transactions,
busy,
scroll
Expand All @@ -47,15 +44,29 @@ impl Screen {
}

#[async_trait::async_trait]
impl AppScreen for Screen {
impl AppScreen for Page {
async fn handle_event(&mut self, event: Event) -> anyhow::Result<bool> {
self.scroll.handle_event(&event);
Ok(false)
}

async fn update(&mut self) {
if !self.update {
return;
}

let crypto = self.crypto.lock().await;

// TODO: fetch transactions
// = session.db.get_transactions(
// session.account, 0, TRANSACTION_CNT_PER_PAGE).expect(
// "Failed to get transactions"
// );

// let transactions = transactions.into_iter().map(|tx| {
// transaction::TransactionDisplay::new(tx, transaction::TransactionDisplayType::Incoming)
// }).collect();
//let transactions = crypto.ge
}

fn render(&mut self, frame: &mut Frame, area: Rect) {
Expand Down Expand Up @@ -100,3 +111,9 @@ impl AppScreen for Screen {
self.scroll.render(frame, content_layout[1]);
}
}

impl super::porfolio::PorfolioPage for Page {
fn on_networks_change(&mut self) {
self.update = true;
}
}

0 comments on commit 1a1ae91

Please sign in to comment.