Skip to content

Commit

Permalink
[+] add history panel
Browse files Browse the repository at this point in the history
  • Loading branch information
heng30 committed Jul 3, 2024
1 parent c64142d commit f59d557
Show file tree
Hide file tree
Showing 24 changed files with 286 additions and 97 deletions.
86 changes: 55 additions & 31 deletions src/logic/accounts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ use crate::{
},
util::{crypto, translator::tr},
};
use anyhow::{Context, Result};
use slint::{ComponentHandle, Model, SharedString, VecModel};
use anyhow::{bail, Context, Result};
use slint::{ComponentHandle, Model, SharedString, VecModel, Weak};
use std::cmp::Ordering;
use uuid::Uuid;
use wallet::{mnemonic, prelude::*};
Expand Down Expand Up @@ -66,6 +66,14 @@ fn is_valid_secret_info(info: &SecretInfo) -> bool {
!info.password.is_empty() && !info.mnemonic.is_empty() && info.current_derive_index >= 0
}

async fn is_valid_password_in_secret_info(password: &str) -> Result<()> {
if get_secrect_info().await?.password != crypto::hash(password) {
bail!("Wrong password");
}

Ok(())
}

fn accounts_sort_fn(a: &UIAccountEntry, b: &UIAccountEntry) -> Ordering {
a.derive_index.cmp(&b.derive_index)
}
Expand Down Expand Up @@ -422,37 +430,30 @@ pub fn init(ui: &AppWindow) {
});

let ui_handle = ui.as_weak();
ui.global::<Logic>().on_remove_account(move |uuid| {
let ui = ui_handle.unwrap();

if ui.global::<Store>().get_current_account().uuid == uuid {
message_warn!(ui, tr("不允许删除当前用户"));
return;
}
ui.global::<Logic>()
.on_remove_account(move |password, uuid| {
let ui = ui_handle.unwrap();

if let Some((index, account)) = get_account(&ui, &uuid) {
if account.derive_index == 0 {
message_warn!(ui, tr("不允许删除主账号"));
if ui.global::<Store>().get_current_account().uuid == uuid {
message_warn!(ui, tr("不允许删除当前用户"));
return;
}

store_accounts!(ui).remove(index);
_remove_account(uuid);
ui.global::<Store>()
.set_current_setting_detail_index(SettingDetailIndex::Accounts);
message_success!(ui, tr("删除账户成功"));
}
});
if let Some((index, account)) = get_account(&ui, &uuid) {
if account.derive_index == 0 {
message_warn!(ui, tr("不允许删除主账号"));
return;
}

let ui_handle = ui.as_weak();
ui.global::<Logic>().on_remove_all_accounts(move || {
let ui = ui_handle.unwrap();
_remove_account(ui.as_weak(), password, uuid, index);
}
});

_remove_all_accounts();
store_accounts!(ui).set_vec(vec![]);
message_success!(ui, tr("删除所有用户成功"));
ui.global::<Store>().set_is_show_setup_page(true);
});
let ui_handle = ui.as_weak();
ui.global::<Logic>()
.on_remove_all_accounts(move |password| {
_remove_all_accounts(ui_handle.clone(), password);
});

let ui_handle = ui.as_weak();
ui.global::<Logic>()
Expand Down Expand Up @@ -556,15 +557,38 @@ fn _update_account(account: AccountEntry) {
});
}

fn _remove_account(uuid: SharedString) {
fn _remove_account(ui_handle: Weak<AppWindow>, password: SharedString, uuid: SharedString, index: usize) {
tokio::spawn(async move {
_ = db::accounts::delete(&uuid).await;
match is_valid_password_in_secret_info(&password).await {
Err(e) => async_message_warn(ui_handle, format!("{e:?}")),
_ => {
_ = db::accounts::delete(&uuid).await;
_ = slint::invoke_from_event_loop(move || {
let ui = ui_handle.unwrap();
store_accounts!(ui).remove(index);
ui.global::<Store>()
.set_current_setting_detail_index(SettingDetailIndex::Accounts);
message_success!(ui, tr("移除账户成功"));
});
}
}
});
}

fn _remove_all_accounts() {
fn _remove_all_accounts(ui_handle: Weak<AppWindow>, password: SharedString) {
tokio::spawn(async move {
_ = db::accounts::delete_all().await;
match is_valid_password_in_secret_info(&password).await {
Err(e) => async_message_warn(ui_handle, format!("{e:?}")),
_ => {
_ = db::accounts::delete_all().await;
_ = slint::invoke_from_event_loop(move || {
let ui = ui_handle.unwrap();
store_accounts!(ui).set_vec(vec![]);
message_success!(ui, tr("删除所有账户成功"));
ui.global::<Store>().set_is_show_setup_page(true);
});
}
}
});
}

Expand Down
4 changes: 2 additions & 2 deletions src/logic/address_book.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::slint_generatedAppWindow::{
AddressBookEntry as UIAddressBookEntry, AddressBookSetting, AppWindow, Logic,
SettingDetailIndex, Store,
SettingDetailIndex, Store, Icons
};
use crate::{
db::{self, address_book::AddressBookEntry},
Expand Down Expand Up @@ -83,7 +83,7 @@ pub fn init(ui: &AppWindow) {
);
Image::from_rgb8(buffer)
}
_ => ui.global::<Store>().get_no_image(),
_ => ui.global::<Icons>().get_no_data(),
}
});

