diff --git a/rm-main/src/ui/tabs/torrents/mod.rs b/rm-main/src/ui/tabs/torrents/mod.rs index 6fc8edb..8dd2fea 100644 --- a/rm-main/src/ui/tabs/torrents/mod.rs +++ b/rm-main/src/ui/tabs/torrents/mod.rs @@ -114,6 +114,18 @@ impl Component for TorrentsTab { self.bottom_stats.set_free_space(free_space); self.ctx.send_action(Action::Render); } + UpdateAction::SearchFilterApply(filter) => { + let mut table_manager_lock = self.table_manager.lock().unwrap(); + table_manager_lock.filter.replace(filter); + table_manager_lock.table.state.borrow_mut().select(Some(0)); + self.ctx.send_action(Action::Render); + } + UpdateAction::SearchFilterClear => { + let mut table_manager_lock = self.table_manager.lock().unwrap(); + table_manager_lock.filter = None; + table_manager_lock.table.state.borrow_mut().select(Some(0)); + self.ctx.send_action(Action::Render); + } _ => (), } } diff --git a/rm-main/src/ui/tabs/torrents/table_manager.rs b/rm-main/src/ui/tabs/torrents/table_manager.rs index 58143a3..8f090b1 100644 --- a/rm-main/src/ui/tabs/torrents/table_manager.rs +++ b/rm-main/src/ui/tabs/torrents/table_manager.rs @@ -1,10 +1,7 @@ use fuzzy_matcher::{skim::SkimMatcherV2, FuzzyMatcher}; use ratatui::{prelude::*, widgets::Row}; use rm_config::main_config::Header; -use std::{ - collections::HashMap, - sync::{Arc, Mutex}, -}; +use std::collections::HashMap; use crate::{app, ui::components::table::GenericTable}; @@ -14,7 +11,7 @@ pub struct TableManager { ctx: app::Ctx, pub table: GenericTable, pub widths: Vec, - pub filter: Arc>>, + pub filter: Option, pub torrents_displaying_no: u16, headers: Vec<&'static str>, } @@ -31,14 +28,14 @@ impl TableManager { ctx, table, widths, - filter: Arc::new(Mutex::new(None)), + filter: None, torrents_displaying_no: 0, headers, } } pub fn rows(&self) -> Vec> { - if let Some(filter) = &*self.filter.lock().unwrap() { + if let Some(filter) = &self.filter { let rows = self.filtered_torrents_rows(&self.table.items, filter); self.table.overwrite_len(rows.len()); rows @@ -59,7 +56,7 @@ impl TableManager { let matcher = SkimMatcherV2::default(); let index = self.table.state.borrow().selected()?; - if let Some(filter) = &*self.filter.lock().unwrap() { + if let Some(filter) = &self.filter { let mut loop_index = 0; for rustmission_torrent in &mut self.table.items { if matcher diff --git a/rm-main/src/ui/tabs/torrents/task_manager.rs b/rm-main/src/ui/tabs/torrents/task_manager.rs index 4759526..08cf683 100644 --- a/rm-main/src/ui/tabs/torrents/task_manager.rs +++ b/rm-main/src/ui/tabs/torrents/task_manager.rs @@ -142,7 +142,7 @@ impl TaskManager { Action::Search => { self.current_task = CurrentTask::FilterBar(FilterBar::new( self.ctx.clone(), - self.table_manager.clone(), + self.table_manager.lock().unwrap().filter.clone(), )); self.ctx.send_update_action(UpdateAction::SwitchToInputMode); } diff --git a/rm-main/src/ui/tabs/torrents/tasks/filter.rs b/rm-main/src/ui/tabs/torrents/tasks/filter.rs index 38b2fe9..a3cd51c 100644 --- a/rm-main/src/ui/tabs/torrents/tasks/filter.rs +++ b/rm-main/src/ui/tabs/torrents/tasks/filter.rs @@ -1,5 +1,3 @@ -use std::sync::{Arc, Mutex}; - use crossterm::event::KeyCode; use ratatui::prelude::*; @@ -7,31 +5,25 @@ use crate::{ app, ui::{ components::{Component, ComponentAction}, - tabs::torrents::{input_manager::InputManager, TableManager}, + tabs::torrents::input_manager::InputManager, to_input_request, }, }; -use rm_shared::action::Action; +use rm_shared::action::{Action, UpdateAction}; pub struct FilterBar { ctx: app::Ctx, input: InputManager, - table_manager: Arc>, } impl FilterBar { - pub fn new(ctx: app::Ctx, table_manager: Arc>) -> Self { - let current_filter = table_manager.lock().unwrap().filter.lock().unwrap().clone(); + pub fn new(ctx: app::Ctx, current_filter: Option) -> Self { let input = InputManager::new_with_value( ctx.clone(), "Search: ".to_string(), current_filter.unwrap_or_default(), ); - Self { - ctx, - input, - table_manager, - } + Self { ctx, input } } } @@ -41,23 +33,24 @@ impl Component for FilterBar { Action::Input(input) => { if matches!(input.code, KeyCode::Enter | KeyCode::Esc) { if self.input.text().is_empty() { - *self.table_manager.lock().unwrap().filter.lock().unwrap() = None; + self.ctx.send_update_action(UpdateAction::SearchFilterClear); } return ComponentAction::Quit; } if let Some(req) = to_input_request(input) { self.input.handle(req); - let table_manager_lock = self.table_manager.lock().unwrap(); - table_manager_lock - .filter - .lock() - .unwrap() - .replace(self.input.text()); - table_manager_lock.table.state.borrow_mut().select(Some(0)); + self.ctx + .send_update_action(UpdateAction::SearchFilterApply(self.input.text())); + // let table_manager_lock = self.table_manager.lock().unwrap(); + // table_manager_lock + // .filter + // .lock() + // .unwrap() + // .replace(self.input.text()); + // table_manager_lock.table.state.borrow_mut().select(Some(0)); - self.ctx.send_action(Action::Render); - return ComponentAction::Nothing; + // self.ctx.send_action(Action::Render); } ComponentAction::Nothing diff --git a/rm-shared/src/action.rs b/rm-shared/src/action.rs index 443ba3f..1f22b35 100644 --- a/rm-shared/src/action.rs +++ b/rm-shared/src/action.rs @@ -46,6 +46,8 @@ pub enum UpdateAction { TaskClear, SessionStats(Arc), FreeSpace(Arc), + SearchFilterApply(String), + SearchFilterClear, // Search Tab SearchStarted, SearchResults(Vec),