refactor to use traits

This commit is contained in:
llama 2025-10-24 19:31:56 +08:00
parent 316e176d64
commit dfe77c8a8a
No known key found for this signature in database
GPG key ID: 0B7543854B9413C3

View file

@ -21,13 +21,12 @@ use anyhow::Result;
use strum::IntoEnumIterator;
use tauri::AppHandle;
use tauri::Emitter;
use tauri::Manager;
use tauri::Runtime;
use tauri::WebviewWindow;
use tauri_plugin_os::locale;
use crate::lang::get_tr;
use crate::lang::setup_i18n;
use crate::app::StateExt;
use crate::lang::I18nExt;
use crate::lang::LANGS;
use crate::lang::LANGS_DEFAULT_REGION;
use crate::lang::LANGS_WITH_REGIONS;
@ -41,7 +40,7 @@ pub async fn i18n_resources<R: Runtime>(
_window: WebviewWindow<R>,
input: I18nResourcesRequest,
) -> Result<generic::Json> {
let tr = get_tr(&app)?;
let tr = app.tr()?;
serde_json::to_vec(&tr.resources_for_js(&input.modules))
.with_context(|| "failed to serialise i18n resources")
.map(Into::into)
@ -87,7 +86,7 @@ pub async fn set_lang<R: Runtime>(
} else {
input.split('-').next().unwrap().to_owned()
};
setup_i18n(&app, &[&*input]);
app.setup_tr(&[&*input]);
Ok(())
}
@ -95,8 +94,7 @@ pub async fn get_state<R: Runtime>(
app: AppHandle<R>,
_window: WebviewWindow<R>,
) -> Result<StateProto> {
let state = app.state::<State>();
let kind = match &*state {
let kind = match app.flow() {
State::LaunchAnki(_) => unreachable!(),
State::OsUnsupported(e) => StateProtoKind::OsUnsupported(format!("{e:?}")),
State::UnknownError(e) => StateProtoKind::UnknownError(format!("{e:?}")),
@ -112,7 +110,7 @@ pub async fn get_mirrors<R: Runtime>(
app: AppHandle<R>,
_window: WebviewWindow<R>,
) -> Result<GetMirrorsResponse> {
let tr = get_tr(&app)?;
let tr = app.tr()?;
Ok(GetMirrorsResponse {
mirrors: Mirror::iter()
.map(|mirror| get_mirrors_response::Pair {
@ -131,7 +129,7 @@ pub async fn get_options<R: Runtime>(
app: AppHandle<R>,
_window: WebviewWindow<R>,
) -> Result<Options> {
let state = app.state::<State>();
let state = app.flow();
let options = (&state.normal()?.initial_options).into();
Ok(options)
}
@ -140,8 +138,7 @@ pub async fn get_available_versions<R: Runtime>(
app: AppHandle<R>,
_window: WebviewWindow<R>,
) -> Result<Versions> {
let state = app.state::<State>();
let state = state.normal()?;
let state = app.flow().normal()?;
let mut rx = state.available_versions.clone().unwrap();
rx.changed().await.unwrap();
let x = rx.borrow();
@ -156,8 +153,7 @@ pub async fn get_existing_versions<R: Runtime>(
app: AppHandle<R>,
_window: WebviewWindow<R>,
) -> Result<ExistingVersions> {
let state = app.state::<State>();
let state = state.normal()?;
let state = app.flow().normal()?;
let mut rx = state.current_versions.clone().unwrap();
rx.changed().await.unwrap();
let x = rx.borrow();
@ -172,8 +168,7 @@ pub async fn choose_version<R: Runtime>(
_window: WebviewWindow<R>,
input: ChooseVersionRequest,
) -> Result<ChooseVersionResponse> {
let state = app.state::<State>();
let state = state.normal()?;
let state = app.flow().normal()?;
let paths = state.paths.clone();
tauri::async_runtime::spawn_blocking(move || {
@ -210,7 +205,7 @@ pub async fn choose_version<R: Runtime>(
}
pub async fn launch_anki<R: Runtime>(app: AppHandle<R>, _window: WebviewWindow<R>) -> Result<()> {
app.state::<State>().paths().and_then(uv::launch_anki)
app.flow().paths().and_then(uv::launch_anki)
}
pub async fn exit<R: Runtime>(app: AppHandle<R>, window: WebviewWindow<R>) -> Result<()> {