accounts: fix list update after creation, creation inside list

This commit is contained in:
ardocrat 2024-04-22 04:20:37 +03:00
parent 5393e140d7
commit 53f86edb34
4 changed files with 120 additions and 102 deletions

View file

@ -49,12 +49,12 @@ wallets:
closing: Closing closing: Closing
checking: Checking checking: Checking
default_wallet: Default wallet default_wallet: Default wallet
new_account_desc: 'Enter name of new account:'
wallet_loading: Loading wallet wallet_loading: Loading wallet
wallet_closing: Closing wallet wallet_closing: Closing wallet
wallet_checking: Checking wallet wallet_checking: Checking wallet
tx_loading: Loading transactions tx_loading: Loading transactions
default_account: Default account default_account: Default account
create_account: Create account
accounts: Accounts accounts: Accounts
tx_sent: Sent tx_sent: Sent
tx_received: Received tx_received: Received

View file

@ -49,12 +49,12 @@ wallets:
closing: Закрывается closing: Закрывается
checking: Проверяется checking: Проверяется
default_wallet: Стандартный кошелёк default_wallet: Стандартный кошелёк
new_account_desc: 'Введите название нового аккаунта:'
wallet_loading: Загрузка кошелька wallet_loading: Загрузка кошелька
wallet_closing: Закрытие кошелька wallet_closing: Закрытие кошелька
wallet_checking: Проверка кошелька wallet_checking: Проверка кошелька
tx_loading: Загрузка транзакций tx_loading: Загрузка транзакций
default_account: Стандартный аккаунт default_account: Стандартный аккаунт
create_account: Создать аккаунт
accounts: Аккаунты accounts: Аккаунты
tx_sent: Отправлено tx_sent: Отправлено
tx_received: Получено tx_received: Получено

View file

