Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions src/app.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
use crate::{infrastructure::terminal::Tui, viewmodels::ViewModel, views::View};
use std::collections::HashMap;

use color_eyre::eyre::Result;
use ratatui::crossterm::event::{self, Event, KeyEventKind};

use crate::model::Model;

#[allow(dead_code)]
pub struct App {
model: Model,
current_view: View,
viewmodels: HashMap<View, Box<dyn ViewModel>>,
terminal: Tui,
}

impl App {
#[allow(dead_code)]
pub fn new(model: Model, terminal: Tui) -> color_eyre::Result<Self> {
Ok(App {
model,
current_view: View::MailingLists,
viewmodels: HashMap::new(),
terminal,
})
}

#[allow(dead_code)]
pub fn get_current_view(&self) -> View {
self.current_view
}

#[allow(dead_code)]
pub fn get_current_viewmodel(&mut self) -> &mut Box<dyn ViewModel> {
self.viewmodels
.entry(self.current_view)
.or_insert_with(|| match self.current_view {
View::MailingLists => {
todo!()
}
View::BookmarkedPatchsets => {
todo!()
}
View::LatestPatchsets => {
todo!()
}
View::PatchsetDetails => {
todo!()
}
View::EditConfig => {
todo!()
}
})
}

#[allow(dead_code, unreachable_code, unused_variables)]
pub fn run(&mut self) -> Result<()> {
loop {
let state = &self.get_current_viewmodel().state();
let current_view = self.get_current_view();
current_view.render_screen(&mut self.terminal, state)?;

if let Event::Key(key) = event::read()? {
if key.kind == KeyEventKind::Release {
continue;
}

self.get_current_viewmodel().handle_key(key);
}
}
}
}
14 changes: 0 additions & 14 deletions src/app/screens/mod.rs

This file was deleted.

2 changes: 1 addition & 1 deletion src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use ratatui::{prelude::Backend, Terminal};

use std::ops::ControlFlow;

use crate::{app::config::Config, infrastructure::terminal::restore};
use crate::{infrastructure::terminal::restore, model::config::Config};

