Sorting films now uses latest films vec, but it hangs the UI

This commit is contained in:
Reinout Meliesie 2024-11-26 16:49:19 +01:00
parent f912281700
commit 988d4016ea
Signed by: zedfrigg
GPG key ID: 3AFCC06481308BC6
3 changed files with 28 additions and 23 deletions

View file

@ -7,7 +7,7 @@ pub struct Collection {
pub series : Vec <Series> , pub series : Vec <Series> ,
} }
pub struct Film { # [ derive (Clone) ] pub struct Film {
pub uuid : String , pub uuid : String ,
pub name : String , pub name : String ,
pub original_name : Option <String> , pub original_name : Option <String> ,

View file

@ -10,14 +10,14 @@ use crate :: {
} ; } ;
pub enum FilmsSortedBy { Name , ReleaseDate , Runtime } pub enum FilmsSortedBy { Name , ReleaseDate , Runtime }
pub enum SeriesSortedBy { Name , FirstReleaseDate } pub enum SeriesSortedBy { Name , FirstReleaseDate }
pub struct FilmsContainer { pub struct FilmsContainer {
films : Vec <Film> , films : & 'static RefCell < Vec <Film> > ,
flow_box : FilmsFlowBox , flow_box : & 'static FilmsFlowBox ,
widget : gtk4 :: Box , widget : gtk4 :: Box ,
sorted_by : & 'static Cell <FilmsSortedBy> ,
} }
pub struct SeriesContainer { pub struct SeriesContainer {
series : Vec <Series> , series : Vec <Series> ,
@ -27,21 +27,21 @@ pub struct SeriesContainer {
impl FilmsContainer { impl FilmsContainer {
pub fn new ( films : Vec <Film> ) -> Self { pub fn new ( films : Vec <Film> ) -> Self {
let flow_box = FilmsFlowBox :: new ( films . as_slice () ) ; let films = leak ( RefCell :: new (films) ) ;
let sorted_by = leak ( Cell :: new ( FilmsSortedBy :: Name ) ) ; let flow_box = leak ( FilmsFlowBox :: new ( films . borrow () . as_slice () ) ) ;
let widget = create_vertical_box ! ( let widget = create_vertical_box ! (
& create_film_collection_menu ( |new_sorted_by| { & create_film_collection_menu ( |sorted_by| {
sorted_by . replace (new_sorted_by) ; flow_box . set_films ( films . borrow () . as_slice () ) ;
} ) , } ) ,
& create_collection_scrolled_window ( flow_box . get_widget () ) , & create_collection_scrolled_window ( flow_box . get_widget () ) ,
) ; ) ;
Self { films , flow_box , widget , sorted_by } Self { films , flow_box , widget }
} }
pub fn set_films ( & mut self , films : Vec <Film> ) { pub fn set_films ( & self , films : Vec <Film> ) {
self . films = films ; * self . films . borrow_mut () = films ;
self . flow_box . set_films ( self . films . as_slice () ) ; self . flow_box . set_films ( self . films . borrow () . as_slice () ) ;
} }
pub fn get_widget ( & self ) -> & gtk4 :: Box { & self . widget } pub fn get_widget ( & self ) -> & gtk4 :: Box { & self . widget }

View file

@ -4,6 +4,7 @@ use {
Align :: * , Align :: * ,
Orientation :: * , Orientation :: * ,
gdk :: Texture , gdk :: Texture ,
glib :: * ,
prelude :: * , prelude :: * ,
} , } ,
libadwaita :: { * , ViewSwitcherPolicy :: * } , libadwaita :: { * , ViewSwitcherPolicy :: * } ,
@ -62,7 +63,12 @@ impl FilmsFlowBox {
pub fn set_films ( & self , films : & [Film] ) { pub fn set_films ( & self , films : & [Film] ) {
self . widget . remove_all () ; self . widget . remove_all () ;
for film in films { for film in films {
self . widget . append ( & create_film_item (film) ) ; let widget = self . widget . clone () ;
let film = film . clone () ;
spawn_future_local ( async move {
widget . append ( & create_film_item ( & film ) ) ;
} ) ;
} }
} }
@ -155,16 +161,15 @@ fn create_collection_item (
. build () ; . build () ;
if let Some (poster_file_path) = poster_file_path { if let Some (poster_file_path) = poster_file_path {
if let Ok (poster_texture) = Texture :: from_filename (poster_file_path) { let poster_texture = Texture :: from_filename (poster_file_path) . unwrap () ;
container . append ( container . append (
& Image :: builder () & Image :: builder ()
. paintable ( & poster_texture ) . paintable ( & poster_texture )
. width_request (300) . width_request (300)
. height_request (300) . height_request (300)
. margin_bottom (10) . margin_bottom (10)
. build () . build ()
) ; ) ;
}
} }
container . append ( container . append (