Updated save path for library

This commit is contained in:
MrDulfin 2024-12-26 23:28:20 -05:00
parent 6b3ac348ac
commit f88d50c720
9 changed files with 30 additions and 41 deletions

View file

@ -29,11 +29,11 @@ impl Default for ConfigLibrary {
}
impl ConfigLibrary {
pub fn new(path: PathBuf, name: String, scan_folders: Option<Vec<PathBuf>>) -> Self {
pub fn new(path: PathBuf, name: String, scan_folders: Option<Vec<PathBuf>>, uuid: Option<Uuid>) -> Self {
ConfigLibrary {
name,
path,
uuid: Uuid::new_v4(),
uuid: uuid.unwrap_or(Uuid::new_v4()),
scan_folders,
}
}
@ -197,6 +197,7 @@ pub mod tests {
PathBuf::from("test-config/library"),
String::from("library"),
None,
None
);
let mut config = Config {
path: PathBuf::from("test-config/config_test.json"),

View file

@ -312,21 +312,8 @@ impl Controller {
if let Ok(mail) = _mail {
match mail {
PlayerCommand::Play => {
if first {
queue_mail.send(QueueCommand::NowPlaying).await.unwrap();
let QueueResponse::Item(item) = queue_mail.recv().await.unwrap() else { unimplemented!() };
let QueueItemType::Single(song) = item.item else { unimplemented!("This is temporary, handle queueItemTypes at some point") };
let prism_uri = prismriver::utils::path_to_uri(&song.song.primary_uri().unwrap().0.as_path().unwrap()).unwrap();
player.write().unwrap().load_new(&prism_uri).unwrap();
player.write().unwrap().play();
player_mail.send(PlayerResponse::NowPlaying(song.song)).await.unwrap();
first = false
} else {
player.write().unwrap().play();
player_mail.send(PlayerResponse::Empty).await.unwrap();
}
player.write().unwrap().play();
player_mail.send(PlayerResponse::Empty).await.unwrap();
}
PlayerCommand::Pause => {
player.write().unwrap().pause();
@ -350,6 +337,7 @@ impl Controller {
};
let prism_uri = prismriver::utils::path_to_uri(&uri.as_path().unwrap()).unwrap();
println!("Playing song at path: {:?}", prism_uri);
player.write().unwrap().load_new(&prism_uri).unwrap();
player.write().unwrap().play();

View file

@ -351,6 +351,7 @@ mod tests {
PathBuf::from("test-config/library2"),
String::from("library2"),
None,
None
);
config.libraries.libraries.push(config_lib.clone());

View file

@ -2,7 +2,7 @@ use ciborium::{from_reader, into_writer};
use deunicode::deunicode_with_tofu;
use file_format::{FileFormat, Kind};
use std::error::Error;
use std::fs::{self, File};
use std::fs::{self, File, OpenOptions};
use std::io::{BufReader, BufWriter};
use std::path::{Path, PathBuf};
use walkdir::WalkDir;
@ -53,7 +53,7 @@ pub(super) fn read_file<T: for<'de> serde::Deserialize<'de>>(
path: PathBuf,
) -> Result<T, Box<dyn Error>> {
// Create a new snap reader over the file
let file_reader = BufReader::new(File::open(path)?);
let file_reader = BufReader::new(OpenOptions::new().read(true).open(path)?);
//let mut d = snap::read::FrameDecoder::new(file_reader);
// Decode the library from the serialized data into the vec

View file

@ -196,7 +196,7 @@ impl<
let empty = self.items.is_empty();
if !empty {
self.items.get_mut(i).expect("There should be an item at index {i}").state == QueueState::NoState;
self.items.get_mut(i).expect("There should be an item at index {i}").state = QueueState::NoState;
}
let len = items.len();

View file

@ -22,22 +22,26 @@ pub fn run() {
let controller_thread = spawn(move || {
let mut config = { tx.recv().unwrap() } ;
let scan_path = { lib_tx.recv().unwrap() };
let save_path = dbg!(config.libraries.library_folder.join("library.dlib"));
let _temp_config = ConfigLibrary::default();
let _lib = config.libraries.get_default().unwrap_or(&_temp_config);
let save_path = if _lib.path == PathBuf::default() {
scan_path.as_ref().unwrap().clone().canonicalize().unwrap().join("library.dlib")
} else {
_lib.path.clone()
};
println!("save_path: {}\nscan_path:{scan_path:?}", save_path.display());
let mut library = MusicLibrary::init(
save_path.clone(),
if let Ok(lib) = config.libraries.get_default() {
lib.uuid
} else {
Uuid::new_v4()
}
_lib.uuid
).unwrap();
let scan_path = scan_path.unwrap_or_else(|| config.libraries.get_default().unwrap().scan_folders.as_ref().unwrap()[0].clone());
if config.libraries.get_default().is_err() {
library.scan_folder(&scan_path).unwrap();
config.push_library( ConfigLibrary::new(save_path.clone(), String::from("Library"), Some(vec![scan_path.clone()])));
config.push_library( ConfigLibrary::new(save_path.clone(), String::from("Library"), Some(vec![scan_path.clone()]), Some(library.uuid)));
}
if library.library.is_empty() {
println!("library is empty");
@ -132,7 +136,6 @@ struct HandleTx(Receiver<ControllerHandle>);
async fn get_config(state: State<'_, ConfigRx>) -> Result<Config, String> {
if let Some(dir) = directories::ProjectDirs::from("", "Dangoware", "dmp") {
let path = dir.config_dir();
// dbg!(&path);
fs::create_dir_all(path).or_else(|err| {
if err.kind() == std::io::ErrorKind::AlreadyExists {
Ok(())
@ -141,8 +144,6 @@ async fn get_config(state: State<'_, ConfigRx>) -> Result<Config, String> {
}
}).unwrap();
// dbg!(&dir);
let config = if let Ok(mut c) = Config::read_file(PathBuf::from(path).join("config")) {
if c.state_path == PathBuf::default() {
c.state_path = PathBuf::from(path).join("state");

View file

@ -1,7 +1,6 @@
{
"$schema": "https://schema.tauri.app/config/2",
"productName": "Dango Music Player",
"mainBinaryName": "DMP",
"version": "0.0.1",
"identifier": "com.dango-music-player.app",
"build": {

View file

@ -1,9 +1,9 @@
import { useEffect, useRef, useState } from "react";
import { useEffect, useState } from "react";
import { convertFileSrc, invoke } from "@tauri-apps/api/core";
import "./App.css";
import { Config } from "./types";
import { EventEmitter } from "@tauri-apps/plugin-shell";
import { listen } from "@tauri-apps/api/event";
// import { EventEmitter } from "@tauri-apps/plugin-shell";
// import { listen } from "@tauri-apps/api/event";
// import { fetch } from "@tauri-apps/plugin-http";
import { getCurrentWebviewWindow } from "@tauri-apps/api/webviewWindow";
@ -26,7 +26,7 @@ function App() {
);
useEffect(() => {
const unlisten = appWindow.listen<any>("now_playing_change", ({ event, payload }) => {
const unlisten = appWindow.listen<any>("now_playing_change", ({ payload, }) => {
// console.log(event);
setNowPlaying(
<NowPlaying
@ -80,7 +80,7 @@ function App() {
</div>
<div className="rightSide">
{ nowPlaying }
<Queue songs={ queue } setSongs={ setQueue } />
<Queue songs={ queue } />
</div>
</main>
@ -201,8 +201,6 @@ function MainView({ lib_ref, viewName }: MainViewProps) {
useEffect(() => {
const unlisten = appWindow.listen<any>("library_loaded", (_) => {
console.log("library_loaded");
invoke('get_playlists').then(() => {})
invoke('get_library').then((lib) => {
setLibrary([...(lib as any[]).map((song) => {
@ -219,6 +217,8 @@ function MainView({ lib_ref, viewName }: MainViewProps) {
)
})])
})
invoke('get_playlists').then(() => {})
})
return () => { unlisten.then((f) => f()) }
}, []);
@ -313,9 +313,8 @@ function NowPlaying({ title, artist, album, artwork }: NowPlayingProps) {
interface QueueProps {
songs: JSX.Element[],
setSongs: React.Dispatch<React.SetStateAction<JSX.Element[]>>
}
function Queue({ songs, setSongs }: QueueProps) {
function Queue({ songs }: QueueProps) {
return (
<section className="Queue">
{ songs }

View file

@ -1,4 +1,4 @@
import React, { StrictMode } from "react";
import { StrictMode } from "react";
import ReactDOM from "react-dom/client";
import App from "./App";