Skip to content

Commit

Permalink
[mod] 添加刷新服务, 尚不可用, 还需要大改
Browse files Browse the repository at this point in the history
  • Loading branch information
Borber committed Sep 23, 2023
1 parent 9d38597 commit e5811fc
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 18 deletions.
7 changes: 5 additions & 2 deletions crates/gui/src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -13,6 +13,7 @@ mod manager;
mod model;
mod resp;
mod service;
mod setup;
mod util;

#[tauri::command]
Expand Down Expand Up @@ -51,10 +52,12 @@ async fn play(url: String) -> Resp<bool> {
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")) {
Expand Down
17 changes: 1 addition & 16 deletions crates/gui/src-tauri/src/manager/mod.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1 @@
// TODO 更新直播
// 1. 获取所有订阅主播
// 2. 将同平台的主播分组
// 3. 每次取不同平台的一个主播获取直播源
// 4. 将获取到的数据通过事件通知前端
// - 开播
// - 存在卡片, 不做动作
// - 不存在卡片, 新增直播卡片
// - 未开播
// - 存在卡片, 删除
// - 不存在卡片, 不做动作

// TODO 定时更新直播

// TODO 手动更新
// 1. 全部获取完毕发送更新完毕通知给前端
pub mod refresh;
67 changes: 67 additions & 0 deletions crates/gui/src-tauri/src/manager/refresh.rs
Original file line number Diff line number Diff line change
@@ -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::<Vec<_>>();

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 设置增加 每次自动刷新的间隔时间
8 changes: 8 additions & 0 deletions crates/gui/src-tauri/src/setup.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use std::error::Error;

use tauri::{App, Manager};

pub fn handler(app: &mut App) -> Result<(), Box<dyn Error>> {

Ok(())
}

0 comments on commit e5811fc

Please sign in to comment.