feat: add information and update chunk loading

This commit is contained in:
PandaDEV 2024-12-16 23:38:35 +10:00
parent c48a0d8239
commit 149e72802c
No known key found for this signature in database
GPG key ID: 13EFF9BAF70EE75C
7 changed files with 435 additions and 151 deletions

View file

@ -28,7 +28,7 @@ pub async fn initialize_history(pool: &SqlitePool) -> Result<(), Box<dyn std::er
#[tauri::command]
pub async fn get_history(pool: tauri::State<'_, SqlitePool>) -> Result<Vec<HistoryItem>, String> {
let rows = sqlx::query(
"SELECT id, source, source_icon, content_type, content, favicon, timestamp FROM history ORDER BY timestamp DESC",
"SELECT id, source, source_icon, content_type, content, favicon, timestamp, language FROM history ORDER BY timestamp DESC",
)
.fetch_all(&*pool)
.await
@ -44,6 +44,7 @@ pub async fn get_history(pool: tauri::State<'_, SqlitePool>) -> Result<Vec<Histo
content: row.get("content"),
favicon: row.get("favicon"),
timestamp: row.get("timestamp"),
language: row.get("language"),
})
.collect();
@ -55,7 +56,8 @@ pub async fn add_history_item(
pool: tauri::State<'_, SqlitePool>,
item: HistoryItem,
) -> Result<(), String> {
let (id, source, source_icon, content_type, content, favicon, timestamp) = item.to_row();
let (id, source, source_icon, content_type, content, favicon, timestamp, language) =
item.to_row();
let last_content: Option<String> = sqlx::query_scalar(
"SELECT content FROM history WHERE content_type = ? ORDER BY timestamp DESC LIMIT 1",
@ -70,7 +72,7 @@ pub async fn add_history_item(
}
sqlx::query(
"INSERT INTO history (id, source, source_icon, content_type, content, favicon, timestamp) VALUES (?, ?, ?, ?, ?, ?, ?)"
"INSERT INTO history (id, source, source_icon, content_type, content, favicon, timestamp, language) VALUES (?, ?, ?, ?, ?, ?, ?, ?)"
)
.bind(id)
.bind(source)
@ -79,6 +81,7 @@ pub async fn add_history_item(
.bind(content)
.bind(favicon)
.bind(timestamp)
.bind(language)
.execute(&*pool)
.await
.map_err(|e| e.to_string())?;
@ -93,7 +96,7 @@ pub async fn search_history(
) -> Result<Vec<HistoryItem>, String> {
let query = format!("%{}%", query);
let rows = sqlx::query(
"SELECT id, source, source_icon, content_type, content, favicon, timestamp FROM history WHERE content LIKE ? ORDER BY timestamp DESC"
"SELECT id, source, source_icon, content_type, content, favicon, timestamp, language FROM history WHERE content LIKE ? ORDER BY timestamp DESC"
)
.bind(query)
.fetch_all(&*pool)
@ -110,6 +113,7 @@ pub async fn search_history(
content: row.get("content"),
favicon: row.get("favicon"),
timestamp: row.get("timestamp"),
language: row.get("language"),
})
.collect();
@ -123,7 +127,7 @@ pub async fn load_history_chunk(
limit: i64,
) -> Result<Vec<HistoryItem>, String> {
let rows = sqlx::query(
"SELECT id, source, source_icon, content_type, content, favicon, timestamp FROM history ORDER BY timestamp DESC LIMIT ? OFFSET ?"
"SELECT id, source, source_icon, content_type, content, favicon, timestamp, language FROM history ORDER BY timestamp DESC LIMIT ? OFFSET ?"
)
.bind(limit)
.bind(offset)
@ -141,6 +145,7 @@ pub async fn load_history_chunk(
content: row.get("content"),
favicon: row.get("favicon"),
timestamp: row.get("timestamp"),
language: row.get("language"),
})
.collect();

View file

@ -1,2 +1,3 @@
ALTER TABLE history ADD COLUMN source TEXT DEFAULT 'System' NOT NULL;
ALTER TABLE history ADD COLUMN source_icon TEXT;
ALTER TABLE history ADD COLUMN language TEXT;

View file

@ -1,3 +1,6 @@
use active_win_pos_rs::get_active_window;
use base64::{engine::general_purpose::STANDARD, Engine};
use image::codecs::png::PngEncoder;
use tauri::PhysicalPosition;
pub fn center_window_on_current_monitor(window: &tauri::WebviewWindow) {
@ -27,4 +30,22 @@ pub fn center_window_on_current_monitor(window: &tauri::WebviewWindow) {
))
.unwrap();
}
}
}
pub fn get_app_info() -> (String, Option<String>) {
match get_active_window() {
Ok(window) => {
let app_name = window.app_name;
(app_name, None)
}
Err(_) => ("System".to_string(), None),
}
}
fn _process_icon_to_base64(path: &str) -> Result<String, Box<dyn std::error::Error>> {
let img = image::open(path)?;
let resized = img.resize(128, 128, image::imageops::FilterType::Lanczos3);
let mut png_buffer = Vec::new();
resized.write_with_encoder(PngEncoder::new(&mut png_buffer))?;
Ok(STANDARD.encode(png_buffer))
}

View file

@ -1,5 +1,5 @@
use serde::{Deserialize, Serialize};
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use std::fmt;
use uuid::Uuid;
@ -12,6 +12,7 @@ pub struct HistoryItem {
pub content: String,
pub favicon: Option<String>,
pub timestamp: DateTime<Utc>,
pub language: Option<String>,
}
#[derive(Debug, Deserialize, Serialize, Clone, PartialEq)]
@ -108,7 +109,14 @@ impl From<String> for ContentType {
}
impl HistoryItem {
pub fn new(source: String, content_type: ContentType, content: String, favicon: Option<String>, source_icon: Option<String>) -> Self {
pub fn new(
source: String,
content_type: ContentType,
content: String,
favicon: Option<String>,
source_icon: Option<String>,
language: Option<String>,
) -> Self {
Self {
id: Uuid::new_v4().to_string(),
source,
@ -117,10 +125,22 @@ impl HistoryItem {
content,
favicon,
timestamp: Utc::now(),
language,
}
}
pub fn to_row(&self) -> (String, String, Option<String>, String, String, Option<String>, DateTime<Utc>) {
pub fn to_row(
&self,
) -> (
String,
String,
Option<String>,
String,
String,
Option<String>,
DateTime<Utc>,
Option<String>,
) {
(
self.id.clone(),
self.source.clone(),
@ -129,6 +149,7 @@ impl HistoryItem {
self.content.clone(),
self.favicon.clone(),
self.timestamp,
self.language.clone(),
)
}
}