diff --git a/src/tui/app/selection.rs b/src/tui/app/selection.rs index 189bcfe..6abe6f1 100644 --- a/src/tui/app/selection.rs +++ b/src/tui/app/selection.rs @@ -147,17 +147,17 @@ impl ArtistSelection { fn reinitialise_with_index( &mut self, artists: &[Artist], - mut index: usize, - mut active_album: Option, + index: usize, + active_album: Option, ) { if artists.is_empty() { self.state.select(None); - self.album.reinitialise(&[], None); + self.album = AlbumSelection::initialise(&[]); + } else if index >= artists.len() { + let end = artists.len() - 1; + self.state.select(Some(end)); + self.album = AlbumSelection::initialise(&artists[end].albums); } else { - if index >= artists.len() { - index = artists.len() - 1; - active_album = None; - } self.state.select(Some(index)); self.album .reinitialise(&artists[index].albums, active_album); @@ -234,17 +234,17 @@ impl AlbumSelection { fn reinitialise_with_index( &mut self, albums: &[Album], - mut index: usize, - mut active_track: Option, + index: usize, + active_track: Option, ) { if albums.is_empty() { self.state.select(None); self.track = TrackSelection::initialise(&[]); + } else if index >= albums.len() { + let end = albums.len() - 1; + self.state.select(Some(end)); + self.track = TrackSelection::initialise(&albums[end].tracks); } else { - if index >= albums.len() { - index = albums.len() - 1; - active_track = None; - } self.state.select(Some(index)); self.track.reinitialise(&albums[index].tracks, active_track); } @@ -303,13 +303,12 @@ impl TrackSelection { } } - fn reinitialise_with_index(&mut self, tracks: &[Track], mut index: usize) { + fn reinitialise_with_index(&mut self, tracks: &[Track], index: usize) { if tracks.is_empty() { self.state.select(None); + } else if index >= tracks.len() { + self.state.select(Some(tracks.len() - 1)); } else { - if index >= tracks.len() { - index = tracks.len() - 1; - } self.state.select(Some(index)); } }