More logic for sorting direction
This commit is contained in:
parent
de24bbcf99
commit
5cccefbd4a
3 changed files with 33 additions and 27 deletions
|
@ -25,10 +25,9 @@ pub struct CollatableSeriesContainer {
|
||||||
impl CollatableFilmsContainer {
|
impl CollatableFilmsContainer {
|
||||||
pub fn new ( films : Vec <Film> ) -> Self {
|
pub fn new ( films : Vec <Film> ) -> Self {
|
||||||
let collated_grid = leak (
|
let collated_grid = leak (
|
||||||
CollatedFilmsGrid :: new ( films , FilmsSortedBy :: Name ) ) ;
|
CollatedFilmsGrid :: new ( films , FilmsSortedBy :: Name , SortingDirection :: Ascending ) ) ;
|
||||||
let film_collation_menu = FilmCollationMenu :: new ( | sorted_by , direction | {
|
let film_collation_menu = FilmCollationMenu :: new ( | sorting , direction |
|
||||||
collated_grid . set_sorting (sorted_by) ;
|
collated_grid . set_sorting ( sorting , direction ) ) ;
|
||||||
} ) ;
|
|
||||||
|
|
||||||
let widget = create_vertical_box ! (
|
let widget = create_vertical_box ! (
|
||||||
film_collation_menu . get_widget () ,
|
film_collation_menu . get_widget () ,
|
||||||
|
@ -39,7 +38,7 @@ impl CollatableFilmsContainer {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_films ( & self , films : Vec <Film> ) {
|
pub fn set_films ( & self , films : Vec <Film> ) {
|
||||||
self . collated_grid . set_films ( films , FilmsSortedBy :: Name ) ;
|
self . collated_grid . set_films ( films , FilmsSortedBy :: Name , SortingDirection :: Ascending ) ;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
impl CollatableSeriesContainer {
|
impl CollatableSeriesContainer {
|
||||||
|
|
|
@ -13,9 +13,9 @@ use crate :: { collection :: * , ui :: component :: * } ;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
pub enum FilmsSortedBy { Name , ReleaseDate , Runtime }
|
# [ derive (PartialEq) ] pub enum FilmsSortedBy { Name , ReleaseDate , Runtime }
|
||||||
pub enum SeriesSortedBy { Name , FirstReleaseDate }
|
# [ derive (PartialEq) ] pub enum SeriesSortedBy { Name , FirstReleaseDate }
|
||||||
pub enum SortingDirection { Ascending , Descending }
|
# [ derive (PartialEq) ] pub enum SortingDirection { Ascending , Descending }
|
||||||
|
|
||||||
pub struct CollatedFilmsGrid {
|
pub struct CollatedFilmsGrid {
|
||||||
film_widget_pairs : RefCell < Vec < ( Film , Box ) > > ,
|
film_widget_pairs : RefCell < Vec < ( Film , Box ) > > ,
|
||||||
|
@ -27,37 +27,37 @@ pub struct CollatedSeriesGrid {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl CollatedFilmsGrid {
|
impl CollatedFilmsGrid {
|
||||||
pub fn new ( films : Vec <Film> , sorting : FilmsSortedBy ) -> Self {
|
pub fn new ( films : Vec <Film> , sorting : FilmsSortedBy , direction : SortingDirection ) -> Self {
|
||||||
let grid_widget = create_flow_box () ;
|
let grid_widget = create_flow_box () ;
|
||||||
let film_widget_pairs = RefCell :: new ( vec ! () ) ;
|
let film_widget_pairs = RefCell :: new ( vec ! () ) ;
|
||||||
|
|
||||||
let component = Self { film_widget_pairs , grid_widget } ;
|
let component = Self { film_widget_pairs , grid_widget } ;
|
||||||
component . set_films ( films , sorting ) ;
|
component . set_films ( films , sorting , direction ) ;
|
||||||
|
|
||||||
component
|
component
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_films ( & self , films : Vec <Film> , sorting : FilmsSortedBy ) {
|
pub fn set_films ( & self , films : Vec <Film> , sorting : FilmsSortedBy , direction : SortingDirection ) {
|
||||||
let widgets = films . iter ()
|
let widgets = films . iter ()
|
||||||
. map (create_film_entry)
|
. map (create_film_entry)
|
||||||
. collect :: < Vec <_> > () ;
|
. collect :: < Vec <_> > () ;
|
||||||
* self . film_widget_pairs . borrow_mut () = zip ( films , widgets )
|
* self . film_widget_pairs . borrow_mut () = zip ( films , widgets )
|
||||||
. collect () ;
|
. collect () ;
|
||||||
|
|
||||||
for ( _ , film_widget ) in self . sort_film_widget_pairs (sorting) {
|
for ( _ , film_widget ) in self . sort_film_widget_pairs ( sorting , direction ) {
|
||||||
self . grid_widget . append ( & film_widget ) ;
|
self . grid_widget . append ( & film_widget ) ;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_sorting ( & self , sorting : FilmsSortedBy ) {
|
pub fn set_sorting ( & self , sorting : FilmsSortedBy , direction : SortingDirection ) {
|
||||||
self . grid_widget . remove_all () ;
|
self . grid_widget . remove_all () ;
|
||||||
|
|
||||||
for ( _ , film_widget ) in self . sort_film_widget_pairs (sorting) {
|
for ( _ , film_widget ) in self . sort_film_widget_pairs ( sorting , direction ) {
|
||||||
self . grid_widget . append ( & film_widget ) ;
|
self . grid_widget . append ( & film_widget ) ;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn sort_film_widget_pairs ( & self , sorting : FilmsSortedBy ) -> Vec < ( Film , Box ) > {
|
fn sort_film_widget_pairs ( & self , sorting : FilmsSortedBy , direction : SortingDirection ) -> Vec < ( Film , Box ) > {
|
||||||
let mut sorted = Vec :: from (
|
let mut sorted = Vec :: from (
|
||||||
self . film_widget_pairs . borrow () . as_slice () ) ;
|
self . film_widget_pairs . borrow () . as_slice () ) ;
|
||||||
|
|
||||||
|
@ -69,7 +69,9 @@ impl CollatedFilmsGrid {
|
||||||
FilmsSortedBy :: Runtime =>
|
FilmsSortedBy :: Runtime =>
|
||||||
film_1 . runtime_minutes . cmp ( & film_2 . runtime_minutes ) ,
|
film_1 . runtime_minutes . cmp ( & film_2 . runtime_minutes ) ,
|
||||||
} ) ;
|
} ) ;
|
||||||
|
if direction == SortingDirection :: Descending { sorted . reverse () }
|
||||||
|
|
||||||
|
// See it, say it, ...
|
||||||
sorted
|
sorted
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,16 +6,19 @@ use {
|
||||||
prelude :: * ,
|
prelude :: * ,
|
||||||
} ,
|
} ,
|
||||||
libadwaita :: * ,
|
libadwaita :: * ,
|
||||||
std :: cell :: * ,
|
std :: { cell :: * , ops :: * } ,
|
||||||
} ;
|
} ;
|
||||||
|
|
||||||
use crate :: ui :: { collated_grid :: * , component :: * , utility :: * } ;
|
use crate :: {
|
||||||
|
ui :: { collated_grid :: * , component :: * , utility :: * } ,
|
||||||
|
utility :: * ,
|
||||||
|
} ;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
pub struct FilmCollationMenu {
|
pub struct FilmCollationMenu {
|
||||||
widget : Box ,
|
widget : Box ,
|
||||||
sorting : RefCell < ( FilmsSortedBy , SortingDirection ) > ,
|
sorting : & 'static RefCell < ( FilmsSortedBy , SortingDirection ) > ,
|
||||||
}
|
}
|
||||||
pub struct SeriesCollationMenu { widget : Box }
|
pub struct SeriesCollationMenu { widget : Box }
|
||||||
|
|
||||||
|
@ -28,12 +31,11 @@ impl FilmCollationMenu {
|
||||||
. name ("film-collation-menu")
|
. name ("film-collation-menu")
|
||||||
. css_classes ( ["toolbar"] )
|
. css_classes ( ["toolbar"] )
|
||||||
. build () ;
|
. build () ;
|
||||||
|
let sorting = leak ( RefCell :: new ( ( FilmsSortedBy :: Name , SortingDirection :: Ascending ) ) ) ;
|
||||||
|
|
||||||
widget . append ( & create_sort_button ( & create_films_sort_menu (on_sort) ) ) ;
|
widget . append ( & create_sort_button ( & create_films_sort_menu ( sorting , on_sort ) ) ) ;
|
||||||
widget . append ( & create_filter_button () ) ;
|
widget . append ( & create_filter_button () ) ;
|
||||||
|
|
||||||
let sorting = RefCell :: new ( ( FilmsSortedBy :: Name , SortingDirection :: Ascending ) ) ;
|
|
||||||
|
|
||||||
Self { widget , sorting }
|
Self { widget , sorting }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -67,7 +69,10 @@ fn create_sort_button ( sort_menu : & Popover ) -> SplitButton {
|
||||||
. build ()
|
. build ()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn create_films_sort_menu < F : Fn ( FilmsSortedBy , SortingDirection ) + 'static > ( on_sort : F ) -> Popover {
|
fn create_films_sort_menu < F : Fn ( FilmsSortedBy , SortingDirection ) + 'static > (
|
||||||
|
sorting : & 'static RefCell < ( FilmsSortedBy , SortingDirection ) > ,
|
||||||
|
on_sort : F ,
|
||||||
|
) -> Popover {
|
||||||
let container = ListBox :: new () ;
|
let container = ListBox :: new () ;
|
||||||
|
|
||||||
container . append ( & create_sort_menu_entry ( "Name" , false ) ) ;
|
container . append ( & create_sort_menu_entry ( "Name" , false ) ) ;
|
||||||
|
@ -76,15 +81,15 @@ fn create_films_sort_menu < F : Fn ( FilmsSortedBy , SortingDirection ) + 'stati
|
||||||
|
|
||||||
container . select_row ( container . row_at_index (0) . as_ref () ) ;
|
container . select_row ( container . row_at_index (0) . as_ref () ) ;
|
||||||
|
|
||||||
container . connect_row_activated ( move | _ , row | on_sort (
|
container . connect_row_activated ( move | _ , row | {
|
||||||
match row . index () {
|
let sorting_property = match row . index () {
|
||||||
0 => FilmsSortedBy :: Name ,
|
0 => FilmsSortedBy :: Name ,
|
||||||
1 => FilmsSortedBy :: ReleaseDate ,
|
1 => FilmsSortedBy :: ReleaseDate ,
|
||||||
2 => FilmsSortedBy :: Runtime ,
|
2 => FilmsSortedBy :: Runtime ,
|
||||||
_ => panic ! () ,
|
_ => panic ! () ,
|
||||||
} ,
|
} ;
|
||||||
SortingDirection :: Ascending ,
|
on_sort ( sorting_property , SortingDirection :: Ascending )
|
||||||
) ) ;
|
} ) ;
|
||||||
|
|
||||||
Popover :: builder ()
|
Popover :: builder ()
|
||||||
. child ( & container )
|
. child ( & container )
|
||||||
|
|
Loading…
Add table
Reference in a new issue