1
1
use std:: {
2
- cmp:: Ordering ,
3
2
fmt:: Display ,
4
3
io,
5
4
time:: { Duration , SystemTime } ,
@@ -19,7 +18,7 @@ use crate::{
19
18
component:: activity_list:: { ActivityListMode , ActivityListState , ActivityViewState } ,
20
19
event:: { input:: EventSender , util:: { table_state_prev, table_state_next} } ,
21
20
input:: InputEvent ,
22
- store:: { activity:: ActivityStore , polyline_compare :: compare } ,
21
+ store:: { activity:: { ActivityStore , Activities } } ,
23
22
} ;
24
23
use crate :: {
25
24
component:: { activity_list, activity_view, unit_formatter:: UnitFormatter } ,
@@ -78,8 +77,8 @@ pub struct App<'a> {
78
77
pub activity_type : Option < String > ,
79
78
pub activity : Option < Activity > ,
80
79
pub activity_anchored : Option < Activity > ,
81
- pub activities : Vec < Activity > ,
82
- pub activities_filtered : Vec < Activity > ,
80
+ pub activities : Activities ,
81
+ pub activities_filtered : Activities ,
83
82
84
83
pub info_message : Option < Notification > ,
85
84
pub error_message : Option < Notification > ,
@@ -161,8 +160,8 @@ impl App<'_> {
161
160
} ,
162
161
activity : None ,
163
162
activity_anchored : None ,
164
- activities : vec ! [ ] ,
165
- activities_filtered : vec ! [ ] ,
163
+ activities : Activities :: new ( ) ,
164
+ activities_filtered : Activities :: new ( ) ,
166
165
store,
167
166
168
167
activity_type : None ,
@@ -228,63 +227,22 @@ impl App<'_> {
228
227
229
228
pub async fn reload ( & mut self ) {
230
229
self . activities = self . store . activities ( ) . await ;
231
- let activities = self . activities . clone ( ) ;
232
- self . activities_filtered = activities
233
- . into_iter ( )
234
- . filter ( |a| {
235
- if !a. title . contains ( self . filters . filter . as_str ( ) ) {
236
- return false ;
237
- }
238
- if let Some ( activity_type) = self . activity_type . clone ( ) {
239
- if a. activity_type != activity_type {
240
- return false ;
241
- }
242
- }
243
-
244
- true
245
- } )
246
- . filter ( |a| {
247
- if self . activity_anchored . is_none ( ) {
248
- return true ;
249
- }
250
- let anchored = self . activity_anchored . as_ref ( ) . unwrap ( ) ;
251
- if anchored. polyline ( ) . is_err ( ) || a. polyline ( ) . is_err ( ) {
252
- return false ;
253
- }
254
- compare ( & anchored. polyline ( ) . unwrap ( ) , & a. polyline ( ) . unwrap ( ) , 100 )
255
- < self . filters . anchor_tolerance
256
- } )
257
- . collect ( )
230
+ self . activities_filtered = self . activities . where_title_contains ( self . filters . filter . as_str ( ) ) ;
231
+ if let Some ( activity_type) = self . activity_type . clone ( ) {
232
+ self . activities_filtered = self . activities_filtered . having_activity_type ( activity_type) ;
233
+ }
234
+ if let Some ( anchored) = & self . activity_anchored {
235
+ self . activities_filtered = self . activities_filtered . withing_distance_of ( anchored, self . filters . anchor_tolerance ) ;
236
+ }
258
237
}
259
238
260
- // TODO: Add a collection object
261
- pub fn unsorted_filtered_activities ( & self ) -> Vec < Activity > {
239
+ pub fn unsorted_filtered_activities ( & self ) -> Activities {
262
240
self . activities_filtered . clone ( )
263
241
}
264
242
265
- pub fn filtered_activities ( & self ) -> Vec < Activity > {
266
- let mut activities = self . unsorted_filtered_activities ( ) ;
267
- activities. sort_by ( |a, b| {
268
- let ordering = match self . filters . sort_by {
269
- SortBy :: Date => a. id . cmp ( & b. id ) ,
270
- SortBy :: Distance => a
271
- . distance
272
- . partial_cmp ( & b. distance )
273
- . unwrap_or ( Ordering :: Less ) ,
274
- SortBy :: Pace => a. kmph ( ) . partial_cmp ( & b. kmph ( ) ) . unwrap_or ( Ordering :: Less ) ,
275
- SortBy :: HeartRate => a
276
- . average_heartrate
277
- . or ( Some ( 0.0 ) )
278
- . partial_cmp ( & b. average_heartrate . or ( Some ( 0.0 ) ) )
279
- . unwrap ( ) ,
280
- SortBy :: Time => a. moving_time . partial_cmp ( & b. moving_time ) . unwrap ( ) ,
281
- } ;
282
- match self . filters . sort_order {
283
- SortOrder :: Asc => ordering,
284
- SortOrder :: Desc => ordering. reverse ( ) ,
285
- }
286
- } ) ;
287
- activities
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 )
288
246
}
289
247
290
248
fn draw < B : Backend > ( & mut self , f : & mut Frame < B > ) -> Result < ( ) , anyhow:: Error > {
0 commit comments