Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: local!! usage statistics #10

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
129 changes: 126 additions & 3 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ tui = "0.12.0"

alphanumeric-sort = "1.4.1"
dirs-next = "2.0.0"
chrono = "0.4.19"
chrono = { version = "0.4.23", features = ["serde"] }
threadpool = "1.8.1"

serde = { version = "1.0.124", features = ["derive"] }
Expand Down
24 changes: 15 additions & 9 deletions src/backend/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ use crate::{
Screen::*,
Terminal,
},
notification::{notify_error, notify_open},
};
use std::{
process::{Command, Stdio},
sync::mpsc::{channel, Receiver, Sender},
notification::notify_error,
};
use std::sync::mpsc::{channel, Receiver, Sender};

#[cfg(not(debug_assertions))]
use crate::notification::notify_open;
#[cfg(not(debug_assertions))]
use std::process::{Command, Stdio};

#[derive(Clone, Debug)]
pub enum FetchState {
Expand Down Expand Up @@ -49,7 +51,7 @@ pub(crate) struct Core {
pub(crate) config: Config,
pub(crate) current_screen: Screen,
channel_list: ChannelList,
pub(crate) playback_history: History,
pub(crate) history: History,
pub(crate) status_sender: Sender<StateUpdate>,
pub(crate) status_receiver: Receiver<StateUpdate>,
}
Expand All @@ -74,7 +76,9 @@ impl Core {
channel_list.select(Some(0));
channel_list.set_filter(current_filter);

let playback_history = History::load();
let mut history = History::load();
history.add_start();


let (status_sender, status_receiver) = channel();

Expand All @@ -83,7 +87,7 @@ impl Core {
config,
current_screen: Channels,
channel_list,
playback_history,
history,
status_sender,
status_receiver,
};
Expand Down Expand Up @@ -201,6 +205,7 @@ impl Core {
}

// call video player
#[cfg(not(debug_assertions))]
let command = Command::new("setsid")
.arg("-f")
.arg(&self.config.video_player)
Expand All @@ -209,8 +214,9 @@ impl Core {
.stdout(Stdio::null())
.spawn();

self.playback_history.add(video.clone());
self.history.video_opened(&video);

#[cfg(not(debug_assertions))]
match command {
Ok(_) => notify_open(&video.get_details()),
Err(error) => notify_error(&error.to_string()),
Expand Down
76 changes: 22 additions & 54 deletions src/backend/draw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@ use crate::backend::{
core::Core,
data::{channel::Channel, channel_list::ChannelList},
io::{history::History, config::Config},
Backend, Screen,
layout::AppLayout,
Screen,
Screen::*,
Terminal,
};
use std::thread;
use tui::widgets::ListItem;
use tui::{
layout::{Alignment, Constraint::*, Direction, Layout, Rect},
layout::Alignment,
style::Style,
text::Span,
widgets::{Block, BorderType, Borders, List, Paragraph},
Frame,
};

const INFO_LINE: &str =
Expand All @@ -32,7 +32,7 @@ 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 history = core.history.clone();
let screen = core.current_screen.clone();
let terminal = core.terminal.clone();
let config = core.config.clone();
Expand Down Expand Up @@ -88,55 +88,6 @@ impl<'a> Widget<'a> {
}
}

pub struct AppLayout {
main: Vec<Rect>,
content: Vec<Rect>,
}

impl AppLayout {
fn load(f: &mut Frame<'_, Backend>, screen: &Screen) -> Self {
let video_size = match screen {
Channels => 0,
Videos => 75,
};

let main_split = vec![Percentage(80), Percentage(19), Percentage(1)];
let content_split = vec![Percentage(100 - video_size), Percentage(video_size)];

let main = Layout::default()
.direction(Direction::Vertical)
.margin(0)
.constraints(main_split)
.split(f.size());

let content = Layout::default()
.direction(Direction::Horizontal)
.margin(0)
.constraints(content_split)
.split(main[0]);

Self {
main,
content,
}
}

fn info(&self) -> Rect {
self.main[2]
}

fn history(&self) -> Rect {
self.main[1]
}

fn channels(&self) -> Rect {
self.content[0]
}

fn videos(&self) -> Rect {
self.content[1]
}
}

#[allow(clippy::unnecessary_unwrap)]
pub fn draw(app: AppState) {
Expand Down Expand Up @@ -180,9 +131,26 @@ pub fn draw(app: AppState) {

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

// let stats_widget = Widget::builder()
// .with_title(" Stats ")
// .with_list(app.history.to_list_items());

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


//////////////////////////////

let info = Paragraph::new(Span::from(INFO_LINE))

let stats = app.history.stats();
let starts = match stats.today() {
Some(stats) => stats.starts,
None => 0,
};
let videos = match stats.today() {
Some(stats) => stats.watched,
None => 0
};
let info = Paragraph::new(Span::from(format!("{} - Videos Today: {} - Starts: {}", INFO_LINE, videos, starts)))
.style(Style::default())
.alignment(Alignment::Left);

Expand Down
Loading