Skip to content

Commit 0fd1aaa

Browse files
committed
feat(App): introduce a central manager
The multiple components in the proposed MVVM architecture require a central manager to orchestrate event handling, controlling and orchestration. Introduce an `App` to be the central manager with draft implementations for methods. Signed-off-by: Ivin Joel Abraham <[email protected]>
1 parent 87d08c5 commit 0fd1aaa

File tree

3 files changed

+70
-2
lines changed

3 files changed

+70
-2
lines changed

src/app.rs

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
use crate::viewmodels::ViewModel;
2+
use std::collections::HashMap;
3+
4+
use color_eyre::eyre::Result;
5+
use ratatui::crossterm::event::{self, Event, KeyEventKind};
6+
7+
use crate::model::{screens::View, Model};
8+
9+
#[allow(dead_code)]
10+
pub struct App {
11+
model: Model,
12+
current_view: View,
13+
viewmodels: HashMap<View, Box<dyn ViewModel>>,
14+
}
15+
16+
impl App {
17+
#[allow(dead_code)]
18+
pub fn new(model: Model) -> color_eyre::Result<Self> {
19+
Ok(App {
20+
model,
21+
current_view: View::MailingListSelection,
22+
viewmodels: HashMap::new(),
23+
})
24+
}
25+
26+
#[allow(dead_code)]
27+
pub fn get_current_view(&self) -> View {
28+
self.current_view
29+
}
30+
31+
#[allow(dead_code)]
32+
pub fn get_current_viewmodel(&mut self) -> &mut Box<dyn ViewModel> {
33+
self.viewmodels
34+
.entry(self.current_view)
35+
.or_insert_with(|| match self.current_view {
36+
View::MailingListSelection => {
37+
todo!()
38+
}
39+
View::BookmarkedPatchsets => {
40+
todo!()
41+
}
42+
View::LatestPatchsets => {
43+
todo!()
44+
}
45+
View::PatchsetDetails => {
46+
todo!()
47+
}
48+
View::EditConfig => {
49+
todo!()
50+
}
51+
})
52+
}
53+
54+
#[allow(dead_code)]
55+
pub fn run(&mut self) -> Result<()> {
56+
loop {
57+
self.get_current_view().draw_screen();
58+
59+
if let Event::Key(key) = event::read()? {
60+
if key.kind == KeyEventKind::Release {
61+
continue;
62+
}
63+
64+
self.get_current_viewmodel().handle_key(key);
65+
}
66+
}
67+
}
68+
}

src/main.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
mod app;
12
mod cli;
23
mod handler;
34
mod infrastructure;
45
mod lore;
56
mod macros;
67
mod model;
78
mod ui;
9+
mod viewmodels;
810

911
use clap::Parser;
1012
use cli::Cli;

src/viewmodels/mod.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
mod mailing_list_viewmodel;
2-
31
use ratatui::crossterm::event::KeyEvent;
42
#[allow(dead_code)]
53
pub trait ViewModel {

0 commit comments

Comments
 (0)