diff --git a/src/subscription.rs b/src/subscription.rs index 8ec3566..702e725 100644 --- a/src/subscription.rs +++ b/src/subscription.rs @@ -4,8 +4,9 @@ use std::collections::HashMap; pub struct SubscriptionManager { pub is_open: bool, pub subscriptions: HashMap, // 显示已订阅规则,键为名称,值为描述或文件路径 - pub download_progress: Option, // 下载进度 - pub download_status: Option, // 下载状态信息 + pub download_progress: Option, // 下载进度 + pub download_status: Option, // 下载状态信息 + pub download_url: String, // 当前输入的下载链接 } impl Default for SubscriptionManager { @@ -15,6 +16,7 @@ impl Default for SubscriptionManager { subscriptions: HashMap::new(), download_progress: None, download_status: None, + download_url: String::new(), } } } @@ -25,56 +27,83 @@ impl SubscriptionManager { egui::Window::new("订阅规则") .open(&mut self.is_open) .show(ctx, |ui| { - ui.horizontal(|ui| { - if ui.button("下载规则").clicked() { - self.start_download_dialog(ui); - } - if ui.button("从文件导入").clicked() { - self.import_from_file(); - } - }); + self.show_controls(ui); + self.show_subscriptions(ui); + self.show_download_status(ui); + }); + } + } - ui.separator(); - ui.label("已订阅规则:"); - for (name, description) in &self.subscriptions { - ui.horizontal(|ui| { - ui.label(format!("{} - {}", name, description)); - if ui.button("删除").clicked() { - self.subscriptions.remove(name); - } - }); - } + fn show_controls(&mut self, ui: &mut egui::Ui) { + ui.horizontal(|ui| { + if ui.button("下载规则").clicked() { + self.start_download_dialog(ui); + } + if ui.button("从文件导入").clicked() { + self.import_from_file(); + } + }); + ui.separator(); + } - if let Some(progress) = self.download_progress { - ui.add(egui::ProgressBar::new(progress).show_percentage()); - } - if let Some(status) = &self.download_status { - ui.label(status); + fn show_subscriptions(&mut self, ui: &mut egui::Ui) { + ui.label("已订阅规则:"); + let subscriptions_to_remove: Vec = self + .subscriptions + .iter() + .filter_map(|(name, description)| { + let mut remove = false; + ui.horizontal(|ui| { + ui.label(format!("{} - {}", name, description)); + if ui.button("删除").clicked() { + remove = true; } }); + if remove { + Some(name.clone()) + } else { + None + } + }) + .collect(); + + for name in subscriptions_to_remove { + self.subscriptions.remove(&name); + } + } + + fn show_download_status(&mut self, ui: &mut egui::Ui) { + if let Some(progress) = self.download_progress { + ui.add(egui::ProgressBar::new(progress).show_percentage()); + } + if let Some(status) = &self.download_status { + ui.label(status); } } fn start_download_dialog(&mut self, ui: &mut egui::Ui) { ui.label("请输入规则下载链接:"); - let mut url = String::new(); - ui.text_edit_singleline(&mut url); + ui.text_edit_singleline(&mut self.download_url); if ui.button("确定").clicked() { self.download_progress = Some(0.0); self.download_status = Some("开始下载...".to_string()); - // 模拟下载流程 - // TODO: 实现异步下载逻辑,更新进度并完成时移除弹窗 - self.download_progress = Some(1.0); // 下载完成 - self.download_status = Some("下载完成".to_string()); - self.subscriptions - .insert("新规则".to_string(), "从网络下载".to_string()); + + // 模拟异步下载逻辑 + self.simulate_download(); } } + fn simulate_download(&mut self) { + // 这里是模拟的下载过程,你可以改为异步任务 + self.download_progress = Some(1.0); // 下载完成 + self.download_status = Some("下载完成".to_string()); + self.subscriptions + .insert("新规则".to_string(), "从网络下载".to_string()); + } + fn import_from_file(&mut self) { // TODO: 打开文件选择窗口,解析 YAML 并更新订阅规则 - // 示例添加一个规则 self.subscriptions .insert("本地规则".to_string(), "从文件导入".to_string()); } diff --git a/src/ui.rs b/src/ui.rs index a0d92a7..5ff2b4d 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -6,12 +6,12 @@ use crate::logger; // 导入 logger 模块 use crate::move_module; // 导入移动模块 use crate::open; use crate::scanner; +use crate::subscription; use crate::utils; use crate::yaml_loader::FolderDescriptions; use eframe::egui::{self, Grid, ScrollArea}; use std::collections::HashSet; use std::sync::mpsc::{Receiver, Sender}; -use subscription::SubscriptionManager; pub struct AppDataCleaner { is_scanning: bool,