From 313346251d496bf989beb6985188eba3892cb3cd Mon Sep 17 00:00:00 2001 From: ardocrat Date: Sun, 19 May 2024 15:28:15 +0300 Subject: [PATCH] messages: scan slatepack qr code --- src/gui/views/wallets/wallet/messages.rs | 99 +++++++++++++++++++++++- 1 file changed, 95 insertions(+), 4 deletions(-) diff --git a/src/gui/views/wallets/wallet/messages.rs b/src/gui/views/wallets/wallet/messages.rs index 9385460..ecd76ad 100644 --- a/src/gui/views/wallets/wallet/messages.rs +++ b/src/gui/views/wallets/wallet/messages.rs @@ -24,8 +24,8 @@ use parking_lot::RwLock; use crate::gui::Colors; use crate::gui::icons::{BROOM, CLIPBOARD_TEXT, COPY, DOWNLOAD_SIMPLE, PROHIBIT, QR_CODE, SCAN, UPLOAD_SIMPLE}; use crate::gui::platform::PlatformCallbacks; -use crate::gui::views::{Modal, Root, View}; -use crate::gui::views::types::{ModalPosition, TextEditOptions}; +use crate::gui::views::{CameraContent, Modal, Root, View}; +use crate::gui::views::types::{ModalPosition, QrScanResult, TextEditOptions}; use crate::gui::views::wallets::wallet::types::{SLATEPACK_MESSAGE_HINT, WalletTab, WalletTabType}; use crate::gui::views::wallets::wallet::WalletContent; use crate::wallet::types::WalletTransaction; @@ -85,11 +85,19 @@ pub struct WalletMessages { request_loading: bool, /// Request result if there is no error at [`Modal`]. request_result: Arc>>>, + + /// Camera content for Slatepack message QR code scanning [`Modal`]. + message_camera_content: CameraContent, + /// Flag to check if there is an error on scanning Slatepack message QR code at [`Modal`]. + message_scan_error: bool, } /// Identifier for amount input [`Modal`]. const AMOUNT_MODAL: &'static str = "amount_modal"; +/// Identifier for QR code Slatepack message scan [`Modal`]. +const QR_SLATEPACK_SCAN_MODAL: &'static str = "qr_slatepack_scan_modal"; + impl WalletTab for WalletMessages { fn get_type(&self) -> WalletTabType { WalletTabType::Messages @@ -154,6 +162,8 @@ impl WalletMessages { request_error: None, request_loading: false, request_result: Arc::new(RwLock::new(None)), + message_camera_content: CameraContent::default(), + message_scan_error: false, } } @@ -191,6 +201,11 @@ impl WalletMessages { self.amount_modal_ui(ui, wallet, modal, cb); }); } + QR_SLATEPACK_SCAN_MODAL => { + Modal::ui(ui.ctx(), |ui, modal| { + self.scan_qr_modal_ui(ui, modal, wallet, cb); + }); + } _ => {} } } @@ -454,7 +469,7 @@ impl WalletMessages { // Draw button to scan Slatepack message QR code. let scan_text = format!("{} {}", SCAN, t!("scan")); View::button(ui, scan_text, Colors::BUTTON, || { - //TODO: scan code + self.show_qr_scan_modal(cb); }); } }); @@ -500,6 +515,82 @@ impl WalletMessages { } } + /// Show QR code recovery phrase scanner [`Modal`]. + pub fn show_qr_scan_modal(&mut self, cb: &dyn PlatformCallbacks) { + self.message_scan_error = false; + // Show QR code scan modal. + Modal::new(QR_SLATEPACK_SCAN_MODAL) + .position(ModalPosition::CenterTop) + .title(t!("scan_qr")) + .closeable(false) + .show(); + cb.start_camera(); + } + + /// Draw QR code scan [`Modal`] content. + fn scan_qr_modal_ui(&mut self, + ui: &mut egui::Ui, + modal: &Modal, + wallet: &Wallet, + cb: &dyn PlatformCallbacks) { + if self.message_scan_error { + ui.add_space(6.0); + ui.vertical_centered(|ui| { + let err_text = format!("{}", t!("wallets.parse_slatepack_err")).replace(":", ""); + ui.label(RichText::new(err_text) + .size(17.0) + .color(Colors::RED)); + }); + ui.add_space(6.0); + + // Setup spacing between buttons. + ui.spacing_mut().item_spacing = egui::Vec2::new(6.0, 0.0); + + ui.columns(2, |columns| { + columns[0].vertical_centered_justified(|ui| { + View::button(ui, t!("close"), Colors::WHITE, || { + self.message_scan_error = false; + modal.close(); + }); + }); + columns[1].vertical_centered_justified(|ui| { + View::button(ui, t!("repeat"), Colors::WHITE, || { + Modal::set_title(t!("scan_qr")); + self.message_scan_error = false; + cb.start_camera(); + }); + }); + }); + ui.add_space(6.0); + return; + } else if let Some(result) = self.message_camera_content.qr_scan_result() { + cb.stop_camera(); + self.message_camera_content.clear_state(); + match &result { + QrScanResult::Slatepack(text) => { + self.message_edit = text.to_string(); + self.parse_message(wallet); + modal.close(); + } + _ => { + self.message_scan_error = true; + } + } + } else { + ui.add_space(6.0); + self.message_camera_content.ui(ui, cb); + ui.add_space(6.0); + } + + ui.vertical_centered_justified(|ui| { + View::button(ui, t!("modal.cancel"), Colors::WHITE, || { + cb.stop_camera(); + modal.close(); + }); + }); + ui.add_space(6.0); + } + /// Check Slatepack message request loading result. fn check_message_loading_result(&mut self, wallet: &Wallet) { // Check finalize post pay result. @@ -605,7 +696,7 @@ impl WalletMessages { } /// Parse message input into [`Slate`] updating slate and response input. - pub fn parse_message(&mut self, wallet: &mut Wallet) { + pub fn parse_message(&mut self, wallet: &Wallet) { self.message_error = None; if self.message_edit.is_empty() { return;