Skip to content

Commit

Permalink
Merge pull request #30 from dantleech/show_split
Browse files Browse the repository at this point in the history
Show split
  • Loading branch information
dantleech committed Sep 2, 2023
2 parents 2d9c10d + c0e34c0 commit 6f35f6d
Show file tree
Hide file tree
Showing 13 changed files with 355 additions and 188 deletions.
52 changes: 40 additions & 12 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,10 @@ use tui::{
use tui_input::Input;

use crate::{
component::activity_list::{ActivityListState, ActivityListMode},
event::input::EventSender,
component::activity_list::{ActivityListMode, ActivityListState, ActivityViewState},
event::{input::EventSender, util::{table_state_prev, table_state_next}},
input::InputEvent,
store::{
activity::ActivityStore,
polyline_compare::{compare},
},
store::{activity::ActivityStore, polyline_compare::compare},
};
use crate::{
component::{activity_list, activity_view, unit_formatter::UnitFormatter},
Expand All @@ -39,13 +36,12 @@ pub struct ActivityFilters {
}

impl ActivityFilters {
pub fn anchor_tolerance_add(&mut self, delta: f64) -> () {
pub fn anchor_tolerance_add(&mut self, delta: f64) {
self.anchor_tolerance += delta;
if self.anchor_tolerance < 0.0 {
self.anchor_tolerance = 0.0;
}
}

}

pub struct Notification {
Expand Down Expand Up @@ -76,6 +72,7 @@ pub struct App<'a> {
pub active_page: ActivePage,
pub unit_formatter: UnitFormatter,
pub activity_list: ActivityListState,
pub activity_view: ActivityViewState,
pub filters: ActivityFilters,

pub activity_type: Option<String>,
Expand Down Expand Up @@ -152,6 +149,10 @@ impl App<'_> {
filter_dialog: false,
sort_dialog: false,
},
activity_view: ActivityViewState {
pace_table_state: TableState::default(),
selected_split: None,
},
filters: ActivityFilters {
sort_by: SortBy::Date,
sort_order: SortOrder::Desc,
Expand Down Expand Up @@ -247,17 +248,18 @@ impl App<'_> {
return true;
}
let anchored = self.activity_anchored.as_ref().unwrap();
if !anchored.polyline().is_ok() || !a.polyline().is_ok() {
if anchored.polyline().is_err() || a.polyline().is_err() {
return false;
}
return compare(&anchored.polyline().unwrap(), &a.polyline().unwrap(), 100) < self.filters.anchor_tolerance;
compare(&anchored.polyline().unwrap(), &a.polyline().unwrap(), 100)
< self.filters.anchor_tolerance
})
.collect()
}

// TODO: Add a collection object
pub fn unsorted_filtered_activities(&self) -> Vec<Activity> {
return self.activities_filtered.clone();
self.activities_filtered.clone()
}

pub fn filtered_activities(&self) -> Vec<Activity> {
Expand Down Expand Up @@ -300,7 +302,7 @@ impl App<'_> {
self.event_queue.push(event);
}

pub(crate) fn anchor_selected(&mut self) -> () {
pub(crate) fn anchor_selected(&mut self) {
let activities = self.filtered_activities();
if let Some(selected) = self.activity_list.table_state().selected() {
if let Some(a) = activities.get(selected) {
Expand All @@ -316,4 +318,30 @@ impl App<'_> {
}
}
}

pub(crate) fn previous_activity(&mut self) {
table_state_prev(
self.activity_list.table_state(),
self.activities_filtered.len(),
false,
);
if let Some(selected) = self.activity_list.table_state().selected() {
if let Some(a) = self.activities_filtered.get(selected) {
self.activity = Some(a.clone());
}
}
}

pub(crate) fn next_activity(&mut self) {
table_state_next(
self.activity_list.table_state(),
self.activities_filtered.len(),
false,
);
if let Some(selected) = self.activity_list.table_state().selected() {
if let Some(a) = self.activities_filtered.get(selected) {
self.activity = Some(a.clone());
}
}
}
}
14 changes: 6 additions & 8 deletions src/component/activity_list/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ use tui_input::backend::crossterm::EventHandler;
use crate::{
app::{App, SortOrder},
event::{
keymap::{MappedKey, StravaEvent},
util::{table_state_next, table_state_prev}, input::InputEvent,
keymap::{MappedKey, StravaEvent}, input::InputEvent,
},
store::activity::Activity,
ui::{centered_rect_absolute, color::ColorTheme}, component::{table_status_select_current},
Expand Down Expand Up @@ -47,7 +46,6 @@ pub fn handle(app: &mut App, key: MappedKey) {

return;
}
let activities = app.filtered_activities();
match key.strava_event {
StravaEvent::Quit => app.quit = true,
StravaEvent::ToggleUnitSystem => {
Expand All @@ -59,18 +57,18 @@ pub fn handle(app: &mut App, key: MappedKey) {
SortOrder::Desc => SortOrder::Asc,
}
}
StravaEvent::Down => table_state_next(&mut app.activity_list.table_state(), activities.len()),
StravaEvent::Up => table_state_prev(&mut app.activity_list.table_state(), activities.len()),
StravaEvent::Down => app.next_activity(),
StravaEvent::Up => app.previous_activity(),
StravaEvent::Filter => toggle_filter(app),
StravaEvent::Sort => toggle_sort(app),
StravaEvent::Enter => table_status_select_current(app),
StravaEvent::Refresh => app.send(InputEvent::Sync),
StravaEvent::IncreaseTolerance => {
app.filters.anchor_tolerance_add(0.001);
app.filters.anchor_tolerance_add(0.01);
app.send(InputEvent::Reload)
}
StravaEvent::DecreaseTolerance => {
app.filters.anchor_tolerance_add(-0.001);
app.filters.anchor_tolerance_add(-0.01);
app.send(InputEvent::Reload);
},
StravaEvent::Anchor => {
Expand Down Expand Up @@ -103,7 +101,7 @@ pub fn draw<B: Backend>(
f.render_stateful_widget(
activity_list_table(app, activities),
area,
&mut app.activity_list.table_state(),
app.activity_list.table_state(),
);

if app.activity_list.filter_dialog {
Expand Down
10 changes: 10 additions & 0 deletions src/component/activity_list/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,16 @@ pub struct ActivityListState {
pub sort_dialog: bool,
}

pub struct ActivityViewState {
pub pace_table_state: TableState,
pub selected_split: Option<i64>,
}
impl ActivityViewState {
pub(crate) fn select_split(&mut self, selected: i64) {
self.selected_split = Some(selected);
}
}

impl ActivityListState {
pub fn table_state(&mut self) -> &mut TableState
{
Expand Down
27 changes: 20 additions & 7 deletions src/component/activity_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,38 @@ use crate::{
};

use super::{
polyline, race_predictor, stats,
table_status_select_current, splits, activity_list::list::activity_list_table,
polyline, race_predictor, stats, splits, activity_list::list::activity_list_table,
};

pub fn handle(app: &mut App, key: MappedKey) {
let activities = app.filtered_activities();
let split_len = match &app.activity {
Some(a) => a.splits.len(),
None => 0,
};

match key.strava_event {
StravaEvent::ToggleUnitSystem => {
app.unit_formatter = app.unit_formatter.toggle();
}
StravaEvent::Quit => app.active_page = ActivePage::ActivityList,
StravaEvent::Enter => app.active_page = ActivePage::ActivityList,
StravaEvent::Down => {
table_state_next(&mut app.activity_list.table_state(), activities.len());
table_status_select_current(app);
app.next_activity();
},
StravaEvent::Up => {
table_state_prev(&mut app.activity_list.table_state(), activities.len());
table_status_select_current(app);
app.previous_activity();
},
StravaEvent::Next => {
table_state_next(&mut app.activity_view.pace_table_state, split_len, true);
if let Some(selected) = app.activity_view.pace_table_state.selected() {
app.activity_view.select_split(selected as i64);
}
},
StravaEvent::Previous => {
table_state_prev(&mut app.activity_view.pace_table_state, split_len, true);
if let Some(selected) = app.activity_view.pace_table_state.selected() {
app.activity_view.select_split(selected as i64);
}
},
StravaEvent::Anchor => {
app.anchor_selected();
Expand Down

0 comments on commit 6f35f6d

Please sign in to comment.