Skip to content

Commit

Permalink
[+] add setting account
Browse files Browse the repository at this point in the history
  • Loading branch information
heng30 committed Jul 2, 2024
1 parent c643a39 commit b049045
Show file tree
Hide file tree
Showing 14 changed files with 341 additions and 92 deletions.
8 changes: 7 additions & 1 deletion src/db/accounts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ use serde::{Deserialize, Serialize};

pub const SECRET_UUID: &str = "secret-uuid";

#[derive(Serialize, Deserialize, Debug, Clone)]
#[derive(Serialize, Deserialize, Debug, Clone, Default)]
pub struct AccountEntry {
pub uuid: String,
pub name: String,
pub pubkey: String,
pub derive_index: i32,
pub avatar_index: i32,
pub balance: String,
}

impl From<UIAccountEntry> for AccountEntry {
Expand All @@ -20,6 +22,8 @@ impl From<UIAccountEntry> for AccountEntry {
name: entry.name.into(),
pubkey: entry.pubkey.into(),
derive_index: entry.derive_index,
avatar_index: entry.avatar_index,
balance: entry.balance.into(),
}
}
}
Expand All @@ -31,6 +35,8 @@ impl From<AccountEntry> for UIAccountEntry {
name: entry.name.into(),
pubkey: entry.pubkey.into(),
derive_index: entry.derive_index,
avatar_index: entry.avatar_index,
balance: entry.balance.into(),
}
}
}
Expand Down
81 changes: 57 additions & 24 deletions src/logic/accounts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ use crate::{
},
logic::message::{async_message_success, async_message_warn},
message_info, message_success, message_warn,
slint_generatedAppWindow::{AccountEntry as UIAccountEntry, AppWindow, Logic, Store},
slint_generatedAppWindow::{
AccountEntry as UIAccountEntry, AppWindow, IconsDialogSetting, Logic, Store, Util,
},
util::{
self,
crypto::{self, md5_hex},
Expand Down Expand Up @@ -378,23 +380,48 @@ pub fn init(ui: &AppWindow) {
});

let ui_handle = ui.as_weak();
ui.global::<Logic>().on_update_account(move |uuid, name| {
let ui = ui_handle.unwrap();
match get_account(&ui, &uuid) {
Some((index, mut account)) => {
account.name = name;
ui.global::<Logic>()
.on_update_account_name(move |uuid, name| {
let ui = ui_handle.unwrap();
match get_account(&ui, &uuid) {
Some((index, mut account)) => {
account.name = name;

if ui.global::<Store>().get_current_account().uuid == uuid {
ui.global::<Store>().set_current_account(account.clone());
}
store_accounts!(ui).set_row_data(index, account.clone());

if ui.global::<Store>().get_current_account().uuid == uuid {
ui.global::<Store>().set_current_account(account.clone());
_update_account(account.into());
message_success!(ui, tr("更新账户成功"));
}
store_accounts!(ui).set_row_data(index, account.clone());
None => message_warn!(ui, "更新账户失败. 账户不存在"),
}
});

let ui_handle = ui.as_weak();
ui.global::<Logic>()
.on_update_account_avatar_index(move |uuid, avatar_index| {
log::debug!("{uuid}, {avatar_index}");

_update_account(account.into());
message_success!(ui, tr("更新账户成功"));
let ui = ui_handle.unwrap();
match get_account(&ui, &uuid) {
Some((index, mut account)) => {
account.avatar_index = avatar_index;

log::debug!("{account:?}");

if ui.global::<Store>().get_current_account().uuid == uuid {
ui.global::<Store>().set_current_account(account.clone());
}
store_accounts!(ui).set_row_data(index, account.clone());

_update_account(account.into());
message_success!(ui, tr("更新账户成功"));
}
None => message_warn!(ui, "更新账户失败. 账户不存在"),
}
None => message_warn!(ui, "更新账户失败. 账户不存在"),
}
});
});

let ui_handle = ui.as_weak();
ui.global::<Logic>().on_remove_account(move |uuid| {
Expand All @@ -404,13 +431,15 @@ pub fn init(ui: &AppWindow) {
message_warn!(ui, tr("不允许删除当前用户"));
}

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

store_accounts!(ui).remove(index);
_remove_account(uuid);
message_success!(ui, tr("删除账户成功"));
}
});

Expand All @@ -428,20 +457,22 @@ pub fn init(ui: &AppWindow) {
// TODO: fetch the account info from the blockchain
message_success!(ui, tr("切换账户成功"));
}
None => message_success!(ui, tr("切换账户失败账户不存在")),
None => message_success!(ui, tr("切换账户失败. 账户不存在")),
}
});
}

