feat(download manager): update db state with ui and emit events

This commit is contained in:
DecDuck
2024-11-28 20:31:04 +11:00
parent 64d7f649c6
commit e4df4eb2d7
8 changed files with 144 additions and 48 deletions

View File

@ -6,8 +6,7 @@
<script setup lang="ts">
import { invoke } from "@tauri-apps/api/core";
// @ts-expect-error
import { AppStatus, type AppState } from "./types.d.ts";
import { AppStatus } from "~/types";
import { listen } from "@tauri-apps/api/event";
import { useAppState } from "./composables/app-state.js";
import { useRouter } from "#vue-router";

View File

@ -0,0 +1,50 @@
<template>
<button
type="button"
:class="[
styles[props.status],
'inline-flex uppercase font-display items-center gap-x-2 rounded-md px-4 py-3 text-md font-semibold shadow-sm focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2',
]"
>
<component :is="buttonIcons[props.status]" class="-mr-0.5 size-5" aria-hidden="true" />
{{ buttonNames[props.status] }}
</button>
</template>
<script setup lang="ts">
import { ArrowDownTrayIcon, PlayIcon, QueueListIcon, TrashIcon } from "@heroicons/vue/20/solid";
import type { Component } from "vue";
import { GameStatus } from "~/types.js";
const props = defineProps<{ status: GameStatus }>();
const styles: { [key in GameStatus]: string } = {
[GameStatus.Remote]:
"bg-blue-600 text-white hover:bg-blue-500 focus-visible:outline-blue-600",
[GameStatus.Queued]:
"bg-zinc-800 text-white hover:bg-zinc-700 focus-visible:outline-zinc-700",
[GameStatus.Downloading]: "bg-zinc-800 text-white hover:bg-zinc-700 focus-visible:outline-zinc-700",
[GameStatus.Installed]:
"bg-green-600 text-white hover:bg-green-500 focus-visible:outline-green-600",
[GameStatus.Updating]: "",
[GameStatus.Uninstalling]: "",
};
const buttonNames: { [key in GameStatus]: string } = {
[GameStatus.Remote]: "Install",
[GameStatus.Queued]: "Queued",
[GameStatus.Downloading]: "Downloading",
[GameStatus.Installed]: "Play",
[GameStatus.Updating]: "Updating",
[GameStatus.Uninstalling]: "Uninstalling",
};
const buttonIcons: {[key in GameStatus]: Component} = {
[GameStatus.Remote]: ArrowDownTrayIcon,
[GameStatus.Queued]: QueueListIcon,
[GameStatus.Downloading]: ArrowDownTrayIcon,
[GameStatus.Installed]: PlayIcon,
[GameStatus.Updating]: ArrowDownTrayIcon,
[GameStatus.Uninstalling]: TrashIcon
}
</script>

View File

