friends list UI
This commit is contained in:
@@ -1,7 +1,8 @@
|
||||
use crate::db::{self, AppDatabase};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use specta::Type;
|
||||
use tauri::State;
|
||||
use tauri::{AppHandle, State};
|
||||
use tauri_specta::Event;
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, Type, sqlx::FromRow)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
@@ -10,9 +11,32 @@ pub struct Friend {
|
||||
pub display_name: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, Type, Event)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct FriendsChanged {
|
||||
pub friends: Vec<Friend>,
|
||||
}
|
||||
|
||||
async fn all(database: &AppDatabase) -> Result<Vec<Friend>, sqlx::Error> {
|
||||
sqlx::query_as::<_, Friend>(
|
||||
"SELECT id, display_name FROM friends ORDER BY display_name COLLATE NOCASE, id",
|
||||
)
|
||||
.fetch_all(database.pool())
|
||||
.await
|
||||
}
|
||||
|
||||
async fn emit_changed(handle: &AppHandle, database: &AppDatabase) -> Result<(), String> {
|
||||
FriendsChanged {
|
||||
friends: all(database).await.map_err(db::command_error)?,
|
||||
}
|
||||
.emit(handle)
|
||||
.map_err(db::command_error)
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
#[specta::specta]
|
||||
pub async fn create_friend(
|
||||
handle: AppHandle,
|
||||
database: State<'_, AppDatabase>,
|
||||
friend: Friend,
|
||||
) -> Result<Friend, String> {
|
||||
@@ -23,18 +47,26 @@ pub async fn create_friend(
|
||||
.await
|
||||
.map_err(db::command_error)?;
|
||||
|
||||
emit_changed(&handle, &database).await?;
|
||||
|
||||
Ok(friend)
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
#[specta::specta]
|
||||
pub async fn list_friends(database: State<'_, AppDatabase>) -> Result<Vec<Friend>, String> {
|
||||
sqlx::query_as::<_, Friend>(
|
||||
"SELECT id, display_name FROM friends ORDER BY display_name COLLATE NOCASE, id",
|
||||
)
|
||||
.fetch_all(database.pool())
|
||||
.await
|
||||
.map_err(db::command_error)
|
||||
pub async fn list_friends(
|
||||
handle: AppHandle,
|
||||
database: State<'_, AppDatabase>,
|
||||
) -> Result<Vec<Friend>, String> {
|
||||
let friends = all(&database).await.map_err(db::command_error)?;
|
||||
|
||||
FriendsChanged {
|
||||
friends: friends.clone(),
|
||||
}
|
||||
.emit(&handle)
|
||||
.map_err(db::command_error)?;
|
||||
|
||||
Ok(friends)
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
@@ -52,12 +84,22 @@ pub async fn get_friend(
|
||||
|
||||
#[tauri::command]
|
||||
#[specta::specta]
|
||||
pub async fn delete_friend(database: State<'_, AppDatabase>, id: String) -> Result<bool, String> {
|
||||
pub async fn delete_friend(
|
||||
handle: AppHandle,
|
||||
database: State<'_, AppDatabase>,
|
||||
id: String,
|
||||
) -> Result<bool, String> {
|
||||
let result = sqlx::query("DELETE FROM friends WHERE id = ?1")
|
||||
.bind(id)
|
||||
.execute(database.pool())
|
||||
.await
|
||||
.map_err(db::command_error)?;
|
||||
|
||||
Ok(result.rows_affected() > 0)
|
||||
let changed = result.rows_affected() > 0;
|
||||
|
||||
if changed {
|
||||
emit_changed(&handle, &database).await?;
|
||||
}
|
||||
|
||||
Ok(changed)
|
||||
}
|
||||
|
||||
@@ -23,7 +23,9 @@ pub fn run() {
|
||||
crate::friends::get_friend,
|
||||
crate::friends::delete_friend
|
||||
])
|
||||
.events(tauri_specta::collect_events![]);
|
||||
.events(tauri_specta::collect_events![
|
||||
crate::friends::FriendsChanged
|
||||
]);
|
||||
|
||||
#[cfg(debug_assertions)]
|
||||
specta_builder
|
||||
@@ -36,7 +38,8 @@ pub fn run() {
|
||||
tauri::Builder::default()
|
||||
.plugin(tauri_plugin_opener::init())
|
||||
.invoke_handler(specta_builder.invoke_handler())
|
||||
.setup(|app| {
|
||||
.setup(move |app| {
|
||||
specta_builder.mount_events(app);
|
||||
tauri::async_runtime::block_on(launch_app(app))?;
|
||||
Ok(())
|
||||
})
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
use tauri::{AppHandle, Manager, WebviewUrl};
|
||||
|
||||
const WINDOW_LABEL: &str = "main";
|
||||
|
||||
pub fn init(handle: &AppHandle) {
|
||||
if let Some(window) = handle.get_webview_window(WINDOW_LABEL) {
|
||||
if !window.is_visible().unwrap() {
|
||||
window.center().unwrap();
|
||||
window.show().unwrap();
|
||||
};
|
||||
window.set_focus().unwrap();
|
||||
return;
|
||||
};
|
||||
|
||||
let builder =
|
||||
tauri::WebviewWindowBuilder::new(handle, WINDOW_LABEL, WebviewUrl::App("/".into()))
|
||||
.title("Wyd")
|
||||
.inner_size(800.0, 600.0)
|
||||
.min_inner_size(500.0, 400.0)
|
||||
.visible(true);
|
||||
|
||||
let window = builder.build().unwrap();
|
||||
let window_c = window.clone();
|
||||
window.on_window_event(move |event: &tauri::WindowEvent| {
|
||||
if let tauri::WindowEvent::CloseRequested { api, .. } = event {
|
||||
api.prevent_close();
|
||||
window_c.hide().unwrap();
|
||||
}
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user