From e5811fcbd085dc74df2c32c40222c92a5e504e95 Mon Sep 17 00:00:00 2001 From: Borber Date: Sat, 23 Sep 2023 14:58:53 +0800 Subject: [PATCH] =?UTF-8?q?[mod]=20=E6=B7=BB=E5=8A=A0=E5=88=B7=E6=96=B0?= =?UTF-8?q?=E6=9C=8D=E5=8A=A1,=20=E5=B0=9A=E4=B8=8D=E5=8F=AF=E7=94=A8,=20?= =?UTF-8?q?=E8=BF=98=E9=9C=80=E8=A6=81=E5=A4=A7=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crates/gui/src-tauri/src/main.rs | 7 ++- crates/gui/src-tauri/src/manager/mod.rs | 17 +----- crates/gui/src-tauri/src/manager/refresh.rs | 67 +++++++++++++++++++++ crates/gui/src-tauri/src/setup.rs | 8 +++ 4 files changed, 81 insertions(+), 18 deletions(-) create mode 100644 crates/gui/src-tauri/src/manager/refresh.rs create mode 100644 crates/gui/src-tauri/src/setup.rs diff --git a/crates/gui/src-tauri/src/main.rs b/crates/gui/src-tauri/src/main.rs index 46e4157..43ce6b1 100644 --- a/crates/gui/src-tauri/src/main.rs +++ b/crates/gui/src-tauri/src/main.rs @@ -3,7 +3,7 @@ use common::CONTEXT; use resp::Resp; use seam_core::{error::SeamError, live::Node}; -use tauri::Manager; +use tauri::{App, Manager}; use window_shadows::set_shadow; mod common; @@ -13,6 +13,7 @@ mod manager; mod model; mod resp; mod service; +mod setup; mod util; #[tauri::command] @@ -51,10 +52,12 @@ async fn play(url: String) -> Resp { util::play(&url).into() } + + #[tokio::main] async fn main() { CONTEXT.get_or_init(common::load).await; - + tauri::Builder::default() .setup(|app| { if cfg!(any(target_os = "macos", target_os = "windows")) { diff --git a/crates/gui/src-tauri/src/manager/mod.rs b/crates/gui/src-tauri/src/manager/mod.rs index e4a403c..1d2e0f5 100644 --- a/crates/gui/src-tauri/src/manager/mod.rs +++ b/crates/gui/src-tauri/src/manager/mod.rs @@ -1,16 +1 @@ -// TODO 更新直播 -// 1. 获取所有订阅主播 -// 2. 将同平台的主播分组 -// 3. 每次取不同平台的一个主播获取直播源 -// 4. 将获取到的数据通过事件通知前端 -// - 开播 -// - 存在卡片, 不做动作 -// - 不存在卡片, 新增直播卡片 -// - 未开播 -// - 存在卡片, 删除 -// - 不存在卡片, 不做动作 - -// TODO 定时更新直播 - -// TODO 手动更新 -// 1. 全部获取完毕发送更新完毕通知给前端 +pub mod refresh; diff --git a/crates/gui/src-tauri/src/manager/refresh.rs b/crates/gui/src-tauri/src/manager/refresh.rs new file mode 100644 index 0000000..9707561 --- /dev/null +++ b/crates/gui/src-tauri/src/manager/refresh.rs @@ -0,0 +1,67 @@ +use crate::{clients, config::headers, service}; + +use std::collections::HashMap; + +use anyhow::Result; +use tauri::{App, Manager}; + +// TODO 声明返回类型, 指明所属平台 +// 刷新单个订阅 +pub async fn refresh(app: &App, live: String, rid: String) -> Result<()> { + let clients = clients!(); + let node = clients + .get(&live) + .unwrap() + .get(&rid, Some(headers(&live))) + .await?; + + app.emit_all("refresh", node)?; + Ok(()) +} + +/// 刷新所有订阅的直播源 +pub async fn refresh_all(app: &App) -> Result<()> { + let lives = service::subscribe::all().await?; + let mut lists = HashMap::new(); + for live in lives { + let entry = lists.entry(live.live).or_insert_with(Vec::new); + entry.push(live.rid); + } + + loop { + if lists.is_empty() { + // TODO 发送更新完毕事件 + break; + } + + let once = lists + .iter_mut() + .map(|(live, rids)| rids.pop().map(|rid| (live.clone(), rid))) + .collect::>(); + + for (live, rid) in once.into_iter().flatten() { + refresh(app, live, rid).await?; + } + + // 去除所需获取主播为空的平台 + lists.retain(|_, rids| !rids.is_empty()); + + // 等待间隔 + tokio::time::sleep(std::time::Duration::from_millis(300)).await; + } + Ok(()) +} + +// TODO 手动更新 +// 1. 全部获取完毕发送更新完毕通知给前端 +// - 开播 +// - 存在卡片, 不做动作 +// - 不存在卡片, 新增直播卡片 +// - 未开播 +// - 存在卡片, 删除 +// - 不存在卡片, 不做动作 + +// TODO 定时更新直播 +// 界面启动时,调用后端命令,然后获取App句柄,随后进行循环命令 + +// TODO 设置增加 每次自动刷新的间隔时间 diff --git a/crates/gui/src-tauri/src/setup.rs b/crates/gui/src-tauri/src/setup.rs new file mode 100644 index 0000000..910b044 --- /dev/null +++ b/crates/gui/src-tauri/src/setup.rs @@ -0,0 +1,8 @@ +use std::error::Error; + +use tauri::{App, Manager}; + +pub fn handler(app: &mut App) -> Result<(), Box> { + + Ok(()) +}