1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-11-26 17:38:39 +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 M1nd3r
parent 7b6ce56ac3
commit e9bb598d2f

View File

@ -147,25 +147,35 @@ impl SwipeFlow {
&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_state = 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.allow_swipe = true;
// send an Attach event to the new page
self.current_page_mut()
.event(ctx, Event::Attach(attach_type));
self.internal_pages = self.current_page_mut().get_internal_page_count() as u16;
match attach_type {
Swipe(Direction::Up) => {
self.internal_state = 0;
}
Swipe(Direction::Down) => {
self.internal_state = self.internal_pages.saturating_sub(1);
}
_ => {}
}
self.update_page_count(attach_type);
ctx.request_paint();
}
@ -191,6 +201,10 @@ impl SwipeFlow {
let mut decision = Decision::Nothing;
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 e = if self.allow_swipe {
@ -199,8 +213,6 @@ impl SwipeFlow {
.get_swipe_config()
.with_pagination(self.internal_state, self.internal_pages);
self.internal_pages = page.get_internal_page_count() as u16;
match self.swipe.event(ctx, event, config) {
Some(SwipeEvent::End(dir)) => {
if let Some(override_decision) = self.decision_override.take() {
@ -270,8 +282,7 @@ impl SwipeFlow {
match decision {
Decision::Transition(new_state, attach) => {
self.state = new_state;
self.goto(ctx, attach);
self.goto(ctx, new_state, attach);
None
}
Decision::Return(msg) => {