#[derive(Debug, Parser)]
#[command(version, about)]
Expand Down
27 changes: 15 additions & 12 deletions src/handler/bookmarked.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,17 @@ use ratatui::{
use std::ops::ControlFlow;

use crate::{
app::{screens::CurrentScreen, App},
loading_screen,
lore::lore_session::B4Result,
ui::popup::{help::HelpPopUpBuilder, info_popup::InfoPopUp, PopUp},
model::Model,
views::{
popup::{help::HelpPopUpBuilder, info_popup::InfoPopUp, PopUp},
View,
},
};

pub fn handle_bookmarked_patchsets<B>(
app: &mut App,
model: &mut Model,
key: KeyEvent,
mut terminal: Terminal<B>,
) -> color_eyre::Result<ControlFlow<(), Terminal<B>>>
Expand All @@ -24,37 +27,37 @@ where
match key.code {
KeyCode::Char('?') => {
let popup = generate_help_popup();
app.popup = Some(popup);
model.popup = Some(popup);
}
KeyCode::Esc | KeyCode::Char('q') => {
app.bookmarked_patchsets.patchset_index = 0;
app.set_current_screen(CurrentScreen::MailingListSelection);
model.bookmarked_patchsets.patchset_index = 0;
model.set_current_screen(View::MailingLists);
}
KeyCode::Char('j') | KeyCode::Down => {
app.bookmarked_patchsets.select_below_patchset();
model.bookmarked_patchsets.select_below_patchset();
}
KeyCode::Char('k') | KeyCode::Up => {
app.bookmarked_patchsets.select_above_patchset();
model.bookmarked_patchsets.select_above_patchset();
}
KeyCode::Enter => {
terminal = loading_screen! {
terminal,
"Loading patchset" => {
let result = app.init_details_actions();
let result = model.init_details_actions();
if result.is_ok() {
// If a patchset has been bookmarked UI, this means that
// b4 was successful in fetching it, so it shouldn't be
// necessary to handle this, but we can't assume that a
// patchset in this list was bookmarked through the UI
match result.unwrap() {
B4Result::PatchFound(_) => {
app.set_current_screen(CurrentScreen::PatchsetDetails);
model.set_current_screen(View::PatchsetDetails);
}
B4Result::PatchNotFound(err_cause) => {
app.popup = Some(InfoPopUp::generate_info_popup(
model.popup = Some(InfoPopUp::generate_info_popup(
"Error",&format!("The selected patchset couldn't be retrieved.\nReason: {err_cause}\nPlease choose another patchset.")
));
app.set_current_screen(CurrentScreen::BookmarkedPatchsets);
model.set_current_screen(View::BookmarkedPatchsets);
}
}
}
Expand Down
27 changes: 15 additions & 12 deletions src/handler/details_actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,22 @@ use ratatui::{
use std::time::Duration;

use crate::{
app::{screens::CurrentScreen, App},
infrastructure::terminal::{setup_user_io, teardown_user_io},
ui::popup::{help::HelpPopUpBuilder, review_trailers::ReviewTrailersPopUp, PopUp},
model::Model,
views::{
popup::{help::HelpPopUpBuilder, review_trailers::ReviewTrailersPopUp, PopUp},
View,
},
};

use super::wait_key_press;

pub fn handle_patchset_details<B: Backend>(
app: &mut App,
model: &mut Model,
key: KeyEvent,
terminal: &mut Terminal<B>,
) -> color_eyre::Result<()> {
let patchset_details_and_actions = app.details_actions.as_mut().unwrap();
let patchset_details_and_actions = model.details_actions.as_mut().unwrap();

if key.modifiers.contains(KeyModifiers::SHIFT) {
match key.code {
Expand Down Expand Up @@ -51,7 +54,7 @@ pub fn handle_patchset_details<B: Backend>(
KeyCode::Char('t') => {
let popup =
ReviewTrailersPopUp::generate_trailers_popup(patchset_details_and_actions);
app.popup = Some(popup);
model.popup = Some(popup);
}
_ => {}
}
Expand All @@ -61,12 +64,12 @@ pub fn handle_patchset_details<B: Backend>(
match key.code {
KeyCode::Char('?') => {
let popup = generate_help_popup();
app.popup = Some(popup);
model.popup = Some(popup);
}
KeyCode::Esc | KeyCode::Char('q') => {
let ps_da_clone = patchset_details_and_actions.last_screen.clone();
app.set_current_screen(ps_da_clone);
app.reset_details_actions();
let ps_da_clone = patchset_details_and_actions.last_screen;
model.set_current_screen(ps_da_clone);
model.reset_details_actions();
}
KeyCode::Char('a') => {
patchset_details_and_actions.toggle_apply_action();
Expand Down Expand Up @@ -109,7 +112,7 @@ pub fn handle_patchset_details<B: Backend>(
KeyCode::Enter => {
if patchset_details_and_actions.actions_require_user_io() {
setup_user_io(terminal)?;
app.consolidate_patchset_actions()?;
model.consolidate_patchset_actions()?;
println!("\nPress ENTER continue...");
loop {
if let Event::Key(key) = event::read()? {
Expand All @@ -120,9 +123,9 @@ pub fn handle_patchset_details<B: Backend>(
}
teardown_user_io(terminal)?;
} else {
app.consolidate_patchset_actions()?;
model.consolidate_patchset_actions()?;
}
app.set_current_screen(CurrentScreen::PatchsetDetails);
model.set_current_screen(View::PatchsetDetails);
}
_ => {}
}
Expand Down
21 changes: 12 additions & 9 deletions src/handler/edit_config.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
use ratatui::crossterm::event::{KeyCode, KeyEvent};

use crate::{
app::{screens::CurrentScreen, App},
ui::popup::{help::HelpPopUpBuilder, PopUp},
model::Model,
views::{
popup::{help::HelpPopUpBuilder, PopUp},
View,
},
};

pub fn handle_edit_config(app: &mut App, key: KeyEvent) -> color_eyre::Result<()> {
if let Some(edit_config_state) = app.edit_config.as_mut() {
pub fn handle_edit_config(model: &mut Model, key: KeyEvent) -> color_eyre::Result<()> {
if let Some(edit_config_state) = model.edit_config.as_mut() {
match edit_config_state.is_editing() {
true => match key.code {
KeyCode::Esc => {
Expand All @@ -29,13 +32,13 @@ pub fn handle_edit_config(app: &mut App, key: KeyEvent) -> color_eyre::Result<()
false => match key.code {
KeyCode::Char('?') => {
let popup = generate_help_popup();
app.popup = Some(popup);
model.popup = Some(popup);
}
KeyCode::Esc | KeyCode::Char('q') => {
app.consolidate_edit_config();
app.config.save_patch_hub_config()?;
app.reset_edit_config();
app.set_current_screen(CurrentScreen::MailingListSelection);
model.consolidate_edit_config();
model.config.save_patch_hub_config()?;
model.reset_edit_config();
model.set_current_screen(View::MailingLists);
}
KeyCode::Enter => {
edit_config_state.toggle_editing();
Expand Down
25 changes: 14 additions & 11 deletions src/handler/latest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,33 @@ use ratatui::{
use std::ops::ControlFlow;

use crate::{
app::{screens::CurrentScreen, App},
loading_screen,
lore::lore_session::B4Result,
ui::popup::{help::HelpPopUpBuilder, info_popup::InfoPopUp, PopUp},
model::Model,
views::{
popup::{help::HelpPopUpBuilder, info_popup::InfoPopUp, PopUp},
View,
},
};

pub fn handle_latest_patchsets<B>(
app: &mut App,
model: &mut Model,
key: KeyEvent,
mut terminal: Terminal<B>,
) -> color_eyre::Result<ControlFlow<(), Terminal<B>>>
where
B: Backend + Send + 'static,
{
let latest_patchsets = app.latest_patchsets.as_mut().unwrap();
let latest_patchsets = model.latest_patchsets.as_mut().unwrap();

match key.code {
KeyCode::Char('?') => {
let popup = generate_help_popup();
app.popup = Some(popup);
model.popup = Some(popup);
}
KeyCode::Esc | KeyCode::Char('q') => {
app.reset_latest_patchsets();
app.set_current_screen(CurrentScreen::MailingListSelection);
model.reset_latest_patchsets();
model.set_current_screen(View::MailingLists);
}
KeyCode::Char('j') | KeyCode::Down => {
latest_patchsets.select_below_patchset();
Expand All @@ -55,17 +58,17 @@ where
terminal = loading_screen! {
terminal,
"Loading patchset" => {
let result = app.init_details_actions();
let result = model.init_details_actions();
if result.is_ok() {
match result.unwrap() {
B4Result::PatchFound(_) => {
app.set_current_screen(CurrentScreen::PatchsetDetails);
model.set_current_screen(View::PatchsetDetails);
}
B4Result::PatchNotFound(err_cause) => {
app.popup = Some(InfoPopUp::generate_info_popup(
model.popup = Some(InfoPopUp::generate_info_popup(
"Error",&format!("The selected patchset couldn't be retrieved.\nReason: {err_cause}\nPlease choose another patchset.")
));
app.set_current_screen(CurrentScreen::LatestPatchsets);
model.set_current_screen(View::LatestPatchsets);
}
}
}
Expand Down
Loading