Add api to get and set the frontend state

Signed-off-by: trivernis <trivernis@protonmail.com>
pull/4/head
trivernis 3 years ago
parent aa568a9365
commit e71ee47e50

@ -1,6 +1,6 @@
[package] [package]
name = "mediarepo-api" name = "mediarepo-api"
version = "0.14.0" version = "0.15.0"
edition = "2018" edition = "2018"
license = "gpl-3" license = "gpl-3"

@ -1,10 +1,12 @@
pub mod error; pub mod error;
pub mod file; pub mod file;
pub mod protocol; pub mod protocol;
pub mod repo;
pub mod tag; pub mod tag;
use crate::client_api::error::{ApiError, ApiResult}; use crate::client_api::error::{ApiError, ApiResult};
use crate::client_api::file::FileApi; use crate::client_api::file::FileApi;
use crate::client_api::repo::RepoApi;
use crate::client_api::tag::TagApi; use crate::client_api::tag::TagApi;
use crate::types::misc::{check_apis_compatible, get_api_version, InfoResponse}; use crate::types::misc::{check_apis_compatible, get_api_version, InfoResponse};
use async_trait::async_trait; use async_trait::async_trait;
@ -43,6 +45,7 @@ pub struct ApiClient {
ctx: PooledContext, ctx: PooledContext,
pub file: FileApi, pub file: FileApi,
pub tag: TagApi, pub tag: TagApi,
pub repo: RepoApi,
} }
impl Clone for ApiClient { impl Clone for ApiClient {
@ -51,6 +54,7 @@ impl Clone for ApiClient {
ctx: self.ctx.clone(), ctx: self.ctx.clone(),
file: self.file.clone(), file: self.file.clone(),
tag: self.tag.clone(), tag: self.tag.clone(),
repo: self.repo.clone(),
} }
} }
} }
@ -61,6 +65,7 @@ impl ApiClient {
Self { Self {
file: FileApi::new(ctx.clone()), file: FileApi::new(ctx.clone()),
tag: TagApi::new(ctx.clone()), tag: TagApi::new(ctx.clone()),
repo: RepoApi::new(ctx.clone()),
ctx, ctx,
} }
} }

@ -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<Context> {
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<FrontendState> {
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(())
}
}

@ -3,6 +3,7 @@ use crate::client_api::ApiClient;
use crate::tauri_plugin::commands::{ApiAccess, AppAccess}; use crate::tauri_plugin::commands::{ApiAccess, AppAccess};
use crate::tauri_plugin::error::{PluginError, PluginResult}; use crate::tauri_plugin::error::{PluginError, PluginResult};
use crate::tauri_plugin::settings::{save_settings, Repository}; use crate::tauri_plugin::settings::{save_settings, Repository};
use crate::types::repo::FrontendState;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::mem; use std::mem;
use std::path::PathBuf; use std::path::PathBuf;
@ -183,6 +184,22 @@ pub async fn select_repository(
Ok(()) Ok(())
} }
#[tauri::command]
pub async fn get_frontend_state(api_state: ApiAccess<'_>) -> PluginResult<String> {
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<String> { async fn get_repo_address(path: String) -> PluginResult<String> {
let tcp_path = PathBuf::from(&path).join("repo.tcp"); let tcp_path = PathBuf::from(&path).join("repo.tcp");
let socket_path = PathBuf::from(&path).join("repo.sock"); let socket_path = PathBuf::from(&path).join("repo.sock");

@ -59,7 +59,9 @@ impl<R: Runtime> MediarepoPlugin<R> {
delete_thumbnails, delete_thumbnails,
read_file, read_file,
delete_repository, delete_repository,
has_executable has_executable,
get_frontend_state,
set_frontend_state
]), ]),
} }
} }

@ -1,4 +1,5 @@
pub mod files; pub mod files;
pub mod identifier; pub mod identifier;
pub mod misc; pub mod misc;
pub mod repo;
pub mod tags; pub mod tags;

@ -0,0 +1,6 @@
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct FrontendState {
pub state: String,
}
Loading…
Cancel
Save