mirror of
https://github.com/Drop-OSS/drop-app.git
synced 2025-11-10 04:22:13 +10:00
feat(download manager): update db state with ui and emit events
This commit is contained in:
3
app.vue
3
app.vue
@ -6,8 +6,7 @@
|
|||||||
|
|
||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import { invoke } from "@tauri-apps/api/core";
|
import { invoke } from "@tauri-apps/api/core";
|
||||||
// @ts-expect-error
|
import { AppStatus } from "~/types";
|
||||||
import { AppStatus, type AppState } from "./types.d.ts";
|
|
||||||
import { listen } from "@tauri-apps/api/event";
|
import { listen } from "@tauri-apps/api/event";
|
||||||
import { useAppState } from "./composables/app-state.js";
|
import { useAppState } from "./composables/app-state.js";
|
||||||
import { useRouter } from "#vue-router";
|
import { useRouter } from "#vue-router";
|
||||||
|
|||||||
50
components/GameStatusButton.vue
Normal file
50
components/GameStatusButton.vue
Normal 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>
|
||||||
@ -18,7 +18,7 @@
|
|||||||
<div class="w-full min-h-screen mx-auto bg-zinc-900 px-5 py-6">
|
<div class="w-full min-h-screen mx-auto bg-zinc-900 px-5 py-6">
|
||||||
<!-- game toolbar -->
|
<!-- game toolbar -->
|
||||||
<div>
|
<div>
|
||||||
<GameButton v-model="status" />
|
<GameStatusButton :status="status" />
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
@ -27,15 +27,22 @@
|
|||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import type { Game } from "@prisma/client";
|
import type { Game } from "@prisma/client";
|
||||||
import { invoke } from "@tauri-apps/api/core";
|
import { invoke } from "@tauri-apps/api/core";
|
||||||
|
import { listen } from "@tauri-apps/api/event";
|
||||||
|
import type { GameStatus } from "~/types";
|
||||||
|
|
||||||
const route = useRoute();
|
const route = useRoute();
|
||||||
const id = route.params.id;
|
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 })
|
await invoke<string>("fetch_game", { id: id })
|
||||||
);
|
);
|
||||||
const game = ref(raw.game);
|
const game = ref(raw.game);
|
||||||
const status = ref(raw.status);
|
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);
|
const bannerUrl = await useObject(game.value.mBannerId);
|
||||||
</script>
|
</script>
|
||||||
|
|||||||
@ -25,6 +25,7 @@ pub struct DatabaseAuth {
|
|||||||
#[derive(Serialize, Clone, Deserialize)]
|
#[derive(Serialize, Clone, Deserialize)]
|
||||||
pub enum DatabaseGameStatus {
|
pub enum DatabaseGameStatus {
|
||||||
Remote,
|
Remote,
|
||||||
|
Queued,
|
||||||
Downloading,
|
Downloading,
|
||||||
Installed,
|
Installed,
|
||||||
Updating,
|
Updating,
|
||||||
|
|||||||
@ -8,6 +8,9 @@ use std::{
|
|||||||
};
|
};
|
||||||
|
|
||||||
use log::{error, info, warn};
|
use log::{error, info, warn};
|
||||||
|
use tauri::{AppHandle, Emitter};
|
||||||
|
|
||||||
|
use crate::{db::DatabaseGameStatus, library::GameUpdateEvent, DB};
|
||||||
|
|
||||||
use super::{
|
use super::{
|
||||||
download_agent::{GameDownloadAgent, GameDownloadError},
|
download_agent::{GameDownloadAgent, GameDownloadError},
|
||||||
@ -64,13 +67,14 @@ pub struct DownloadManagerBuilder {
|
|||||||
sender: Sender<DownloadManagerSignal>,
|
sender: Sender<DownloadManagerSignal>,
|
||||||
progress: Arc<Mutex<Option<ProgressObject>>>,
|
progress: Arc<Mutex<Option<ProgressObject>>>,
|
||||||
status: Arc<Mutex<DownloadManagerStatus>>,
|
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
|
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>,
|
active_control_flag: Option<DownloadThreadControl>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl DownloadManagerBuilder {
|
impl DownloadManagerBuilder {
|
||||||
pub fn build() -> DownloadManager {
|
pub fn build(app_handle: AppHandle) -> DownloadManager {
|
||||||
let queue = Queue::new();
|
let queue = Queue::new();
|
||||||
let (command_sender, command_receiver) = channel();
|
let (command_sender, command_receiver) = channel();
|
||||||
let active_progress = Arc::new(Mutex::new(None));
|
let active_progress = Arc::new(Mutex::new(None));
|
||||||
@ -85,6 +89,7 @@ impl DownloadManagerBuilder {
|
|||||||
status: status.clone(),
|
status: status.clone(),
|
||||||
sender: command_sender.clone(),
|
sender: command_sender.clone(),
|
||||||
progress: active_progress.clone(),
|
progress: active_progress.clone(),
|
||||||
|
app_handle,
|
||||||
};
|
};
|
||||||
|
|
||||||
let terminator = spawn(|| manager.manage_queue());
|
let terminator = spawn(|| manager.manage_queue());
|
||||||
@ -92,6 +97,23 @@ impl DownloadManagerBuilder {
|
|||||||
DownloadManager::new(terminator, queue, active_progress, command_sender)
|
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<(), ()> {
|
fn manage_queue(mut self) -> Result<(), ()> {
|
||||||
loop {
|
loop {
|
||||||
let signal = match self.command_receiver.recv() {
|
let signal = match self.command_receiver.recv() {
|
||||||
@ -145,6 +167,8 @@ impl DownloadManagerBuilder {
|
|||||||
self.download_agent_registry.remove(&game_id);
|
self.download_agent_registry.remove(&game_id);
|
||||||
self.active_control_flag = None;
|
self.active_control_flag = None;
|
||||||
*self.progress.lock().unwrap() = None;
|
*self.progress.lock().unwrap() = None;
|
||||||
|
|
||||||
|
self.set_game_status(game_id, DatabaseGameStatus::Installed);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
self.sender.send(DownloadManagerSignal::Go).unwrap();
|
self.sender.send(DownloadManagerSignal::Go).unwrap();
|
||||||
@ -160,54 +184,61 @@ impl DownloadManagerBuilder {
|
|||||||
));
|
));
|
||||||
let agent_status = GameDownloadStatus::Uninitialised;
|
let agent_status = GameDownloadStatus::Uninitialised;
|
||||||
let interface_data = AgentInterfaceData {
|
let interface_data = AgentInterfaceData {
|
||||||
id,
|
id: id.clone(),
|
||||||
status: Mutex::new(agent_status),
|
status: Mutex::new(agent_status),
|
||||||
};
|
};
|
||||||
self.download_agent_registry
|
self.download_agent_registry
|
||||||
.insert(interface_data.id.clone(), download_agent);
|
.insert(interface_data.id.clone(), download_agent);
|
||||||
self.download_queue.append(interface_data);
|
self.download_queue.append(interface_data);
|
||||||
|
|
||||||
|
self.set_game_status(id, DatabaseGameStatus::Queued);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn manage_go_signal(&mut self) {
|
fn manage_go_signal(&mut self) {
|
||||||
info!("Got signal 'Go'");
|
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();
|
if !(!self.download_agent_registry.is_empty() && !self.download_queue.empty()) {
|
||||||
*self.progress.lock().unwrap() = Some(progress_object);
|
return;
|
||||||
|
|
||||||
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");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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) {
|
fn manage_error_signal(&self, error: GameDownloadError) {
|
||||||
let current_status = self.current_game_interface.clone().unwrap();
|
let current_status = self.current_game_interface.clone().unwrap();
|
||||||
|
|||||||
@ -28,6 +28,7 @@ use std::{
|
|||||||
collections::HashMap,
|
collections::HashMap,
|
||||||
sync::{LazyLock, Mutex},
|
sync::{LazyLock, Mutex},
|
||||||
};
|
};
|
||||||
|
use tauri::{AppHandle, Manager};
|
||||||
use tauri_plugin_deep_link::DeepLinkExt;
|
use tauri_plugin_deep_link::DeepLinkExt;
|
||||||
|
|
||||||
#[derive(Clone, Copy, Serialize)]
|
#[derive(Clone, Copy, Serialize)]
|
||||||
@ -69,12 +70,12 @@ fn fetch_state(state: tauri::State<'_, Mutex<AppState>>) -> Result<AppState, Str
|
|||||||
Ok(cloned_state)
|
Ok(cloned_state)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn setup() -> AppState {
|
fn setup(handle: AppHandle) -> AppState {
|
||||||
debug!("Starting env");
|
debug!("Starting env");
|
||||||
env_logger::Builder::from_env(Env::default().default_filter_or("info")).init();
|
env_logger::Builder::from_env(Env::default().default_filter_or("info")).init();
|
||||||
|
|
||||||
let games = HashMap::new();
|
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");
|
debug!("Checking if database is set up");
|
||||||
let is_set_up = DB.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)]
|
#[cfg_attr(mobile, tauri::mobile_entry_point)]
|
||||||
pub fn run() {
|
pub fn run() {
|
||||||
let state = setup();
|
|
||||||
info!("initialized drop client");
|
|
||||||
|
|
||||||
let mut builder = tauri::Builder::default().plugin(tauri_plugin_dialog::init());
|
let mut builder = tauri::Builder::default().plugin(tauri_plugin_dialog::init());
|
||||||
|
|
||||||
#[cfg(desktop)]
|
#[cfg(desktop)]
|
||||||
@ -117,7 +115,6 @@ pub fn run() {
|
|||||||
|
|
||||||
builder
|
builder
|
||||||
.plugin(tauri_plugin_deep_link::init())
|
.plugin(tauri_plugin_deep_link::init())
|
||||||
.manage(Mutex::new(state))
|
|
||||||
.invoke_handler(tauri::generate_handler![
|
.invoke_handler(tauri::generate_handler![
|
||||||
// DB
|
// DB
|
||||||
fetch_state,
|
fetch_state,
|
||||||
@ -144,6 +141,11 @@ pub fn run() {
|
|||||||
.plugin(tauri_plugin_shell::init())
|
.plugin(tauri_plugin_shell::init())
|
||||||
.plugin(tauri_plugin_dialog::init())
|
.plugin(tauri_plugin_dialog::init())
|
||||||
.setup(|app| {
|
.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)))]
|
#[cfg(any(target_os = "linux", all(debug_assertions, windows)))]
|
||||||
{
|
{
|
||||||
use tauri_plugin_deep_link::DeepLinkExt;
|
use tauri_plugin_deep_link::DeepLinkExt;
|
||||||
|
|||||||
@ -33,6 +33,11 @@ pub struct Game {
|
|||||||
m_cover_id: String,
|
m_cover_id: String,
|
||||||
m_image_library: Vec<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> {
|
fn fetch_library_logic(app: AppHandle) -> Result<String, RemoteAccessError> {
|
||||||
let base_url = DB.fetch_base_url();
|
let base_url = DB.fetch_base_url();
|
||||||
|
|||||||
@ -27,6 +27,7 @@ export enum AppStatus {
|
|||||||
|
|
||||||
export enum GameStatus {
|
export enum GameStatus {
|
||||||
Remote = "Remote",
|
Remote = "Remote",
|
||||||
|
Queued = "Queued",
|
||||||
Downloading = "Downloading",
|
Downloading = "Downloading",
|
||||||
Installed = "Installed",
|
Installed = "Installed",
|
||||||
Updating = "Updating",
|
Updating = "Updating",
|
||||||
Reference in New Issue
Block a user