Skip to content

Commit

Permalink
fix: fix offset not correctly applied when going up
Browse files Browse the repository at this point in the history
Allowing mutable changes to 'core' from draw thread allowed to offset to be applied correctly
  • Loading branch information
jooooscha committed Jan 30, 2024
1 parent 6a3658c commit 4913cc3
Show file tree
Hide file tree
Showing 7 changed files with 190 additions and 182 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 11 additions & 25 deletions src/backend/core.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::{
backend::{
data::{channel::Channel, channel_list::ChannelList, video::Video},
draw::draw,
io::config::Config,
io::{history::History, write_config, FileType::DbFile},
Action,
Expand All @@ -12,10 +11,7 @@ use crate::{
},
notification::{notify_error, notify_open},
};
use std::{
process::{Command, Stdio},
sync::mpsc::{channel, Receiver, Sender},
};
use std::process::{Command, Stdio};

#[derive(Clone, Debug)]
pub enum FetchState {
Expand Down Expand Up @@ -51,8 +47,6 @@ pub(crate) struct Core {
pub(crate) current_screen: Screen,
channel_list: ChannelList,
pub(crate) playback_history: History,
pub(crate) status_sender: Sender<StateUpdate>,
pub(crate) status_receiver: Receiver<StateUpdate>,
}

impl Core {
Expand All @@ -77,16 +71,12 @@ impl Core {

let playback_history = History::load();

let (status_sender, status_receiver) = channel();

let core = Core {
terminal,
config,
current_screen: Channels,
channel_list,
playback_history,
status_sender,
status_receiver,
};

Ok(core)
Expand All @@ -98,18 +88,10 @@ impl Core {
}

/// receive all status updates from status channel
pub(crate) fn update_status_line(&mut self) -> bool {
let mut changed = false;

for item in self.status_receiver.try_iter() {
changed = true;

if let Some(channel) = self.channel_list.get_unfiltered_mut_by_id(&item.text) {
channel.fetch_state = item.state.clone();
}
pub(crate) fn update_status_line(&mut self, item: StateUpdate) {
if let Some(channel) = self.channel_list.get_unfiltered_mut_by_id(&item.text) {
channel.fetch_state = item.state.clone();
}

changed
}

pub(crate) fn update_at_start(&self) -> bool {
Expand Down Expand Up @@ -222,9 +204,9 @@ impl Core {
}();
}

pub(crate) fn draw(&self) {
draw(self.into());
}
// pub(crate) fn draw(&self) {
// draw(self.into());
// }

pub fn toggle_filter(&mut self) {
self.channel_list.toggle_filter();
Expand All @@ -246,6 +228,10 @@ impl Core {
&self.channel_list
}

pub fn channel_list_mut(&mut self) -> &mut ChannelList {
&mut self.channel_list
}

pub(crate) fn get_selected_channel_index(&self) -> Option<usize> {
self.channel_list.selected()
}
Expand Down
5 changes: 5 additions & 0 deletions src/backend/data/channel/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,15 @@ impl Channel {
&self.tag
}

#[allow(dead_code)]
pub fn state(&self) -> ListState {
self.list_state.clone()
}

pub fn state_mut(&mut self) -> &mut ListState {
&mut self.list_state
}

pub fn push(&mut self, video: Video) {
self.videos.push(video);
}
Expand Down
8 changes: 6 additions & 2 deletions src/backend/data/channel_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,12 @@ impl ChannelList {
self.list_state.selected()
}

pub(crate) fn state(&self) -> ListState {
self.list_state.clone()
pub(crate) fn state(&self) -> &ListState {
&self.list_state
}

pub(crate) fn state_mut(&mut self) -> &mut ListState {
&mut self.list_state
}

pub(crate) fn push(&mut self, channel: Channel) {
Expand Down
95 changes: 41 additions & 54 deletions src/backend/draw.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
use crate::backend::{
core::Core,
data::{channel::Channel, channel_list::ChannelList},
io::{history::History, config::Config},
Screen,
Screen::*,
Terminal,
};
use std::{thread, rc::Rc};
use std::{thread, rc::Rc, sync::{RwLock, Arc}};
use tui::widgets::ListItem;
use tui::{
layout::{Alignment, Constraint::*, Direction, Layout, Rect},
Expand All @@ -19,35 +16,6 @@ use tui::{
const INFO_LINE: &str =
"q close; o open video/select; Enter/l select; Esc/h go back; m mark; M unmark";

pub struct AppState {
channel: Option<Channel>,
channel_list: ChannelList,
history: History,
screen: Screen,
terminal: Terminal,
config: Config,
}

impl From<&Core> for AppState {
fn from(core: &Core) -> Self {
let channel = core.get_selected_channel().cloned();
let channel_list = core.channel_list().clone();
let history = core.playback_history.clone();
let screen = core.current_screen.clone();
let terminal = core.terminal.clone();
let config = core.config.clone();

AppState {
channel,
channel_list,
history,
screen,
terminal,
config,
}
}
}

#[derive(Default)]
struct Widget<'a> {
title: String,
Expand Down Expand Up @@ -139,44 +107,63 @@ impl AppLayout {
}

#[allow(clippy::unnecessary_unwrap)]
pub fn draw(app: AppState) {
pub fn draw(core: Arc<RwLock<Core>>) {
thread::spawn(move || {
let channels = app.channel_list.clone();
let (
channels,
current_screen,
app_title,
terminal,
history,
) = {
let core_read_lock = core.read().unwrap();
(
core_read_lock.channel_list().clone(),
core_read_lock.current_screen.clone(),
core_read_lock.config.app_title.clone(),
core_read_lock.terminal.term.clone(),
core_read_lock.playback_history.clone(),
)
};

let channel_symbol = match app.screen {
let channel_symbol = match current_screen {
Channels => ">> ",
Videos => "-",
};
let chan_widget = Widget::builder()
.with_title(&format!(" {} ", app.config.app_title))
.with_title(&format!(" {} ", app_title))
.with_symbol(channel_symbol)
.with_list(channels.get_spans_list());

let _ = app.terminal.term.clone().lock().unwrap().draw(|f| {
let layout = AppLayout::load(f, &app.screen);

f.render_stateful_widget(
chan_widget.render(),
layout.channels(),
&mut channels.state(),
);
let _ = terminal.lock().unwrap().draw(|f| {
let layout = AppLayout::load(f, &current_screen);

if let Some(channel) = app.channel {
let video_widget = Widget::builder()
.with_title(&format!(" {} ", channel.name()))
.with_symbol(">> ")
.with_list(channel.get_spans_list());
if let Ok(mut core_write_lock) = core.try_write() {

f.render_stateful_widget(
video_widget.render(),
layout.videos(),
&mut channel.state(),
chan_widget.render(),
layout.channels(),
core_write_lock.channel_list_mut().state_mut(),
);

if let Some(channel) = core_write_lock.get_selected_channel_mut() {
let c = channel.clone();
let video_widget = Widget::builder()
.with_title(&format!(" {} ", channel.name()))
.with_symbol(">> ")
.with_list(c.get_spans_list());

f.render_stateful_widget(
video_widget.render(),
layout.videos(),
channel.state_mut(),
);
}
}

let history_widget = Widget::builder()
.with_title(" Playback History ")
.with_list(app.history.to_list_items());
.with_list(history.to_list_items());

f.render_widget(history_widget.render(), layout.history());

Expand Down
2 changes: 1 addition & 1 deletion src/backend/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
pub(crate) mod core;
pub(crate) mod data;

mod draw;
pub mod draw;
pub(super) mod io;
pub(super) mod dearrow;

Expand Down
Loading

0 comments on commit 4913cc3

Please sign in to comment.