Skip to content

Commit

Permalink
refactor(core/mercury): use ShowInfoParams
Browse files Browse the repository at this point in the history
Supply the rust layout with dedicated type instead of plain
micropython::Obj

[no changelog]
  • Loading branch information
obrusvit committed Sep 26, 2024
1 parent e8b0b92 commit 7b39754
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 49 deletions.
40 changes: 10 additions & 30 deletions core/embed/rust/src/ui/model_mercury/flow/confirm_summary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use heapless::Vec;

use crate::{
error,
micropython::{iter::IterBuf, obj::Obj, util},
strutil::TString,
translations::TR,
ui::{
Expand All @@ -19,7 +18,8 @@ use crate::{
use super::{
super::{
component::{
Frame, FrameMsg, PromptMsg, PromptScreen, VerticalMenu, VerticalMenuChoiceMsg,
Frame, FrameMsg, PromptMsg, PromptScreen, SwipeContent, VerticalMenu,
VerticalMenuChoiceMsg,
},
theme,
},
Expand Down Expand Up @@ -76,23 +76,15 @@ impl FlowController for ConfirmSummary {

pub fn new_confirm_summary(
title: TString<'static>,
items: Obj,
account_items: Obj,
fee_items: Obj,
summary_params: ShowInfoParams,
account_params: ShowInfoParams,
fee_params: ShowInfoParams,
br_name: TString<'static>,
br_code: u16,
cancel_text: Option<TString<'static>>,
) -> Result<SwipeFlow, error::Error> {
// Summary
let mut summary = ShowInfoParams::new(title)
.with_menu_button()
.with_footer(TR::instructions__swipe_up.into(), None)
.with_swipe_up();
for pair in IterBuf::new().try_iterate(items)? {
let [label, value]: [TString; 2] = util::iter_into_array(pair)?;
summary = unwrap!(summary.add(label, value));
}
let content_summary = summary
let content_summary = summary_params
.into_layout()?
.one_button_request(ButtonRequest::from_num(br_code, br_name))
// Summary(1) + Hold(1)
Expand All @@ -114,24 +106,12 @@ pub fn new_confirm_summary(
});

// FeeInfo
let mut has_fee_info = false;
let mut fee = ShowInfoParams::new(TR::confirm_total__title_fee.into()).with_cancel_button();
for pair in IterBuf::new().try_iterate(fee_items)? {
let [label, value]: [TString; 2] = util::iter_into_array(pair)?;
fee = unwrap!(fee.add(label, value));
has_fee_info = true;
}
let content_fee = fee.into_layout()?;
let has_fee_info = !fee_params.is_empty();
let content_fee = fee_params.into_layout()?;

// AccountInfo
let mut has_account_info = false;
let mut account = ShowInfoParams::new(TR::send__send_from.into()).with_cancel_button();
for pair in IterBuf::new().try_iterate(account_items)? {
let [label, value]: [TString; 2] = util::iter_into_array(pair)?;
account = unwrap!(account.add(label, value));
has_account_info = true;
}
let content_account = account.into_layout()?;
let has_account_info = !account_params.is_empty();
let content_account = account_params.into_layout()?;

// Menu
let mut menu = VerticalMenu::empty();
Expand Down
4 changes: 4 additions & 0 deletions core/embed/rust/src/ui/model_mercury/flow/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,10 @@ impl ShowInfoParams {
}
}

pub fn is_empty(&self) -> bool {
self.items.is_empty()
}

#[inline(never)]
pub const fn with_subtitle(mut self, subtitle: Option<TString<'static>>) -> Self {
self.subtitle = subtitle;
Expand Down
58 changes: 39 additions & 19 deletions core/embed/rust/src/ui/model_mercury/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ use crate::{
component::{check_homescreen_format, SwipeContent},
flow::new_confirm_action_simple,
flow::util::ConfirmBlobParams,
flow::util::ShowInfoParams,
theme::ICON_BULLET_CHECKMARK,
},
},
Expand Down Expand Up @@ -273,19 +274,15 @@ extern "C" fn new_confirm_blob(n_args: usize, args: *const Obj, kwargs: *mut Map
let chunkify: bool = kwargs.get_or(Qstr::MP_QSTR_chunkify, false)?;
let prompt_screen: bool = kwargs.get_or(Qstr::MP_QSTR_prompt_screen, true)?;

ConfirmBlobParams::new(
title,
data,
description,
)
.with_extra(extra)
.with_chunkify(chunkify)
.with_verb_cancel(verb_cancel)
.with_hold(hold)
.with_prompt(prompt_screen)
.into_flow()
.and_then(LayoutObj::new)
.map(Into::into)
ConfirmBlobParams::new(title, data, description)
.with_extra(extra)
.with_chunkify(chunkify)
.with_verb_cancel(verb_cancel)
.with_hold(hold)
.with_prompt(prompt_screen)
.into_flow()
.and_then(LayoutObj::new)
.map(Into::into)
};
unsafe { util::try_with_args_and_kwargs(n_args, args, kwargs, block) }
}
Expand Down Expand Up @@ -544,11 +541,34 @@ extern "C" fn new_confirm_summary(n_args: usize, args: *const Obj, kwargs: *mut
let cancel_text: Option<TString> =
kwargs.get(Qstr::MP_QSTR_cancel_text)?.try_into_option()?;

let flow = flow::confirm_summary::new_confirm_summary(
let mut summary_params = ShowInfoParams::new(title.clone())
.with_menu_button()
.with_footer(TR::instructions__swipe_up.into(), None)
.with_swipe_up();
for pair in IterBuf::new().try_iterate(items)? {
let [label, value]: [TString; 2] = util::iter_into_array(pair)?;
summary_params = unwrap!(summary_params.add(label, value));
}

let mut account_params =
ShowInfoParams::new(TR::send__send_from.into()).with_cancel_button();
for pair in IterBuf::new().try_iterate(account_items)? {
let [label, value]: [TString; 2] = util::iter_into_array(pair)?;
account_params = unwrap!(account_params.add(label, value));
}

let mut fee_params =
ShowInfoParams::new(TR::confirm_total__title_fee.into()).with_cancel_button();
for pair in IterBuf::new().try_iterate(fee_items)? {
let [label, value]: [TString; 2] = util::iter_into_array(pair)?;
fee_params = unwrap!(fee_params.add(label, value));
}

let flow = flow::new_confirm_summary(
title,
items,
account_items,
fee_items,
summary_params,
account_params,
fee_params,
br_name,
br_code,
cancel_text,
Expand Down Expand Up @@ -632,8 +652,8 @@ extern "C" fn new_confirm_value(n_args: usize, args: *const Obj, kwargs: *mut Ma
.with_prompt(hold)
.with_hold(hold)
.into_flow()
.and_then(LayoutObj::new)
.map(Into::into)
.and_then(LayoutObj::new)
.map(Into::into)
};
unsafe { util::try_with_args_and_kwargs(n_args, args, kwargs, block) }
}
Expand Down

0 comments on commit 7b39754

Please sign in to comment.