Skip to content

Commit

Permalink
[*] improve ui
Browse files Browse the repository at this point in the history
  • Loading branch information
heng30 committed Jul 15, 2024
1 parent 548b331 commit 43cc55a
Show file tree
Hide file tree
Showing 21 changed files with 366 additions and 21 deletions.
5 changes: 5 additions & 0 deletions src/config/conf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ pub fn developer_mode() -> data::DeveloperMode {
CONFIG.lock().unwrap().developer_mode.clone()
}

pub fn security_privacy() -> data::SecurityPrivacy {
CONFIG.lock().unwrap().security_privacy.clone()
}

pub fn db_path() -> PathBuf {
CONFIG.lock().unwrap().db_path.clone()
}
Expand Down Expand Up @@ -125,6 +129,7 @@ impl Config {
self.appid = c.appid;
self.ui = c.ui;
self.developer_mode = c.developer_mode;
self.security_privacy = c.security_privacy;
Ok(())
}
Err(_) => {
Expand Down
21 changes: 17 additions & 4 deletions src/config/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,7 @@ pub struct Config {

pub ui: UI,
pub developer_mode: DeveloperMode,
}

pub fn appid_default() -> String {
Uuid::new_v4().to_string()
pub security_privacy: SecurityPrivacy,
}

#[derive(Serialize, Deserialize, Debug, Clone, Derivative)]
Expand All @@ -46,3 +43,19 @@ pub struct DeveloperMode {
#[derivative(Default(value = "\"test\".to_string()"))]
pub network: String,
}

#[derive(Serialize, Deserialize, Debug, Clone, Derivative)]
#[derivative(Default)]
pub struct SecurityPrivacy {
#[serde(default = "prioritization_fee_default")]
#[derivative(Default(value = "1000"))]
pub max_prioritization_fee: u64,
}

pub fn appid_default() -> String {
Uuid::new_v4().to_string()
}

pub fn prioritization_fee_default() -> u64 {
1000
}
2 changes: 1 addition & 1 deletion src/config/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
mod conf;
mod data;

pub use conf::{all, cache_dir, db_path, developer_mode, init, is_first_run, save, ui};
pub use conf::{all, cache_dir, db_path, security_privacy, developer_mode, init, is_first_run, save, ui};
5 changes: 5 additions & 0 deletions src/logic/accounts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,11 @@ pub fn init(ui: &AppWindow) {
}
});

let ui_handle = ui.as_weak();
ui.global::<Logic>().on_update_home_page(move || {
super::tokens::init_tokens(&ui_handle.unwrap());
});

let ui_handle = ui.as_weak();
ui.global::<Logic>().on_show_mnemonic(move |password| {
let ui_handle = ui_handle.clone();
Expand Down
21 changes: 20 additions & 1 deletion src/logic/setting.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use super::tr::tr;
use crate::{
config,
slint_generatedAppWindow::SettingDeveloperMode,
slint_generatedAppWindow::{AppWindow, Logic, Store, Theme},
slint_generatedAppWindow::{SettingDeveloperMode, SettingSecurityPrivacy},
};
use slint::ComponentHandle;

Expand Down Expand Up @@ -67,6 +67,25 @@ pub fn init(ui: &AppWindow) {
all.developer_mode.network = setting.network.into();
_ = config::save(all);
});

ui.global::<Logic>()
.on_get_setting_security_privacy(move || {
let setting = config::security_privacy();

SettingSecurityPrivacy {
max_prioritization_fee: slint::format!("{}", setting.max_prioritization_fee),
}
});

ui.global::<Logic>()
.on_set_setting_security_privacy(move |setting| {
let mut all = config::all();
all.security_privacy.max_prioritization_fee = setting
.max_prioritization_fee
.parse::<u64>()
.unwrap_or(1000_u64);
_ = config::save(all);
});
}

