work on saving continuing
This commit is contained in:
parent
75cd9752a4
commit
d08de3d2b4
3 changed files with 58 additions and 13 deletions
|
|
@ -55,6 +55,14 @@ pub fn get_basepath(app_handle: tauri::AppHandle) -> Option<String> {
|
||||||
return base_path.map(|p| p.to_string_lossy().into_owned());
|
return base_path.map(|p| p.to_string_lossy().into_owned());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn _get_client_id(app_handle: &tauri::AppHandle) -> String {
|
||||||
|
let config = app_handle.state::<Mutex<Config>>();
|
||||||
|
|
||||||
|
// Lock the mutex to get mutable access:
|
||||||
|
let config = config.lock().unwrap();
|
||||||
|
let client_id = config.core_cfg.client_id.clone();
|
||||||
|
return client_id;
|
||||||
|
}
|
||||||
#[tauri::command]
|
#[tauri::command]
|
||||||
pub fn set_basepath(app_handle: tauri::AppHandle, path: String) -> bool {
|
pub fn set_basepath(app_handle: tauri::AppHandle, path: String) -> bool {
|
||||||
if let Ok(resolved_path) = shellexpand::full(&path) {
|
if let Ok(resolved_path) = shellexpand::full(&path) {
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,7 @@ use std::{
|
||||||
str::FromStr,
|
str::FromStr,
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::get_basepath;
|
use crate::{config::_get_client_id, get_basepath};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use sqlx::{migrate::MigrateDatabase, Sqlite, SqlitePool};
|
use sqlx::{migrate::MigrateDatabase, Sqlite, SqlitePool};
|
||||||
use tauri::{ipc::RuntimeCapability, App, AssetResolver, Manager, Url};
|
use tauri::{ipc::RuntimeCapability, App, AssetResolver, Manager, Url};
|
||||||
|
|
@ -75,7 +75,7 @@ pub async fn initialize_database(app_handle: tauri::AppHandle) -> Result<(), DBE
|
||||||
id INTEGER NOT NULL,
|
id INTEGER NOT NULL,
|
||||||
client_id TEXT NOT NULL,
|
client_id TEXT NOT NULL,
|
||||||
diff_text TEXT NOT NULL,
|
diff_text TEXT NOT NULL,
|
||||||
working_diff BOOLEAN,
|
current_diff BOOLEAN,
|
||||||
FOREIGN KEY (id)
|
FOREIGN KEY (id)
|
||||||
REFERENCES file_diffs (parent_diff_id)
|
REFERENCES file_diffs (parent_diff_id)
|
||||||
FOREIGN KEY (id)
|
FOREIGN KEY (id)
|
||||||
|
|
@ -171,33 +171,68 @@ async fn get_last_file_diff(
|
||||||
Ok(parent_diff_id)
|
Ok(parent_diff_id)
|
||||||
}
|
}
|
||||||
#[tauri::command]
|
#[tauri::command]
|
||||||
async fn store_diff(
|
pub async fn store_diff(
|
||||||
app_handle: &tauri::AppHandle,
|
app_handle: &tauri::AppHandle,
|
||||||
relative_file_path: &str,
|
relative_file_path: &str,
|
||||||
file_name: &str,
|
file_name: &str,
|
||||||
diff: String,
|
diff: String,
|
||||||
) -> Result<(), DBError> {
|
) -> Result<(), DBError> {
|
||||||
let file_id = get_file_id(app_handle, relative_file_path, file_name)
|
|
||||||
.await
|
|
||||||
.map_err(|e| {
|
|
||||||
DBError::DatabaseQueryError("Got file diff for non existant file".to_string())
|
|
||||||
});
|
|
||||||
|
|
||||||
let unicode_db_path = get_database_path(&app_handle)?;
|
let unicode_db_path = get_database_path(&app_handle)?;
|
||||||
|
let client_id = _get_client_id(&app_handle);
|
||||||
let db = SqlitePool::connect(&unicode_db_path)
|
let db = SqlitePool::connect(&unicode_db_path)
|
||||||
.await
|
.await
|
||||||
.map_err(|e| DBError::DatabaseConnectionError(e.to_string()))?;
|
.map_err(|e| DBError::DatabaseConnectionError(e.to_string()))?;
|
||||||
|
|
||||||
|
// 1 insert into file_diffs with parent
|
||||||
|
// 2 make last diff not currennt diff
|
||||||
|
//
|
||||||
|
// id INTEGER NOT NULL,
|
||||||
|
// client_id TEXT NOT NULL,
|
||||||
|
// diff_text TEXT NOT NULL,
|
||||||
|
// current_diff BOOLEAN,
|
||||||
|
// FOREIGN KEY (id)
|
||||||
|
// REFERENCES file_diffs (parent_diff_id)
|
||||||
|
// FOREIGN KEY (id)
|
||||||
|
// REFERENCES files (file_id)
|
||||||
|
// PRIMARY KEY (id, client_id)
|
||||||
let _ = sqlx::query(&format!(
|
let _ = sqlx::query(&format!(
|
||||||
"
|
"
|
||||||
INSERT INTO
|
UPDATE file_diffs
|
||||||
file_diffs (relative_path, file_name)
|
SET current_diff = 0
|
||||||
VALUES
|
WHERE relative_path IS '{relative_file_path}'
|
||||||
('{relative_file_path}', '{file_name}');"
|
AND file_name IS '{file_name}'
|
||||||
|
AND working_diff is 1
|
||||||
|
"
|
||||||
))
|
))
|
||||||
.execute(&db)
|
.execute(&db)
|
||||||
.await
|
.await
|
||||||
.map_err(|e| DBError::DatabaseQueryError(e.to_string()))?;
|
.map_err(|e| DBError::DatabaseQueryError(e.to_string()))?;
|
||||||
|
let _ = sqlx::query(&format!(
|
||||||
|
"
|
||||||
|
INSERT INTO
|
||||||
|
file_diffs (client_id, diff_text, current_diff, file_diffs, file_id)
|
||||||
|
VALUES
|
||||||
|
('{client_id}', '{diff}', 1, (
|
||||||
|
SELECT id
|
||||||
|
FROM file_diffs
|
||||||
|
WHERE relative_path IS '{relative_file_path}'
|
||||||
|
AND file_name IS '{file_name}'
|
||||||
|
AND current_diff is 1
|
||||||
|
LIMIT 1
|
||||||
|
),
|
||||||
|
(
|
||||||
|
SELECT id
|
||||||
|
FROM files
|
||||||
|
WHERE relative_path IS '{relative_file_path}'
|
||||||
|
AND file_name IS '{file_name}'
|
||||||
|
LIMIT 1
|
||||||
|
));"
|
||||||
|
))
|
||||||
|
.execute(&db)
|
||||||
|
.await
|
||||||
|
.map_err(|e| DBError::DatabaseQueryError(e.to_string()))?;
|
||||||
|
|
||||||
db.close().await;
|
db.close().await;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
use crate::{
|
use crate::{
|
||||||
config::{get_open_file_path, set_open_file_path},
|
config::{get_open_file_path, set_open_file_path},
|
||||||
|
database::store_diff,
|
||||||
get_basepath,
|
get_basepath,
|
||||||
};
|
};
|
||||||
use diff_match_patch_rs::{Compat, DiffMatchPatch, Error, PatchInput};
|
use diff_match_patch_rs::{Compat, DiffMatchPatch, Error, PatchInput};
|
||||||
|
|
@ -24,6 +25,7 @@ fn compare_content(old_content: &str, new_content: &str) -> Result<String, Error
|
||||||
pub async fn save_file(app_handle: tauri::AppHandle, content: String) {
|
pub async fn save_file(app_handle: tauri::AppHandle, content: String) {
|
||||||
if let Some(path) = get_open_file_path(app_handle.clone()) {
|
if let Some(path) = get_open_file_path(app_handle.clone()) {
|
||||||
if let Ok(file_content) = load_file(app_handle, path).await {
|
if let Ok(file_content) = load_file(app_handle, path).await {
|
||||||
|
store_diff(&app_handle, path, file_name, diff);
|
||||||
println!("{:?}", compare_content(&file_content, &content));
|
println!("{:?}", compare_content(&file_content, &content));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue