From 65e9887f3807eb57b33b8d202c0ea8afd4c88525 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Andersson?= Date: Tue, 16 Mar 2021 20:43:17 +0100 Subject: [PATCH] Refactor needs_download and rename library.items to better convey usage (#466) * Refactor * Rename items to playlists and update error message --- src/library.rs | 20 ++++++-------------- src/ui/contextmenu.rs | 2 +- src/ui/playlists.rs | 4 ++-- src/ui/queue.rs | 2 +- 4 files changed, 10 insertions(+), 18 deletions(-) diff --git a/src/library.rs b/src/library.rs index e7eb475..86f7351 100644 --- a/src/library.rs +++ b/src/library.rs @@ -60,10 +60,8 @@ impl Library { library } - pub fn items(&self) -> RwLockReadGuard> { - self.playlists - .read() - .expect("could not readlock listview content") + pub fn playlists(&self) -> RwLockReadGuard> { + self.playlists.read().expect("can't readlock playlists") } fn load_cache(&self, cache_path: PathBuf, store: Arc>>) { @@ -99,17 +97,11 @@ impl Library { } fn needs_download(&self, remote: &SimplifiedPlaylist) -> bool { - for local in self - .playlists - .read() - .expect("can't readlock playlists") + self.playlists() .iter() - { - if local.id == remote.id { - return local.snapshot_id != remote.snapshot_id; - } - } - true + .find(|local| local.id == remote.id) + .and_then(|local| Some(local.snapshot_id != remote.snapshot_id)) + .unwrap_or(true) } fn append_or_update(&self, updated: &Playlist) -> usize { diff --git a/src/ui/contextmenu.rs b/src/ui/contextmenu.rs index 8a20c08..13281db 100644 --- a/src/ui/contextmenu.rs +++ b/src/ui/contextmenu.rs @@ -51,7 +51,7 @@ impl ContextMenu { let mut list_select: SelectView = SelectView::new(); let current_user_id = library.user_id.as_ref().unwrap(); - for list in library.items().iter() { + for list in library.playlists().iter() { if current_user_id == &list.owner_id || list.collaborative { list_select.add_item(list.name.clone(), list.clone()); } diff --git a/src/ui/playlists.rs b/src/ui/playlists.rs index 2204e04..8e0006c 100644 --- a/src/ui/playlists.rs +++ b/src/ui/playlists.rs @@ -27,8 +27,8 @@ impl PlaylistsView { } pub fn delete_dialog(&mut self) -> Option> { - let store = self.library.items(); - let current = store.get(self.list.get_selected_index()); + let playlists = self.library.playlists(); + let current = playlists.get(self.list.get_selected_index()); if let Some(playlist) = current { let library = self.library.clone(); diff --git a/src/ui/queue.rs b/src/ui/queue.rs index 166d19c..c919895 100644 --- a/src/ui/queue.rs +++ b/src/ui/queue.rs @@ -67,7 +67,7 @@ impl QueueView { let mut list_select: SelectView> = SelectView::new().autojump(); list_select.add_item("[Create new]", None); - for list in library.items().iter() { + for list in library.playlists().iter() { list_select.add_item(list.name.clone(), Some(list.id.clone())); }