Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

playlist_management: Use async #43

Merged
merged 1 commit into from
Feb 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion tori/src/events/action.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,29 @@
use super::Command;

#[derive(Debug, Default, Clone)]
pub enum Level {
#[default]
Ok,
Info,
Error,
}

#[derive(Debug, Clone)]
pub enum Action {
Rerender,
Tick,
Command(Command),

Notify(Level, String),

ScrollDown,
ScrollUp,
Command(Command),

RefreshSongs,
RefreshPlaylists,
SelectSong(usize),
SelectPlaylist(usize),

CloseModal,
AddPlaylist {
name: String,
Expand Down
46 changes: 24 additions & 22 deletions tori/src/m3u/playlist_management.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,32 @@ use std::{
result::Result as StdResult,
};

use crate::{app::App, config::Config, error::Result, m3u};
use crate::{
config::Config,
error::Result,
events::{action::Level, channel::Tx, Action},
m3u,
};

/// Adds a song to an existing playlist
pub fn add_song(app: &mut App, playlist: &str, song_path: String) {
todo!("gotta rewrite add_song!")
// app.state.notify_info(format!("Adding {}...", song_path));
//
// if surely_invalid_path(&song_path) {
// app.state.notify_err(format!("Failed to add song path '{}'. Doesn't look like a URL and is not a valid path in your filesystem.", song_path));
// return;
// }
//
// let sender = app.channel.tx.clone();
// let playlist = playlist.to_string();
// thread::spawn(move || {
// add_song_recursively(&song_path, &playlist);
//
// // Extract last part (separated by '/') of the song_path
// let mut rsplit = song_path.trim_end_matches('/').rsplit('/');
// let song = rsplit.next().unwrap_or(&song_path).to_string();
//
// let event = Event::Action(Action::SongAdded { playlist, song });
// sender.send(event).expect("Failed to send internal event");
// });
pub async fn add_song(tx: Tx, playlist: String, song_path: String) -> Result<()> {
tx.send(Action::Notify(
Level::Info,
format!("Adding {}...", song_path),
))?;

if surely_invalid_path(&song_path) {
return Err(format!("Failed to add song path '{}'. Doesn't look like a URL and is not a valid path in your filesystem.", song_path).into());
}

add_song_recursively(&song_path, &playlist);

// Extract last part (separated by '/') of the song_path
let mut rsplit = song_path.trim_end_matches('/').rsplit('/');
let song = rsplit.next().unwrap_or(&song_path).to_string();

tx.send(Action::SongAdded { playlist, song })?;
Ok(())
}

/// Adds songs from some path. If the path points to a directory, it'll traverse the directory
Expand Down
Loading