Introduce sorting structs

This commit is contained in:
Reinout Meliesie 2025-01-31 16:24:22 +01:00
commit e2330cae50
Signed by: zedfrigg
GPG key ID: 3AFCC06481308BC6
5 changed files with 106 additions and 71 deletions

View file

@ -23,56 +23,56 @@ pub struct CollatedSeriesGrid {
}
impl CollatedFilmsGrid {
pub fn new ( films : Vec <Film> , sorting : FilmsSortedBy , direction : SortingDirection ) -> Self {
pub fn new ( films : Vec <Film> , sorting : FilmsSorting ) -> Self {
let grid_widget = create_flow_box () ;
let film_widget_pairs = RefCell :: new ( vec ! () ) ;
let component = Self { film_widget_pairs , grid_widget } ;
component . set_films ( films , sorting , direction ) ;
component . set_films ( films , sorting ) ;
component
}
pub fn set_films ( & self , films : Vec <Film> , sorting : FilmsSortedBy , direction : SortingDirection ) {
pub fn set_films ( & self , films : Vec <Film> , sorting : FilmsSorting ) {
let widgets = films . iter ()
. map (create_film_entry)
. collect :: < Vec <_> > () ;
* self . film_widget_pairs . borrow_mut () = zip ( films , widgets )
. collect () ;
for ( _ , film_widget ) in self . sort_film_widget_pairs ( sorting , direction ) {
for ( _ , film_widget ) in self . sort_film_widget_pairs (sorting) {
self . grid_widget . append ( & film_widget ) ;
}
}
pub fn set_sorting ( & self , sorting : FilmsSortedBy , direction : SortingDirection ) {
pub fn set_sorting ( & self , sorting : FilmsSorting ) {
self . grid_widget . remove_all () ;
for ( _ , film_widget ) in self . sort_film_widget_pairs ( sorting , direction ) {
for ( _ , film_widget ) in self . sort_film_widget_pairs (sorting) {
self . grid_widget . append ( & film_widget ) ;
}
}
fn sort_film_widget_pairs ( & self , sorting : FilmsSortedBy , direction : SortingDirection ) -> Vec < ( Film , Box ) > {
fn sort_film_widget_pairs ( & self , sorting : FilmsSorting ) -> Vec < ( Film , Box ) > {
let mut sorted = Vec :: from (
self . film_widget_pairs . borrow () . as_slice () ) ;
sorted . sort_by ( | ( film_1 , _ ) , ( film_2 , _ ) | match sorting {
FilmsSortedBy :: Name =>
sorted . sort_by ( | ( film_1 , _ ) , ( film_2 , _ ) | match sorting . property {
FilmProperty :: Name =>
film_1 . name . cmp ( & film_2 . name ) ,
FilmsSortedBy :: ReleaseDate =>
FilmProperty :: ReleaseDate =>
film_1 . release_date . cmp ( & film_2 . release_date ) ,
FilmsSortedBy :: Runtime =>
FilmProperty :: Runtime =>
film_1 . runtime_minutes . cmp ( & film_2 . runtime_minutes ) ,
} ) ;
if direction == SortingDirection :: Descending { sorted . reverse () }
if sorting . direction == SortingDirection :: Descending { sorted . reverse () }
// See it, say it, ...
sorted
}
}
impl CollatedSeriesGrid {
pub fn new ( series : Vec <Series> , sorting : SeriesSortedBy ) -> Self {
pub fn new ( series : Vec <Series> , sorting : SeriesSorting ) -> Self {
let grid_widget = create_flow_box () ;
let series_widget_pairs = RefCell :: new ( vec ! () ) ;
@ -82,7 +82,7 @@ impl CollatedSeriesGrid {
component
}
pub fn set_series ( & self , series : Vec <Series> , sorting : SeriesSortedBy ) {
pub fn set_series ( & self , series : Vec <Series> , sorting : SeriesSorting ) {
let widgets = series . iter ()
. map (create_series_entry)
. collect :: < Vec <_> > () ;
@ -94,7 +94,7 @@ impl CollatedSeriesGrid {
}
}
pub fn set_sorting ( & self , sorting : SeriesSortedBy ) {
pub fn set_sorting ( & self , sorting : SeriesSorting ) {
self . grid_widget . remove_all () ;
for ( _ , series_widget ) in self . sort_series_widget_pairs (sorting) {
@ -102,13 +102,13 @@ impl CollatedSeriesGrid {
}
}
fn sort_series_widget_pairs ( & self , sorting : SeriesSortedBy ) -> Vec < ( Series , Box ) > {
fn sort_series_widget_pairs ( & self , sorting : SeriesSorting ) -> Vec < ( Series , Box ) > {
let mut sorted = Vec :: from (
self . series_widget_pairs . borrow () . as_slice () ) ;
sorted . sort_by ( | ( series_1 , _ ) , ( series_2 , _ ) | match sorting {
SeriesSortedBy :: Name => series_1 . name . cmp ( & series_2 . name ) ,
SeriesSortedBy :: FirstReleaseDate => todo ! () ,
sorted . sort_by ( | ( series_1 , _ ) , ( series_2 , _ ) | match sorting . property {
SeriesProperty :: Name => series_1 . name . cmp ( & series_2 . name ) ,
SeriesProperty :: FirstReleaseDate => todo ! () ,
} ) ;
sorted