Skip to content

Commit

Permalink
Merge pull request #8 from thep0y/main
Browse files Browse the repository at this point in the history
发布 v0.1.0-alpha.3
  • Loading branch information
thep0y authored Jan 15, 2024
2 parents 2482a7e + 86448d6 commit b1adb54
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 25 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

在局域网中使用手机快速向 PC 传递文件的工具,适配 Windows、macOS 和 Linux 桌面发行版。

在未开发手机端前,只能通过手机浏览器上传文件,建议使用 [Edge](https://play.google.com/store/search?q=edge&c=apps)[Chrome](https://play.google.com/store/search?q=Chrome&c=apps)[Firefox](https://play.google.com/store/apps/details?id=org.mozilla.firefox)[QQ浏览器](https://browser.qq.com/mobile)

## 特点

本软件可能有别于其他同功能软件的特点有:
Expand Down
23 changes: 15 additions & 8 deletions src-tauri/src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,23 +66,22 @@ impl Writer for ServerError {
#[derive(Debug, Serialize, Clone)]
struct Task {
name: String,
progress: f64,
percent: f64,
speed: f64, // MB/s
}

impl Into<String> for Task {
fn into(self) -> String {
format!(
r#"{{"name":"{}", "progress":{}}}"#,
self.name, self.progress
)
format!(r#"{{"name":"{}", "progress":{}}}"#, self.name, self.percent)
}
}

impl Task {
fn new<S: Into<String>>(name: S, progress: f64) -> Self {
fn new<S: Into<String>>(name: S, progress: f64, speed: f64) -> Self {
Self {
name: name.into(),
progress,
percent: progress,
speed,
}
}
}
Expand Down Expand Up @@ -183,10 +182,18 @@ async fn upload(req: &mut Request) -> Result<()> {
let name = name.clone();
move |_, progress| {
let name = name.clone();

let progress_end = Instant::now();

// cost 不能是秒, 秒转为整数可能是 0
let cost = progress_end.duration_since(start).as_millis() as f64;

let speed = (progress / 1024 / 1024) as f64 / (cost / 1000.0);

if let Some(w) = MAIN_WINDOW.get() {
let _ = w.emit(
UPLOAD_EVENT,
Task::new(name, (progress * 10000 / size) as f64 / 100.0),
Task::new(name, (progress * 10000 / size) as f64 / 100.0, speed),
);
}
}
Expand Down
35 changes: 19 additions & 16 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ import { open as pick } from "@tauri-apps/api/dialog";
interface FileListItemProps {
name: string;
percent: number;
speed?: number;
}

const FileListItem = ({ name, percent }: FileListItemProps) => (
const FileListItem = ({ name, percent, speed }: FileListItemProps) => (
<div>
<div
style={{
Expand All @@ -28,6 +29,7 @@ const FileListItem = ({ name, percent }: FileListItemProps) => (
}}
>
{name}
{speed ? `(${speed.toFixed(1)} MB/s)` : ""}
</div>
<Progress percent={percent} />
</div>
Expand All @@ -41,7 +43,7 @@ const App = () => {
const [progressList, setProgressList] = useState<OrderedSet<TaskMessage>>(
new OrderedSet("name"),
);
const [fileList, setFileList] = useState<TaskMessage[]>([]);
const [fileList, setFileList] = useState<Omit<TaskMessage, "speed">[]>([]);

const [openDropDown, setOpenDropDown] = useState(false);

Expand All @@ -57,13 +59,15 @@ const App = () => {

setProgressList((pre) => pre.push(e.payload));

if (e.payload.progress === 100) {
const { name, percent } = e.payload;

if (percent === 100) {
setProgressList((pre) => pre.remove(e.payload));

setFileList((pre) => {
const t = pre.find((v) => v.name === e.payload.name);
const t = pre.find((v) => v.name === name);

return t ? pre : [...pre, e.payload];
return t ? pre : [...pre, { name, percent }];
});
}
});
Expand All @@ -82,18 +86,16 @@ const App = () => {
useEffect(() => {
if (!qrcode) return;

const query = async () => {
const timer = setInterval(async () => {
const used = await getQrCodeState(qrcode.id);
const timer = setInterval(async () => {
const used = await getQrCodeState(qrcode.id);

if (used) {
clearTimeout(timer);
setQrcode(null);
}
}, 500);
};
if (used) {
clearTimeout(timer);
setQrcode(null);
}
}, 500);

query();
return () => clearTimeout(timer);
}, [qrcode]);

const pickDirectory = async () => {
Expand Down Expand Up @@ -169,7 +171,8 @@ const App = () => {
<FileListItem
key={progress.name}
name={progress.name}
percent={Math.round(progress.progress)}
percent={Math.round(progress.percent)}
speed={progress.speed}
/>
))}

Expand Down
3 changes: 2 additions & 1 deletion types/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
interface TaskMessage {
name: string;
progress: number;
percent: number;
speed: number;
}

interface QrCode {
Expand Down

0 comments on commit b1adb54

Please sign in to comment.