fix queue deletion & make get_full_path return an option

This commit is contained in:
krolxon 2024-04-28 23:59:42 +05:30
parent 66a1cfccf4
commit 6b66a44ed0
3 changed files with 49 additions and 31 deletions

View File

@ -113,11 +113,11 @@ impl App {
Ok(())
}
pub fn handle_remove_or_from_current_playlist(&mut self) -> AppResult<()> {
pub fn handle_add_or_remove_from_current_playlist(&mut self) -> AppResult<()> {
match self.selected_tab {
SelectedTab::DirectoryBrowser => {
let (_, file) = self.browser.filetree.get(self.browser.selected).unwrap();
self.browser.selected += 1;
let mut status = false;
for (i, song) in self.queue_list.list.clone().iter().enumerate() {
if song.contains(file) {
@ -127,14 +127,21 @@ impl App {
}
if !status {
let song = self
.conn
.get_song_with_only_filename(&self.conn.get_full_path(&file).unwrap());
if let Some(full_path) = &self.conn.get_full_path(&file) {
let song = self.conn.get_song_with_only_filename(full_path);
self.conn.conn.push(&song)?;
}
}
if self.browser.selected != self.browser.filetree.len() - 1 {
self.browser.selected += 1;
}
}
SelectedTab::Queue => {
if self.queue_list.list.len() == 0 {
return Ok(());
}
let file = self
.queue_list
.list
@ -145,6 +152,9 @@ impl App {
for (i, song) in self.queue_list.list.clone().iter().enumerate() {
if song.contains(&file) {
self.conn.conn.delete(i as u32).unwrap();
if self.queue_list.index != 0 {
self.queue_list.index -= 1;
}
}
}
}
@ -205,9 +215,10 @@ impl App {
}
pub fn search_song(&mut self) -> AppResult<()> {
let filename = self.conn.get_full_path(&self.search_input)?;
if let Some(filename) = self.conn.get_full_path(&self.search_input) {
let song = self.conn.get_song_with_only_filename(&filename);
self.conn.push(&song)?;
}
Ok(())
}

View File

@ -188,18 +188,26 @@ impl Connection {
}
/// Given a song name from a directory, it returns the full path of the song in the database
pub fn get_full_path(&self, short_path: &str) -> AppResult<String> {
let list = self
.songs_filenames
.iter()
.map(|f| f.as_str())
.collect::<Vec<&str>>();
let (filename, _) = rust_fuzzy_search::fuzzy_search_sorted(&short_path, &list)
.get(0)
.unwrap()
.clone();
pub fn get_full_path(&self, short_path: &str) -> Option<String> {
// let list = self
// .songs_filenames
// .iter()
// .map(|f| f.as_str())
// .collect::<Vec<&str>>();
// let (filename, _) = rust_fuzzy_search::fuzzy_search_sorted(&short_path, &list)
// .get(0)
// .unwrap()
// .clone();
Ok(filename.to_string())
for (i, f) in self.songs_filenames.iter().enumerate() {
if f.contains(short_path) {
return Some(self.songs_filenames.get(i).unwrap().to_string());
}
}
None
// Ok(filename.to_string())
}
/// Print status to stdout

View File

@ -137,7 +137,7 @@ pub fn handle_key_events(key_event: KeyEvent, app: &mut App) -> AppResult<()> {
_ => {}
}
let full_path = app.conn.get_full_path(&short_path)?;
if let Some(full_path) = app.conn.get_full_path(&short_path) {
let song = app.conn.get_song_with_only_filename(&full_path);
if pl_name == "Current Playlist" {
@ -146,7 +146,7 @@ pub fn handle_key_events(key_event: KeyEvent, app: &mut App) -> AppResult<()> {
} else {
app.conn.add_to_playlist(pl_name, &song)?;
}
}
// hide the playlist popup
app.playlist_popup = false;
app.append_list.index = 0;
@ -237,7 +237,6 @@ pub fn handle_key_events(key_event: KeyEvent, app: &mut App) -> AppResult<()> {
// add to queue
KeyCode::Char('a') => app.playlist_popup = true,
// Fast forward
KeyCode::Char('f') => {
let place = app.conn.conn.status().unwrap().song.unwrap().pos;
@ -319,7 +318,7 @@ pub fn handle_key_events(key_event: KeyEvent, app: &mut App) -> AppResult<()> {
// Remove from Current Playlsit
KeyCode::Char(' ') | KeyCode::Backspace => {
app.handle_remove_or_from_current_playlist()?;
app.handle_add_or_remove_from_current_playlist()?;
}
// Change playlist name