Skip to content

Commit

Permalink
refactor: migrate album tree component to list component
Browse files Browse the repository at this point in the history
  • Loading branch information
lautarodragan committed Jan 10, 2025
1 parent e2c40cb commit fa7900c
Show file tree
Hide file tree
Showing 6 changed files with 197 additions and 34 deletions.
9 changes: 4 additions & 5 deletions src/components/library/album_tree/album_tree_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,9 @@ impl PartialOrd for AlbumTreeItem {

impl Display for AlbumTreeItem {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
let x = match self {
AlbumTreeItem::Artist(s) => s,
AlbumTreeItem::Album(_, s) => s,
};
write!(f, "{x}")
match self {
AlbumTreeItem::Artist(s) => write!(f, "{s}"),
AlbumTreeItem::Album(_, s) => write!(f, " {s}"),
}
}
}
65 changes: 59 additions & 6 deletions src/components/library/keyboard_handler.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,78 @@
use crate::structs::{Action, NavigationAction, OnAction};
use crate::{
components::library::album_tree::AlbumTreeItem,
structs::{Action, ListAction, NavigationAction, OnAction},
};

use super::library::Library;

impl OnAction for Library<'_> {
fn on_action(&self, action: Action) {
// log::trace!(target: "::library.on_action", "{action:?}");

let i = self.focused_component.get();

match action {
Action::Navigation(NavigationAction::FocusNext) => {
let focused_component = self.focused_component.get();
self.focused_component.set({
if i < self.components.len().saturating_sub(1) {
i + 1
if focused_component < self.components.len().saturating_sub(1) {
focused_component + 1
} else {
0
}
});
}
Action::ListAction(ListAction::OpenClose) => {
let (artist_index, artist_album_count) = self.album_tree_2.with_items(|items| {
let artist_index = {
let mut artist_index = self.album_tree_2.selected_index();
log::debug!("OpenClose {artist_index} {:?}", items[artist_index]);
loop {
if let AlbumTreeItem::Artist(_) = items[artist_index] {
break artist_index;
} else {
artist_index -= 1;
}
}
};

log::debug!("OpenClose artist_index={artist_index} {:?}", items[artist_index]);

let artist_album_count = {
let mut artist_album_count = 1;
loop {
let item_index = artist_index + artist_album_count;
log::debug!("matching {artist_index} {artist_album_count} {:?}", items[item_index]);

if item_index >= items.len() - 1 {
break artist_album_count;
}

match items[item_index] {
AlbumTreeItem::Album(ar, al) => artist_album_count += 1,
_ => break artist_album_count - 1,
}
}
};

log::debug!("OpenClose artist_album_count={artist_album_count}");
log::debug!("OpenClose artist_album_count={artist_album_count} {:?}", items[artist_index + artist_album_count]);

(artist_index, artist_album_count)
});

log::debug!("toggling {artist_index}..{artist_album_count}");
let is_open = !self.album_tree_2.is_open(artist_index);
self.album_tree_2.set_is_open(artist_index, is_open);

for i in artist_index + 1..artist_index + 1 + artist_album_count {
self.album_tree_2.set_is_visible(i, is_open);
}

if !is_open {
self.album_tree_2.set_selected_index(artist_index);
}
}
_ => {
if let Some(a) = self.components.get(i) {
if let Some(a) = self.components.get(self.focused_component.get()) {
a.on_action(action);
}
}
Expand Down
17 changes: 17 additions & 0 deletions src/components/library/library.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::{
cell::Cell,
collections::HashSet,
rc::Rc,
sync::{Mutex, MutexGuard},
};
Expand All @@ -16,6 +17,7 @@ pub struct Library<'a> {

pub(super) song_list: Rc<List<'a, Song>>,
pub(super) album_tree: Rc<AlbumTree<'a>>,
pub(super) album_tree_2: Rc<List<'a, AlbumTreeItem>>,
pub(super) components: Rc<Vec<Component<'a>>>,
pub(super) focused_component: Rc<Cell<usize>>,

Expand All @@ -28,6 +30,7 @@ impl<'a> Library<'a> {

let songs_by_artist = Rc::new(Mutex::new(crate::files::Library::from_file()));
let album_tree = Rc::new(AlbumTree::new(theme));
let album_tree_2 = Rc::new(List::new(theme, vec![]));
let song_list = Rc::new(List::new(theme, vec![]));

album_tree.on_select({
Expand Down Expand Up @@ -148,6 +151,7 @@ impl<'a> Library<'a> {

let components: Rc<Vec<Component>> = Rc::new(vec![
Component::Ref(album_tree.clone()),
Component::Ref(album_tree_2.clone()),
Component::Ref(song_list.clone()),
]);

Expand All @@ -160,6 +164,7 @@ impl<'a> Library<'a> {
songs_by_artist,
song_list,
album_tree,
album_tree_2,
components,
};

Expand Down Expand Up @@ -194,12 +199,24 @@ impl<'a> Library<'a> {
}

fn refresh_artist_tree(&self, songs_by_artist: &MutexGuard<crate::files::Library>) {
let mut items = vec![];

for (artist, songs) in &songs_by_artist.songs_by_artist {
items.push(AlbumTreeItem::Artist(artist.clone()));
let mut albums = HashSet::new();

for song in songs {
let album = song.album.clone().unwrap_or("(no album)".to_string());
albums.insert(album.clone());
self.album_tree.add_album(artist.clone(), album);
}

for album in albums {
items.push(AlbumTreeItem::Album(artist.clone(), album));
}
}

self.album_tree_2.set_items(items);
}

fn refresh_song_list(&self, songs_by_artist: &MutexGuard<crate::files::Library>) {
Expand Down
10 changes: 9 additions & 1 deletion src/components/library/widget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,15 @@ impl WidgetRef for Library<'_> {
.horizontal_margin(2)
.areas(area);

self.album_tree.render_ref(area_left, buf);
let [area_left_top, _, area_left_bottom] = Layout::vertical([
Constraint::Percentage(50),
Constraint::Length(5),
Constraint::Percentage(50),
])
.areas(area_left);

self.album_tree.render_ref(area_left_top, buf);
self.album_tree_2.render_ref(area_left_bottom, buf);
self.song_list.render_ref(area_right, buf);
}
}
Loading

0 comments on commit fa7900c

Please sign in to comment.