Skip to content

Commit

Permalink
Merge branch 'new_gui'
Browse files Browse the repository at this point in the history
  • Loading branch information
Borber committed Sep 23, 2023
2 parents e9f3fb3 + 58f8207 commit ced3e50
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 23 deletions.
1 change: 1 addition & 0 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- 给 Client 添加函数返回其函数调用信息, 命令, 平台名称
2 changes: 1 addition & 1 deletion crates/gui/src-tauri/src/database/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ pub mod subscribe;
use anyhow::Result;
use sea_orm::*;

// TODO 初始化创建表
/// 初始化数据库
pub async fn init(db: &DatabaseConnection) -> Result<ExecResult, DbErr> {
let backend = DbBackend::Sqlite;
let schema = Schema::new(backend);
Expand Down
21 changes: 17 additions & 4 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::{AppHandle, 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 All @@ -31,21 +32,31 @@ async fn url(live: String, rid: String) -> Resp<Node> {
}
}

#[tauri::command]
async fn all_subscribe() -> Resp<Vec<database::subscribe::Model>> {
service::subscribe::all().await.into()
}

#[tauri::command]
async fn add_subscribe(live: String, rid: String) -> Resp<bool> {
service::subscribe::add(live, rid).await.into()
}

#[tauri::command]
async fn all_subscribe() -> Resp<Vec<database::subscribe::Model>> {
service::subscribe::all().await.into()
async fn remove_subscribe(live: String, rid: String) -> Resp<bool> {
service::subscribe::remove(live, rid).await.into()
}

#[tauri::command]
async fn play(url: String) -> Resp<bool> {
util::play(&url).into()
}

#[tauri::command]
async fn refresh(app: AppHandle, live: String, rid: String) -> Resp<()> {
manager::refresh::refresh(&app, live, rid).await.into()
}

#[tokio::main]
async fn main() {
CONTEXT.get_or_init(common::load).await;
Expand All @@ -60,9 +71,11 @@ async fn main() {
})
.invoke_handler(tauri::generate_handler![
url,
add_subscribe,
all_subscribe,
add_subscribe,
remove_subscribe,
play,
refresh,
])
.run(tauri::generate_context!())
.expect("error while running tauri application");
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;
74 changes: 74 additions & 0 deletions crates/gui/src-tauri/src/manager/refresh.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
use crate::{clients, config::headers, service};

use std::collections::HashMap;

use anyhow::Result;
use seam_core::live::Node;
use serde::Serialize;
use tauri::{AppHandle, Manager};

#[derive(Clone, Debug, Serialize)]
pub struct ReFreshMessage {
pub live: String,
pub node: Node,
}

// TODO 声明返回类型, 指明所属平台
// 刷新单个订阅
pub async fn refresh(app: &AppHandle, live: String, rid: String) -> Result<()> {
let clients = clients!();
let node = clients
.get(&live)
.unwrap()
.get(&rid, Some(headers(&live)))
.await?;

app.emit_all("refresh", ReFreshMessage { live, node })?;
Ok(())
}

/// 刷新所有订阅的直播源
pub async fn refresh_all(app: &AppHandle) -> 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() {
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 设置增加 每次自动刷新的间隔时间
22 changes: 20 additions & 2 deletions crates/gui/src-tauri/src/service/subscribe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,16 @@ use sea_orm::*;

use crate::{database::subscribe, pool};

/// 获取所有订阅
///
/// Get all subscribe
pub async fn all() -> Result<Vec<subscribe::Model>> {
Ok(subscribe::Entity::find().all(pool!()).await?)
}

/// 添加订阅
///
/// Add subscribe
pub async fn add(live: String, rid: String) -> Result<bool> {
subscribe::Entity::insert(subscribe::ActiveModel {
live: Set(live),
Expand All @@ -13,6 +23,14 @@ pub async fn add(live: String, rid: String) -> Result<bool> {
Ok(true)
}

pub async fn all() -> Result<Vec<subscribe::Model>> {
Ok(subscribe::Entity::find().all(pool!()).await?)
/// 删除订阅
///
/// Delete subscribe
pub async fn remove(live: String, rid: String) -> Result<bool> {
subscribe::Entity::delete_many()
.filter(subscribe::Column::Live.eq(live))
.filter(subscribe::Column::Rid.eq(rid))
.exec(pool!())
.await?;
Ok(true)
}
7 changes: 7 additions & 0 deletions crates/gui/src-tauri/src/setup.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
use std::error::Error;

use tauri::{App, Manager};

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

0 comments on commit ced3e50

Please sign in to comment.