Skip to content

Commit c3abf99

Browse files
committed
Code refactoring
1 parent 85fb6a3 commit c3abf99

File tree

2 files changed

+116
-76
lines changed

2 files changed

+116
-76
lines changed

src/windows/stats_window.rs

Lines changed: 96 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use crate::{
22
helper::get_title,
33
layout::LayoutColorTypeEnum,
4-
storage::storage_handler::StorageHandler,
5-
windows::{Window, Instruction, InstructionWidget}
4+
storage::{stats::Stat, storage_handler::StorageHandler},
5+
windows::{Instruction, InstructionWidget, Window}
66
};
77

88
use ratatui::{
@@ -85,8 +85,88 @@ impl Window for StatsWindow {
8585
])
8686
.split(chunks[0]);
8787

88-
let stats = storage_handler.get_data_stats_reversed().unwrap();
88+
let last_runs_table_widget_data = storage_handler.get_data_stats_reversed().unwrap();
89+
let last_runs_table_widget = self.get_last_runs_table_widget(
90+
&layout,
91+
&last_runs_table_widget_data,
92+
version);
8993

94+
let chart_widget_data = storage_handler.get_data_for_chart();
95+
let chart_widget = self.get_chart_widget(&layout, &chart_widget_data);
96+
97+
let right_widget = self.get_right_widget(&layout, &storage_handler);
98+
99+
frame.render_widget(last_runs_table_widget, left_widget[0]);
100+
frame.render_widget(chart_widget, left_widget[1]);
101+
frame.render_widget(right_widget, chunks[1]);
102+
}
103+
}
104+
105+
impl StatsWindow {
106+
107+
/// Gets the right widget (Best score)
108+
fn get_right_widget(
109+
&self,
110+
layout: &TukaiLayout,
111+
storage_handler: &StorageHandler
112+
) -> Table {
113+
let stats = storage_handler.get_data_stats_bets().unwrap();
114+
115+
let block = Block::new()
116+
.title(" Best score ")
117+
.title_style(Style::new().fg(layout.get_primary_color()))
118+
.borders(Borders::ALL)
119+
.border_style(Style::default().fg(layout.get_primary_color()))
120+
.border_type(BorderType::Rounded);
121+
122+
let default_cell_style = Style::default()
123+
.fg(layout.get_text_color());
124+
125+
let rows = stats.iter()
126+
.map(|stat| {
127+
Row::new(vec![
128+
Cell::from(stat.get_average_wpm().to_string())
129+
.style(default_cell_style),
130+
131+
Cell::from(format!("{}%", stat.get_accuracy().to_string()))
132+
.style(default_cell_style),
133+
])
134+
}).collect::<Vec<Row>>();
135+
136+
let widths = [
137+
Constraint::Percentage(50),
138+
Constraint::Percentage(50),
139+
];
140+
141+
let default_header_cell_style = Style::default()
142+
.fg(layout.get_primary_color())
143+
.bold();
144+
145+
let table = Table::new(rows, widths)
146+
.block(block)
147+
.column_spacing(1)
148+
.style(Style::new().bg(layout.get_background_color()))
149+
.highlight_symbol("X")
150+
.header(
151+
Row::new(vec![
152+
Cell::from("🔥 Average WPM")
153+
.style(default_header_cell_style),
154+
155+
Cell::from("🎯 Accuracy")
156+
.style(default_header_cell_style),
157+
]).bottom_margin(1)
158+
);
159+
160+
table
161+
}
162+
163+
/// Gets the main table widget (Last runs)
164+
fn get_last_runs_table_widget<'a>(
165+
&self,
166+
layout: &TukaiLayout,
167+
stats: &Vec<Stat>,
168+
version: &String
169+
) -> Table<'a> {
90170
let block_title = get_title(
91171
version,
92172
layout.get_active_layout_title(),
@@ -152,7 +232,16 @@ impl Window for StatsWindow {
152232
]).bottom_margin(1)
153233
);
154234

155-
let (best_wpm, chart_data) = storage_handler.get_data_for_chart();
235+
table
236+
}
237+
238+
/// Gets the left bottom widget (Chart)
239+
fn get_chart_widget<'a>(
240+
&self,
241+
layout: &TukaiLayout,
242+
chart_widget_data: &'a (usize, Vec<(f64, f64)>)
243+
) -> Chart<'a> {
244+
let (best_wpm, chart_data) = chart_widget_data;
156245

