2023-08-03 00:00:23 +03:00
|
|
|
// Copyright 2023 The Grim Developers
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
2023-08-03 23:49:11 +03:00
|
|
|
// limitations under the License.
|
|
|
|
|
2024-04-16 15:24:22 +03:00
|
|
|
use egui::{Id, Margin, RichText, ScrollArea};
|
|
|
|
use egui::scroll_area::ScrollBarVisibility;
|
|
|
|
use grin_core::core::{amount_from_hr_string, amount_to_hr_string};
|
2024-04-20 16:59:54 +03:00
|
|
|
use grin_wallet_libwallet::{Slate, SlateState};
|
2024-04-18 16:55:37 +03:00
|
|
|
use log::error;
|
2023-08-16 04:42:05 +03:00
|
|
|
|
|
|
|
use crate::gui::Colors;
|
2024-04-22 14:34:19 +03:00
|
|
|
use crate::gui::icons::{BROOM, CLIPBOARD_TEXT, COPY, DOWNLOAD_SIMPLE, PROHIBIT, UPLOAD_SIMPLE};
|
2023-08-03 23:49:11 +03:00
|
|
|
use crate::gui::platform::PlatformCallbacks;
|
2024-04-16 15:24:22 +03:00
|
|
|
use crate::gui::views::{Modal, Root, View};
|
|
|
|
use crate::gui::views::types::{ModalPosition, TextEditOptions};
|
|
|
|
use crate::gui::views::wallets::wallet::types::{SLATEPACK_MESSAGE_HINT, WalletTab, WalletTabType};
|
2023-08-12 04:24:23 +03:00
|
|
|
use crate::gui::views::wallets::wallet::WalletContent;
|
2024-04-20 16:59:54 +03:00
|
|
|
use crate::wallet::types::WalletTransaction;
|
2023-08-09 02:22:16 +03:00
|
|
|
use crate::wallet::Wallet;
|
2023-08-03 23:49:11 +03:00
|
|
|
|
2024-04-18 16:55:37 +03:00
|
|
|
#[derive(Clone, Eq, PartialEq, Debug, thiserror::Error)]
|
|
|
|
enum MessageError {
|
|
|
|
#[error("{0}")]
|
|
|
|
Response(String),
|
|
|
|
#[error("{0}")]
|
|
|
|
Parse(String),
|
|
|
|
#[error("{0}")]
|
|
|
|
Finalize(String),
|
|
|
|
#[error("{0}")]
|
|
|
|
Other(String),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl MessageError {
|
|
|
|
pub fn text(&self) -> &String {
|
|
|
|
match self {
|
|
|
|
MessageError::Response(text) => text,
|
|
|
|
MessageError::Parse(text) => text,
|
|
|
|
MessageError::Finalize(text) => text,
|
|
|
|
MessageError::Other(text) => text
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-18 05:20:49 +03:00
|
|
|
/// Slatepacks messages interaction tab content.
|
|
|
|
pub struct WalletMessages {
|
|
|
|
/// Slatepack message to create response message.
|
2023-08-21 08:30:50 +03:00
|
|
|
message_edit: String,
|
2024-04-18 05:20:49 +03:00
|
|
|
/// Parsed Slatepack message.
|
|
|
|
message_slate: Option<Slate>,
|
2024-04-18 16:55:37 +03:00
|
|
|
/// Slatepack error on finalization, parse and response creation.
|
|
|
|
message_error: Option<MessageError>,
|
2024-04-18 05:20:49 +03:00
|
|
|
/// Generated Slatepack response message.
|
2023-08-21 08:30:50 +03:00
|
|
|
response_edit: String,
|
2024-04-18 05:20:49 +03:00
|
|
|
/// Flag to check if Dandelion is needed to finalize transaction.
|
2024-04-20 16:59:54 +03:00
|
|
|
dandelion: bool,
|
2024-04-16 15:24:22 +03:00
|
|
|
|
2024-04-30 18:15:03 +03:00
|
|
|
/// Flag to check if send or invoice request was opened for [`Modal`].
|
|
|
|
send_request: bool,
|
|
|
|
/// Amount to send or receive at [`Modal`].
|
2024-04-16 15:24:22 +03:00
|
|
|
amount_edit: String,
|
2024-04-30 18:15:03 +03:00
|
|
|
/// Generated Slatepack message as request to send or receive funds at [`Modal`].
|
2024-04-16 15:24:22 +03:00
|
|
|
request_edit: String,
|
2024-04-30 18:15:03 +03:00
|
|
|
/// Flag to check if there is an error happened on request creation at [`Modal`].
|
2024-04-18 16:55:37 +03:00
|
|
|
request_error: Option<MessageError>,
|
2023-08-21 08:30:50 +03:00
|
|
|
}
|
|
|
|
|
2024-04-30 18:15:03 +03:00
|
|
|
/// Identifier for amount input [`Modal`].
|
2024-04-18 05:20:49 +03:00
|
|
|
const AMOUNT_MODAL: &'static str = "amount_modal";
|
2024-04-16 15:24:22 +03:00
|
|
|
|
2024-04-18 05:20:49 +03:00
|
|
|
impl WalletTab for WalletMessages {
|
2023-08-03 23:49:11 +03:00
|
|
|
fn get_type(&self) -> WalletTabType {
|
2024-04-18 05:20:49 +03:00
|
|
|
WalletTabType::Messages
|
2023-08-03 23:49:11 +03:00
|
|
|
}
|
|
|
|
|
2023-08-11 17:29:25 +03:00
|
|
|
fn ui(&mut self,
|
|
|
|
ui: &mut egui::Ui,
|
2024-04-16 15:24:22 +03:00
|
|
|
_: &mut eframe::Frame,
|
2023-08-11 17:29:25 +03:00
|
|
|
wallet: &mut Wallet,
|
|
|
|
cb: &dyn PlatformCallbacks) {
|
2024-04-16 15:24:22 +03:00
|
|
|
if WalletContent::sync_ui(ui, wallet) {
|
2023-08-12 04:24:23 +03:00
|
|
|
return;
|
|
|
|
}
|
2023-08-16 04:42:05 +03:00
|
|
|
|
2024-04-16 15:24:22 +03:00
|
|
|
// Show modal content for this ui container.
|
|
|
|
self.modal_content_ui(ui, wallet, cb);
|
|
|
|
|
2024-04-18 05:20:49 +03:00
|
|
|
// Show manual wallet content panel.
|
2023-08-16 04:42:05 +03:00
|
|
|
egui::CentralPanel::default()
|
|
|
|
.frame(egui::Frame {
|
2023-08-16 05:15:35 +03:00
|
|
|
stroke: View::ITEM_STROKE,
|
2023-08-16 04:42:05 +03:00
|
|
|
fill: Colors::WHITE,
|
|
|
|
inner_margin: Margin {
|
|
|
|
left: View::far_left_inset_margin(ui) + 4.0,
|
|
|
|
right: View::get_right_inset() + 4.0,
|
|
|
|
top: 3.0,
|
|
|
|
bottom: 4.0,
|
|
|
|
},
|
|
|
|
..Default::default()
|
|
|
|
})
|
|
|
|
.show_inside(ui, |ui| {
|
2024-04-16 15:24:22 +03:00
|
|
|
ScrollArea::vertical()
|
|
|
|
.scroll_bar_visibility(ScrollBarVisibility::AlwaysVisible)
|
2024-04-30 18:15:03 +03:00
|
|
|
.id_source(Id::from("wallet_messages").with(wallet.get_config().id))
|
2024-04-16 15:24:22 +03:00
|
|
|
.auto_shrink([false; 2])
|
|
|
|
.show(ui, |ui| {
|
|
|
|
ui.vertical_centered(|ui| {
|
|
|
|
View::max_width_ui(ui, Root::SIDE_PANEL_WIDTH * 1.3, |ui| {
|
2024-04-18 05:20:49 +03:00
|
|
|
self.ui(ui, wallet, cb);
|
2024-04-16 15:24:22 +03:00
|
|
|
});
|
|
|
|
});
|
2023-08-21 08:30:50 +03:00
|
|
|
});
|
2023-08-16 04:42:05 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-18 05:20:49 +03:00
|
|
|
impl WalletMessages {
|
2024-05-04 12:20:35 +03:00
|
|
|
/// Create new content instance, put message into input if provided.
|
|
|
|
pub fn new(dandelion: bool, message: Option<String>) -> Self {
|
|
|
|
Self {
|
|
|
|
send_request: false,
|
|
|
|
message_edit: message.unwrap_or("".to_string()),
|
|
|
|
message_slate: None,
|
|
|
|
message_error: None,
|
|
|
|
response_edit: "".to_string(),
|
|
|
|
dandelion,
|
|
|
|
amount_edit: "".to_string(),
|
|
|
|
request_edit: "".to_string(),
|
|
|
|
request_error: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-18 05:20:49 +03:00
|
|
|
/// Draw manual wallet transaction interaction content.
|
|
|
|
pub fn ui(&mut self,
|
|
|
|
ui: &mut egui::Ui,
|
|
|
|
wallet: &mut Wallet,
|
|
|
|
cb: &dyn PlatformCallbacks) {
|
2024-04-24 01:42:56 +03:00
|
|
|
ui.add_space(3.0);
|
2024-04-18 05:20:49 +03:00
|
|
|
|
|
|
|
// Show creation of request to send or receive funds.
|
2024-05-01 04:49:48 +03:00
|
|
|
self.request_ui(ui, wallet, cb);
|
2024-04-18 05:20:49 +03:00
|
|
|
|
|
|
|
ui.add_space(12.0);
|
2023-08-21 08:30:50 +03:00
|
|
|
View::horizontal_line(ui, Colors::ITEM_STROKE);
|
2024-04-24 01:42:56 +03:00
|
|
|
ui.add_space(6.0);
|
2024-04-18 05:20:49 +03:00
|
|
|
|
|
|
|
// Show Slatepack message input field.
|
|
|
|
self.input_slatepack_ui(ui, wallet, cb);
|
|
|
|
|
|
|
|
ui.add_space(6.0);
|
2024-04-16 15:24:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Draw [`Modal`] content for this ui container.
|
|
|
|
fn modal_content_ui(&mut self,
|
|
|
|
ui: &mut egui::Ui,
|
|
|
|
wallet: &mut Wallet,
|
|
|
|
cb: &dyn PlatformCallbacks) {
|
|
|
|
match Modal::opened() {
|
|
|
|
None => {}
|
|
|
|
Some(id) => {
|
|
|
|
match id {
|
2024-04-18 05:20:49 +03:00
|
|
|
AMOUNT_MODAL => {
|
2024-04-16 15:24:22 +03:00
|
|
|
Modal::ui(ui.ctx(), |ui, modal| {
|
2024-04-18 05:20:49 +03:00
|
|
|
self.amount_modal_ui(ui, wallet, modal, cb);
|
2024-04-16 15:24:22 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-30 18:15:03 +03:00
|
|
|
/// Draw creation of request to send or receive funds.
|
|
|
|
fn request_ui(&mut self,
|
|
|
|
ui: &mut egui::Ui,
|
2024-05-01 04:49:48 +03:00
|
|
|
wallet: &Wallet,
|
2024-04-30 18:15:03 +03:00
|
|
|
cb: &dyn PlatformCallbacks) {
|
|
|
|
ui.label(RichText::new(t!("wallets.create_request_desc"))
|
|
|
|
.size(16.0)
|
|
|
|
.color(Colors::INACTIVE_TEXT));
|
|
|
|
ui.add_space(7.0);
|
|
|
|
|
2024-05-01 04:49:48 +03:00
|
|
|
// Show send button only if balance is not empty.
|
|
|
|
let data = wallet.get_data().unwrap();
|
|
|
|
if data.info.amount_currently_spendable > 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| {
|
|
|
|
// Draw send request creation button.
|
|
|
|
let send_text = format!("{} {}", UPLOAD_SIMPLE, t!("wallets.send"));
|
|
|
|
View::button(ui, send_text, Colors::BUTTON, || {
|
|
|
|
// Setup modal values.
|
|
|
|
self.send_request = true;
|
|
|
|
self.amount_edit = "".to_string();
|
|
|
|
self.request_error = None;
|
|
|
|
// Show send amount modal.
|
|
|
|
Modal::new(AMOUNT_MODAL)
|
|
|
|
.position(ModalPosition::CenterTop)
|
|
|
|
.title(t!("wallets.send"))
|
|
|
|
.show();
|
|
|
|
cb.show_keyboard();
|
|
|
|
});
|
2024-04-30 18:15:03 +03:00
|
|
|
});
|
2024-05-01 04:49:48 +03:00
|
|
|
columns[1].vertical_centered_justified(|ui| {
|
|
|
|
// Draw invoice request creation button.
|
|
|
|
self.receive_button_ui(ui, cb);
|
2024-04-30 18:15:03 +03:00
|
|
|
});
|
|
|
|
});
|
2024-05-01 04:49:48 +03:00
|
|
|
} else {
|
|
|
|
// Draw invoice creation button.
|
|
|
|
self.receive_button_ui(ui, cb);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Draw invoice request creation button.
|
|
|
|
fn receive_button_ui(&mut self, ui: &mut egui::Ui, cb: &dyn PlatformCallbacks) {
|
|
|
|
let receive_text = format!("{} {}", DOWNLOAD_SIMPLE, t!("wallets.receive"));
|
|
|
|
View::button(ui, receive_text, Colors::BUTTON, || {
|
|
|
|
// Setup modal values.
|
|
|
|
self.send_request = false;
|
|
|
|
self.amount_edit = "".to_string();
|
|
|
|
self.request_error = None;
|
|
|
|
// Show receive amount modal.
|
|
|
|
Modal::new(AMOUNT_MODAL)
|
|
|
|
.position(ModalPosition::CenterTop)
|
|
|
|
.title(t!("wallets.receive"))
|
|
|
|
.show();
|
|
|
|
cb.show_keyboard();
|
2024-04-30 18:15:03 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-04-18 05:20:49 +03:00
|
|
|
/// Draw Slatepack message input content.
|
|
|
|
fn input_slatepack_ui(&mut self,
|
|
|
|
ui: &mut egui::Ui,
|
|
|
|
wallet: &mut Wallet,
|
|
|
|
cb: &dyn PlatformCallbacks) {
|
2024-04-16 15:24:22 +03:00
|
|
|
// Setup description.
|
2023-08-21 08:30:50 +03:00
|
|
|
let response_empty = self.response_edit.is_empty();
|
2024-04-18 16:55:37 +03:00
|
|
|
if let Some(err) = &self.message_error {
|
|
|
|
ui.label(RichText::new(err.text()).size(16.0).color(Colors::RED));
|
2023-08-21 08:30:50 +03:00
|
|
|
} else {
|
2024-04-20 23:49:19 +03:00
|
|
|
let desc_text = if self.message_slate.is_none() {
|
2024-04-18 05:20:49 +03:00
|
|
|
t!("wallets.input_slatepack_desc")
|
2024-04-16 15:24:22 +03:00
|
|
|
} else {
|
2024-04-21 23:43:00 +03:00
|
|
|
let slate = self.message_slate.clone().unwrap();
|
2024-04-18 05:20:49 +03:00
|
|
|
let amount = amount_to_hr_string(slate.amount, true);
|
|
|
|
match slate.state {
|
|
|
|
SlateState::Standard1 => {
|
|
|
|
t!("wallets.parse_s1_slatepack_desc","amount" => amount)
|
|
|
|
}
|
|
|
|
SlateState::Standard2 => {
|
|
|
|
t!("wallets.parse_s2_slatepack_desc","amount" => amount)
|
|
|
|
}
|
2024-04-20 16:59:54 +03:00
|
|
|
SlateState::Standard3 => {
|
|
|
|
t!("wallets.parse_s3_slatepack_desc","amount" => amount)
|
|
|
|
}
|
2024-04-18 05:20:49 +03:00
|
|
|
SlateState::Invoice1 => {
|
2024-04-18 16:55:37 +03:00
|
|
|
t!("wallets.parse_i1_slatepack_desc","amount" => amount)
|
2024-04-18 05:20:49 +03:00
|
|
|
}
|
|
|
|
SlateState::Invoice2 => {
|
|
|
|
t!("wallets.parse_i2_slatepack_desc","amount" => amount)
|
|
|
|
}
|
2024-04-20 16:59:54 +03:00
|
|
|
SlateState::Invoice3 => {
|
|
|
|
t!("wallets.parse_i3_slatepack_desc","amount" => amount)
|
|
|
|
}
|
2024-04-18 05:20:49 +03:00
|
|
|
_ => {
|
|
|
|
t!("wallets.input_slatepack_desc")
|
|
|
|
}
|
|
|
|
}
|
2024-04-16 15:24:22 +03:00
|
|
|
};
|
|
|
|
ui.label(RichText::new(desc_text).size(16.0).color(Colors::INACTIVE_TEXT));
|
|
|
|
}
|
2024-04-20 16:59:54 +03:00
|
|
|
ui.add_space(6.0);
|
2023-08-21 08:30:50 +03:00
|
|
|
|
2024-04-16 15:24:22 +03:00
|
|
|
// Setup Slatepack message text input.
|
2024-04-20 16:59:54 +03:00
|
|
|
let message = if response_empty {
|
2023-08-21 08:30:50 +03:00
|
|
|
&mut self.message_edit
|
|
|
|
} else {
|
|
|
|
&mut self.response_edit
|
|
|
|
};
|
|
|
|
|
2024-04-18 05:20:49 +03:00
|
|
|
// Save message to check for changes.
|
|
|
|
let message_before = message.clone();
|
|
|
|
|
2023-08-21 08:30:50 +03:00
|
|
|
View::horizontal_line(ui, Colors::ITEM_STROKE);
|
|
|
|
ui.add_space(3.0);
|
|
|
|
ScrollArea::vertical()
|
|
|
|
.max_height(128.0)
|
2024-04-18 16:55:37 +03:00
|
|
|
.id_source(Id::from(
|
|
|
|
if response_empty {
|
|
|
|
"message_input"
|
|
|
|
} else {
|
|
|
|
"response_input"
|
|
|
|
}).with(wallet.get_config().id))
|
2023-08-21 08:30:50 +03:00
|
|
|
.auto_shrink([false; 2])
|
|
|
|
.show(ui, |ui| {
|
|
|
|
ui.add_space(7.0);
|
2024-05-15 21:26:15 +03:00
|
|
|
let resp = egui::TextEdit::multiline(message)
|
2023-08-21 08:30:50 +03:00
|
|
|
.font(egui::TextStyle::Small)
|
|
|
|
.desired_rows(5)
|
|
|
|
.interactive(response_empty)
|
2024-04-16 15:24:22 +03:00
|
|
|
.hint_text(SLATEPACK_MESSAGE_HINT)
|
2023-08-21 08:30:50 +03:00
|
|
|
.desired_width(f32::INFINITY)
|
|
|
|
.show(ui);
|
2024-05-15 21:26:15 +03:00
|
|
|
// Show soft keyboard on click.
|
|
|
|
if response_empty && resp.response.clicked() {
|
|
|
|
cb.show_keyboard();
|
|
|
|
}
|
2023-08-21 08:30:50 +03:00
|
|
|
ui.add_space(6.0);
|
|
|
|
});
|
|
|
|
ui.add_space(2.0);
|
|
|
|
View::horizontal_line(ui, Colors::ITEM_STROKE);
|
|
|
|
ui.add_space(10.0);
|
|
|
|
|
2024-04-20 16:59:54 +03:00
|
|
|
// Parse Slatepack message if input field was changed, resetting message error.
|
|
|
|
if &message_before != message {
|
|
|
|
self.parse_message(wallet);
|
|
|
|
}
|
|
|
|
|
2024-04-16 15:24:22 +03:00
|
|
|
// Draw buttons to clear/copy/paste.
|
2024-04-18 05:20:49 +03:00
|
|
|
let fields_empty = self.message_edit.is_empty() && self.response_edit.is_empty();
|
|
|
|
let columns_num = if fields_empty { 1 } else { 2 };
|
|
|
|
let mut show_dandelion = false;
|
2023-08-21 08:30:50 +03:00
|
|
|
ui.scope(|ui| {
|
|
|
|
// Setup spacing between buttons.
|
2024-04-20 16:59:54 +03:00
|
|
|
ui.spacing_mut().item_spacing = egui::Vec2::new(6.0, 0.0);
|
2023-08-16 04:42:05 +03:00
|
|
|
|
2023-08-21 08:30:50 +03:00
|
|
|
ui.columns(columns_num, |columns| {
|
|
|
|
let first_column_content = |ui: &mut egui::Ui| {
|
2024-04-20 16:59:54 +03:00
|
|
|
if self.message_slate.is_some() {
|
|
|
|
if self.response_edit.is_empty() {
|
|
|
|
let clear_text = format!("{} {}", BROOM, t!("clear"));
|
|
|
|
View::button(ui, clear_text, Colors::BUTTON, || {
|
|
|
|
self.message_edit.clear();
|
|
|
|
self.response_edit.clear();
|
|
|
|
self.message_error = None;
|
|
|
|
self.message_slate = None;
|
|
|
|
});
|
|
|
|
} else {
|
2024-04-21 23:43:00 +03:00
|
|
|
let cancel = format!("{} {}", PROHIBIT, t!("modal.cancel"));
|
|
|
|
View::colored_text_button(ui, cancel, Colors::RED, Colors::BUTTON, || {
|
2024-04-20 16:59:54 +03:00
|
|
|
let slate = self.message_slate.clone().unwrap();
|
|
|
|
if let Some(tx) = wallet.tx_by_slate(&slate) {
|
|
|
|
wallet.cancel(tx.data.id);
|
|
|
|
self.message_edit.clear();
|
|
|
|
self.response_edit.clear();
|
|
|
|
self.message_slate = None;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2024-04-16 15:24:22 +03:00
|
|
|
} else {
|
2024-04-21 23:43:00 +03:00
|
|
|
let paste = format!("{} {}", CLIPBOARD_TEXT, t!("paste"));
|
|
|
|
View::button(ui, paste, Colors::BUTTON, || {
|
2024-04-20 16:59:54 +03:00
|
|
|
let buf = cb.get_string_from_buffer();
|
|
|
|
let previous = self.message_edit.clone();
|
2024-05-15 21:26:15 +03:00
|
|
|
self.message_edit = buf.clone().trim().to_string();
|
2024-04-20 16:59:54 +03:00
|
|
|
// Parse Slatepack message resetting message error.
|
|
|
|
if buf != previous {
|
|
|
|
self.parse_message(wallet);
|
|
|
|
}
|
2023-08-21 08:30:50 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
if columns_num == 1 {
|
|
|
|
columns[0].vertical_centered(first_column_content);
|
|
|
|
} else {
|
|
|
|
columns[0].vertical_centered_justified(first_column_content);
|
|
|
|
columns[1].vertical_centered_justified(|ui| {
|
2024-04-20 16:59:54 +03:00
|
|
|
if self.message_slate.is_some() {
|
|
|
|
if !self.response_edit.is_empty() {
|
|
|
|
let copy_text = format!("{} {}", COPY, t!("copy"));
|
|
|
|
View::button(ui, copy_text, Colors::BUTTON, || {
|
|
|
|
cb.copy_string_to_buffer(self.response_edit.clone());
|
2024-04-18 16:55:37 +03:00
|
|
|
self.message_edit.clear();
|
2024-04-20 16:59:54 +03:00
|
|
|
self.response_edit.clear();
|
2024-04-18 16:55:37 +03:00
|
|
|
self.message_slate = None;
|
2024-04-20 16:59:54 +03:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
show_dandelion = true;
|
|
|
|
View::button(ui, t!("wallets.finalize"), Colors::GOLD, || {
|
|
|
|
let slate = self.message_slate.clone().unwrap();
|
|
|
|
if slate.state == SlateState::Invoice3 ||
|
|
|
|
slate.state == SlateState::Standard3 {
|
2024-04-24 01:42:56 +03:00
|
|
|
if wallet.post(&slate, self.dandelion).is_ok() {
|
2024-04-20 16:59:54 +03:00
|
|
|
self.message_edit.clear();
|
|
|
|
self.message_slate = None;
|
|
|
|
} else {
|
|
|
|
self.message_error = Some(
|
|
|
|
MessageError::Finalize(
|
|
|
|
t!("wallets.finalize_slatepack_err")
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} else {
|
2024-04-24 01:42:56 +03:00
|
|
|
let r = wallet.finalize(&self.message_edit, self.dandelion);
|
|
|
|
if r.is_ok() {
|
2024-04-20 16:59:54 +03:00
|
|
|
self.message_edit.clear();
|
|
|
|
self.message_slate = None;
|
|
|
|
} else {
|
|
|
|
self.message_error = Some(
|
|
|
|
MessageError::Finalize(
|
|
|
|
t!("wallets.finalize_slatepack_err")
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
let clear_text = format!("{} {}", BROOM, t!("clear"));
|
|
|
|
View::button(ui, clear_text, Colors::BUTTON, || {
|
|
|
|
self.message_error = None;
|
|
|
|
self.message_edit.clear();
|
|
|
|
self.response_edit.clear();
|
|
|
|
self.message_slate = None;
|
2023-08-21 08:30:50 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2024-04-21 23:43:00 +03:00
|
|
|
|
|
|
|
ui.add_space(12.0);
|
|
|
|
|
|
|
|
// Draw clear button on response.
|
|
|
|
if !self.response_edit.is_empty() && self.message_slate.is_some() {
|
|
|
|
let clear_text = format!("{} {}", BROOM, t!("clear"));
|
|
|
|
View::button(ui, clear_text, Colors::BUTTON, || {
|
|
|
|
self.message_error = None;
|
|
|
|
self.message_edit.clear();
|
|
|
|
self.response_edit.clear();
|
|
|
|
self.message_slate = None;
|
|
|
|
});
|
|
|
|
}
|
2023-08-21 08:30:50 +03:00
|
|
|
});
|
|
|
|
|
2024-04-18 05:20:49 +03:00
|
|
|
// Draw setup of ability to post transaction with Dandelion.
|
|
|
|
if show_dandelion {
|
2024-04-20 16:59:54 +03:00
|
|
|
let dandelion_before = self.dandelion;
|
|
|
|
View::checkbox(ui, dandelion_before, t!("wallets.use_dandelion"), || {
|
|
|
|
self.dandelion = !dandelion_before;
|
|
|
|
wallet.update_use_dandelion(self.dandelion);
|
2024-04-18 05:20:49 +03:00
|
|
|
});
|
|
|
|
}
|
2024-04-18 16:55:37 +03:00
|
|
|
}
|
2024-04-18 05:20:49 +03:00
|
|
|
|
2024-04-20 16:59:54 +03:00
|
|
|
/// Parse message input into [`Slate`] updating slate and response input.
|
2024-05-04 12:20:35 +03:00
|
|
|
pub fn parse_message(&mut self, wallet: &mut Wallet) {
|
2024-04-20 16:59:54 +03:00
|
|
|
self.message_error = None;
|
2024-04-18 16:55:37 +03:00
|
|
|
if self.message_edit.is_empty() {
|
|
|
|
return;
|
|
|
|
}
|
2024-05-15 21:26:15 +03:00
|
|
|
// Trim message.
|
|
|
|
self.message_edit = self.message_edit.trim().to_string();
|
2024-04-20 23:49:19 +03:00
|
|
|
|
2024-05-15 21:26:15 +03:00
|
|
|
// Parse message.
|
|
|
|
if let Ok(mut slate) = wallet.parse_slatepack(&self.message_edit) {
|
2024-04-20 23:49:19 +03:00
|
|
|
// Try to setup empty amount from transaction by id.
|
|
|
|
if slate.amount == 0 {
|
|
|
|
let _ = wallet.get_data().unwrap().txs.clone().iter().map(|tx| {
|
|
|
|
if tx.data.tx_slate_id == Some(slate.id) {
|
|
|
|
if slate.amount == 0 {
|
|
|
|
slate.amount = tx.amount;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
tx
|
|
|
|
}).collect::<Vec<&WalletTransaction>>();
|
|
|
|
}
|
|
|
|
|
|
|
|
if slate.amount == 0 {
|
|
|
|
self.message_error = Some(
|
|
|
|
MessageError::Response(t!("wallets.resp_slatepack_err"))
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-04-18 16:55:37 +03:00
|
|
|
// Make operation based on incoming state status.
|
|
|
|
match slate.state {
|
2024-04-20 23:49:19 +03:00
|
|
|
SlateState::Standard1 | SlateState::Invoice1 => {
|
|
|
|
let resp = if slate.state == SlateState::Standard1 {
|
2024-04-24 01:42:56 +03:00
|
|
|
wallet.receive(&self.message_edit)
|
2024-04-20 23:49:19 +03:00
|
|
|
} else {
|
2024-04-24 01:42:56 +03:00
|
|
|
wallet.pay(&self.message_edit)
|
2024-04-20 23:49:19 +03:00
|
|
|
};
|
|
|
|
if resp.is_ok() {
|
|
|
|
self.response_edit = resp.unwrap();
|
2024-04-18 16:55:37 +03:00
|
|
|
} else {
|
2024-04-24 01:42:56 +03:00
|
|
|
match resp.err().unwrap() {
|
|
|
|
grin_wallet_libwallet::Error::TransactionWasCancelled {..} => {
|
|
|
|
// Set already canceled transaction error message.
|
|
|
|
self.message_error = Some(
|
|
|
|
MessageError::Response(t!("wallets.resp_canceled_err"))
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
2024-04-18 16:55:37 +03:00
|
|
|
// Check if tx with same slate id already exists.
|
2024-04-20 16:59:54 +03:00
|
|
|
let exists_tx = wallet.tx_by_slate(&slate).is_some();
|
2024-04-18 16:55:37 +03:00
|
|
|
if exists_tx {
|
2024-04-20 23:49:19 +03:00
|
|
|
let mut sl = slate.clone();
|
|
|
|
sl.state = if sl.state == SlateState::Standard1 {
|
|
|
|
SlateState::Standard2
|
|
|
|
} else {
|
|
|
|
SlateState::Invoice2
|
|
|
|
};
|
|
|
|
match wallet.read_slatepack(&sl) {
|
|
|
|
None => {
|
|
|
|
self.message_error = Some(
|
|
|
|
MessageError::Response(t!("wallets.resp_slatepack_err"))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
Some(sp) => {
|
|
|
|
self.message_slate = Some(slate);
|
|
|
|
self.response_edit = sp;
|
|
|
|
}
|
|
|
|
}
|
2024-04-18 16:55:37 +03:00
|
|
|
return;
|
2024-04-18 05:20:49 +03:00
|
|
|
}
|
2024-04-18 16:55:37 +03:00
|
|
|
|
|
|
|
// Set default response error message.
|
|
|
|
self.message_error = Some(
|
2024-04-20 23:49:19 +03:00
|
|
|
MessageError::Response(t!("wallets.resp_slatepack_err"))
|
2024-04-18 16:55:37 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2024-04-20 23:49:19 +03:00
|
|
|
SlateState::Standard2 | SlateState::Invoice2 => {
|
|
|
|
// Check if slatepack with same id and state already exists.
|
|
|
|
let mut sl = slate.clone();
|
|
|
|
sl.state = if sl.state == SlateState::Standard2 {
|
|
|
|
SlateState::Standard1
|
|
|
|
} else {
|
|
|
|
SlateState::Invoice1
|
|
|
|
};
|
|
|
|
match wallet.read_slatepack(&sl) {
|
|
|
|
None => {
|
|
|
|
match wallet.read_slatepack(&slate) {
|
|
|
|
None => {
|
2024-04-18 16:55:37 +03:00
|
|
|
self.message_error = Some(
|
2024-04-20 23:49:19 +03:00
|
|
|
MessageError::Response(t!("wallets.resp_slatepack_err"))
|
2024-04-18 16:55:37 +03:00
|
|
|
);
|
2024-04-18 05:20:49 +03:00
|
|
|
}
|
2024-04-20 23:49:19 +03:00
|
|
|
Some(sp) => {
|
|
|
|
self.message_slate = Some(sl);
|
|
|
|
self.response_edit = sp;
|
|
|
|
return;
|
|
|
|
}
|
2024-04-18 05:20:49 +03:00
|
|
|
}
|
|
|
|
}
|
2024-04-20 23:49:19 +03:00
|
|
|
Some(_) => {
|
|
|
|
self.message_slate = Some(slate.clone());
|
|
|
|
return;
|
|
|
|
}
|
2024-04-18 05:20:49 +03:00
|
|
|
}
|
|
|
|
}
|
2024-04-20 16:59:54 +03:00
|
|
|
_ => {
|
|
|
|
self.response_edit = "".to_string();
|
|
|
|
}
|
2024-04-18 16:55:37 +03:00
|
|
|
}
|
2024-04-20 23:49:19 +03:00
|
|
|
self.message_slate = Some(slate);
|
2024-04-18 16:55:37 +03:00
|
|
|
} else {
|
|
|
|
self.message_slate = None;
|
2024-04-20 23:49:19 +03:00
|
|
|
self.message_error = Some(MessageError::Parse(t!("wallets.resp_slatepack_err")));
|
2024-04-18 05:20:49 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-30 18:15:03 +03:00
|
|
|
/// Draw amount input [`Modal`] content to create invoice or request to send funds.
|
2024-04-18 05:20:49 +03:00
|
|
|
fn amount_modal_ui(&mut self,
|
|
|
|
ui: &mut egui::Ui,
|
|
|
|
wallet: &mut Wallet,
|
|
|
|
modal: &Modal,
|
|
|
|
cb: &dyn PlatformCallbacks) {
|
2024-04-16 15:24:22 +03:00
|
|
|
ui.add_space(6.0);
|
|
|
|
if self.request_edit.is_empty() {
|
|
|
|
ui.vertical_centered(|ui| {
|
2024-04-18 05:20:49 +03:00
|
|
|
let enter_text = if self.send_request {
|
|
|
|
let data = wallet.get_data().unwrap();
|
|
|
|
let amount = amount_to_hr_string(data.info.amount_currently_spendable, true);
|
|
|
|
t!("wallets.enter_amount_send","amount" => amount)
|
|
|
|
} else {
|
|
|
|
t!("wallets.enter_amount_receive")
|
|
|
|
};
|
|
|
|
ui.label(RichText::new(enter_text)
|
2024-04-16 15:24:22 +03:00
|
|
|
.size(17.0)
|
|
|
|
.color(Colors::GRAY));
|
|
|
|
});
|
2023-08-21 08:30:50 +03:00
|
|
|
ui.add_space(8.0);
|
2024-04-16 15:24:22 +03:00
|
|
|
|
|
|
|
// Draw invoice amount text edit.
|
|
|
|
let amount_edit_id = Id::from(modal.id).with(wallet.get_config().id);
|
|
|
|
let amount_edit_opts = TextEditOptions::new(amount_edit_id).h_center();
|
2024-04-20 16:59:54 +03:00
|
|
|
let amount_edit_before = self.amount_edit.clone();
|
|
|
|
View::text_edit(ui, cb, &mut self.amount_edit, amount_edit_opts);
|
2024-04-18 16:55:37 +03:00
|
|
|
|
|
|
|
// Check value if input was changed.
|
|
|
|
if amount_edit_before != self.amount_edit {
|
|
|
|
self.request_error = None;
|
2024-04-20 16:59:54 +03:00
|
|
|
if !self.amount_edit.is_empty() {
|
|
|
|
match amount_from_hr_string(self.amount_edit.as_str()) {
|
|
|
|
Ok(a) => {
|
|
|
|
if !self.amount_edit.contains(".") {
|
|
|
|
// To avoid input of several "0".
|
|
|
|
if a == 0 {
|
|
|
|
self.amount_edit = "0".to_string();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Check input after ".".
|
|
|
|
let parts = self.amount_edit.split(".").collect::<Vec<&str>>();
|
|
|
|
if parts.len() == 2 && parts[1].len() > 9 {
|
|
|
|
self.amount_edit = amount_edit_before;
|
|
|
|
return;
|
|
|
|
}
|
2024-04-18 16:55:37 +03:00
|
|
|
}
|
2024-04-20 16:59:54 +03:00
|
|
|
|
|
|
|
// Do not input amount more than balance in sending.
|
|
|
|
if self.send_request {
|
|
|
|
let b = wallet.get_data().unwrap().info.amount_currently_spendable;
|
|
|
|
if b < a {
|
|
|
|
self.amount_edit = amount_edit_before;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(_) => {
|
|
|
|
self.amount_edit = amount_edit_before;
|
2024-04-18 16:55:37 +03:00
|
|
|
}
|
2024-04-16 15:24:22 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-30 18:15:03 +03:00
|
|
|
// Show request creation error.
|
2024-04-18 16:55:37 +03:00
|
|
|
if self.request_error.is_some() {
|
2024-04-16 15:24:22 +03:00
|
|
|
ui.add_space(12.0);
|
2024-04-18 16:55:37 +03:00
|
|
|
ui.vertical_centered(|ui| {
|
|
|
|
ui.label(RichText::new(self.request_error.clone().unwrap().text())
|
|
|
|
.size(17.0)
|
|
|
|
.color(Colors::RED));
|
|
|
|
});
|
2024-04-16 15:24:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
ui.add_space(12.0);
|
2024-04-21 23:43:00 +03:00
|
|
|
|
|
|
|
// 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!("modal.cancel"), Colors::WHITE, || {
|
|
|
|
self.amount_edit = "".to_string();
|
|
|
|
self.request_error = None;
|
|
|
|
cb.hide_keyboard();
|
|
|
|
modal.close();
|
2024-04-16 15:24:22 +03:00
|
|
|
});
|
2024-04-21 23:43:00 +03:00
|
|
|
});
|
|
|
|
columns[1].vertical_centered_justified(|ui| {
|
|
|
|
// Button to create Slatepack message for request.
|
|
|
|
View::button(ui, t!("continue"), Colors::WHITE, || {
|
2024-04-22 21:55:34 +03:00
|
|
|
if self.amount_edit.is_empty() {
|
|
|
|
return;
|
|
|
|
}
|
2024-04-21 23:43:00 +03:00
|
|
|
if let Ok(a) = amount_from_hr_string(self.amount_edit.as_str()) {
|
|
|
|
let message = if self.send_request {
|
|
|
|
wallet.send(a)
|
|
|
|
} else {
|
|
|
|
wallet.issue_invoice(a)
|
|
|
|
};
|
|
|
|
match message {
|
2024-04-30 18:15:03 +03:00
|
|
|
Ok((_, message)) => {
|
2024-04-21 23:43:00 +03:00
|
|
|
self.request_edit = message;
|
|
|
|
cb.hide_keyboard();
|
|
|
|
}
|
|
|
|
Err(err) => {
|
|
|
|
match err {
|
|
|
|
grin_wallet_libwallet::Error::NotEnoughFunds { .. } => {
|
|
|
|
let m = t!(
|
2024-04-18 16:55:37 +03:00
|
|
|
"wallets.pay_balance_error",
|
|
|
|
"amount" => self.amount_edit
|
|
|
|
);
|
2024-04-21 23:43:00 +03:00
|
|
|
self.request_error = Some(MessageError::Other(m));
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
let m = t!("wallets.invoice_slatepack_err");
|
|
|
|
self.request_error = Some(MessageError::Other(m));
|
2024-04-16 15:24:22 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-04-21 23:43:00 +03:00
|
|
|
} else {
|
|
|
|
self.request_error = Some(
|
|
|
|
MessageError::Other(t!("wallets.invoice_slatepack_err"))
|
|
|
|
);
|
|
|
|
}
|
2024-04-16 15:24:22 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
ui.add_space(6.0);
|
|
|
|
} else {
|
|
|
|
ui.vertical_centered(|ui| {
|
|
|
|
let amount = amount_from_hr_string(self.amount_edit.as_str()).unwrap();
|
|
|
|
let amount_format = amount_to_hr_string(amount, true);
|
2024-04-18 05:20:49 +03:00
|
|
|
let desc_text = if self.send_request {
|
|
|
|
t!("wallets.send_request_desc","amount" => amount_format)
|
|
|
|
} else {
|
|
|
|
t!("wallets.invoice_desc","amount" => amount_format)
|
|
|
|
};
|
2024-04-21 19:59:12 +03:00
|
|
|
ui.label(RichText::new(desc_text).size(16.0).color(Colors::GRAY));
|
2024-04-16 15:24:22 +03:00
|
|
|
ui.add_space(6.0);
|
|
|
|
View::horizontal_line(ui, Colors::ITEM_STROKE);
|
|
|
|
ui.add_space(3.0);
|
|
|
|
|
2024-04-18 05:20:49 +03:00
|
|
|
// Draw output Slatepack message text.
|
|
|
|
let input_id = if self.send_request {
|
|
|
|
Id::from("send_request_output").with(wallet.get_config().id)
|
|
|
|
} else {
|
|
|
|
Id::from("receive_request_output").with(wallet.get_config().id)
|
|
|
|
};
|
2024-04-16 15:24:22 +03:00
|
|
|
ScrollArea::vertical()
|
|
|
|
.max_height(128.0)
|
2024-04-18 05:20:49 +03:00
|
|
|
.id_source(input_id)
|
2024-04-16 15:24:22 +03:00
|
|
|
.auto_shrink([false; 2])
|
|
|
|
.show(ui, |ui| {
|
|
|
|
ui.add_space(7.0);
|
|
|
|
egui::TextEdit::multiline(&mut self.request_edit)
|
|
|
|
.font(egui::TextStyle::Small)
|
|
|
|
.desired_rows(5)
|
|
|
|
.interactive(false)
|
|
|
|
.hint_text(SLATEPACK_MESSAGE_HINT)
|
|
|
|
.desired_width(f32::INFINITY)
|
|
|
|
.show(ui);
|
|
|
|
ui.add_space(6.0);
|
|
|
|
});
|
|
|
|
ui.add_space(2.0);
|
|
|
|
View::horizontal_line(ui, Colors::ITEM_STROKE);
|
2024-04-21 19:59:12 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
ui.add_space(12.0);
|
2024-04-21 23:43:00 +03:00
|
|
|
|
2024-04-21 19:59:12 +03:00
|
|
|
// Setup spacing between buttons.
|
|
|
|
ui.spacing_mut().item_spacing = egui::Vec2::new(6.0, 0.0);
|
2024-04-16 15:24:22 +03:00
|
|
|
|
2024-04-21 19:59:12 +03:00
|
|
|
ui.columns(2, |columns| {
|
|
|
|
columns[0].vertical_centered_justified(|ui| {
|
|
|
|
// Button to cancel transaction.
|
2024-04-21 23:43:00 +03:00
|
|
|
let cancel = format!("{} {}", PROHIBIT, t!("modal.cancel"));
|
|
|
|
View::colored_text_button(ui, cancel, Colors::RED, Colors::BUTTON, || {
|
2024-04-24 01:42:56 +03:00
|
|
|
if let Ok(slate) = wallet.parse_slatepack(&self.request_edit) {
|
2024-04-21 19:59:12 +03:00
|
|
|
if let Some(tx) = wallet.tx_by_slate(&slate) {
|
|
|
|
wallet.cancel(tx.data.id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
self.amount_edit = "".to_string();
|
|
|
|
self.request_edit = "".to_string();
|
2024-04-21 20:42:01 +03:00
|
|
|
cb.hide_keyboard();
|
2024-04-21 19:59:12 +03:00
|
|
|
modal.close();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
columns[1].vertical_centered_justified(|ui| {
|
|
|
|
// Draw copy button.
|
|
|
|
let copy_text = format!("{} {}", COPY, t!("copy"));
|
|
|
|
View::button(ui, copy_text, Colors::BUTTON, || {
|
|
|
|
cb.copy_string_to_buffer(self.request_edit.clone());
|
|
|
|
self.amount_edit = "".to_string();
|
|
|
|
self.request_edit = "".to_string();
|
|
|
|
modal.close();
|
|
|
|
});
|
2024-04-16 15:24:22 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// Draw button to close modal.
|
|
|
|
ui.add_space(12.0);
|
|
|
|
ui.vertical_centered_justified(|ui| {
|
|
|
|
View::button(ui, t!("close"), Colors::WHITE, || {
|
|
|
|
self.amount_edit = "".to_string();
|
|
|
|
self.request_edit = "".to_string();
|
|
|
|
modal.close();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
ui.add_space(6.0);
|
2023-08-21 08:30:50 +03:00
|
|
|
}
|
2023-08-03 23:49:11 +03:00
|
|
|
}
|
|
|
|
}
|