Skip to content

Commit

Permalink
make filter mutexless
Browse files Browse the repository at this point in the history
  • Loading branch information
micielski committed Jul 15, 2024
1 parent c9b86f7 commit 925f289
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 31 deletions.
12 changes: 12 additions & 0 deletions rm-main/src/ui/tabs/torrents/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
_ => (),
}
}
Expand Down
13 changes: 5 additions & 8 deletions rm-main/src/ui/tabs/torrents/table_manager.rs
Original file line number Diff line number Diff line change
@@ -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};

Expand All @@ -14,7 +11,7 @@ pub struct TableManager {
ctx: app::Ctx,
pub table: GenericTable<RustmissionTorrent>,
pub widths: Vec<Constraint>,
pub filter: Arc<Mutex<Option<String>>>,
pub filter: Option<String>,
pub torrents_displaying_no: u16,
headers: Vec<&'static str>,
}
Expand All @@ -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<Row<'_>> {
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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion rm-main/src/ui/tabs/torrents/task_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
37 changes: 15 additions & 22 deletions rm-main/src/ui/tabs/torrents/tasks/filter.rs
Original file line number Diff line number Diff line change
@@ -1,37 +1,29 @@
use std::sync::{Arc, Mutex};

use crossterm::event::KeyCode;
use ratatui::prelude::*;

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<Mutex<TableManager>>,
}

impl FilterBar {
pub fn new(ctx: app::Ctx, table_manager: Arc<Mutex<TableManager>>) -> Self {
let current_filter = table_manager.lock().unwrap().filter.lock().unwrap().clone();
pub fn new(ctx: app::Ctx, current_filter: Option<String>) -> 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 }
}
}

Expand All @@ -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
Expand Down
2 changes: 2 additions & 0 deletions rm-shared/src/action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ pub enum UpdateAction {
TaskClear,
SessionStats(Arc<SessionStats>),
FreeSpace(Arc<FreeSpace>),
SearchFilterApply(String),
SearchFilterClear,
// Search Tab
SearchStarted,
SearchResults(Vec<Magnet>),
Expand Down

0 comments on commit 925f289

Please sign in to comment.