Skip to content

Commit f50f506

Browse files
authored
Feat/swipe gesture (#410)
* feat: swipe-gesture * fix: remove redundant code * fix: format * fix: lint
1 parent d8e52d4 commit f50f506

8 files changed

Lines changed: 116 additions & 26 deletions

File tree

infrastructure/eid-wallet/src-tauri/Cargo.lock

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

infrastructure/eid-wallet/src-tauri/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ uuid = { version = "1.0", features = ["v4"] }
3131
argon2 = { version = "0.5.3" }
3232
rand_core = { version = "0.6", features = ["std"] }
3333
thiserror = { version = "2.0.11" }
34+
tauri-plugin-process = "2"
3435

3536
[target.'cfg(any(target_os = "android", target_os = "ios"))'.dependencies]
3637
tauri-plugin-barcode-scanner = "2"

infrastructure/eid-wallet/src-tauri/capabilities/mobile.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
"barcode-scanner:default",
1414
"deep-link:default",
1515
"crypto-hw:default",
16-
"notification:default"
16+
"notification:default",
17+
"process:default"
1718
],
1819
"platforms": [
1920
"iOS",

infrastructure/eid-wallet/src-tauri/src/lib.rs

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
mod errors;
22
mod funcs;
33

4-
use uuid::Uuid;
54
use std::env;
5+
use uuid::Uuid;
66

77
// Learn more about Tauri commands at https://tauri.app/develop/calling-rust/
88
// #[tauri::command]
@@ -52,26 +52,33 @@ async fn get_device_id() -> Result<String, String> {
5252
async fn get_platform() -> Result<String, String> {
5353
#[cfg(target_os = "android")]
5454
return Ok("android".to_string());
55-
55+
5656
#[cfg(target_os = "ios")]
5757
return Ok("ios".to_string());
58-
58+
5959
#[cfg(target_os = "windows")]
6060
return Ok("windows".to_string());
61-
61+
6262
#[cfg(target_os = "macos")]
6363
return Ok("macos".to_string());
64-
64+
6565
#[cfg(target_os = "linux")]
6666
return Ok("linux".to_string());
67-
68-
#[cfg(not(any(target_os = "android", target_os = "ios", target_os = "windows", target_os = "macos", target_os = "linux")))]
67+
68+
#[cfg(not(any(
69+
target_os = "android",
70+
target_os = "ios",
71+
target_os = "windows",
72+
target_os = "macos",
73+
target_os = "linux"
74+
)))]
6975
return Ok("unknown".to_string());
7076
}
7177

7278
#[cfg_attr(mobile, tauri::mobile_entry_point)]
7379
pub fn run() {
7480
tauri::Builder::default()
81+
.plugin(tauri_plugin_process::init())
7582
.plugin(tauri_plugin_opener::init())
7683
.plugin(tauri_plugin_store::Builder::new().build())
7784
.plugin(tauri_plugin_deep_link::init())
@@ -86,7 +93,12 @@ pub fn run() {
8693
Ok(())
8794
})
8895
// Register the commands with Tauri.
89-
.invoke_handler(tauri::generate_handler![hash, verify, get_device_id, get_platform])
96+
.invoke_handler(tauri::generate_handler![
97+
hash,
98+
verify,
99+
get_device_id,
100+
get_platform
101+
])
90102
.run(tauri::generate_context!())
91103
.expect("error while running tauri application");
92104
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export * from "./mergeClasses";
22
export * from "./clickOutside";
33
export * from "./capitalize";
4+
export * from "./swipeGesture";
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// utils/swipedetect.ts
2+
export const swipedetect = (
3+
el: HTMLElement,
4+
callback: (dir: string) => void,
5+
) => {
6+
let startX = 0;
7+
let startY = 0;
8+
let distX = 0;
9+
let distY = 0;
10+
11+
const threshold = 100; // min distance for swipe
12+
const restraint = 100; // max perpendicular distance
13+
const allowedTime = 400; // ms
14+
15+
let startTime = 0;
16+
17+
el.addEventListener(
18+
"touchstart",
19+
(e: TouchEvent) => {
20+
const touchobj = e.changedTouches[0];
21+
startX = touchobj.pageX;
22+
startY = touchobj.pageY;
23+
startTime = new Date().getTime();
24+
},
25+
false,
26+
);
27+
28+
el.addEventListener(
29+
"touchend",
30+
(e: TouchEvent) => {
31+
const touchobj = e.changedTouches[0];
32+
distX = touchobj.pageX - startX;
33+
distY = touchobj.pageY - startY;
34+
const elapsedTime = new Date().getTime() - startTime;
35+
36+
if (
37+
elapsedTime <= allowedTime &&
38+
Math.abs(distX) >= threshold &&
39+
Math.abs(distY) <= restraint
40+
) {
41+
const dir = distX < 0 ? "right" : "left";
42+
callback(dir);
43+
}
44+
},
45+
false,
46+
);
47+
};

infrastructure/eid-wallet/src/routes/+layout.svelte

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { goto, onNavigate } from "$app/navigation";
66
import { GlobalState } from "$lib/global/state";
77
88
import { runtime } from "$lib/global/runtime.svelte";
9+
import { swipedetect } from "$lib/utils";
910
import { type Status, checkStatus } from "@tauri-apps/plugin-biometric";
1011
1112
const { children } = $props();
@@ -16,6 +17,7 @@ let showSplashScreen = $state(false);
1617
let previousRoute = null;
1718
let navigationStack: string[] = [];
1819
let globalDeepLinkHandler: ((event: Event) => void) | undefined;
20+
let mainWrapper: HTMLElement | undefined = $state(undefined);
1921
2022
setContext("globalState", () => globalState);
2123
@@ -420,6 +422,14 @@ onNavigate((navigation) => {
420422
});
421423
});
422424
});
425+
426+
$effect(() => {
427+
if (mainWrapper) {
428+
swipedetect(mainWrapper, (dir: string) => {
429+
if (dir === "right") window.history.back();
430+
});
431+
}
432+
});
423433
</script>
424434

425435
{#if showSplashScreen}
@@ -428,7 +438,10 @@ onNavigate((navigation) => {
428438
<div
429439
class="fixed top-0 left-0 right-0 h-[env(safe-area-inset-top)] bg-primary z-50"
430440
></div>
431-
<div class="bg-white h-screen overflow-scroll pt-10">
441+
<div
442+
bind:this={mainWrapper}
443+
class="bg-white h-screen overflow-scroll pt-10"
444+
>
432445
{#if children}
433446
{@render children()}
434447
{/if}

pnpm-lock.yaml

Lines changed: 20 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)