From af830d603d588dc40ea7c0a78e84d28e248ae120 Mon Sep 17 00:00:00 2001 From: veeso Date: Thu, 25 Feb 2021 16:15:06 +0100 Subject: [PATCH] Now bookmarks and recents are sorted in the UI (bookmarks are sorted by name; recents are sorted by connection datetime) --- CHANGELOG.md | 1 + src/ui/activities/auth_activity/bookmarks.rs | 104 +++++++++++-------- src/ui/activities/auth_activity/layout.rs | 12 +-- src/ui/activities/auth_activity/mod.rs | 4 + 4 files changed, 71 insertions(+), 50 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 191af5f..dde520e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,6 +20,7 @@ Released on ??? - Enhancements: - Default choice for deleting file set to "NO" (way too easy to delete files by mistake) - Added CLI options to set starting workind directory on both local and remote hosts + - Now bookmarks and recents are sorted in the UI (bookmarks are sorted by name; recents are sorted by connection datetime) ## 0.3.2 diff --git a/src/ui/activities/auth_activity/bookmarks.rs b/src/ui/activities/auth_activity/bookmarks.rs index 0e2f99c..8ea8c45 100644 --- a/src/ui/activities/auth_activity/bookmarks.rs +++ b/src/ui/activities/auth_activity/bookmarks.rs @@ -41,18 +41,14 @@ impl AuthActivity { pub(super) fn del_bookmark(&mut self, idx: usize) { if let Some(bookmarks_cli) = self.bookmarks_client.as_mut() { // Iterate over kyes - let mut name: Option = None; - for (i, key) in bookmarks_cli.iter_bookmarks().enumerate() { - if i == idx { - name = Some(key.clone()); - break; - } - } + let name: Option<&String> = self.bookmarks_list.get(idx); if let Some(name) = name { bookmarks_cli.del_bookmark(&name); // Write bookmarks self.write_bookmarks(); } + // Delete element from vec + self.recents_list.remove(idx); } } @@ -62,20 +58,16 @@ impl AuthActivity { pub(super) fn load_bookmark(&mut self, idx: usize) { if let Some(bookmarks_cli) = self.bookmarks_client.as_ref() { // Iterate over bookmarks - for (i, key) in bookmarks_cli.iter_bookmarks().enumerate() { - if i == idx { - if let Some(bookmark) = bookmarks_cli.get_bookmark(&key) { - // Load parameters - self.address = bookmark.0; - self.port = bookmark.1.to_string(); - self.protocol = bookmark.2; - self.username = bookmark.3; - if let Some(password) = bookmark.4 { - self.password = password; - } + if let Some(key) = self.bookmarks_list.get(idx) { + if let Some(bookmark) = bookmarks_cli.get_bookmark(&key) { + // Load parameters + self.address = bookmark.0; + self.port = bookmark.1.to_string(); + self.protocol = bookmark.2; + self.username = bookmark.3; + if let Some(password) = bookmark.4 { + self.password = password; } - // Break - break; } } } @@ -112,7 +104,7 @@ impl AuthActivity { DialogYesNoOption::No => None, }; bookmarks_cli.add_bookmark( - name, + name.clone(), self.address.clone(), port, self.protocol, @@ -121,6 +113,9 @@ impl AuthActivity { ); // Save bookmarks self.write_bookmarks(); + // Push bookmark to list and sort + self.bookmarks_list.push(name); + self.sort_bookmarks(); } } /// ### del_recent @@ -128,19 +123,14 @@ impl AuthActivity { /// Delete recent pub(super) fn del_recent(&mut self, idx: usize) { if let Some(client) = self.bookmarks_client.as_mut() { - // Iterate over kyes - let mut name: Option = None; - for (i, key) in client.iter_recents().enumerate() { - if i == idx { - name = Some(key.clone()); - break; - } - } + let name: Option<&String> = self.recents_list.get(idx); if let Some(name) = name { client.del_recent(&name); - // Save bookmarks + // Write bookmarks self.write_bookmarks(); } + // Delete element from vec + self.recents_list.remove(idx); } } @@ -150,17 +140,13 @@ impl AuthActivity { pub(super) fn load_recent(&mut self, idx: usize) { if let Some(client) = self.bookmarks_client.as_ref() { // Iterate over bookmarks - for (i, key) in client.iter_recents().enumerate() { - if i == idx { - if let Some(bookmark) = client.get_recent(key) { - // Load parameters - self.address = bookmark.0; - self.port = bookmark.1.to_string(); - self.protocol = bookmark.2; - self.username = bookmark.3; - // Break - break; - } + if let Some(key) = self.recents_list.get(idx) { + if let Some(bookmark) = client.get_recent(key) { + // Load parameters + self.address = bookmark.0; + self.port = bookmark.1.to_string(); + self.protocol = bookmark.2; + self.username = bookmark.3; } } } @@ -233,7 +219,26 @@ impl AuthActivity { config_dir_path.as_path(), 16, ) { - Ok(cli) => self.bookmarks_client = Some(cli), + Ok(cli) => { + // Load bookmarks into list + let mut bookmarks_list: Vec = + Vec::with_capacity(cli.iter_bookmarks().count()); + for bookmark in cli.iter_bookmarks() { + bookmarks_list.push(bookmark.clone()); + } + // Load recents into list + let mut recents_list: Vec = + Vec::with_capacity(cli.iter_recents().count()); + for recent in cli.iter_recents() { + recents_list.push(recent.clone()); + } + self.bookmarks_client = Some(cli); + self.bookmarks_list = bookmarks_list; + self.recents_list = recents_list; + // Sort bookmark list + self.sort_bookmarks(); + self.sort_recents(); + } Err(err) => { self.popup = Some(Popup::Alert( Color::Red, @@ -256,4 +261,19 @@ impl AuthActivity { } } } + + /// ### sort_bookmarks + /// + /// Sort bookmarks in list + fn sort_bookmarks(&mut self) { + self.bookmarks_list.sort(); + } + + /// ### sort_recents + /// + /// Sort recents in list + fn sort_recents(&mut self) { + // Reverse order + self.recents_list.sort_by(|a, b| b.cmp(a)); + } } diff --git a/src/ui/activities/auth_activity/layout.rs b/src/ui/activities/auth_activity/layout.rs index a779aa1..020b0e0 100644 --- a/src/ui/activities/auth_activity/layout.rs +++ b/src/ui/activities/auth_activity/layout.rs @@ -320,10 +320,8 @@ impl AuthActivity { fn draw_bookmarks_tab(&self) -> Option { self.bookmarks_client.as_ref()?; let hosts: Vec = self - .bookmarks_client - .as_ref() - .unwrap() - .iter_bookmarks() + .bookmarks_list + .iter() .map(|key: &String| { let entry: (String, u16, FileTransferProtocol, String, _) = self .bookmarks_client @@ -368,10 +366,8 @@ impl AuthActivity { fn draw_recents_tab(&self) -> Option { self.bookmarks_client.as_ref()?; let hosts: Vec = self - .bookmarks_client - .as_ref() - .unwrap() - .iter_recents() + .recents_list + .iter() .map(|key: &String| { let entry: (String, u16, FileTransferProtocol, String) = self .bookmarks_client diff --git a/src/ui/activities/auth_activity/mod.rs b/src/ui/activities/auth_activity/mod.rs index 95de2b9..1f2d58b 100644 --- a/src/ui/activities/auth_activity/mod.rs +++ b/src/ui/activities/auth_activity/mod.rs @@ -115,7 +115,9 @@ pub struct AuthActivity { input_txt: String, // Input text choice_opt: DialogYesNoOption, // Dialog popup selected option bookmarks_idx: usize, // Index of selected bookmark + bookmarks_list: Vec, // List of bookmarks recents_idx: usize, // Index of selected recent + recents_list: Vec, // list of recents } impl Default for AuthActivity { @@ -149,7 +151,9 @@ impl AuthActivity { input_txt: String::new(), choice_opt: DialogYesNoOption::Yes, bookmarks_idx: 0, + bookmarks_list: Vec::new(), recents_idx: 0, + recents_list: Vec::new(), } }