feat: 日期可手选,刷新显示加载并自动关闭提示

This commit is contained in:
2026-11-19 16:59:37 +08:00
parent 46a7b0a1ae
commit 3b737a881f
4 changed files with 298 additions and 100 deletions
+117 -36
View File
@@ -1,5 +1,10 @@
use std::process::ExitCode;
use std::rc::Rc;
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::Arc;
use std::time::Duration;
use chrono::{Datelike, NaiveDate};
slint::include_modules!();
@@ -11,14 +16,25 @@ fn main() -> ExitCode {
return ExitCode::FAILURE;
}
};
let (start, end) = punchlog::last_week(chrono::Local::now().date_naive());
ui.set_start_date(to_slint(start));
ui.set_end_date(to_slint(end));
let ui_weak = ui.as_weak();
show(&ui, load());
ui.on_refresh(move || {
let Some(ui) = ui_weak.upgrade() else {
return;
};
show(&ui, load());
});
let loads = Arc::new(AtomicU64::new(0));
let toasts = Arc::new(AtomicU64::new(0));
reload(ui_weak.clone(), Arc::clone(&loads), Arc::clone(&toasts), false);
let weak = ui_weak.clone();
let load_gen = Arc::clone(&loads);
let toast_gen = Arc::clone(&toasts);
ui.on_refresh(move || reload(weak.clone(), Arc::clone(&load_gen), Arc::clone(&toast_gen), true));
let weak = ui_weak.clone();
let load_gen = Arc::clone(&loads);
let toast_gen = Arc::clone(&toasts);
ui.on_range_changed(move || reload(weak.clone(), Arc::clone(&load_gen), Arc::clone(&toast_gen), false));
if let Err(err) = ui.run() {
eprintln!("{err}");
return ExitCode::FAILURE;
@@ -26,38 +42,103 @@ fn main() -> ExitCode {
ExitCode::SUCCESS
}
fn load() -> punchlog::Result<punchlog::WeekReport> {
let mut config = punchlog::AppConfig::load()?;
punchlog::load_last_week(&mut config)
fn reload(ui_weak: slint::Weak<MainWindow>, generation: Arc<AtomicU64>, toasts: Arc<AtomicU64>, announce: bool) {
let Some(ui) = ui_weak.upgrade() else {
return;
};
let Ok(start) = from_slint(ui.get_start_date()) else {
ui.set_loading(false);
show_toast(&ui, ui_weak, &toasts, "开始日期无效");
return;
};
let Ok(end) = from_slint(ui.get_end_date()) else {
ui.set_loading(false);
show_toast(&ui, ui_weak, &toasts, "结束日期无效");
return;
};
let (start, end) = if start <= end { (start, end) } else { (end, start) };
ui.set_start_date(to_slint(start));
ui.set_end_date(to_slint(end));
ui.set_loading(true);
ui.set_toast(String::new().into());
let ticket = generation.fetch_add(1, Ordering::Relaxed) + 1;
std::thread::spawn(move || {
let loaded = load(start, end);
let _ = slint::invoke_from_event_loop(move || {
if generation.load(Ordering::Relaxed) != ticket {
return;
}
let Some(ui) = ui_weak.upgrade() else {
return;
};
ui.set_loading(false);
match loaded {
Ok(report) => {
show(&ui, &report);
if announce {
show_toast(&ui, ui_weak.clone(), &toasts, "已刷新");
}
}
Err(err) => {
ui.set_error(err.to_string().into());
ui.set_days(Rc::new(slint::VecModel::<DayRow>::default()).into());
show_toast(&ui, ui_weak.clone(), &toasts, "刷新失败");
}
}
});
});
}
fn show(ui: &MainWindow, loaded: punchlog::Result<punchlog::WeekReport>) {
match loaded {
Ok(report) => {
ui.set_error(String::new().into());
ui.set_range(format!("{} — {}", report.start, report.end).into());
let rows: Vec<DayRow> = report
.days
.iter()
.map(|day| {
let (site, memo) = day.content();
DayRow {
date: day.day.format("%m-%d").to_string().into(),
mark: if day.has_supplement { "补" } else { "" }.into(),
time: format!("{} – {}", day.first.format("%H:%M"), day.last.format("%H:%M")).into(),
hours: format!("{:.1}h", day.hours).into(),
status: day.status.label().into(),
site: site.into(),
memo: memo.replace(" / ", "\n").into(),
problem: day.status.is_problem(),
}
})
.collect();
ui.set_days(Rc::new(slint::VecModel::from(rows)).into());
fn show_toast(ui: &MainWindow, ui_weak: slint::Weak<MainWindow>, toasts: &Arc<AtomicU64>, text: &str) {
ui.set_toast(text.into());
let ticket = toasts.fetch_add(1, Ordering::Relaxed) + 1;
let toasts = Arc::clone(toasts);
slint::Timer::single_shot(Duration::from_secs(3), move || {
if toasts.load(Ordering::Relaxed) != ticket {
return;
}
Err(err) => {
ui.set_error(err.to_string().into());
ui.set_days(Rc::new(slint::VecModel::<DayRow>::default()).into());
if let Some(ui) = ui_weak.upgrade() {
ui.set_toast(String::new().into());
}
});
}
fn load(start: NaiveDate, end: NaiveDate) -> punchlog::Result<punchlog::WeekReport> {
let mut config = punchlog::AppConfig::load()?;
punchlog::load_range(&mut config, start, end)
}
fn show(ui: &MainWindow, report: &punchlog::WeekReport) {
ui.set_error(String::new().into());
let rows: Vec<DayRow> = report
.days
.iter()
.map(|day| {
let (site, memo) = day.content();
DayRow {
date: day.day.format("%m-%d").to_string().into(),
mark: if day.has_supplement { "补" } else { "" }.into(),
time: format!("{} – {}", day.first.format("%H:%M"), day.last.format("%H:%M")).into(),
hours: format!("{:.1}h", day.hours).into(),
status: day.status.label().into(),
site: site.into(),
memo: memo.replace(" / ", "\n").into(),
problem: day.status.is_problem(),
}
})
.collect();
ui.set_days(Rc::new(slint::VecModel::from(rows)).into());
}
fn to_slint(day: NaiveDate) -> Date {
Date {
year: day.year(),
month: day.month() as i32,
day: day.day() as i32,
}
}
fn from_slint(date: Date) -> Result<NaiveDate, ()> {
NaiveDate::from_ymd_opt(date.year, date.month as u32, date.day as u32).ok_or(())
}
+1 -1
View File
@@ -17,4 +17,4 @@ pub use error::{Error, Result};
pub use iepms::IepmsClient;
pub use phone::{read_phone, save_phone, PhoneIdentity};
pub use record::PunchRecord;
pub use report::{load_last_week, WeekReport};
pub use report::{load_last_week, load_range, WeekReport};
+6
View File
@@ -17,6 +17,12 @@ pub struct WeekReport {
/// 读取最近一周。token 失效时用账号密码重新登录,并尽量把新 token 写回 `.env`。
pub fn load_last_week(config: &mut AppConfig) -> Result<WeekReport> {
let (start, end) = last_week(Local::now().date_naive());
load_range(config, start, end)
}
/// 读取指定日期范围。结束日之后 5 天内审批通过的补卡仍会计入范围内的那一天。
pub fn load_range(config: &mut AppConfig, start: NaiveDate, end: NaiveDate) -> Result<WeekReport> {
let (start, end) = if start <= end { (start, end) } else { (end, start) };
let rows = match fetch(config, start, end) {
Err(Error::AuthExpired) => {
let session = auth::login(config)?;
+174 -63
View File
@@ -1,4 +1,4 @@
import { ScrollView } from "std-widgets.slint";
import { ScrollView, DatePickerPopup, Date } from "std-widgets.slint";
export struct DayRow {
date: string,
@@ -11,54 +11,115 @@ export struct DayRow {
problem: bool,
}
component DayBlock inherits Rectangle {
in property <DayRow> day;
height: 132px;
component DateField inherits Rectangle {
in-out property <Date> date;
in property <string> title;
callback accepted();
height: 28px;
border-width: 1px;
border-color: #d5d5d0;
background: #f7f7f5;
function two(n: int) -> string {
if (n < 10) {
return "0" + n;
}
return "" + n;
}
Text {
x: 24px;
y: 14px;
width: 56px;
text: day.date;
font-size: 15px;
font-weight: 600;
x: 8px;
width: parent.width - 16px;
height: parent.height;
text: root.date.year + "-" + root.two(root.date.month) + "-" + root.two(root.date.day);
font-size: 13px;
color: #1a1a1a;
horizontal-alignment: left;
horizontal-alignment: center;
vertical-alignment: center;
}
Text {
x: 88px;
y: 16px;
width: 120px;
text: day.time;
font-size: 13px;
color: #6a6a6a;
horizontal-alignment: left;
picker := DatePickerPopup {
title: root.title;
date: root.date;
accepted(date) => {
root.date = date;
root.accepted();
}
}
Text {
x: 216px;
y: 16px;
width: 52px;
text: day.hours;
font-size: 13px;
color: #6a6a6a;
horizontal-alignment: left;
TouchArea {
width: parent.width;
height: parent.height;
clicked => {
picker.show();
}
}
}
component DayBlock inherits Rectangle {
in property <DayRow> day;
height: 112px;
background: #f7f7f5;
HorizontalLayout {
x: 24px;
y: 12px;
width: parent.width - 48px;
height: 22px;
spacing: 8px;
alignment: start;
Text {
text: day.date;
font-size: 15px;
font-weight: 600;
color: #1a1a1a;
vertical-alignment: center;
}
Text {
text: day.time;
font-size: 13px;
color: #6a6a6a;
vertical-alignment: center;
}
Text {
text: day.hours;
font-size: 13px;
color: #6a6a6a;
vertical-alignment: center;
}
if day.mark != "": Text {
text: day.mark;
font-size: 13px;
color: #6a6a6a;
vertical-alignment: center;
}
Rectangle {
horizontal-stretch: 1;
min-width: 24px;
clip: true;
Text {
width: parent.width;
height: parent.height;
text: day.site;
font-size: 12px;
color: #8a8a8a;
horizontal-alignment: right;
vertical-alignment: center;
overflow: elide;
}
}
}
if day.mark != "": Text {
x: 276px;
y: 16px;
text: day.mark;
font-size: 13px;
color: #6a6a6a;
horizontal-alignment: left;
}
Text {
status-text := Text {
x: parent.width - 24px - self.width;
y: 16px;
y: parent.height - 16px - self.height;
text: day.status;
font-size: 13px;
color: day.problem ? #1a1a1a : #6a6a6a;
@@ -68,20 +129,8 @@ component DayBlock inherits Rectangle {
Text {
x: 24px;
y: 42px;
width: parent.width - 48px;
height: 18px;
text: day.site;
font-size: 12px;
color: #8a8a8a;
horizontal-alignment: left;
overflow: elide;
}
Text {
x: 24px;
y: 64px;
width: parent.width - 48px;
height: 52px;
width: max(0px, status-text.x - 24px - 16px);
height: parent.height - 42px - 14px;
text: day.memo;
font-size: 14px;
color: #1a1a1a;
@@ -101,9 +150,13 @@ component DayBlock inherits Rectangle {
export component MainWindow inherits Window {
in property <[DayRow]> days;
in property <string> range;
in-out property <Date> start-date;
in-out property <Date> end-date;
in property <bool> loading;
in-out property <string> toast;
in property <string> error;
callback refresh();
callback range-changed();
title: "打卡";
preferred-width: 880px;
@@ -121,27 +174,45 @@ export component MainWindow inherits Window {
horizontal-alignment: left;
}
DateField {
x: 76px;
y: 12px;
width: 108px;
date <=> root.start-date;
title: "开始日期";
accepted => { root.range-changed(); }
}
Text {
x: 72px;
y: 18px;
text: root.range;
x: 190px;
y: 16px;
text: "—";
font-size: 13px;
color: #8a8a8a;
horizontal-alignment: left;
}
DateField {
x: 210px;
y: 12px;
width: 108px;
date <=> root.end-date;
title: "结束日期";
accepted => { root.range-changed(); }
}
TouchArea {
x: parent.width - 72px;
x: parent.width - 28px - 80px;
y: 0px;
width: 72px;
width: 80px;
height: 52px;
enabled: !root.loading;
Text {
x: 0px;
y: 18px;
width: parent.width;
text: "刷新";
text: root.loading ? "正在刷新" : "刷新";
font-size: 13px;
color: #1a1a1a;
color: root.loading ? #8a8a8a : #1a1a1a;
horizontal-alignment: right;
}
clicked => { root.refresh(); }
@@ -166,10 +237,10 @@ export component MainWindow inherits Window {
horizontal-alignment: left;
}
if root.error == "" && root.days.length == 0: Text {
if !root.loading && root.error == "" && root.days.length == 0: Text {
x: 24px;
y: 68px;
text: "这一周没有打卡记录。";
text: "这个范围没有打卡记录。";
font-size: 14px;
color: #8a8a8a;
horizontal-alignment: left;
@@ -188,4 +259,44 @@ export component MainWindow inherits Window {
}
}
}
if root.toast != "": island := Rectangle {
property <length> capsule-width: min(parent.width - 80px, notice.preferred-width + close-mark.preferred-width + 32px);
x: (parent.width - self.width) / 2;
y: 0px;
width: capsule-width;
height: 30px;
border-top-left-radius: 0px;
border-top-right-radius: 0px;
border-bottom-left-radius: 15px;
border-bottom-right-radius: 15px;
background: #1a1a1a;
notice := Text {
x: 16px;
height: parent.height;
text: root.toast;
font-size: 13px;
color: #f7f7f5;
vertical-alignment: center;
overflow: elide;
}
close-mark := Text {
x: parent.width - 8px - self.width;
height: parent.height;
text: "×";
font-size: 20px;
font-weight: 700;
color: #ff2430;
vertical-alignment: center;
}
TouchArea {
width: parent.width;
height: parent.height;
clicked => { root.toast = ""; }
}
}
}