Skip to content

Commit

Permalink
make status working again
Browse files Browse the repository at this point in the history
  • Loading branch information
micielski committed Jul 16, 2024
1 parent b9a1e50 commit 94c1327
Show file tree
Hide file tree
Showing 12 changed files with 119 additions and 108 deletions.
13 changes: 10 additions & 3 deletions rm-main/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,16 @@ impl App {
let mut client = transmission::utils::client_from_config(&config);

let (trans_tx, trans_rx) = mpsc::unbounded_channel();
let ctx = Ctx::new(&mut client, config, action_tx.clone(), update_tx, trans_tx).await?;

tokio::spawn(transmission::action_handler(client, trans_rx, action_tx));
let ctx = Ctx::new(
&mut client,
config,
action_tx.clone(),
update_tx.clone(),
trans_tx,
)
.await?;

tokio::spawn(transmission::action_handler(client, trans_rx, update_tx));
Ok(Self {
should_quit: false,
main_window: MainWindow::new(ctx.clone()),
Expand Down
40 changes: 20 additions & 20 deletions rm-main/src/transmission/action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ use transmission_rpc::types::{
};
use transmission_rpc::TransClient;

use rm_shared::action::Action;
use rm_shared::action::ErrorMessage;
use rm_shared::action::UpdateAction;

const FAILED_TO_COMMUNICATE: &'static str = "Failed to communicate with Transmission";

Expand Down Expand Up @@ -43,7 +43,7 @@ pub enum TorrentAction {
pub async fn action_handler(
mut client: TransClient,
mut trans_rx: UnboundedReceiver<TorrentAction>,
action_tx: UnboundedSender<Action>,
action_tx: UnboundedSender<UpdateAction>,
) {
while let Some(action) = trans_rx.recv().await {
match action {
Expand All @@ -62,14 +62,15 @@ pub async fn action_handler(
};
match client.torrent_add(args).await {
Ok(_) => {
action_tx.send(Action::TaskSuccess).unwrap();
action_tx.send(UpdateAction::TaskSuccess).unwrap();
}
Err(err) => {
let msg = format!("Failed to add torrent with URL/Path: \"{url}\"");
let err_message = ErrorMessage::new(FAILED_TO_COMMUNICATE, msg, err);
action_tx
.send(Action::Error(Box::new(err_message)))
.send(UpdateAction::Error(Box::new(err_message)))
.unwrap();
action_tx.send(UpdateAction::TaskFailure).unwrap();
}
}
}
Expand All @@ -80,7 +81,7 @@ pub async fn action_handler(
let msg = format!("Failed to stop torrents with these IDs: {:?}", ids);
let err_message = ErrorMessage::new(FAILED_TO_COMMUNICATE, msg, err);
action_tx
.send(Action::Error(Box::new(err_message)))
.send(UpdateAction::Error(Box::new(err_message)))
.unwrap();
}
}
Expand All @@ -92,35 +93,34 @@ pub async fn action_handler(
let msg = format!("Failed to start torrents with these IDs: {:?}", ids);
let err_message = ErrorMessage::new(FAILED_TO_COMMUNICATE, msg, err);
action_tx
.send(Action::Error(Box::new(err_message)))
.send(UpdateAction::Error(Box::new(err_message)))
.unwrap();
}
}
}
TorrentAction::DeleteWithFiles(ids) => {
match client.torrent_remove(ids.clone(), true).await {
Ok(_) => action_tx.send(Action::TaskSuccess).unwrap(),
Ok(_) => action_tx.send(UpdateAction::TaskSuccess).unwrap(),
Err(err) => {
let msg = format!("Failed to remove torrents with these IDs: {:?}", ids);
let err_message = ErrorMessage::new(FAILED_TO_COMMUNICATE, msg, err);
action_tx
.send(Action::Error(Box::new(err_message)))
.send(UpdateAction::Error(Box::new(err_message)))
.unwrap();
action_tx.send(UpdateAction::TaskFailure).unwrap();
}
}

client.torrent_remove(ids, true).await.unwrap();
action_tx.send(Action::TaskSuccess).unwrap();
}
TorrentAction::DeleteWithoutFiles(ids) => {
match client.torrent_remove(ids.clone(), false).await {
Ok(_) => action_tx.send(Action::TaskSuccess).unwrap(),
Ok(_) => action_tx.send(UpdateAction::TaskSuccess).unwrap(),
Err(err) => {
let msg = format!("Failed to remove torrents with these IDs: {:?}", ids);
let err_message = ErrorMessage::new(FAILED_TO_COMMUNICATE, msg, err);
action_tx
.send(Action::Error(Box::new(err_message)))
.send(UpdateAction::Error(Box::new(err_message)))
.unwrap();
action_tx.send(UpdateAction::TaskFailure).unwrap();
}
}
}
Expand All @@ -134,7 +134,7 @@ pub async fn action_handler(
);
let err_message = ErrorMessage::new(FAILED_TO_COMMUNICATE, msg, err);
action_tx
.send(Action::Error(Box::new(err_message)))
.send(UpdateAction::Error(Box::new(err_message)))
.unwrap();
}
}
Expand All @@ -147,7 +147,7 @@ pub async fn action_handler(
let msg = format!("Failed to get session data");
let err_message = ErrorMessage::new(FAILED_TO_COMMUNICATE, msg, err);
action_tx
.send(Action::Error(Box::new(err_message)))
.send(UpdateAction::Error(Box::new(err_message)))
.unwrap();
}
},
Expand All @@ -159,7 +159,7 @@ pub async fn action_handler(
let msg = format!("Failed to move torrent to new directory:\n{new_directory}");
let err_message = ErrorMessage::new(FAILED_TO_COMMUNICATE, msg, err);
action_tx
.send(Action::Error(Box::new(err_message)))
.send(UpdateAction::Error(Box::new(err_message)))
.unwrap();
}
}
Expand All @@ -169,7 +169,7 @@ pub async fn action_handler(
let msg = format!("Failed to get session stats");
let err_message = ErrorMessage::new(FAILED_TO_COMMUNICATE, msg, err);
action_tx
.send(Action::Error(Box::new(err_message)))
.send(UpdateAction::Error(Box::new(err_message)))
.unwrap();
}
},
Expand All @@ -179,7 +179,7 @@ pub async fn action_handler(
let msg = format!("Failed to get free space info");
let err_message = ErrorMessage::new(FAILED_TO_COMMUNICATE, msg, err);
action_tx
.send(Action::Error(Box::new(err_message)))
.send(UpdateAction::Error(Box::new(err_message)))
.unwrap();
}
},
Expand All @@ -190,7 +190,7 @@ pub async fn action_handler(
let msg = format!("Failed to fetch torrent data");
let err_message = ErrorMessage::new(FAILED_TO_COMMUNICATE, msg, err);
action_tx
.send(Action::Error(Box::new(err_message)))
.send(UpdateAction::Error(Box::new(err_message)))
.unwrap();
}
}
Expand All @@ -202,7 +202,7 @@ pub async fn action_handler(
let msg = format!("Failed to fetch torrents with these IDs: {:?}", ids);
let err_message = ErrorMessage::new(FAILED_TO_COMMUNICATE, msg, err);
action_tx
.send(Action::Error(Box::new(err_message)))
.send(UpdateAction::Error(Box::new(err_message)))
.unwrap();
}
}
Expand Down
2 changes: 0 additions & 2 deletions rm-main/src/ui/global_popups/error.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::ops::RemAssign;

