@@ -16,7 +16,7 @@ use iced::{
1616} ;
1717use serde_json:: json;
1818
19- use crate :: keyboard_config:: KeyboardConfig ;
19+ use crate :: { config :: Lesson , keyboard_config:: KeyboardConfig } ;
2020
2121mod config;
2222mod environment;
@@ -44,6 +44,7 @@ fn main() -> iced::Result {
4444#[ derive( Default ) ]
4545struct Raiti {
4646 config : Config ,
47+ lesson : Option < Lesson > ,
4748 exercise : Vec < Exercise > ,
4849 was_errors : u64 ,
4950 was_wpm : f64 ,
@@ -73,9 +74,19 @@ impl Raiti {
7374 )
7475 . expect ( "Error loading keyboard config" ) ;
7576
77+ let lesson = if !config. current_lesson . is_empty ( ) {
78+ Some (
79+ Lesson :: load ( Config :: data_dir ( ) . join ( format ! ( "{}.yaml" , config. current_lesson) ) )
80+ . expect ( "Error loading lesson" ) ,
81+ )
82+ } else {
83+ None
84+ } ;
85+
7686 (
7787 Self {
7888 config : config. clone ( ) ,
89+ lesson,
7990 exercise : vec ! [ ] ,
8091 keyboard : KeyboardComponent :: new ( keyboard_config) ,
8192 ..Default :: default ( )
@@ -155,7 +166,11 @@ impl Raiti {
155166 }
156167 Message :: LessonSelected ( lesson) => {
157168 // TODO: find a way to fail lesson load without unwrap
158- self . config . load_lesson ( & lesson. file ) . unwrap ( ) ;
169+ self . lesson = Some (
170+ self . config
171+ . load_lesson ( & lesson. file )
172+ . expect ( "Error loading lesson" ) ,
173+ ) ;
159174 Task :: none ( )
160175 }
161176 Message :: Confirm => self . exit_with_save ( ) ,
@@ -183,7 +198,10 @@ impl Raiti {
183198 . center_y ( Length :: Fill )
184199 . into ( ) ;
185200 }
186- if let Some ( page) = self . config . get_page ( ) {
201+ if let Some ( lesson) = & self . lesson {
202+ let page = lesson
203+ . get_page ( self . config . current_page )
204+ . expect ( "No page found" ) ;
187205 let title = text ( & page. title ) . size ( 25 ) ;
188206 let mut page_content = column ! [ title] ;
189207 let reg = Handlebars :: new ( ) ;
@@ -243,30 +261,37 @@ impl Raiti {
243261 self . exercise . clear ( ) ;
244262 self . keyboard . update ( keyboard_component:: Message :: ClearKeys ) ;
245263 self . config . next_page ( ) ;
246- if let Some ( page) = self . config . get_page ( ) {
264+ if let Some ( lesson) = & self . lesson {
265+ let page = lesson
266+ . get_page ( self . config . current_page )
267+ . expect ( "No page found" ) ;
247268 if !page. show_keys . is_empty ( ) {
248269 self . keyboard
249270 . update ( keyboard_component:: Message :: SetShowKeys (
250271 page. show_keys . clone ( ) ,
251272 ) )
252273 }
253274 }
254- if let Some ( ex) = self . config . get_exercise ( ) {
255- match ex {
256- config:: Exercise :: None => { }
257- config:: Exercise :: OneLineNoEnter ( line) => {
258- self . exercise . push ( Exercise :: new ( line) ) ;
259- }
260- config:: Exercise :: Multiline ( lines) => {
261- for line in lines. lines ( ) {
262- let mut ex = Exercise :: new ( line) ;
263- if self . exercise . is_empty ( ) {
264- ex. update ( exercise:: Message :: SetFocus ( true ) )
275+ if let Some ( lesson) = & self . lesson {
276+ if let Some ( ex) =
277+ lesson. get_exercise ( self . config . current_page , self . config . current_exercise )
278+ {
279+ match ex {
280+ config:: Exercise :: None => { }
281+ config:: Exercise :: OneLineNoEnter ( line) => {
282+ self . exercise . push ( Exercise :: new ( line) ) ;
283+ }
284+ config:: Exercise :: Multiline ( lines) => {
285+ for line in lines. lines ( ) {
286+ let mut ex = Exercise :: new ( line) ;
287+ if self . exercise . is_empty ( ) {
288+ ex. update ( exercise:: Message :: SetFocus ( true ) )
289+ }
290+ self . exercise . push ( ex) ;
265291 }
266- self . exercise . push ( ex) ;
267292 }
268293 }
269- }
294+ } ;
270295 }
271296 }
272297
0 commit comments