Skip to content

Commit

Permalink
feat: update progress
Browse files Browse the repository at this point in the history
  • Loading branch information
oplik0 committed Mar 26, 2023
1 parent 05d0a40 commit 7c50615
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 11 deletions.
1 change: 0 additions & 1 deletion src-tauri/src/oath.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ pub async fn register_oath(uuid: String, label: String, issuer: Option<String>,
"sha256" => Digest::Sha256,
_ => return Err("Unsupported credential type".to_string())
};
println!("label: {:?}", label);
let credential = Credential{
label,
issuer,
Expand Down
2 changes: 0 additions & 2 deletions src-tauri/src/solo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ pub struct Solo2Info {
impl From<Solo2> for Solo2Info {
fn from(solo2: Solo2) -> Solo2Info {
let uuid = solo2.uuid().simple().to_string();
println!("uuid: {}", uuid);
Solo2Info {
uuid: uuid.to_uppercase(),
version: solo2.version(),
Expand Down Expand Up @@ -45,7 +44,6 @@ pub struct Solo2List(pub Mutex<BTreeMap<String, Solo2Info>>);

#[memoize]
pub fn get_secure_status(uuid: String) -> Option<bool> {
println!("get_secure_status: {}", uuid);
let converted_uuid = Uuid::from_str(&uuid).unwrap();
let mut device = Solo2::having(converted_uuid).unwrap();
let mut admin = Admin::select(&mut device).unwrap();
Expand Down
21 changes: 17 additions & 4 deletions src-tauri/src/update.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
use serde::Serialize;
use solo2::{Device, Firmware, Uuid, UuidSelectable};
use tauri::{command, State};
use tauri::{command, State, Window};

use crate::solo::Solo2List;

#[derive (Serialize, Clone)]
struct ProgressData {
completed: usize,
total: usize,
uuid: String,
}

#[command]
pub async fn update_key(uuid: String, file: Option<String>, state: State<'_, Solo2List>) -> Result<(), String> {
pub async fn update_key(uuid: String, file: Option<String>, state: State<'_, Solo2List>, window: Window) -> Result<(), String> {
let list = state.0.lock().await;
if list.contains_key(&uuid) {
let firmware = match file {
Expand All @@ -20,12 +27,18 @@ pub async fn update_key(uuid: String, file: Option<String>, state: State<'_, Sol
};
let converted_uuid = Uuid::from_u128(u128::from_str_radix(&uuid, 16).unwrap());
let device = Device::having(converted_uuid).unwrap();
match device.program(firmware, true) {
let total = firmware.len() as u64;
let bar = indicatif::ProgressBar::new(total);
let progress = |bytes: usize| {
window.emit("update_progress", ProgressData{completed: bytes, total: total as usize, uuid: uuid.clone()}).unwrap();
bar.set_position(bytes as u64);
};

match device.program(firmware, true, Some(&progress)) {
Ok(_) => Ok(()),
Err(e) => Err(e.to_string()),
}
} else {
println!("List: {:?}", list);
Err("Key not found".to_string())
}
}
20 changes: 18 additions & 2 deletions src/lib/KeyCard.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,34 @@
TextBox,
} from "fluent-svelte";
import { parse, gt, major } from "semver";
import type { Solo2 } from "src/types";
import type { Solo2, UpdateData } from "$types";
import { saveKeyName } from "$lib/keyName";
import { link } from "svelte-spa-router";
import { listen } from "@tauri-apps/api/event";
export let key: Solo2;
export let latest_version = "0.0.0";
let dialogOpen = false;
let updating = false;
let updateProgress = 0;
let expanding = false;
let selected_file: string | undefined;
let keyName = key.name ?? key.uuid;
let editingKeyName = false;
listen("update_progress", async (data) => {
const updateData = data.payload as UpdateData;
if (updateData.uuid === key.uuid) {
const progress = Math.round(
100 * (updateData.completed / updateData.total)
);
if (progress === 100) {
updating = false;
} else {
updating = true;
updateProgress = progress;
}
}
});
async function update() {
updating = true;
try {
Expand Down Expand Up @@ -210,7 +226,7 @@
style="width: 85px"
>
{#if updating}
<ProgressRing size={20} />
<ProgressRing size={20} value={updateProgress} />
{:else}
<TextBlock>Update</TextBlock>
{/if}
Expand Down
3 changes: 2 additions & 1 deletion src/routes/keys.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
import { addNamesToKeys } from "$lib/keyName";
import { listen } from "@tauri-apps/api/event";
import { invoke } from "@tauri-apps/api/tauri";
import type { Solo2List } from "src/types";
import type { Solo2List } from "$types";
import { onMount } from "svelte";
import type { UpdateData } from "../types";
let keyList: Solo2List;
let latest_version: string;
Expand Down
6 changes: 6 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,9 @@ export interface Solo2 {
export interface Solo2List {
[uuid: string]: Solo2;
}

export interface UpdateData {
uuid: string;
completed: number;
total: number;
}
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"$lib/*": ["./src/lib/*"],
"$routes/*": ["./src/routes/*"],
"$assets/*": ["./src/assets/*"],
"$icons/*": ["./src/assets/icons/*"]
"$icons/*": ["./src/assets/icons/*"],
"$types": ["./src/types.ts"]
},
"allowSyntheticDefaultImports": true,
"target": "ESNext",
Expand Down

0 comments on commit 7c50615

Please sign in to comment.