Expand Down
6 changes: 3 additions & 3 deletions src/logic/clipboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ fn copy_from_clipboard() -> Result<String> {

// TODO
#[cfg(target_os = "android")]
fn copy_to_clipboard(msg: &str) -> Result<()> {
Ok(())
fn copy_to_clipboard(_msg: &str) -> Result<()> {
bail!("Not unimplement");
}

// TODO
#[cfg(target_os = "android")]
fn copy_from_clipboard() -> Result<String> {
Ok("TODO".to_string())
bail!("Not unimplement");
}

pub fn init(ui: &AppWindow) {
Expand Down
6 changes: 1 addition & 5 deletions src/logic/ok_cancel_dialog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,11 @@ pub fn init(ui: &AppWindow) {
"remove-all-cache" => {
ui.global::<Logic>().invoke_remove_all_cache();
}
"remove-account" => {
ui.global::<Logic>().invoke_remove_account(user_data);
}
"remove-all-accounts" => {
ui.global::<Logic>().invoke_remove_account(user_data);
ui.global::<PasswordSetting>().invoke_set(
true,
handle_type,
"".into(),
user_data,
);
}
"remove-address-book-entry" => {
Expand Down
2 changes: 2 additions & 0 deletions src/util/translator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ pub fn tr(text: &str) -> String {
items.insert("不允许删除当前用户", "Not allow delete current account");
items.insert("不允许删除主账号", "Not allow delete the main account");
items.insert("删除账户成功", "Delete account success");
items.insert("移除账户成功", "Remove account success");
items.insert("切换账户成功", "Switch account success");
items.insert("切换账户失败. 账户不存在", "Switch account failed. The account don't exist");
items.insert("创建用户失败. 非法密码", "Create account failed. Invalid password");
Expand All @@ -194,6 +195,7 @@ pub fn tr(text: &str) -> String {
items.insert("更新地址失败", "Update address failed");
items.insert("切换账户", "Switch account");
items.insert("是否删除所有账户?", "Delete all account or not?");
items.insert("删除所有账户成功", "Delete all accounts success");

// TODO
items.insert(
Expand Down
20 changes: 15 additions & 5 deletions ui/appwindow.slint
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { Setup } from "./setup/panel.slint";
import { CreateAccount } from "./setup/create-account.slint";
import { AccountMnemonicSetting } from "./panel/bodyer/setting.slint";

import { AddressBook, AddressBookDetail } from "./base/widgets.slint";
import { AddressBook, AddressBookDetail, Mnemonic } from "./base/widgets.slint";

export component AppWindow inherits Window {
default-font-size: Theme.default-font-size;
Expand Down Expand Up @@ -42,6 +42,10 @@ export component AppWindow inherits Window {
if PasswordSetting.show: Password {
private property <string> err;

init => {
self.focus();
}

back => {
PasswordSetting.show = false;
}
Expand All @@ -60,8 +64,10 @@ export component AppWindow inherits Window {
Logic.new-account("", password);
} else if (handle-type == "show-mnemonic") {
Logic.show-mnemonic(password);
} else if (handle-type == "remove-account") {
Logic.remove_account(password, user_data);
} else if (handle-type == "remove-all-accounts") {
Logic.remove-all-accounts();
Logic.remove-all-accounts(password);
}
PasswordSetting.show = false;
return "";
Expand Down Expand Up @@ -107,13 +113,17 @@ export component AppWindow inherits Window {
Rectangle {
visible: false;
background: white;
AddressBookDetail {}
// AddressBookDetail {}
// AddressBook {}
// Password { }

// Login {}
// SignIn {}
// Mnemonic {
// init => {
// self.reset-mnemonics(24);
// }
// }
}
}

export { Util, Logic, Store, Theme, IconsDialogSetting, SettingDetailIndex, AccountMnemonicSetting, AddressBookEntry, AddressBookSetting, PasswordSetting }
export { Util, Logic, Store, Theme, Icons, IconsDialogSetting, SettingDetailIndex, AccountMnemonicSetting, AddressBookEntry, AddressBookSetting, PasswordSetting }
5 changes: 3 additions & 2 deletions ui/base/address-book.slint
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export global AddressBookSetting {
];

in-out property <AddressBookEntry> current-entry;
in-out property <bool> enabled-input;

public function clear() {
self.entries = [];
Expand Down Expand Up @@ -125,7 +126,7 @@ export component AddressBookDetail inherits SettingDetail {
}

LineInput {
enabled: false;
enabled: AddressBookSetting.enabled-input;
width: root.width - Theme.padding * 4;
text: entry.name;
icon: self.enabled ? Icons.checked-box : Icons.edit;
Expand Down Expand Up @@ -153,7 +154,7 @@ export component AddressBookDetail inherits SettingDetail {
}

address-input := LineInput {
enabled: false;
enabled: AddressBookSetting.enabled-input;
width: root.width - Theme.padding * 4;
text: entry.address;

Expand Down
2 changes: 2 additions & 0 deletions ui/base/login.slint
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ export component Login inherits Rectangle {
password-lineedit.text = "";
}

forward-focus: username-lineedit;

vbox := VerticalLayout {
if is-show-header: Head {
title: Logic.tr(Store.is-cn, "返回");
Expand Down
29 changes: 4 additions & 25 deletions ui/base/mnemonic.slint
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export component Mnemonic inherits Rectangle {
in property <bool> is-show-switch-mnemonic-counts-btn;
in property <string> header-title;

private property <int> cell-pre-row: 3;
private property <int> cell-pre-row: 2;
private property <length> label-width: 20px;
private property <int> max-blocks: mnemonics.length == 24 ? 2 : (mnemonics.length == 12 ? 1 : 0);

Expand Down Expand Up @@ -136,15 +136,15 @@ export component Mnemonic inherits Rectangle {
VerticalLayout {
spacing: Theme.spacing * 2;

for index in 4: HorizontalLayout {
for index in 6: HorizontalLayout {
spacing: Theme.padding * 2;
alignment: LayoutAlignment.space-between;

private property <int> cell-start-index: index * cell-pre-row + block-start-index;

HorizontalLayout {
width: 33%;
spacing: Theme.spacing;
horizontal-stretch: 1;

Label {
width: label-width;
Expand All @@ -164,7 +164,7 @@ export component Mnemonic inherits Rectangle {
}

HorizontalLayout {
width: 33%;
horizontal-stretch: 1;
spacing: Theme.spacing;

Label {
Expand All @@ -183,27 +183,6 @@ export component Mnemonic inherits Rectangle {
}
}
}

HorizontalLayout {
width: 33%;
spacing: Theme.spacing;

Label {
width: label-width;
text: cell-start-index + 3;
horizontal-alignment: TextHorizontalAlignment.right;
}

LineEdit {
horizontal-stretch: 1;
read-only: is-read-only;
text: mnemonics[cell-start-index + 2];

edited => {
mnemonics[cell-start-index + 2] = self.text;
}
}
}
}
}
}
Expand Down
1 change: 1 addition & 0 deletions ui/base/password.slint
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export component Password inherits Rectangle {
// (handle-type, password, user-data) -> string
callback confirm(string, string, string) -> string;

forward-focus: password-lineedit;
background: Theme.base-background;

public function reset() {
Expand Down
2 changes: 2 additions & 0 deletions ui/base/sign-in.slint
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ export component SignIn inherits Rectangle {
password-second-lineedit.text = "";
}

forward-focus: username-lineedit;

vbox := VerticalLayout {
if is-show-header: Head {
title: Logic.tr(Store.is-cn, "返回");
Expand Down
3 changes: 2 additions & 1 deletion ui/base/slide-card.slint
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export component SlideCard inherits Rectangle {

callback icon-clicked(int);

in-out property <MouseCursor> mouse-cursor <=> ta.mouse-cursor;
in property <[{icon: image, text: string, colorize: color}]> icons;
in property <color> icons-background;
in property <bool> hide-icons-after-clicked: true;
Expand Down Expand Up @@ -57,7 +58,7 @@ export component SlideCard inherits Rectangle {
easing: ease-in-out;
}

TouchArea {
ta := TouchArea {
moved => {
if (!root.is-show-icons) {
if (self.pressed-x - self.mouse-x > parent.max-move-pixels-up-bound) {
Expand Down
Loading

0 comments on commit f59d557

Please sign in to comment.