feat(toggle): add toggle all

This commit is contained in:
Kristofers Solo 2024-01-30 18:49:36 +02:00
parent 95a6d3483a
commit 61675c3422
3 changed files with 37 additions and 13 deletions

View File

@ -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<()> {

View File

@ -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> {

View File

@ -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.
_ => (),