Skip to content

Commit

Permalink
refactor(core/mercury): use params structs
Browse files Browse the repository at this point in the history
Supply the rust layout with dedicated paremeter type instead of plain
micropython::Obj. The types used are ConfirmBlobParams and
ShowInfoParams.

[no changelog]
  • Loading branch information
obrusvit committed Sep 30, 2024
1 parent a8f0f9f commit b616b27
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 126 deletions.
80 changes: 17 additions & 63 deletions core/embed/rust/src/ui/model_mercury/flow/confirm_output.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 Down Expand Up @@ -212,40 +211,29 @@ fn get_cancel_page(
}

pub fn new_confirm_output(
title: Option<TString<'static>>,
subtitle: Option<TString<'static>>,
main_params: ConfirmBlobParams,
account: Option<TString<'static>>,
account_path: Option<TString<'static>>,
br_name: TString<'static>,
br_code: u16,
message: Obj,
amount: Option<Obj>,
chunkify: bool,
text_mono: bool,
address: Option<Obj>,
content_amount_params: Option<ConfirmBlobParams>,
address_params: Option<ConfirmBlobParams>,
address_title: Option<TString<'static>>,
summary_items: Obj,
fee_items: Obj,
summary_title: Option<TString<'static>>,
summary_items_params: Option<ShowInfoParams>,
fee_items_params: ShowInfoParams,
summary_br_name: Option<TString<'static>>,
summary_br_code: Option<u16>,
cancel_text: Option<TString<'static>>,
) -> Result<SwipeFlow, error::Error> {
// Main
let main_content = ConfirmBlobParams::new(title.unwrap_or(TString::empty()), message, None)
.with_subtitle(subtitle)
.with_menu_button()
.with_footer(TR::instructions__swipe_up.into(), None)
.with_chunkify(chunkify)
.with_text_mono(text_mono)
.with_swipe_up()
let main_content = main_params
.into_layout()?
.one_button_request(ButtonRequest::from_num(br_code, br_name));

// MainMenu
let mut main_menu = VerticalMenu::empty();
let mut main_menu_items = Vec::<usize, 3>::new();
if address.is_some() {
if address_params.is_some() {
main_menu = main_menu.item(
theme::ICON_CHEVRON_RIGHT,
address_title.unwrap_or(TR::words__address.into()),
Expand Down Expand Up @@ -279,37 +267,20 @@ pub fn new_confirm_output(
let ac = AddressDetails::new(TR::send__send_from.into(), account, account_path)?;
let account_content = ac.map(|_| Some(FlowMsg::Cancelled));

let res = if amount.is_some() {
let content_amount =
ConfirmBlobParams::new(TR::words__amount.into(), amount.unwrap(), None)
.with_subtitle(subtitle)
.with_menu_button()
.with_footer(TR::instructions__swipe_up.into(), None)
.with_text_mono(text_mono)
.with_swipe_up()
.with_swipe_down()
.into_layout()?
.one_button_request(ButtonRequest::from_num(br_code, br_name));
let res = if let Some(content_amount_params) = content_amount_params {
let content_amount = content_amount_params
.into_layout()?
.one_button_request(ButtonRequest::from_num(br_code, br_name));

SwipeFlow::new(&ConfirmOutputWithAmount::Address)?
.with_page(&ConfirmOutputWithAmount::Address, main_content)?
.with_page(&ConfirmOutputWithAmount::Amount, content_amount)?
.with_page(&ConfirmOutputWithAmount::Menu, content_main_menu)?
.with_page(&ConfirmOutputWithAmount::AccountInfo, account_content)?
.with_page(&ConfirmOutputWithAmount::CancelTap, get_cancel_page())?
} else if summary_items != Obj::const_none() {
} else if let Some(summary_items_params) = summary_items_params {
// Summary
let mut summary =
ShowInfoParams::new(summary_title.unwrap_or(TR::words__title_summary.into()))
.with_menu_button()
.with_footer(TR::instructions__swipe_up.into(), None)
.with_swipe_up()
.with_swipe_down();
for pair in IterBuf::new().try_iterate(summary_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_items_params
.into_layout()?
.one_button_request(ButtonRequest::from_num(
summary_br_code.unwrap(),
Expand All @@ -333,16 +304,8 @@ pub fn new_confirm_output(
});

// FeeInfo
let mut has_fee_info = false;
let mut fee = ShowInfoParams::new(TR::confirm_total__title_fee.into()).with_cancel_button();
if fee_items != Obj::const_none() {
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_items_params.is_empty();
let content_fee = fee_items_params.into_layout()?;

// SummaryMenu
let mut summary_menu = VerticalMenu::empty();
Expand Down Expand Up @@ -389,17 +352,8 @@ pub fn new_confirm_output(
.with_page(&ConfirmOutputWithSummary::Main, main_content)?
.with_page(&ConfirmOutputWithSummary::MainMenu, content_main_menu)?
.with_page(&ConfirmOutputWithSummary::MainMenuCancel, get_cancel_page())?;
if address.is_some() {
let address_content = ConfirmBlobParams::new(
address_title.unwrap_or(TR::words__address.into()),
address.unwrap(),
None,
)
.with_cancel_button()
.with_chunkify(true)
.with_text_mono(true)
.with_swipe_right()
.into_layout()?;
if let Some(address_params) = address_params {
let address_content = address_params.into_layout()?;
flow = flow.with_page(&ConfirmOutputWithSummary::AddressInfo, address_content)?;
} else {
// dummy page - this will never be shown since there is no menu item pointing to
Expand Down
41 changes: 10 additions & 31 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 @@ -75,24 +75,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 +105,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
Loading

0 comments on commit b616b27

Please sign in to comment.