fn init_setting(ui: &AppWindow) {
Expand Down
79 changes: 73 additions & 6 deletions src/logic/tokens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ use crate::{
logic::message::{async_message_info, async_message_success, async_message_warn},
message_info, message_success, message_warn,
slint_generatedAppWindow::{
AppWindow, HomeIndex, Icons, LoadingStatus, Logic, SendTokenProps, Store,
TokenTileEntry as UITokenTileEntry, TokenTileWithSwitchEntry as UITokenTileWithSwitchEntry,
TokensSetting, TransactionTileStatus, Util,
AppWindow, HomeIndex, Icons, LoadingStatus, Logic, PrioritizationFeeStatus, SendTokenProps,
Store, TokenTileEntry as UITokenTileEntry,
TokenTileWithSwitchEntry as UITokenTileWithSwitchEntry, TokensSetting,
TransactionTileStatus, Util,
},
};
use anyhow::{bail, Result};
Expand All @@ -27,6 +28,8 @@ use wallet::{
transaction::{self, SendLamportsProps, DEFAULT_TIMEOUT_SECS, DEFAULT_TRY_COUNTS},
};

static PRIORITIZATION_FEES: Lazy<Mutex<(u64, u64, u64)>> = Lazy::new(|| Mutex::new((0, 0, 0)));

static SOL_PRICE: Lazy<Mutex<f64>> = Lazy::new(|| Mutex::new(0.0));

static SPL_TOKENS_PRICE_INFO: Lazy<Mutex<HashMap<&'static str, SplTokenPriceInfo>>> =
Expand Down Expand Up @@ -185,6 +188,18 @@ pub async fn update_spl_tokens_price() {
}
}

pub async fn update_prioritization_fees(network: &str) {
let rpc_url_ty = RpcUrlType::from_str(network).unwrap_or(RpcUrlType::Main);
match transaction::prioritization_fees(rpc_url_ty, Some(transaction::DEFAULT_TIMEOUT_SECS))
.await
{
Err(e) => log::warn!("{e:?}"),
Ok(v) => {
*PRIORITIZATION_FEES.lock().unwrap() = v;
}
}
}

async fn get_from_db(network: &str, account_address: &str) -> Vec<UITokenTileEntry> {
match db::entry::select_all(TOKENS_TABLE).await {
Ok(items) => items
Expand Down Expand Up @@ -281,6 +296,7 @@ pub fn init_tokens(ui: &AppWindow) {

update_sol_price().await;
update_spl_tokens_price().await;
update_prioritization_fees(&network).await;
});
}

Expand Down Expand Up @@ -425,6 +441,37 @@ pub fn init(ui: &AppWindow) {
Err(_) => tr("非法地址").into(),
_ => SharedString::default(),
});

ui.global::<Logic>()
.on_prioritization_fee(move |ty| match ty {
PrioritizationFeeStatus::Slow => PRIORITIZATION_FEES.lock().unwrap().0 as i32,
PrioritizationFeeStatus::Normal => PRIORITIZATION_FEES.lock().unwrap().1 as i32,
PrioritizationFeeStatus::Fast => PRIORITIZATION_FEES.lock().unwrap().2 as i32,
});

ui.global::<Logic>()
.on_is_valid_prioritization_fee(move |fee| {
if fee.trim().is_empty() {
return SharedString::default();
}

match fee.parse::<u64>() {
Err(_) => tr("非法优先费用").into(),
Ok(v) => {
let max_prioritization_fee = config::security_privacy().max_prioritization_fee;
if v <= max_prioritization_fee {
SharedString::default()
} else {
slint::format!(
"{}{}, {}",
tr("最大优先费用"),
max_prioritization_fee,
tr("请设置更大的优先费用")
)
}
}
}
});
}