@ -18,7 +18,7 @@
<div class="w-full min-h-screen mx-auto bg-zinc-900 px-5 py-6">
<!-- game toolbar -->
<div>
<GameButton v-model="status" />
<GameStatusButton :status="status" />
</div>
</div>
</div>
@ -27,15 +27,22 @@
<script setup lang="ts">
import type { Game } from "@prisma/client";
import { invoke } from "@tauri-apps/api/core";
import { listen } from "@tauri-apps/api/event";
import type { GameStatus } from "~/types";
const route = useRoute();
const id = route.params.id;
const raw: { game: Game; status: any } = JSON.parse(
const raw: { game: Game; status: GameStatus } = JSON.parse(
await invoke<string>("fetch_game", { id: id })
);
const game = ref(raw.game);
const status = ref(raw.status);
listen(`update_game/${game.value.id}`, (event) => {
const payload: { status: GameStatus } = event.payload as any;
status.value = payload.status;
});
const bannerUrl = await useObject(game.value.mBannerId);
</script>

View File

@ -25,6 +25,7 @@ pub struct DatabaseAuth {
#[derive(Serialize, Clone, Deserialize)]
pub enum DatabaseGameStatus {
Remote,
Queued,
Downloading,
Installed,
Updating,

View File

@ -8,6 +8,9 @@ use std::{
};
use log::{error, info, warn};
use tauri::{AppHandle, Emitter};
use crate::{db::DatabaseGameStatus, library::GameUpdateEvent, DB};
use super::{
download_agent::{GameDownloadAgent, GameDownloadError},
@ -64,13 +67,14 @@ pub struct DownloadManagerBuilder {
sender: Sender<DownloadManagerSignal>,
progress: Arc<Mutex<Option<ProgressObject>>>,
status: Arc<Mutex<DownloadManagerStatus>>,
app_handle: AppHandle,
current_game_interface: Option<Arc<AgentInterfaceData>>, // Should be the only game download agent in the map with the "Go" flag
active_control_flag: Option<DownloadThreadControl>,
}
impl DownloadManagerBuilder {
pub fn build() -> DownloadManager {
pub fn build(app_handle: AppHandle) -> DownloadManager {
let queue = Queue::new();
let (command_sender, command_receiver) = channel();
let active_progress = Arc::new(Mutex::new(None));
@ -85,6 +89,7 @@ impl DownloadManagerBuilder {
status: status.clone(),
sender: command_sender.clone(),
progress: active_progress.clone(),
app_handle,
};
let terminator = spawn(|| manager.manage_queue());
@ -92,6 +97,23 @@ impl DownloadManagerBuilder {
DownloadManager::new(terminator, queue, active_progress, command_sender)
}
fn set_game_status(&self, id: String, status: DatabaseGameStatus) {
let mut db_handle = DB.borrow_data_mut().unwrap();
db_handle
.games
.games_statuses
.insert(id.clone(), status.clone());
self.app_handle
.emit(
&format!("update_game/{}", id),
GameUpdateEvent {
game_id: id,
status: status,
},
)
.unwrap();
}
fn manage_queue(mut self) -> Result<(), ()> {
loop {
let signal = match self.command_receiver.recv() {
@ -145,6 +167,8 @@ impl DownloadManagerBuilder {
self.download_agent_registry.remove(&game_id);
self.active_control_flag = None;
*self.progress.lock().unwrap() = None;
self.set_game_status(game_id, DatabaseGameStatus::Installed);
}
}
self.sender.send(DownloadManagerSignal::Go).unwrap();
@ -160,54 +184,61 @@ impl DownloadManagerBuilder {
));
let agent_status = GameDownloadStatus::Uninitialised;
let interface_data = AgentInterfaceData {
id,
id: id.clone(),
status: Mutex::new(agent_status),
};
self.download_agent_registry
.insert(interface_data.id.clone(), download_agent);
self.download_queue.append(interface_data);
self.set_game_status(id, DatabaseGameStatus::Queued);
}
fn manage_go_signal(&mut self) {
info!("Got signal 'Go'");
if !self.download_agent_registry.is_empty() && !self.download_queue.empty() {
info!("Starting download agent");
let agent_data = self.download_queue.read().front().unwrap().clone();
let download_agent = self
.download_agent_registry
.get(&agent_data.id)
.unwrap()
.clone();
self.current_game_interface = Some(agent_data);
let progress_object = download_agent.progress.clone();
*self.progress.lock().unwrap() = Some(progress_object);
let active_control_flag = download_agent.control_flag.clone();
self.active_control_flag = Some(active_control_flag.clone());
let sender = self.sender.clone();
info!("Spawning download");
spawn(move || {
match download_agent.download() {
// Returns once we've exited the download
// (not necessarily completed)
// The download agent will fire the completed event for us
Ok(_) => {}
// If an error occurred while *starting* the download
Err(err) => {
error!("error while managing download: {}", err);
sender.send(DownloadManagerSignal::Error(err)).unwrap();
}
};
});
active_control_flag.set(DownloadThreadControlFlag::Go);
self.set_status(DownloadManagerStatus::Downloading);
} else {
info!("Nothing was set");
if !(!self.download_agent_registry.is_empty() && !self.download_queue.empty()) {
return;
}
info!("Starting download agent");
let agent_data = self.download_queue.read().front().unwrap().clone();
let download_agent = self
.download_agent_registry
.get(&agent_data.id)
.unwrap()
.clone();
self.current_game_interface = Some(agent_data);
let progress_object = download_agent.progress.clone();
*self.progress.lock().unwrap() = Some(progress_object);
let active_control_flag = download_agent.control_flag.clone();
self.active_control_flag = Some(active_control_flag.clone());
let sender = self.sender.clone();
info!("Spawning download");
spawn(move || {
match download_agent.download() {
// Returns once we've exited the download
// (not necessarily completed)
// The download agent will fire the completed event for us
Ok(_) => {}
// If an error occurred while *starting* the download
Err(err) => {
error!("error while managing download: {}", err);
sender.send(DownloadManagerSignal::Error(err)).unwrap();
}
};
});
active_control_flag.set(DownloadThreadControlFlag::Go);
self.set_status(DownloadManagerStatus::Downloading);
self.set_game_status(
self.current_game_interface.as_ref().unwrap().id.clone(),
DatabaseGameStatus::Downloading,
);
}
fn manage_error_signal(&self, error: GameDownloadError) {
let current_status = self.current_game_interface.clone().unwrap();

View File

@ -28,6 +28,7 @@ use std::{
collections::HashMap,
sync::{LazyLock, Mutex},
};
use tauri::{AppHandle, Manager};
use tauri_plugin_deep_link::DeepLinkExt;
#[derive(Clone, Copy, Serialize)]
@ -69,12 +70,12 @@ fn fetch_state(state: tauri::State<'_, Mutex<AppState>>) -> Result<AppState, Str
Ok(cloned_state)
}
fn setup() -> AppState {
fn setup(handle: AppHandle) -> AppState {
debug!("Starting env");
env_logger::Builder::from_env(Env::default().default_filter_or("info")).init();
let games = HashMap::new();
let download_manager = Arc::new(DownloadManagerBuilder::build());
let download_manager = Arc::new(DownloadManagerBuilder::build(handle));
debug!("Checking if database is set up");
let is_set_up = DB.database_is_set_up();
@ -102,9 +103,6 @@ pub static DB: LazyLock<DatabaseInterface> = LazyLock::new(DatabaseInterface::se
#[cfg_attr(mobile, tauri::mobile_entry_point)]
pub fn run() {
let state = setup();
info!("initialized drop client");
let mut builder = tauri::Builder::default().plugin(tauri_plugin_dialog::init());
#[cfg(desktop)]
@ -117,7 +115,6 @@ pub fn run() {
builder
.plugin(tauri_plugin_deep_link::init())
.manage(Mutex::new(state))
.invoke_handler(tauri::generate_handler![
// DB
fetch_state,
@ -144,6 +141,11 @@ pub fn run() {
.plugin(tauri_plugin_shell::init())
.plugin(tauri_plugin_dialog::init())
.setup(|app| {
let handle = app.handle().clone();
let state = setup(handle);
info!("initialized drop client");
app.manage(Mutex::new(state));
#[cfg(any(target_os = "linux", all(debug_assertions, windows)))]
{
use tauri_plugin_deep_link::DeepLinkExt;

View File

@ -33,6 +33,11 @@ pub struct Game {
m_cover_id: String,
m_image_library: Vec<String>,
}
#[derive(serde::Serialize, Clone)]
pub struct GameUpdateEvent {
pub game_id: String,
pub status: DatabaseGameStatus,
}
fn fetch_library_logic(app: AppHandle) -> Result<String, RemoteAccessError> {
let base_url = DB.fetch_base_url();

View File

@ -27,6 +27,7 @@ export enum AppStatus {
export enum GameStatus {
Remote = "Remote",
Queued = "Queued",
Downloading = "Downloading",
Installed = "Installed",
Updating = "Updating",