use ratatui::{
prelude::*,
widgets::{Block, Clear, Paragraph, Wrap},
Expand Down
21 changes: 13 additions & 8 deletions rm-main/src/ui/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,6 @@ impl Component for MainWindow {
use Action as A;

match action {
A::Error(error) => {
let error_popup = ErrorPopup::new(error.title, error.description, error.source);
self.global_popup_manager.error_popup = Some(error_popup);
self.ctx.send_action(A::Render);
}
A::ShowHelp => {
self.global_popup_manager.handle_actions(action);
}
Expand All @@ -72,9 +67,19 @@ impl Component for MainWindow {
}

fn handle_update_action(&mut self, action: UpdateAction) {
match self.tabs.current_tab {
CurrentTab::Torrents => self.torrents_tab.handle_update_action(action),
CurrentTab::Search => self.search_tab.handle_update_action(action),
match action {
UpdateAction::Error(err) => {
let error_popup = ErrorPopup::new(err.title, err.description, err.source);
self.global_popup_manager.error_popup = Some(error_popup);
self.ctx.send_action(Action::Render);
}
action if self.tabs.current_tab == CurrentTab::Torrents => {
self.torrents_tab.handle_update_action(action)
}
action if self.tabs.current_tab == CurrentTab::Search => {
self.search_tab.handle_update_action(action)
}
_ => unreachable!(),
}
}

Expand Down
1 change: 0 additions & 1 deletion rm-main/src/ui/tabs/torrents/input_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use ratatui::{
use tui_input::{Input, InputRequest};

use crate::{app, ui::components::Component};
use rm_shared::action::Action;

pub struct InputManager {
input: Input,
Expand Down
6 changes: 5 additions & 1 deletion rm-main/src/ui/tabs/torrents/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,13 @@ impl Component for TorrentsTab {
UpdateAction::UpdateCurrentTorrent(_) => {
self.popup_manager.handle_update_action(action)
}
_ => (),
other => self.task_manager.handle_update_action(other),
}
}

fn tick(&mut self) {
self.task_manager.tick();
}
}

impl TorrentsTab {
Expand Down
37 changes: 13 additions & 24 deletions rm-main/src/ui/tabs/torrents/task_manager.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::sync::{Arc, Mutex};

use ratatui::prelude::*;
use throbber_widgets_tui::ThrobberState;

Expand Down Expand Up @@ -59,46 +57,24 @@ impl Component for TaskManager {
match &mut self.current_task {
CurrentTask::AddMagnetBar(magnet_bar) => {
if magnet_bar.handle_actions(action).is_quit() {
// Some(A::TaskPending(task)) => self.pending_task(task),
self.cancel_task()
}
}

CurrentTask::DeleteBar(delete_bar) => {
if delete_bar.handle_actions(action).is_quit() {
// Some(A::TaskPending(task)) => {
// let selected = self
// .table_manager
// .lock()
// .unwrap()
// .table
// .state
// .borrow()
// .selected();

// // select closest existing torrent
// if let Some(idx) = selected {
// if idx > 0 {
// self.table_manager.lock().unwrap().table.previous();
// }
// }
// self.pending_task(task)
// }
self.cancel_task()
}
}
CurrentTask::MoveBar(move_bar) => {
if move_bar.handle_actions(action).is_quit() {
self.cancel_task()
// Some(A::TaskPending(task)) => self.pending_task(task),
}
}
CurrentTask::FilterBar(filter_bar) => {
if filter_bar.handle_actions(action).is_quit() {
self.cancel_task()
}
}

CurrentTask::Status(status_bar) => {
if status_bar.handle_actions(action).is_quit() {
self.cancel_task()
Expand All @@ -110,6 +86,19 @@ impl Component for TaskManager {
ComponentAction::Nothing
}

fn handle_update_action(&mut self, action: UpdateAction) {
match action {
UpdateAction::TaskClear => self.cancel_task(),
UpdateAction::TaskSet(task) => self.pending_task(task),
UpdateAction::TaskFailure => {
if let CurrentTask::Status(status_bar) = &mut self.current_task {
status_bar.set_failure()
}
}
_ => (),
}
}

fn render(&mut self, f: &mut Frame, rect: Rect) {
match &mut self.current_task {
CurrentTask::AddMagnetBar(magnet_bar) => magnet_bar.render(f, rect),
Expand Down
7 changes: 4 additions & 3 deletions rm-main/src/ui/tabs/torrents/tasks/add_magnet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,10 @@ impl AddMagnetBar {
self.input_magnet_mgr.text(),
Some(self.input_location_mgr.text()),
));
// return Some(Action::TaskPending(StatusTask::Add(
// self.input_magnet_mgr.text(),
// )));
self.ctx
.send_update_action(UpdateAction::TaskSet(StatusTask::Add(
self.input_magnet_mgr.text(),
)));
self.ctx
.send_update_action(UpdateAction::SwitchToNormalMode);
return ComponentAction::Quit;
Expand Down
17 changes: 11 additions & 6 deletions rm-main/src/ui/tabs/torrents/tasks/delete_torrent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@ use transmission_rpc::types::Id;
use crate::{
app,
transmission::TorrentAction,
ui::{components::{Component, ComponentAction}, tabs::torrents::input_manager::InputManager, to_input_request},
ui::{
components::{Component, ComponentAction},
tabs::torrents::input_manager::InputManager,
to_input_request,
},
};
use rm_shared::action::Action;
use rm_shared::action::{Action, UpdateAction};
use rm_shared::status_task::StatusTask;

#[derive(Clone)]
Expand Down Expand Up @@ -73,11 +77,12 @@ impl Component for DeleteBar {
))
}
}
self.ctx.send_action(Action::TaskPending(StatusTask::Delete(
self.torrents_to_delete[0].name.clone(),
)));
self.ctx
.send_update_action(UpdateAction::TaskSet(StatusTask::Delete(
self.torrents_to_delete[0].name.clone(),
)));
} else if text == "n" || text == "no" {
return ComponentAction::Quit
return ComponentAction::Quit;
}
}

Expand Down
8 changes: 6 additions & 2 deletions rm-main/src/ui/tabs/torrents/tasks/move_torrent.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use crossterm::event::{KeyCode, KeyEvent};
use ratatui::prelude::*;
use rm_shared::{action::Action, status_task::StatusTask};
use rm_shared::{
action::{Action, UpdateAction},
status_task::StatusTask,
};
use transmission_rpc::types::Id;

use crate::{
Expand Down Expand Up @@ -36,7 +39,8 @@ impl MoveBar {
let torrents_to_move = self.torrents_to_move.clone();
self.ctx
.send_torrent_action(TorrentAction::Move(torrents_to_move, new_location.clone()));
// return Some(Action::TaskPending(StatusTask::Move(new_location)));
self.ctx
.send_update_action(UpdateAction::TaskSet(StatusTask::Move(new_location)));
return ComponentAction::Quit;
}

Expand Down
Loading

0 comments on commit 94c1327

Please sign in to comment.