@@ -4,8 +4,6 @@ use std::{
4
4
time:: { Duration , SystemTime } ,
5
5
} ;
6
6
7
- use strum:: EnumIter ;
8
-
9
7
use tokio:: sync:: mpsc:: { Receiver , Sender } ;
10
8
use tui:: {
11
9
backend:: { Backend , CrosstermBackend } ,
@@ -18,7 +16,7 @@ use crate::{
18
16
component:: activity_list:: { ActivityListMode , ActivityListState , ActivityViewState } ,
19
17
event:: { input:: EventSender , util:: { table_state_prev, table_state_next} } ,
20
18
input:: InputEvent ,
21
- store:: { activity:: { ActivityStore , Activities } } ,
19
+ store:: { activity:: { ActivityStore , Activities , SortBy , SortOrder } } ,
22
20
} ;
23
21
use crate :: {
24
22
component:: { activity_list, activity_view, unit_formatter:: UnitFormatter } ,
@@ -34,6 +32,11 @@ pub struct ActivityFilters {
34
32
pub filter : String ,
35
33
}
36
34
35
+ pub struct RankOptions {
36
+ pub rank_by : SortBy ,
37
+ pub rank_order : SortOrder ,
38
+ }
39
+
37
40
impl ActivityFilters {
38
41
pub fn anchor_tolerance_add ( & mut self , delta : f64 ) {
39
42
self . anchor_tolerance += delta;
@@ -73,12 +76,12 @@ pub struct App<'a> {
73
76
pub activity_list : ActivityListState ,
74
77
pub activity_view : ActivityViewState ,
75
78
pub filters : ActivityFilters ,
79
+ pub ranking : RankOptions ,
76
80
77
81
pub activity_type : Option < String > ,
78
82
pub activity : Option < Activity > ,
79
83
pub activity_anchored : Option < Activity > ,
80
84
pub activities : Activities ,
81
- pub activities_filtered : Activities ,
82
85
83
86
pub info_message : Option < Notification > ,
84
87
pub error_message : Option < Notification > ,
@@ -96,39 +99,6 @@ pub enum ActivePage {
96
99
Activity ,
97
100
}
98
101
99
- #[ derive( EnumIter ) ]
100
- pub enum SortBy {
101
- Date ,
102
- Distance ,
103
- Pace ,
104
- HeartRate ,
105
- Time ,
106
- }
107
-
108
- impl Display for SortBy {
109
- fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
110
- write ! ( f, "{}" , self . to_label( ) )
111
- }
112
- }
113
-
114
- pub enum SortOrder {
115
- Asc ,
116
- Desc ,
117
- }
118
-
119
- impl Display for SortOrder {
120
- fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
121
- write ! (
122
- f,
123
- "{}" ,
124
- match self {
125
- SortOrder :: Asc => "ascending" ,
126
- SortOrder :: Desc => "descending" ,
127
- }
128
- )
129
- }
130
- }
131
-
132
102
impl App < ' _ > {
133
103
pub fn new < ' a > (
134
104
store : & ' a mut ActivityStore < ' a > ,
@@ -147,6 +117,7 @@ impl App<'_> {
147
117
filter_text_area : Input :: default ( ) ,
148
118
filter_dialog : false ,
149
119
sort_dialog : false ,
120
+ rank_dialog : false ,
150
121
} ,
151
122
activity_view : ActivityViewState {
152
123
pace_table_state : TableState :: default ( ) ,
@@ -158,10 +129,13 @@ impl App<'_> {
158
129
filter : "" . to_string ( ) ,
159
130
anchor_tolerance : 0.005 ,
160
131
} ,
132
+ ranking : RankOptions {
133
+ rank_by : SortBy :: Pace ,
134
+ rank_order : SortOrder :: Desc ,
135
+ } ,
161
136
activity : None ,
162
137
activity_anchored : None ,
163
138
activities : Activities :: new ( ) ,
164
- activities_filtered : Activities :: new ( ) ,
165
139
store,
166
140
167
141
activity_type : None ,
@@ -177,8 +151,6 @@ impl App<'_> {
177
151
& mut self ,
178
152
terminal : & mut Terminal < CrosstermBackend < io:: Stdout > > ,
179
153
) -> Result < ( ) , anyhow:: Error > {
180
- self . activities = self . store . activities ( ) . await ;
181
-
182
154
loop {
183
155
if self . quit {
184
156
break ;
@@ -226,23 +198,21 @@ impl App<'_> {
226
198
}
227
199
228
200
pub async fn reload ( & mut self ) {
229
- self . activities = self . store . activities ( ) . await ;
230
- self . activities_filtered = self . activities . where_title_contains ( self . filters . filter . as_str ( ) ) ;
201
+ let mut activities = self . store . activities ( ) . await ;
202
+ activities = activities. where_title_contains ( self . filters . filter . as_str ( ) ) ;
231
203
if let Some ( activity_type) = self . activity_type . clone ( ) {
232
- self . activities_filtered = self . activities_filtered . having_activity_type ( activity_type) ;
204
+ activities = activities . having_activity_type ( activity_type) ;
233
205
}
234
206
if let Some ( anchored) = & self . activity_anchored {
235
- self . activities_filtered = self . activities_filtered . withing_distance_of ( anchored, self . filters . anchor_tolerance ) ;
207
+ activities = activities . withing_distance_of ( anchored, self . filters . anchor_tolerance ) ;
236
208
}
209
+ self . activities = activities
210
+ . rank ( & self . ranking . rank_by , & self . ranking . rank_order )
211
+ . sort ( & self . filters . sort_by , & self . filters . sort_order )
237
212
}
238
213
239
- pub fn unsorted_filtered_activities ( & self ) -> Activities {
240
- self . activities_filtered . clone ( )
241
- }
242
-
243
- pub fn filtered_activities ( & self ) -> Activities {
244
- let activities = self . unsorted_filtered_activities ( ) ;
245
- activities. sort ( & self . filters . sort_by , & self . filters . sort_order )
214
+ pub fn activities ( & self ) -> Activities {
215
+ self . activities . clone ( )
246
216
}
247
217
248
218
fn draw < B : Backend > ( & mut self , f : & mut Frame < B > ) -> Result < ( ) , anyhow:: Error > {
@@ -261,7 +231,7 @@ impl App<'_> {
261
231
}
262
232
263
233
pub ( crate ) fn anchor_selected ( & mut self ) {
264
- let activities = self . filtered_activities ( ) ;
234
+ let activities = self . activities ( ) ;
265
235
if let Some ( selected) = self . activity_list . table_state ( ) . selected ( ) {
266
236
if let Some ( a) = activities. get ( selected) {
267
237
if self . activity_anchored . is_some ( ) {
@@ -280,11 +250,11 @@ impl App<'_> {
280
250
pub ( crate ) fn previous_activity ( & mut self ) {
281
251
table_state_prev (
282
252
self . activity_list . table_state ( ) ,
283
- self . activities_filtered . len ( ) ,
253
+ self . activities . len ( ) ,
284
254
false ,
285
255
) ;
286
256
if let Some ( selected) = self . activity_list . table_state ( ) . selected ( ) {
287
- if let Some ( a) = self . activities_filtered . get ( selected) {
257
+ if let Some ( a) = self . activities . get ( selected) {
288
258
self . activity = Some ( a. clone ( ) ) ;
289
259
}
290
260
}
@@ -293,11 +263,11 @@ impl App<'_> {
293
263
pub ( crate ) fn next_activity ( & mut self ) {
294
264
table_state_next (
295
265
self . activity_list . table_state ( ) ,
296
- self . activities_filtered . len ( ) ,
266
+ self . activities . len ( ) ,
297
267
false ,
298
268
) ;
299
269
if let Some ( selected) = self . activity_list . table_state ( ) . selected ( ) {
300
- if let Some ( a) = self . activities_filtered . get ( selected) {
270
+ if let Some ( a) = self . activities . get ( selected) {
301
271
self . activity = Some ( a. clone ( ) ) ;
302
272
}
303
273
}
0 commit comments