T-031: Lesezeichen mit Notiz — bookmarks.json, Panel in der Leiste (Sprung, Notiz editieren, delete)
This commit is contained in:
90
src-tauri/src/bookmarks.rs
Normal file
90
src-tauri/src/bookmarks.rs
Normal file
@ -0,0 +1,90 @@
|
||||
//! Lesezeichen mit Notiz je Buch (T-031), Ablage: bookmarks.json.
|
||||
|
||||
use std::collections::BTreeMap;
|
||||
use std::fs;
|
||||
use std::path::PathBuf;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::audible::books_dir;
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct Bookmark {
|
||||
pub time: f64,
|
||||
pub note: String,
|
||||
}
|
||||
|
||||
pub type Bookmarks = BTreeMap<String, Vec<Bookmark>>;
|
||||
|
||||
fn path() -> PathBuf {
|
||||
books_dir()
|
||||
.parent()
|
||||
.map(|p| p.join("bookmarks.json"))
|
||||
.unwrap_or_else(|| PathBuf::from("bookmarks.json"))
|
||||
}
|
||||
|
||||
pub fn load() -> Bookmarks {
|
||||
fs::read_to_string(path())
|
||||
.ok()
|
||||
.and_then(|t| serde_json::from_str(&t).ok())
|
||||
.unwrap_or_default()
|
||||
}
|
||||
|
||||
pub fn list(asin: &str) -> Result<Vec<Bookmark>, String> {
|
||||
Ok(load().remove(asin.trim()).unwrap_or_default())
|
||||
}
|
||||
|
||||
pub fn add(asin: &str, time: f64, note: &str) -> Result<Vec<Bookmark>, String> {
|
||||
let asin = asin.trim().to_string();
|
||||
let mut all = load();
|
||||
let list = all.entry(asin).or_default();
|
||||
list.push(Bookmark { time: time.max(0.0), note: note.trim().to_string() });
|
||||
list.sort_by(|a, b| a.time.partial_cmp(&b.time).unwrap_or(std::cmp::Ordering::Equal));
|
||||
let updated = list.clone();
|
||||
save(&all)?;
|
||||
Ok(updated)
|
||||
}
|
||||
|
||||
pub fn remove(asin: &str, index: usize) -> Result<Vec<Bookmark>, String> {
|
||||
let asin = asin.trim().to_string();
|
||||
let mut all = load();
|
||||
let Some(list) = all.get_mut(&asin) else {
|
||||
return Err("kein Lesezeichen gefunden".into());
|
||||
};
|
||||
if index >= list.len() {
|
||||
return Err("Index außerhalb".into());
|
||||
}
|
||||
list.remove(index);
|
||||
if list.is_empty() {
|
||||
all.remove(&asin);
|
||||
}
|
||||
let updated = all.get(&asin).cloned().unwrap_or_default();
|
||||
save(&all)?;
|
||||
Ok(updated)
|
||||
}
|
||||
|
||||
pub fn set_note(asin: &str, index: usize, note: &str) -> Result<Vec<Bookmark>, String> {
|
||||
let asin = asin.trim().to_string();
|
||||
let mut all = load();
|
||||
let Some(list) = all.get_mut(&asin) else {
|
||||
return Err("kein Lesezeichen gefunden".into());
|
||||
};
|
||||
if index >= list.len() {
|
||||
return Err("Index außerhalb".into());
|
||||
}
|
||||
list[index].note = note.trim().to_string();
|
||||
let updated = list.clone();
|
||||
save(&all)?;
|
||||
Ok(updated)
|
||||
}
|
||||
|
||||
fn save(all: &Bookmarks) -> Result<(), String> {
|
||||
if let Some(parent) = path().parent() {
|
||||
fs::create_dir_all(parent).map_err(|e| format!("Ordner: {e}"))?;
|
||||
}
|
||||
let p = path();
|
||||
let tmp = p.with_extension("json.tmp");
|
||||
fs::write(&tmp, serde_json::to_vec(all).map_err(|e| e.to_string())?)
|
||||
.map_err(|e| format!("Lesezeichen schreiben: {e}"))?;
|
||||
fs::rename(&tmp, &p).map_err(|e| format!("Lesezeichen speichern: {e}"))
|
||||
}
|
||||
@ -1,4 +1,5 @@
|
||||
mod audible;
|
||||
mod bookmarks;
|
||||
mod download;
|
||||
mod player;
|
||||
|
||||
@ -162,6 +163,45 @@ async fn player_stop(pl: tauri::State<'_, SharedPlayer>) -> Result<PlayerState,
|
||||
.map_err(|e| format!("Hintergrundthread fehlgeschlagen: {e}"))?
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
async fn bookmark_list(asin: String) -> Result<Vec<bookmarks::Bookmark>, String> {
|
||||
tauri::async_runtime::spawn_blocking(move || bookmarks::list(&asin))
|
||||
.await
|
||||
.map_err(|e| format!("Hintergrundthread fehlgeschlagen: {e}"))?
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
async fn bookmark_add(
|
||||
asin: String,
|
||||
time: f64,
|
||||
note: String,
|
||||
) -> Result<Vec<bookmarks::Bookmark>, String> {
|
||||
tauri::async_runtime::spawn_blocking(move || bookmarks::add(&asin, time, ¬e))
|
||||
.await
|
||||
.map_err(|e| format!("Hintergrundthread fehlgeschlagen: {e}"))?
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
async fn bookmark_remove(
|
||||
asin: String,
|
||||
index: usize,
|
||||
) -> Result<Vec<bookmarks::Bookmark>, String> {
|
||||
tauri::async_runtime::spawn_blocking(move || bookmarks::remove(&asin, index))
|
||||
.await
|
||||
.map_err(|e| format!("Hintergrundthread fehlgeschlagen: {e}"))?
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
async fn bookmark_set_note(
|
||||
asin: String,
|
||||
index: usize,
|
||||
note: String,
|
||||
) -> Result<Vec<bookmarks::Bookmark>, String> {
|
||||
tauri::async_runtime::spawn_blocking(move || bookmarks::set_note(&asin, index, ¬e))
|
||||
.await
|
||||
.map_err(|e| format!("Hintergrundthread fehlgeschlagen: {e}"))?
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
async fn player_snapshot(pl: tauri::State<'_, SharedPlayer>) -> Result<PlayerState, String> {
|
||||
let p = pl.inner().clone();
|
||||
@ -212,6 +252,10 @@ pub fn run() {
|
||||
player_set_speed,
|
||||
player_stop,
|
||||
player_snapshot,
|
||||
bookmark_list,
|
||||
bookmark_add,
|
||||
bookmark_remove,
|
||||
bookmark_set_note,
|
||||
download_list,
|
||||
download_enqueue,
|
||||
download_cancel,
|
||||
|
||||
Reference in New Issue
Block a user