refactor(toggle): add toggle method to Torrents

This commit is contained in:
Kristofers Solo 2024-01-30 18:34:53 +02:00
parent c199325d50
commit 95a6d3483a
2 changed files with 36 additions and 11 deletions

32
src/app/command.rs Normal file
View File

@ -0,0 +1,32 @@
use anyhow::Result;
use transmission_rpc::types::{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")? {
TorrentStatus::Stopped => TorrentAction::StartNow,
_ => TorrentAction::Stop,
};
self.client.torrent_action(action, vec![id]).await?;
Ok(())
}
pub async fn toggle_all(&mut self) -> transmission_rpc::types::Result<()> {
todo!()
}
pub fn move_dir(&mut self) -> Result<()> {
todo!()
}
pub fn delete(&mut self) -> Result<()> {
todo!()
}
pub fn rename(&mut self) -> Result<()> {
todo!()
}
}

View File

@ -3,7 +3,8 @@ mod torrent;
pub mod utils;
use ratatui::widgets::TableState;
use transmission_rpc::types::{Result, Torrent, TorrentAction, TorrentStatus};
use transmission_rpc::types::Torrent;
mod command;
pub use self::{tab::Tab, torrent::Torrents};
@ -117,17 +118,9 @@ impl<'a> App<'a> {
self.show_popup = true;
}
pub async fn toggle_torrent(&mut self) -> Result<()> {
pub async fn toggle_torrent(&mut self) -> transmission_rpc::types::Result<()> {
let torrent = self.selected().ok_or_else(|| "Torrent not found")?;
let id = torrent.id().ok_or_else(|| "ID not found")?;
let action = match torrent.status.ok_or_else(|| "Torrent status not found")? {
TorrentStatus::Stopped => TorrentAction::StartNow,
_ => TorrentAction::Stop,
};
self.torrents
.client
.torrent_action(action, vec![id])
.await?;
self.torrents.toggle(&torrent.clone()).await?;
self.close_popup();
Ok(())
}