1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-11-15 20:19:23 +00:00

refactor(core/rust): create a single code path allowed to modify SwipeFlow state

This commit is contained in:
matejcik 2024-09-06 12:04:18 +02:00 committed by matejcik
parent 0d3af6a96a
commit dbe6c1c664

View File

@ -102,8 +102,8 @@ pub struct SwipeFlow {
swipe: SwipeDetect, swipe: SwipeDetect,
/// Swipe allowed /// Swipe allowed
allow_swipe: bool, allow_swipe: bool,
/// Current internal state /// Current page index
internal_state: u16, internal_page_idx: u16,
/// Internal pages count /// Internal pages count
internal_pages: u16, internal_pages: u16,
/// If triggering swipe by event, make this decision instead of default /// If triggering swipe by event, make this decision instead of default
@ -118,7 +118,7 @@ impl SwipeFlow {
swipe: SwipeDetect::new(), swipe: SwipeDetect::new(),
store: Vec::new(), store: Vec::new(),
allow_swipe: true, allow_swipe: true,
internal_state: 0, internal_page_idx: 0,
internal_pages: 1, internal_pages: 1,
decision_override: None, decision_override: None,
}) })
@ -147,25 +147,35 @@ impl SwipeFlow {
&mut self.store[self.state.index()] &mut self.store[self.state.index()]
} }
fn goto(&mut self, ctx: &mut EventCtx, attach_type: AttachType) { fn update_page_count(&mut self, attach_type: AttachType) {
// update page count
self.internal_pages = self.current_page_mut().get_internal_page_count() as u16;
// reset internal state:
self.internal_page_idx = if let Swipe(Direction::Down) = attach_type {
// if coming from below, set to the last page
self.internal_pages.saturating_sub(1)
} else {
// else reset to the first page
0
};
}
/// Transition to a different state.
///
/// This is the only way to change the current flow state.
fn goto(&mut self, ctx: &mut EventCtx, new_state: FlowState, attach_type: AttachType) {
// update current page
self.state = new_state;
// reset and unlock swipe config
self.swipe = SwipeDetect::new(); self.swipe = SwipeDetect::new();
self.allow_swipe = true; self.allow_swipe = true;
// send an Attach event to the new page
self.current_page_mut() self.current_page_mut()
.event(ctx, Event::Attach(attach_type)); .event(ctx, Event::Attach(attach_type));
self.internal_pages = self.current_page_mut().get_internal_page_count() as u16; self.update_page_count(attach_type);
match attach_type {
Swipe(Direction::Up) => {
self.internal_state = 0;
}
Swipe(Direction::Down) => {
self.internal_state = self.internal_pages.saturating_sub(1);
}
_ => {}
}
ctx.request_paint(); ctx.request_paint();
} }
@ -191,15 +201,17 @@ impl SwipeFlow {
let mut decision = Decision::Nothing; let mut decision = Decision::Nothing;
let mut return_transition: AttachType = AttachType::Initial; let mut return_transition: AttachType = AttachType::Initial;
if let Event::Attach(attach_type) = event {
self.update_page_count(attach_type);
}
let mut attach = false; let mut attach = false;
let e = if self.allow_swipe { let e = if self.allow_swipe {
let page = self.current_page(); let page = self.current_page();
let config = page let config = page
.get_swipe_config() .get_swipe_config()
.with_pagination(self.internal_state, self.internal_pages); .with_pagination(self.internal_page_idx, self.internal_pages);
self.internal_pages = page.get_internal_page_count() as u16;
match self.swipe.event(ctx, event, config) { match self.swipe.event(ctx, event, config) {
Some(SwipeEvent::End(dir)) => { Some(SwipeEvent::End(dir)) => {
@ -211,10 +223,10 @@ impl SwipeFlow {
return_transition = AttachType::Swipe(dir); return_transition = AttachType::Swipe(dir);
let new_internal_state = let new_internal_page_idx =
config.paging_event(dir, self.internal_state, self.internal_pages); config.paging_event(dir, self.internal_page_idx, self.internal_pages);
if new_internal_state != self.internal_state { if new_internal_page_idx != self.internal_page_idx {
self.internal_state = new_internal_state; self.internal_page_idx = new_internal_page_idx;
decision = Decision::Nothing; decision = Decision::Nothing;
attach = true; attach = true;
} }
@ -270,8 +282,7 @@ impl SwipeFlow {
match decision { match decision {
Decision::Transition(new_state, attach) => { Decision::Transition(new_state, attach) => {
self.state = new_state; self.goto(ctx, new_state, attach);
self.goto(ctx, attach);
None None
} }
Decision::Return(msg) => { Decision::Return(msg) => {