From e71ee47e50423ff1adbb5121a1396ab9ae5ea7d5 Mon Sep 17 00:00:00 2001 From: trivernis Date: Sun, 12 Dec 2021 13:50:46 +0100 Subject: [PATCH] Add api to get and set the frontend state Signed-off-by: trivernis --- mediarepo-api/Cargo.toml | 2 +- mediarepo-api/src/client_api/mod.rs | 5 +++ mediarepo-api/src/client_api/repo.rs | 41 +++++++++++++++++++ .../src/tauri_plugin/commands/repo.rs | 17 ++++++++ mediarepo-api/src/tauri_plugin/mod.rs | 4 +- mediarepo-api/src/types/mod.rs | 3 +- mediarepo-api/src/types/repo.rs | 6 +++ 7 files changed, 75 insertions(+), 3 deletions(-) create mode 100644 mediarepo-api/src/client_api/repo.rs create mode 100644 mediarepo-api/src/types/repo.rs diff --git a/mediarepo-api/Cargo.toml b/mediarepo-api/Cargo.toml index 44221e3..3a7cb92 100644 --- a/mediarepo-api/Cargo.toml +++ b/mediarepo-api/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "mediarepo-api" -version = "0.14.0" +version = "0.15.0" edition = "2018" license = "gpl-3" diff --git a/mediarepo-api/src/client_api/mod.rs b/mediarepo-api/src/client_api/mod.rs index 43b577b..a8c499c 100644 --- a/mediarepo-api/src/client_api/mod.rs +++ b/mediarepo-api/src/client_api/mod.rs @@ -1,10 +1,12 @@ pub mod error; pub mod file; pub mod protocol; +pub mod repo; pub mod tag; use crate::client_api::error::{ApiError, ApiResult}; use crate::client_api::file::FileApi; +use crate::client_api::repo::RepoApi; use crate::client_api::tag::TagApi; use crate::types::misc::{check_apis_compatible, get_api_version, InfoResponse}; use async_trait::async_trait; @@ -43,6 +45,7 @@ pub struct ApiClient { ctx: PooledContext, pub file: FileApi, pub tag: TagApi, + pub repo: RepoApi, } impl Clone for ApiClient { @@ -51,6 +54,7 @@ impl Clone for ApiClient { ctx: self.ctx.clone(), file: self.file.clone(), tag: self.tag.clone(), + repo: self.repo.clone(), } } } @@ -61,6 +65,7 @@ impl ApiClient { Self { file: FileApi::new(ctx.clone()), tag: TagApi::new(ctx.clone()), + repo: RepoApi::new(ctx.clone()), ctx, } } diff --git a/mediarepo-api/src/client_api/repo.rs b/mediarepo-api/src/client_api/repo.rs new file mode 100644 index 0000000..03a0812 --- /dev/null +++ b/mediarepo-api/src/client_api/repo.rs @@ -0,0 +1,41 @@ +use crate::client_api::error::ApiResult; +use crate::client_api::IPCApi; +use crate::types::repo::FrontendState; +use bromine::prelude::*; + +#[derive(Clone)] +pub struct RepoApi { + ctx: PooledContext, +} + +impl IPCApi for RepoApi { + fn namespace() -> &'static str { + "repo" + } + + fn ctx(&self) -> PoolGuard { + self.ctx.acquire() + } +} + +impl RepoApi { + pub fn new(ctx: PooledContext) -> Self { + Self { ctx } + } + + /// Returns the state of the frontend that is stored in the repo + #[tracing::instrument(level = "debug", skip(self))] + pub async fn get_frontend_state(&self) -> ApiResult { + let state = self.emit_and_get("frontend_state", ()).await?; + + Ok(state) + } + + /// Sets the state of the frontend + #[tracing::instrument(level = "debug", skip(self))] + pub async fn set_frontend_state(&self, state: FrontendState) -> ApiResult<()> { + self.emit("set_frontend_state", state).await?; + + Ok(()) + } +} diff --git a/mediarepo-api/src/tauri_plugin/commands/repo.rs b/mediarepo-api/src/tauri_plugin/commands/repo.rs index d54af95..742a639 100644 --- a/mediarepo-api/src/tauri_plugin/commands/repo.rs +++ b/mediarepo-api/src/tauri_plugin/commands/repo.rs @@ -3,6 +3,7 @@ use crate::client_api::ApiClient; use crate::tauri_plugin::commands::{ApiAccess, AppAccess}; use crate::tauri_plugin::error::{PluginError, PluginResult}; use crate::tauri_plugin::settings::{save_settings, Repository}; +use crate::types::repo::FrontendState; use serde::{Deserialize, Serialize}; use std::mem; use std::path::PathBuf; @@ -183,6 +184,22 @@ pub async fn select_repository( Ok(()) } +#[tauri::command] +pub async fn get_frontend_state(api_state: ApiAccess<'_>) -> PluginResult { + let api = api_state.api().await?; + let state = api.repo.get_frontend_state().await?; + + Ok(state.state) +} + +#[tauri::command] +pub async fn set_frontend_state(api_state: ApiAccess<'_>, state: String) -> PluginResult<()> { + let api = api_state.api().await?; + api.repo.set_frontend_state(FrontendState { state }).await?; + + Ok(()) +} + async fn get_repo_address(path: String) -> PluginResult { let tcp_path = PathBuf::from(&path).join("repo.tcp"); let socket_path = PathBuf::from(&path).join("repo.sock"); diff --git a/mediarepo-api/src/tauri_plugin/mod.rs b/mediarepo-api/src/tauri_plugin/mod.rs index b19f587..3254f3a 100644 --- a/mediarepo-api/src/tauri_plugin/mod.rs +++ b/mediarepo-api/src/tauri_plugin/mod.rs @@ -59,7 +59,9 @@ impl MediarepoPlugin { delete_thumbnails, read_file, delete_repository, - has_executable + has_executable, + get_frontend_state, + set_frontend_state ]), } } diff --git a/mediarepo-api/src/types/mod.rs b/mediarepo-api/src/types/mod.rs index 2a5ed7e..11a3de9 100644 --- a/mediarepo-api/src/types/mod.rs +++ b/mediarepo-api/src/types/mod.rs @@ -1,4 +1,5 @@ pub mod files; pub mod identifier; pub mod misc; -pub mod tags; \ No newline at end of file +pub mod repo; +pub mod tags; diff --git a/mediarepo-api/src/types/repo.rs b/mediarepo-api/src/types/repo.rs new file mode 100644 index 0000000..cba1d48 --- /dev/null +++ b/mediarepo-api/src/types/repo.rs @@ -0,0 +1,6 @@ +use serde::{Deserialize, Serialize}; + +#[derive(Serialize, Deserialize, Clone, Debug)] +pub struct FrontendState { + pub state: String, +}