fn _add_token(entry: TokenTileEntry) {
Expand Down Expand Up @@ -817,7 +864,12 @@ async fn _evaluate_spl_token_transaction_fee(
};

let prioritization_fee = if !props.prioritization_fee.trim().is_empty() {
Some(props.prioritization_fee.trim().parse::<u64>()?)
let fee = props.prioritization_fee.trim().parse::<u64>()?;
if fee == 0 {
None
} else {
Some(fee)
}
} else {
None
};
Expand Down Expand Up @@ -921,7 +973,12 @@ async fn _send_sol(
};

let prioritization_fee = if !props.prioritization_fee.trim().is_empty() {
Some(props.prioritization_fee.trim().parse::<u64>()?)
let fee = props.prioritization_fee.trim().parse::<u64>()?;
if fee == 0 {
None
} else {
Some(fee)
}
} else {
None
};
Expand Down Expand Up @@ -990,7 +1047,12 @@ async fn _send_spl_token(
};

let prioritization_fee = if !props.prioritization_fee.trim().is_empty() {
Some(props.prioritization_fee.trim().parse::<u64>()?)
let fee = props.prioritization_fee.trim().parse::<u64>()?;
if fee == 0 {
None
} else {
Some(fee)
}
} else {
None
};
Expand Down Expand Up @@ -1129,6 +1191,11 @@ fn _send_token(
props: SendTokenProps,
is_token_account_exist: bool,
) {
let network = props.network.clone();
tokio::spawn(async move {
update_prioritization_fees(&network).await;
});

tokio::spawn(async move {
match super::accounts::is_valid_password_in_secret_info(&password).await {
Err(e) => {
Expand Down
10 changes: 10 additions & 0 deletions src/logic/tr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,16 @@ pub fn tr(text: &str) -> String {
items.insert("创建组记词", "Create mnemonic");
items.insert("恢复账户", "Recover account");
items.insert("二维码", "QrCode");
items.insert("高级设置", "Advance setting");
items.insert("备注", "Memo");
items.insert("优先费用", "Prioritization fee");
items.insert("基础费用", "Base fee");
items.insert("最大优先费用", "Max prioritization fee");
items.insert("慢", "Slow");
items.insert("正常", "Normal");
items.insert("快", "Fast");
items.insert("非法优先费用", "Invalid prioritization fee");
items.insert("请设置更大的优先费用", "Please setting max prioritization fee");
items.insert(
"更新账户余额失败. 账户不存在",
"Refresh account balance failed. The account is not found",
Expand Down
4 changes: 2 additions & 2 deletions ui/appwindow.slint
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Theme, Icons } from "./theme.slint";
import { Logic } from "./logic.slint";
import { Store, SettingDetailIndex, SettingDeveloperMode, SendTokenProps, HomeIndex } from "./store.slint";
import { Store, SettingDetailIndex, SettingDeveloperMode, SettingSecurityPrivacy, SendTokenProps, HomeIndex } from "./store.slint";
import { Util } from "./util.slint";
import { Panel } from "./panel/panel.slint";
import { LoadingStatus, Toast, IconsDialogSetting, IconsDialog, OkCancelDialogV2, Blanket, LanguageDialog, OkCancelDialogSetting, LandingPage, Password, PasswordSetting, Password, AddressBookEntry, AddressBookSetting, AboutSetting, TransactionTileEntry, TransactionTileStatus, TokenTileEntry, TokenTileWithSwitchEntry } from "./base/widgets.slint";
Expand Down Expand Up @@ -114,4 +114,4 @@ export component AppWindow inherits Window {
}
}

export { Util, Logic, Store, Theme, Icons, IconsDialogSetting, LoadingStatus, SettingDetailIndex, AccountMnemonicSetting, AddressBookEntry, AddressBookSetting, PasswordSetting, AboutSetting, TransactionTileEntry, TransactionTileStatus, HistorySetting, SettingDeveloperMode, TokenTileEntry, TokensSetting, TokenTileWithSwitchEntry, SendTokenProps, HomeIndex }
export { Util, Logic, Store, Theme, Icons, IconsDialogSetting, LoadingStatus, SettingDetailIndex, AccountMnemonicSetting, AddressBookEntry, AddressBookSetting, PasswordSetting, AboutSetting, TransactionTileEntry, TransactionTileStatus, HistorySetting, SettingDeveloperMode, TokenTileEntry, TokensSetting, TokenTileWithSwitchEntry, SendTokenProps, HomeIndex, SettingSecurityPrivacy }
7 changes: 6 additions & 1 deletion ui/base/def.slint
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@

export enum LoadingStatus {
Loading,
Fail,
Success,
}

export enum PrioritizationFeeStatus {
Slow,
Normal,
Fast,
}
Loading

0 comments on commit 43cc55a

Please sign in to comment.