feat: 日期可手选,刷新显示加载并自动关闭提示
This commit is contained in:
+97
-16
@@ -1,5 +1,10 @@
|
|||||||
use std::process::ExitCode;
|
use std::process::ExitCode;
|
||||||
use std::rc::Rc;
|
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!();
|
slint::include_modules!();
|
||||||
|
|
||||||
@@ -11,14 +16,25 @@ fn main() -> ExitCode {
|
|||||||
return ExitCode::FAILURE;
|
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();
|
let ui_weak = ui.as_weak();
|
||||||
show(&ui, load());
|
let loads = Arc::new(AtomicU64::new(0));
|
||||||
ui.on_refresh(move || {
|
let toasts = Arc::new(AtomicU64::new(0));
|
||||||
let Some(ui) = ui_weak.upgrade() else {
|
reload(ui_weak.clone(), Arc::clone(&loads), Arc::clone(&toasts), false);
|
||||||
return;
|
|
||||||
};
|
let weak = ui_weak.clone();
|
||||||
show(&ui, load());
|
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() {
|
if let Err(err) = ui.run() {
|
||||||
eprintln!("{err}");
|
eprintln!("{err}");
|
||||||
return ExitCode::FAILURE;
|
return ExitCode::FAILURE;
|
||||||
@@ -26,16 +42,75 @@ fn main() -> ExitCode {
|
|||||||
ExitCode::SUCCESS
|
ExitCode::SUCCESS
|
||||||
}
|
}
|
||||||
|
|
||||||
fn load() -> punchlog::Result<punchlog::WeekReport> {
|
fn reload(ui_weak: slint::Weak<MainWindow>, generation: Arc<AtomicU64>, toasts: Arc<AtomicU64>, announce: bool) {
|
||||||
let mut config = punchlog::AppConfig::load()?;
|
let Some(ui) = ui_weak.upgrade() else {
|
||||||
punchlog::load_last_week(&mut config)
|
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());
|
||||||
|
|
||||||
fn show(ui: &MainWindow, loaded: punchlog::Result<punchlog::WeekReport>) {
|
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 {
|
match loaded {
|
||||||
Ok(report) => {
|
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_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;
|
||||||
|
}
|
||||||
|
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());
|
ui.set_error(String::new().into());
|
||||||
ui.set_range(format!("{} — {}", report.start, report.end).into());
|
|
||||||
let rows: Vec<DayRow> = report
|
let rows: Vec<DayRow> = report
|
||||||
.days
|
.days
|
||||||
.iter()
|
.iter()
|
||||||
@@ -55,9 +130,15 @@ fn show(ui: &MainWindow, loaded: punchlog::Result<punchlog::WeekReport>) {
|
|||||||
.collect();
|
.collect();
|
||||||
ui.set_days(Rc::new(slint::VecModel::from(rows)).into());
|
ui.set_days(Rc::new(slint::VecModel::from(rows)).into());
|
||||||
}
|
}
|
||||||
Err(err) => {
|
|
||||||
ui.set_error(err.to_string().into());
|
fn to_slint(day: NaiveDate) -> Date {
|
||||||
ui.set_days(Rc::new(slint::VecModel::<DayRow>::default()).into());
|
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
@@ -17,4 +17,4 @@ pub use error::{Error, Result};
|
|||||||
pub use iepms::IepmsClient;
|
pub use iepms::IepmsClient;
|
||||||
pub use phone::{read_phone, save_phone, PhoneIdentity};
|
pub use phone::{read_phone, save_phone, PhoneIdentity};
|
||||||
pub use record::PunchRecord;
|
pub use record::PunchRecord;
|
||||||
pub use report::{load_last_week, WeekReport};
|
pub use report::{load_last_week, load_range, WeekReport};
|
||||||
|
|||||||
@@ -17,6 +17,12 @@ pub struct WeekReport {
|
|||||||
/// 读取最近一周。token 失效时用账号密码重新登录,并尽量把新 token 写回 `.env`。
|
/// 读取最近一周。token 失效时用账号密码重新登录,并尽量把新 token 写回 `.env`。
|
||||||
pub fn load_last_week(config: &mut AppConfig) -> Result<WeekReport> {
|
pub fn load_last_week(config: &mut AppConfig) -> Result<WeekReport> {
|
||||||
let (start, end) = last_week(Local::now().date_naive());
|
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) {
|
let rows = match fetch(config, start, end) {
|
||||||
Err(Error::AuthExpired) => {
|
Err(Error::AuthExpired) => {
|
||||||
let session = auth::login(config)?;
|
let session = auth::login(config)?;
|
||||||
|
|||||||
+155
-44
@@ -1,4 +1,4 @@
|
|||||||
import { ScrollView } from "std-widgets.slint";
|
import { ScrollView, DatePickerPopup, Date } from "std-widgets.slint";
|
||||||
|
|
||||||
export struct DayRow {
|
export struct DayRow {
|
||||||
date: string,
|
date: string,
|
||||||
@@ -11,54 +11,115 @@ export struct DayRow {
|
|||||||
problem: bool,
|
problem: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
component DayBlock inherits Rectangle {
|
component DateField inherits Rectangle {
|
||||||
in property <DayRow> day;
|
in-out property <Date> date;
|
||||||
height: 132px;
|
in property <string> title;
|
||||||
|
callback accepted();
|
||||||
|
|
||||||
|
height: 28px;
|
||||||
|
border-width: 1px;
|
||||||
|
border-color: #d5d5d0;
|
||||||
background: #f7f7f5;
|
background: #f7f7f5;
|
||||||
|
|
||||||
|
function two(n: int) -> string {
|
||||||
|
if (n < 10) {
|
||||||
|
return "0" + n;
|
||||||
|
}
|
||||||
|
return "" + n;
|
||||||
|
}
|
||||||
|
|
||||||
Text {
|
Text {
|
||||||
|
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: center;
|
||||||
|
vertical-alignment: center;
|
||||||
|
}
|
||||||
|
|
||||||
|
picker := DatePickerPopup {
|
||||||
|
title: root.title;
|
||||||
|
date: root.date;
|
||||||
|
accepted(date) => {
|
||||||
|
root.date = date;
|
||||||
|
root.accepted();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
x: 24px;
|
||||||
y: 14px;
|
y: 12px;
|
||||||
width: 56px;
|
width: parent.width - 48px;
|
||||||
|
height: 22px;
|
||||||
|
spacing: 8px;
|
||||||
|
alignment: start;
|
||||||
|
|
||||||
|
Text {
|
||||||
text: day.date;
|
text: day.date;
|
||||||
font-size: 15px;
|
font-size: 15px;
|
||||||
font-weight: 600;
|
font-weight: 600;
|
||||||
color: #1a1a1a;
|
color: #1a1a1a;
|
||||||
horizontal-alignment: left;
|
vertical-alignment: center;
|
||||||
}
|
}
|
||||||
|
|
||||||
Text {
|
Text {
|
||||||
x: 88px;
|
|
||||||
y: 16px;
|
|
||||||
width: 120px;
|
|
||||||
text: day.time;
|
text: day.time;
|
||||||
font-size: 13px;
|
font-size: 13px;
|
||||||
color: #6a6a6a;
|
color: #6a6a6a;
|
||||||
horizontal-alignment: left;
|
vertical-alignment: center;
|
||||||
}
|
}
|
||||||
|
|
||||||
Text {
|
Text {
|
||||||
x: 216px;
|
|
||||||
y: 16px;
|
|
||||||
width: 52px;
|
|
||||||
text: day.hours;
|
text: day.hours;
|
||||||
font-size: 13px;
|
font-size: 13px;
|
||||||
color: #6a6a6a;
|
color: #6a6a6a;
|
||||||
horizontal-alignment: left;
|
vertical-alignment: center;
|
||||||
}
|
}
|
||||||
|
|
||||||
if day.mark != "": Text {
|
if day.mark != "": Text {
|
||||||
x: 276px;
|
|
||||||
y: 16px;
|
|
||||||
text: day.mark;
|
text: day.mark;
|
||||||
font-size: 13px;
|
font-size: 13px;
|
||||||
color: #6a6a6a;
|
color: #6a6a6a;
|
||||||
horizontal-alignment: left;
|
vertical-alignment: center;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Rectangle {
|
||||||
|
horizontal-stretch: 1;
|
||||||
|
min-width: 24px;
|
||||||
|
clip: true;
|
||||||
|
|
||||||
Text {
|
Text {
|
||||||
|
width: parent.width;
|
||||||
|
height: parent.height;
|
||||||
|
text: day.site;
|
||||||
|
font-size: 12px;
|
||||||
|
color: #8a8a8a;
|
||||||
|
horizontal-alignment: right;
|
||||||
|
vertical-alignment: center;
|
||||||
|
overflow: elide;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
status-text := Text {
|
||||||
x: parent.width - 24px - self.width;
|
x: parent.width - 24px - self.width;
|
||||||
y: 16px;
|
y: parent.height - 16px - self.height;
|
||||||
text: day.status;
|
text: day.status;
|
||||||
font-size: 13px;
|
font-size: 13px;
|
||||||
color: day.problem ? #1a1a1a : #6a6a6a;
|
color: day.problem ? #1a1a1a : #6a6a6a;
|
||||||
@@ -68,20 +129,8 @@ component DayBlock inherits Rectangle {
|
|||||||
Text {
|
Text {
|
||||||
x: 24px;
|
x: 24px;
|
||||||
y: 42px;
|
y: 42px;
|
||||||
width: parent.width - 48px;
|
width: max(0px, status-text.x - 24px - 16px);
|
||||||
height: 18px;
|
height: parent.height - 42px - 14px;
|
||||||
text: day.site;
|
|
||||||
font-size: 12px;
|
|
||||||
color: #8a8a8a;
|
|
||||||
horizontal-alignment: left;
|
|
||||||
overflow: elide;
|
|
||||||
}
|
|
||||||
|
|
||||||
Text {
|
|
||||||
x: 24px;
|
|
||||||
y: 64px;
|
|
||||||
width: parent.width - 48px;
|
|
||||||
height: 52px;
|
|
||||||
text: day.memo;
|
text: day.memo;
|
||||||
font-size: 14px;
|
font-size: 14px;
|
||||||
color: #1a1a1a;
|
color: #1a1a1a;
|
||||||
@@ -101,9 +150,13 @@ component DayBlock inherits Rectangle {
|
|||||||
|
|
||||||
export component MainWindow inherits Window {
|
export component MainWindow inherits Window {
|
||||||
in property <[DayRow]> days;
|
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;
|
in property <string> error;
|
||||||
callback refresh();
|
callback refresh();
|
||||||
|
callback range-changed();
|
||||||
|
|
||||||
title: "打卡";
|
title: "打卡";
|
||||||
preferred-width: 880px;
|
preferred-width: 880px;
|
||||||
@@ -121,27 +174,45 @@ export component MainWindow inherits Window {
|
|||||||
horizontal-alignment: left;
|
horizontal-alignment: left;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
DateField {
|
||||||
|
x: 76px;
|
||||||
|
y: 12px;
|
||||||
|
width: 108px;
|
||||||
|
date <=> root.start-date;
|
||||||
|
title: "开始日期";
|
||||||
|
accepted => { root.range-changed(); }
|
||||||
|
}
|
||||||
|
|
||||||
Text {
|
Text {
|
||||||
x: 72px;
|
x: 190px;
|
||||||
y: 18px;
|
y: 16px;
|
||||||
text: root.range;
|
text: "—";
|
||||||
font-size: 13px;
|
font-size: 13px;
|
||||||
color: #8a8a8a;
|
color: #8a8a8a;
|
||||||
horizontal-alignment: left;
|
horizontal-alignment: left;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
DateField {
|
||||||
|
x: 210px;
|
||||||
|
y: 12px;
|
||||||
|
width: 108px;
|
||||||
|
date <=> root.end-date;
|
||||||
|
title: "结束日期";
|
||||||
|
accepted => { root.range-changed(); }
|
||||||
|
}
|
||||||
|
|
||||||
TouchArea {
|
TouchArea {
|
||||||
x: parent.width - 72px;
|
x: parent.width - 28px - 80px;
|
||||||
y: 0px;
|
y: 0px;
|
||||||
width: 72px;
|
width: 80px;
|
||||||
height: 52px;
|
height: 52px;
|
||||||
|
enabled: !root.loading;
|
||||||
Text {
|
Text {
|
||||||
x: 0px;
|
|
||||||
y: 18px;
|
y: 18px;
|
||||||
width: parent.width;
|
width: parent.width;
|
||||||
text: "刷新";
|
text: root.loading ? "正在刷新" : "刷新";
|
||||||
font-size: 13px;
|
font-size: 13px;
|
||||||
color: #1a1a1a;
|
color: root.loading ? #8a8a8a : #1a1a1a;
|
||||||
horizontal-alignment: right;
|
horizontal-alignment: right;
|
||||||
}
|
}
|
||||||
clicked => { root.refresh(); }
|
clicked => { root.refresh(); }
|
||||||
@@ -166,10 +237,10 @@ export component MainWindow inherits Window {
|
|||||||
horizontal-alignment: left;
|
horizontal-alignment: left;
|
||||||
}
|
}
|
||||||
|
|
||||||
if root.error == "" && root.days.length == 0: Text {
|
if !root.loading && root.error == "" && root.days.length == 0: Text {
|
||||||
x: 24px;
|
x: 24px;
|
||||||
y: 68px;
|
y: 68px;
|
||||||
text: "这一周没有打卡记录。";
|
text: "这个范围没有打卡记录。";
|
||||||
font-size: 14px;
|
font-size: 14px;
|
||||||
color: #8a8a8a;
|
color: #8a8a8a;
|
||||||
horizontal-alignment: left;
|
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 = ""; }
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user