fn _new_account(ui: &AppWindow, name: SharedString, password: SharedString) {
let derive_index = get_unused_derive_index(&ui);
let avatar_index = ui.global::<IconsDialogSetting>().invoke_rand_icon_index();

let ui_handle = ui.as_weak();

tokio::spawn(async move {
match get_secrect_info().await {
Ok(info) => {
if crypto::hash(&password) != info.password {
async_message_warn(ui_handle.clone(), tr("创建用户失败非法密码"));
async_message_warn(ui_handle.clone(), tr("创建用户失败. 非法密码"));
}

match get_keypair(&password, &info.mnemonic, derive_index) {
Expand All @@ -455,6 +486,8 @@ fn _new_account(ui: &AppWindow, name: SharedString, password: SharedString) {
},
pubkey: kp.pubkey().to_string(),
derive_index,
avatar_index,
balance: String::from("0"),
};

let data = serde_json::to_string(&account).unwrap();
Expand Down Expand Up @@ -487,12 +520,12 @@ fn _new_account(ui: &AppWindow, name: SharedString, password: SharedString) {

fn _update_account(account: AccountEntry) {
tokio::spawn(async move {
_ = db::accounts::update(&account.uuid, &serde_json::to_string(&account).unwrap());
_ = db::accounts::update(&account.uuid, &serde_json::to_string(&account).unwrap()).await;
});
}

fn _remove_account(uuid: SharedString) {
tokio::spawn(async move {
_ = db::accounts::delete(&uuid);
_ = db::accounts::delete(&uuid).await;
});
}
14 changes: 14 additions & 0 deletions src/util/translator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,20 @@ pub fn tr(text: &str) -> String {
items.insert("生成组记词失败", "New mnemonic failed");
items.insert("组记词数量不对,仅支持12和24个组记词", "Mnemonic counts is no correct. Only support 12 or 24 word counts mnemonic");
items.insert("非法组记词", "Invalid mnemonic");
items.insert("用户名不能为空", "Username can not be empty");
items.insert("密码不相同", "Two passwords is different");
items.insert("密码不能小于8位", "Password can not less than 8 chars");
items.insert("更新账户成功", "Update account success");
items.insert("更新账户失败. 账户不存在", "Update account failed. The account don't exist");
items.insert("不允许删除当前用户", "Not allow delete current account");
items.insert("不允许删除主账号", "Not allow delete the main account");
items.insert("删除账户成功", "Delete account success");
items.insert("切换账户成功", "Switch account success");
items.insert("切换账户失败. 账户不存在", "Switch account failed. The account don't exist");
items.insert("创建用户失败. 非法密码", "Create account failed. Invalid password");
items.insert("创建用户成功", "Create account success");
items.insert("创建用户失败", "Create account failed");


// TODO
items.insert(
Expand Down
49 changes: 43 additions & 6 deletions ui/appwindow.slint
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Logic } from "./logic.slint";
import { Store } from "./store.slint";
import { Util } from "./util.slint";
import { Panel } from "./panel/panel.slint";
import { Toast, IconsDialogSetting, OkCancelDialogV2, Blanket, LanguageDialog, OkCancelDialogSetting, LandingPage } from "./base/widgets.slint";
import { Toast, IconsDialogSetting, IconsDialog, OkCancelDialogV2, Blanket, LanguageDialog, OkCancelDialogSetting, LandingPage, Password, PasswordSetting } from "./base/widgets.slint";

import { SignIn, Login, Password, Mnemonic } from "./base/widgets.slint";
import { Setup } from "./setup/panel.slint";
Expand All @@ -18,16 +18,52 @@ export component AppWindow inherits Window {
icon: Icons.brand;
title: "rssbox";

if !Store.is-show-setup-page : Panel { }
if Store.is-show-setup-page : Setup {}
if !Store.is-show-setup-page: Panel { }
if Store.is-show-setup-page: Setup { }

if IconsDialogSetting.show || OkCancelDialogSetting.body-text != "": Blanket {
if IconsDialogSetting.show || PasswordSetting.show || OkCancelDialogSetting.body-text != "": Blanket {
clicked => {
IconsDialogSetting.show = false;
PasswordSetting.show = false;
OkCancelDialogSetting.body-text = "";
}
}

if IconsDialogSetting.show: IconsDialog {
is-prevent-event-forward: true;
select-index(handle-type, index, user-data) => {
if (handle-type == "account") {
debug(handle-type, index, user-data);
Logic.update-account-avatar-index(user-data, index);
}
}
}

if PasswordSetting.show: Password {
private property <string> err;

back => {
PasswordSetting.show = false;
}

cancel => {
PasswordSetting.show = false;
}

confirm(handle-type, password, user-data) => {
self.err = Logic.is-valid-password(password);

if (self.err != "") {
return self.err;
}
if (handle-type == "new-account") {
Logic.new-account("", password);
}
PasswordSetting.show = false;
return "";
}
}

if OkCancelDialogSetting.body-text != "": OkCancelDialogV2 {
is-prevent-event-forward: true;
width: Math.min(root.width * 0.9, 380px);
Expand Down Expand Up @@ -67,10 +103,11 @@ export component AppWindow inherits Window {
Rectangle {
visible: false;
background: white;
Password {}
Password { }

// Login {}
// SignIn {}
}
}

export { Util, Logic, Store, Theme }
export { Util, Logic, Store, Theme, IconsDialogSetting }
14 changes: 12 additions & 2 deletions ui/base/icons-dialog.slint
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,17 @@ import { Dialog } from "./dialog.slint";
import { Theme } from "../theme.slint";
import { Store } from "../store.slint";
import { Logic } from "../logic.slint";
import { Util } from "../util.slint";

export global IconsDialogSetting {
in-out property <bool> show;
in property <string> handle-type;
in property <string> user-data;

public function rand-icon-index() -> int {
return Util.rand-int(0, IconsDialogSetting.icons.length);
}

out property <[image]> icons: [
@image-url("./icons/1.svg"),
@image-url("./icons/2.svg"),
Expand Down Expand Up @@ -54,6 +62,8 @@ export global IconsDialogSetting {
@image-url("./icons/46.svg"),
@image-url("./icons/47.svg"),
@image-url("./icons/48.svg"),
@image-url("./icons/49.svg"),
@image-url("./icons/50.svg"),
];
}

Expand All @@ -66,7 +76,7 @@ export component IconsDialog inherits Dialog {
property <length> icon-spacing: Theme.spacing;
property <int> column-count: 5;

callback select-index(int);
callback select-index(string, int, string); // (handle-type, icon-index, user-data) -> void

flick := Flickable {
height: 300px;
Expand Down Expand Up @@ -108,7 +118,7 @@ export component IconsDialog inherits Dialog {
touch := TouchArea {
mouse-cursor: self.has-hover ? pointer : default;
clicked => {
root.select-index(index);
root.select-index(IconsDialogSetting.handle-type, index, IconsDialogSetting.user-data);
IconsDialogSetting.show = false;
}
}
Expand Down
1 change: 1 addition & 0 deletions ui/base/icons/49.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions ui/base/icons/50.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit b049045

Please sign in to comment.