157246
let datasets = vec![
158247
Dataset::default()
@@ -169,8 +258,8 @@ impl Window for StatsWindow {
169258
let y_axis = Axis::default()
170259
.title("Words per minute")
171260
.style(Style::default().fg(layout.get_primary_color()))
172-
.bounds([0.0, best_wpm as f64])
173-
.labels((0..=best_wpm).step_by(25).map(|y| y.to_string()).collect::<Vec<String>>());
261+
.bounds([0.0, *best_wpm as f64])
262+
.labels((0..=*best_wpm).step_by(25).map(|y| y.to_string()).collect::<Vec<String>>());
174263

175264
let chart_block = Block::new()
176265
.title_style(Style::new().fg(layout.get_primary_color()))
@@ -184,67 +273,6 @@ impl Window for StatsWindow {
184273
.x_axis(x_axis)
185274
.y_axis(y_axis);
186275

187-
let right_widget = self.get_right_widget(&layout, &storage_handler);
188-
189-
frame.render_widget(table, left_widget[0]);
190-
frame.render_widget(chart, left_widget[1]);
191-
frame.render_widget(right_widget, chunks[1]);
192-
}
193-
}
194-
195-
impl StatsWindow {
196-
fn get_right_widget(
197-
&self,
198-
layout: &TukaiLayout,
199-
storage_handler: &StorageHandler
200-
) -> Table {
201-
let stats = storage_handler.get_data_stats_bets().unwrap();
202-
203-
let block = Block::new()
204-
.title(" Best score ")
205-
.title_style(Style::new().fg(layout.get_primary_color()))
206-
.borders(Borders::ALL)
207-
.border_style(Style::default().fg(layout.get_primary_color()))
208-
.border_type(BorderType::Rounded);
209-
210-
let default_cell_style = Style::default()
211-
.fg(layout.get_text_color());
212-
213-
let rows = stats.iter()
214-
.map(|stat| {
215-
Row::new(vec![
216-
Cell::from(stat.get_average_wpm().to_string())
217-
.style(default_cell_style),
218-
219-
Cell::from(format!("{}%", stat.get_accuracy().to_string()))
220-
.style(default_cell_style),
221-
])
222-
}).collect::<Vec<Row>>();
223-
224-
let widths = [
225-
Constraint::Percentage(50),
226-
Constraint::Percentage(50),
227-
];
228-
229-
let default_header_cell_style = Style::default()
230-
.fg(layout.get_primary_color())
231-
.bold();
232-
233-
let table = Table::new(rows, widths)
234-
.block(block)
235-
.column_spacing(1)
236-
.style(Style::new().bg(layout.get_background_color()))
237-
.highlight_symbol("X")
238-
.header(
239-
Row::new(vec![
240-
Cell::from("🔥 Average WPM")
241-
.style(default_header_cell_style),
242-
243-
Cell::from("🎯 Accuracy")
244-
.style(default_header_cell_style),
245-
]).bottom_margin(1)
246-
);
247-
248-
table
276+
chart
249277
}
250278
}

src/windows/typing_window.rs

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,30 +17,36 @@ use crate::{
1717

1818
/// Handler for incorrect symbols
1919
///
20-
/// Insert incorrect symbol into the set
20+
/// Inserts incorrect characters into a HashSet
2121
pub struct MistakeHandler {
2222
mistakes_indexes: HashSet<usize>
2323
}
2424

2525
impl MistakeHandler {
26+
27+
/// Creates MistakeHandler with empty HashSet
2628
fn new() -> Self {
2729
Self {
2830
mistakes_indexes: HashSet::new()
2931
}
3032
}
3133

34+
/// Verifies if the character is mistaken
3235
pub fn is_char_mistaken(&self, char_index: usize) -> bool {
3336
self.mistakes_indexes.contains(&char_index)
3437
}
3538

36-
pub fn remove_from_mistakes_indexes(&mut self, char_index: usize) -> bool {
37-
self.mistakes_indexes.remove(&char_index)
38-
}
39-
39+
/// Adds the typed character into the mistakes
4040
pub fn add_to_mistakes_indexes(&mut self, char_index: usize) -> bool {
4141
self.mistakes_indexes.insert(char_index)
4242
}
4343

44+
/// Removes the typed character from mistakes
45+
pub fn remove_from_mistakes_indexes(&mut self, char_index: usize) -> bool {
46+
self.mistakes_indexes.remove(&char_index)
47+
}
48+
49+
/// Gets the current mistake count
4450
pub fn get_mistakes_counter(&self) -> usize {
4551
self.mistakes_indexes.len()
4652
}
@@ -50,13 +56,13 @@ pub struct TypingWindow {
5056
/// Random generated text
5157
pub generated_text: String,
5258

53-
/// User typed input
59+
/// User whole typed input
5460
pub input: String,
5561

56-
/// Handle incorrect symbols
62+
/// Handle incorrect characters
5763
pub mistake_handler: MistakeHandler,
5864

59-
/// User statistics after the run is completed
65+
/// User statistics after the current run is completed
6066
pub stat: Option<Stat>,
6167

6268
/// The TypingWindow is currently active window
@@ -84,19 +90,25 @@ impl Window for TypingWindow {
8490
fn default() -> Self {
8591
Self {
8692
generated_text: Generator::generate_random_string(50),
93+
8794
input: String::new(),
95+
8896
mistake_handler: MistakeHandler::new(),
8997

9098
stat: None,
9199

92100
is_active: false,
101+
93102
is_running: false,
103+
94104
is_popup_visible: false,
95105

96106
time_secs: 0,
107+
97108
cursor_index: 0,
98109

99110
config: TypingWindowConfig::default(),
111+
100112
motto: Generator::generate_random_motto()
101113
}
102114
}

0 commit comments

Comments
 (0)