From 61675c34229e60301f1086559b01d259447c3e5c Mon Sep 17 00:00:00 2001 From: Kristofers Solo Date: Tue, 30 Jan 2024 18:49:36 +0200 Subject: [PATCH] feat(toggle): add toggle all --- src/app/command.rs | 37 +++++++++++++++++++++++++++++-------- src/app/mod.rs | 7 +++---- src/handler.rs | 6 +++++- 3 files changed, 37 insertions(+), 13 deletions(-) diff --git a/src/app/command.rs b/src/app/command.rs index 2acada9..b0b75e2 100644 --- a/src/app/command.rs +++ b/src/app/command.rs @@ -1,21 +1,42 @@ use anyhow::Result; -use transmission_rpc::types::{Torrent, TorrentAction, TorrentStatus}; +use transmission_rpc::types::{Id, Torrent, TorrentAction, TorrentStatus}; use super::Torrents; impl Torrents { - pub async fn toggle(&mut self, torrent: &Torrent) -> transmission_rpc::types::Result<()> { - let id = torrent.id().ok_or_else(|| "ID not found")?; - let action = match torrent.status.ok_or_else(|| "Torrent status not found")? { + pub async fn toggle(&mut self, torrent: &Torrent) { + let id = torrent.id().expect("ID not found"); + let action = match torrent.status.expect("Torrent status not found") { TorrentStatus::Stopped => TorrentAction::StartNow, _ => TorrentAction::Stop, }; - self.client.torrent_action(action, vec![id]).await?; - Ok(()) + self.client + .torrent_action(action, vec![id]) + .await + .expect("Error toggling torrent"); } - pub async fn toggle_all(&mut self) -> transmission_rpc::types::Result<()> { - todo!() + pub async fn toggle_all(&mut self) { + let torrents: Vec<(Id, TorrentAction)> = self + .torrents() + .iter() + .map(|torrent| { + ( + torrent.id().unwrap(), + match torrent.status { + Some(TorrentStatus::Stopped) => TorrentAction::StartNow, + _ => TorrentAction::Stop, + }, + ) + }) + .collect(); + + for (id, action) in torrents { + self.client + .torrent_action(action, vec![id]) + .await + .expect("Error toggling torrent"); + } } pub fn move_dir(&mut self) -> Result<()> { diff --git a/src/app/mod.rs b/src/app/mod.rs index 2acaabd..34ae935 100644 --- a/src/app/mod.rs +++ b/src/app/mod.rs @@ -118,11 +118,10 @@ impl<'a> App<'a> { self.show_popup = true; } - pub async fn toggle_torrent(&mut self) -> transmission_rpc::types::Result<()> { - let torrent = self.selected().ok_or_else(|| "Torrent not found")?; - self.torrents.toggle(&torrent.clone()).await?; + pub async fn toggle_torrent(&mut self) { + let torrent = self.selected().expect("Torrent not found"); + self.torrents.toggle(&torrent.clone()).await; self.close_popup(); - Ok(()) } fn selected(&self) -> Option<&Torrent> { diff --git a/src/handler.rs b/src/handler.rs index 366a209..8a61bb8 100644 --- a/src/handler.rs +++ b/src/handler.rs @@ -30,7 +30,11 @@ pub async fn handle_key_events(key_event: KeyEvent, app: &mut App<'_>) -> Result KeyCode::Char('4') => app.switch_tab(3), KeyCode::Char('t') | KeyCode::Enter | KeyCode::Menu => { app.toggle_popup(); - app.toggle_torrent().await.unwrap(); + app.toggle_torrent().await; + } + KeyCode::Char('a') => { + app.toggle_popup(); + app.torrents.toggle_all().await; } // Other handlers you could add here. _ => (),