@ -13,14 +13,13 @@
// limitations under the License. // limitations under the License.
use std::time::Duration; use std::time::Duration;
use egui::{Align, Id, Layout, Margin, RichText, ScrollArea, Widget};
use egui::{Align, Id, Layout, Margin, RichText, Rounding, ScrollArea, Widget};
use grin_chain::SyncStatus; use grin_chain::SyncStatus;
use grin_core::core::amount_to_hr_string; use grin_core::core::amount_to_hr_string;
use crate::AppConfig; use crate::AppConfig;
use crate::gui::Colors; use crate::gui::Colors;
use crate::gui::icons::{BRIDGE, CHAT_CIRCLE_TEXT, CHECK, CHECK_FAT, FILE_ARCHIVE, GEAR_FINE, LIST, PACKAGE, PLUS, POWER, REPEAT, WALLET}; use crate::gui::icons::{BRIDGE, CHECK, CHECK_FAT, ENVELOPE_OPEN, FILES, FOLDER_USER, GEAR_FINE, HASH, PACKAGE, POWER, QR_CODE, REPEAT, USERS_THREE};
use crate::gui::platform::PlatformCallbacks; use crate::gui::platform::PlatformCallbacks;
use crate::gui::views::{Modal, Root, View}; use crate::gui::views::{Modal, Root, View};
use crate::gui::views::types::{ModalPosition, TextEditOptions}; use crate::gui::views::types::{ModalPosition, TextEditOptions};
@ -35,6 +34,8 @@ pub struct WalletContent {
/// List of wallet accounts for [`Modal`]. /// List of wallet accounts for [`Modal`].
accounts: Vec<WalletAccount>, accounts: Vec<WalletAccount>,
/// Flag to check if account is creating.
account_creating: bool,
/// Account label [`Modal`] value. /// Account label [`Modal`] value.
account_label_edit: String, account_label_edit: String,
/// Flag to check if error occurred during account creation at [`Modal`]. /// Flag to check if error occurred during account creation at [`Modal`].
@ -48,6 +49,7 @@ impl Default for WalletContent {
fn default() -> Self { fn default() -> Self {
Self { Self {
accounts: vec![], accounts: vec![],
account_creating: false,
account_label_edit: "".to_string(), account_label_edit: "".to_string(),
account_creation_error: false, account_creation_error: false,
current_tab: Box::new(WalletInfo::default()) current_tab: Box::new(WalletInfo::default())
@ -55,8 +57,6 @@ impl Default for WalletContent {
} }
} }
/// Identifier for account creation [`Modal`].
const CREATE_ACCOUNT_MODAL: &'static str = "create_account_modal";
/// Identifier for account list [`Modal`]. /// Identifier for account list [`Modal`].
const ACCOUNT_LIST_MODAL: &'static str = "account_list_modal"; const ACCOUNT_LIST_MODAL: &'static str = "account_list_modal";
@ -94,8 +94,8 @@ impl WalletContent {
}) })
.show_animated_inside(ui, show_balance, |ui| { .show_animated_inside(ui, show_balance, |ui| {
ui.vertical_centered(|ui| { ui.vertical_centered(|ui| {
// Draw wallet tabs. // Draw account info.
View::max_width_ui(ui, Root::SIDE_PANEL_WIDTH * 1.35, |ui| { View::max_width_ui(ui, Root::SIDE_PANEL_WIDTH * 1.25, |ui| {
self.account_ui(ui, wallet, data.unwrap(), cb); self.account_ui(ui, wallet, data.unwrap(), cb);
}); });
}); });
@ -151,14 +151,9 @@ impl WalletContent {
None => {} None => {}
Some(id) => { Some(id) => {
match id { match id {
CREATE_ACCOUNT_MODAL => {
Modal::ui(ui.ctx(), |ui, modal| {
self.create_account_modal_ui(ui, wallet, modal, cb);
});
},
ACCOUNT_LIST_MODAL => { ACCOUNT_LIST_MODAL => {
Modal::ui(ui.ctx(), |ui, modal| { Modal::ui(ui.ctx(), |ui, modal| {
self.account_list_modal_ui(ui, wallet, modal); self.account_list_modal_ui(ui, wallet, modal, cb);
}); });
} }
_ => {} _ => {}
@ -177,36 +172,33 @@ impl WalletContent {
rect.set_height(75.0); rect.set_height(75.0);
// Draw round background. // Draw round background.
let rounding = View::item_rounding(0, 2, false); let rounding = View::item_rounding(0, 2, false);
ui.painter().rect(rect, rounding, Colors::BUTTON, View::ITEM_STROKE); ui.painter().rect(rect, rounding, Colors::BUTTON, View::HOVER_STROKE);
ui.allocate_ui_with_layout(rect.size(), Layout::right_to_left(Align::Center), |ui| { ui.allocate_ui_with_layout(rect.size(), Layout::right_to_left(Align::Center), |ui| {
// Setup padding for item buttons. // Setup padding for item buttons.
ui.style_mut().spacing.button_padding = egui::vec2(14.0, 0.0); ui.style_mut().spacing.button_padding = egui::vec2(14.0, 0.0);
// Draw button to add new account. // Draw button to scan QR code.
View::item_button(ui, View::item_rounding(0, 2, true), PLUS, None, || { View::item_button(ui, View::item_rounding(0, 2, true), QR_CODE, None, || {
// Show account creation modal. //TODO: Scan with QR code.
Modal::new(CREATE_ACCOUNT_MODAL)
.position(ModalPosition::CenterTop)
.title(t!("wallets.create_account"))
.show();
cb.show_keyboard();
}); });
// Draw button to show list of accounts. // Draw button to show list of accounts.
View::item_button(ui, Rounding::ZERO, LIST, None, || { View::item_button(ui, View::item_rounding(1, 3, true), USERS_THREE, None, || {
// Load accounts. // Load accounts.
self.account_label_edit = "".to_string();
self.accounts = wallet.accounts(); self.accounts = wallet.accounts();
self.account_creating = false;
// Show account list modal. // Show account list modal.
Modal::new(ACCOUNT_LIST_MODAL) Modal::new(ACCOUNT_LIST_MODAL)
.position(ModalPosition::Center) .position(ModalPosition::CenterTop)
.title(t!("wallets.accounts")) .title(t!("wallets.accounts"))
.show(); .show();
}); });
let layout_size = ui.available_size(); let layout_size = ui.available_size();
ui.allocate_ui_with_layout(layout_size, Layout::left_to_right(Align::Center), |ui| { ui.allocate_ui_with_layout(layout_size, Layout::left_to_right(Align::Center), |ui| {
ui.add_space(6.0); ui.add_space(8.0);
ui.vertical(|ui| { ui.vertical(|ui| {
ui.add_space(3.0); ui.add_space(3.0);
// Show spendable amount. // Show spendable amount.
@ -223,7 +215,7 @@ impl WalletContent {
} else { } else {
account.to_owned() account.to_owned()
}; };
let acc_text = format!("{} {}", FILE_ARCHIVE, acc_label); let acc_text = format!("{} {}", FOLDER_USER, acc_label);
View::ellipsize_text(ui, acc_text, 15.0, Colors::TEXT); View::ellipsize_text(ui, acc_text, 15.0, Colors::TEXT);
// Show confirmed height. // Show confirmed height.
@ -235,75 +227,38 @@ impl WalletContent {
} }
/// Draw account list [`Modal`] content. /// Draw account list [`Modal`] content.
fn account_list_modal_ui(&mut self, ui: &mut egui::Ui, wallet: &mut Wallet, modal: &Modal) { fn account_list_modal_ui(&mut self,
ui.add_space(3.0); ui: &mut egui::Ui,
wallet: &mut Wallet,
// Show list of accounts. modal: &Modal,
let size = self.accounts.len(); cb: &dyn PlatformCallbacks) {
ScrollArea::vertical() if self.account_creating {
.max_height(300.0)
.id_source("account_list_modal_scroll")
.auto_shrink([true; 2])
.show_rows(ui, ACCOUNT_ITEM_HEIGHT, size, |ui, row_range| {
for index in row_range {
// Add space before the first item.
if index == 0 {
ui.add_space(4.0);
}
let acc = self.accounts.get(index).unwrap();
account_item_ui(ui, modal, wallet, acc, index, size);
if index == size - 1 {
ui.add_space(4.0);
}
}
});
ui.add_space(2.0);
View::horizontal_line(ui, Colors::STROKE);
ui.add_space(6.0);
ui.vertical_centered_justified(|ui| {
View::button(ui, t!("close"), Colors::WHITE, || {
// Close modal.
modal.close();
});
ui.add_space(6.0); ui.add_space(6.0);
}); ui.vertical_centered(|ui| {
} ui.label(RichText::new(t!("wallets.new_account_desc"))
/// Draw account creation [`Modal`] content.
fn create_account_modal_ui(&mut self,
ui: &mut egui::Ui,
wallet: &mut Wallet,
modal: &Modal,
cb: &dyn PlatformCallbacks) {
ui.add_space(6.0);
ui.vertical_centered(|ui| {
ui.label(RichText::new(t!("wallets.name"))
.size(17.0)
.color(Colors::GRAY));
ui.add_space(8.0);
// Draw account name edit.
let text_edit_id = Id::from(modal.id).with(wallet.get_config().id);
let text_edit_opts = TextEditOptions::new(text_edit_id);
View::text_edit(ui, cb, &mut self.account_label_edit, text_edit_opts);
// Show error occurred during account creation..
if self.account_creation_error {
ui.add_space(12.0);
ui.label(RichText::new(t!("error"))
.size(17.0) .size(17.0)
.color(Colors::RED)); .color(Colors::GRAY));
} ui.add_space(8.0);
ui.add_space(12.0);
}); // Draw account name edit.
let text_edit_id = Id::from(modal.id).with(wallet.get_config().id);
let text_edit_opts = TextEditOptions::new(text_edit_id);
View::text_edit(ui, cb, &mut self.account_label_edit, text_edit_opts);
// Show error occurred during account creation..
if self.account_creation_error {
ui.add_space(12.0);
ui.label(RichText::new(t!("error"))
.size(17.0)
.color(Colors::RED));
}
ui.add_space(12.0);
});
// Show modal buttons.
ui.scope(|ui| {
// Setup spacing between buttons. // Setup spacing between buttons.
ui.spacing_mut().item_spacing = egui::Vec2::new(8.0, 0.0); ui.spacing_mut().item_spacing = egui::Vec2::new(6.0, 0.0);
// Show modal buttons.
ui.columns(2, |columns| { ui.columns(2, |columns| {
columns[0].vertical_centered_justified(|ui| { columns[0].vertical_centered_justified(|ui| {
View::button(ui, t!("modal.cancel"), Colors::WHITE, || { View::button(ui, t!("modal.cancel"), Colors::WHITE, || {
@ -336,7 +291,52 @@ impl WalletContent {
}); });
}); });
ui.add_space(6.0); ui.add_space(6.0);
}); } else {
ui.add_space(3.0);
// Show list of accounts.
let size = self.accounts.len();
ScrollArea::vertical()
.max_height(280.0)
.id_source("account_list_modal_scroll")
.auto_shrink([true; 2])
.show_rows(ui, ACCOUNT_ITEM_HEIGHT, size, |ui, row_range| {
for index in row_range {
// Add space before the first item.
if index == 0 {
ui.add_space(4.0);
}
let acc = self.accounts.get(index).unwrap();
account_item_ui(ui, modal, wallet, acc, index, size);
if index == size - 1 {
ui.add_space(4.0);
}
}
});
ui.add_space(2.0);
View::horizontal_line(ui, Colors::STROKE);
ui.add_space(6.0);
// Setup spacing between buttons.
ui.spacing_mut().item_spacing = egui::Vec2::new(6.0, 0.0);
// Show modal buttons.
ui.columns(2, |columns| {
columns[0].vertical_centered_justified(|ui| {
View::button(ui, t!("modal.cancel"), Colors::WHITE, || {
modal.close();
});
});
columns[1].vertical_centered_justified(|ui| {
View::button(ui, t!("create"), Colors::WHITE, || {
self.account_creating = true;
cb.show_keyboard();
});
});
});
ui.add_space(6.0);
}
} }
/// Draw tab buttons in the bottom of the screen. /// Draw tab buttons in the bottom of the screen.
@ -351,13 +351,13 @@ impl WalletContent {
let current_type = self.current_tab.get_type(); let current_type = self.current_tab.get_type();
ui.columns(4, |columns| { ui.columns(4, |columns| {
columns[0].vertical_centered_justified(|ui| { columns[0].vertical_centered_justified(|ui| {
View::tab_button(ui, WALLET, current_type == WalletTabType::Txs, || { View::tab_button(ui, FILES, current_type == WalletTabType::Txs, || {
self.current_tab = Box::new(WalletInfo::default()); self.current_tab = Box::new(WalletInfo::default());
}); });
}); });
columns[1].vertical_centered_justified(|ui| { columns[1].vertical_centered_justified(|ui| {
let is_messages = current_type == WalletTabType::Messages; let is_messages = current_type == WalletTabType::Messages;
View::tab_button(ui, CHAT_CIRCLE_TEXT, is_messages, || { View::tab_button(ui, ENVELOPE_OPEN, is_messages, || {
self.current_tab = Box::new( self.current_tab = Box::new(
WalletMessages::new(wallet.can_use_dandelion()) WalletMessages::new(wallet.can_use_dandelion())
); );
@ -543,10 +543,12 @@ fn account_item_ui(ui: &mut egui::Ui,
} else { } else {
acc.label.to_owned() acc.label.to_owned()
}; };
View::ellipsize_text(ui, acc_label, 15.0, Colors::TEXT); let acc_name = format!("{} {}", FOLDER_USER, acc_label);
View::ellipsize_text(ui, acc_name, 15.0, Colors::TEXT);
// Show account BIP32 derivation path. // Show account BIP32 derivation path.
ui.label(RichText::new(acc.path.to_owned()).size(15.0).color(Colors::GRAY)); let acc_path = format!("{} {}", HASH, acc.path);
ui.label(RichText::new(acc_path).size(15.0).color(Colors::GRAY));
ui.add_space(3.0); ui.add_space(3.0);
}); });
}); });

View file

@ -371,6 +371,15 @@ impl Wallet {
controller::owner_single_use(None, None, Some(&mut api), |api, m| { controller::owner_single_use(None, None, Some(&mut api), |api, m| {
api.create_account_path(m, label)?; api.create_account_path(m, label)?;
// Update account list at separate thread.
if let Some(data) = self.get_data() {
let last_height = data.info.last_confirmed_height;
let wallet = self.clone();
thread::spawn(move || {
update_accounts(&wallet, last_height, None);
});
}
// Sync wallet data. // Sync wallet data.
self.sync(); self.sync();
Ok(()) Ok(())
@ -921,7 +930,12 @@ fn sync_wallet_data(wallet: &Wallet) {
if wallet.info_sync_progress() == 100 { if wallet.info_sync_progress() == 100 {
// Retrieve accounts data. // Retrieve accounts data.
let last_height = info.1.last_confirmed_height; let last_height = info.1.last_confirmed_height;
update_accounts(wallet, last_height, info.1.amount_currently_spendable); let spendable = if wallet.get_data().is_none() {
None
} else {
Some(info.1.amount_currently_spendable)
};
update_accounts(wallet, last_height, spendable);
// Update txs sync progress at separate thread. // Update txs sync progress at separate thread.
let wallet_txs = wallet.clone(); let wallet_txs = wallet.clone();
@ -1076,13 +1090,13 @@ fn sync_wallet_data(wallet: &Wallet) {
} }
/// Update wallet accounts data. /// Update wallet accounts data.
fn update_accounts(wallet: &Wallet, current_height: u64, current_spendable: u64) { fn update_accounts(wallet: &Wallet, current_height: u64, current_spendable: Option<u64>) {
// Update only current account if list is not empty. // Update only current account if list is not empty.
if !wallet.accounts.read().unwrap().is_empty() { if current_spendable.is_some() {
let mut accounts = wallet.accounts.read().unwrap().clone(); let mut accounts = wallet.accounts.read().unwrap().clone();
for mut a in accounts.iter_mut() { for mut a in accounts.iter_mut() {
if a.label == wallet.get_config().account { if a.label == wallet.get_config().account {
a.spendable_amount = current_spendable; a.spendable_amount = current_spendable.unwrap();
} }
} }
// Save accounts data. // Save accounts data.
@ -1113,8 +1127,10 @@ fn update_accounts(wallet: &Wallet, current_height: u64, current_spendable: u64)
spendable_amount, spendable_amount,
label: a.label, label: a.label,
path: a.path.to_bip_32_string(), path: a.path.to_bip_32_string(),
}) });
} }
// Sort in reverse.
accounts.reverse();
// Save accounts data. // Save accounts data.
let mut w_data = wallet.accounts.write().unwrap(); let mut w_data = wallet.accounts.write().unwrap();