Replace single data model with data management struct and views
This commit is contained in:
parent
c14d2ab6f1
commit
7610b291a4
6 changed files with 88 additions and 96 deletions
|
@ -1,38 +0,0 @@
|
||||||
use std :: path :: * ;
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
pub struct Collection {
|
|
||||||
pub films : Vec <Film> ,
|
|
||||||
pub series : Vec <Series> ,
|
|
||||||
}
|
|
||||||
|
|
||||||
# [ derive (Clone) ] pub struct Film {
|
|
||||||
pub uuid : String ,
|
|
||||||
pub name : String ,
|
|
||||||
pub original_name : Option <String> ,
|
|
||||||
pub release_date : String , // TODO: Switch to chrono types, I think rusqlite has crate option for it
|
|
||||||
pub runtime_minutes : u32 ,
|
|
||||||
pub poster_file_path : Option <PathBuf> ,
|
|
||||||
}
|
|
||||||
|
|
||||||
# [ derive (Clone) ] pub struct Series {
|
|
||||||
pub uuid : String ,
|
|
||||||
pub name : String ,
|
|
||||||
pub original_name : Option <String> ,
|
|
||||||
pub poster_file_path : Option <PathBuf> ,
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct Season <'l> {
|
|
||||||
pub uuid : String ,
|
|
||||||
pub name : Option <String> ,
|
|
||||||
pub series : & 'l Series ,
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct Episode <'l> {
|
|
||||||
pub uuid : String ,
|
|
||||||
pub name : Option <String> ,
|
|
||||||
pub release_date : String ,
|
|
||||||
pub season : & 'l Season <'l> ,
|
|
||||||
pub video_file_path : Option <PathBuf> ,
|
|
||||||
}
|
|
|
@ -1,4 +1,3 @@
|
||||||
mod collection ;
|
|
||||||
mod error ;
|
mod error ;
|
||||||
mod persistence ;
|
mod persistence ;
|
||||||
mod ui ;
|
mod ui ;
|
||||||
|
@ -44,8 +43,9 @@ fn show_window ( app : & Application ) {
|
||||||
spawn_future_local ( async move {
|
spawn_future_local ( async move {
|
||||||
async_unit_result_context (
|
async_unit_result_context (
|
||||||
async {
|
async {
|
||||||
let collection = get_collection_from_file () . await ? ;
|
let collection = Collection :: new () . await ? ;
|
||||||
ui . render_collection (collection) ;
|
let collection_overview = collection . get_overview () . await ;
|
||||||
|
ui . render_collection_overview (collection_overview) ;
|
||||||
Ok (())
|
Ok (())
|
||||||
} ,
|
} ,
|
||||||
|error| {
|
|error| {
|
||||||
|
|
|
@ -8,18 +8,24 @@ use {
|
||||||
std :: path :: * ,
|
std :: path :: * ,
|
||||||
} ;
|
} ;
|
||||||
|
|
||||||
use crate :: { collection :: * , error :: { * , ZoodexError :: * } } ;
|
use crate :: error :: { * , ZoodexError :: * } ;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
pub async fn get_collection_from_file () -> Result <Collection> {
|
pub struct Collection { sqlite_client : Client }
|
||||||
|
|
||||||
|
impl Collection {
|
||||||
|
pub async fn new () -> Result <Collection> {
|
||||||
let sqlite_client = ClientBuilder :: new ()
|
let sqlite_client = ClientBuilder :: new ()
|
||||||
. path ("zoodex.sqlite")
|
. path ("zoodex.sqlite")
|
||||||
. flags ( OpenFlags :: SQLITE_OPEN_READ_ONLY | OpenFlags :: SQLITE_OPEN_NO_MUTEX )
|
. flags ( OpenFlags :: SQLITE_OPEN_READ_WRITE | OpenFlags :: SQLITE_OPEN_NO_MUTEX )
|
||||||
. open ()
|
. open ()
|
||||||
. await ? ;
|
. await ? ;
|
||||||
|
Ok ( Self { sqlite_client } )
|
||||||
|
}
|
||||||
|
|
||||||
let collection = sqlite_client . conn ( |sqlite_connection| {
|
pub async fn get_overview ( & self ) -> CollectionOverview {
|
||||||
|
self . sqlite_client . conn ( |sqlite_connection| {
|
||||||
let films = sqlite_connection
|
let films = sqlite_connection
|
||||||
. prepare ( "
|
. prepare ( "
|
||||||
select uuid , name , original_name , release_date , runtime_minutes , poster_file_path
|
select uuid , name , original_name , release_date , runtime_minutes , poster_file_path
|
||||||
|
@ -27,23 +33,48 @@ pub async fn get_collection_from_file () -> Result <Collection> {
|
||||||
order by release_date desc
|
order by release_date desc
|
||||||
" ) ?
|
" ) ?
|
||||||
. query (()) ?
|
. query (()) ?
|
||||||
. map (row_to_film)
|
. map (row_to_film_overview)
|
||||||
. collect () ? ;
|
. collect () ? ;
|
||||||
|
|
||||||
let series = sqlite_connection
|
let series = sqlite_connection
|
||||||
. prepare ( "
|
. prepare ( "
|
||||||
select uuid , name , original_name , poster_file_path
|
select series . uuid , series . name , series . original_name , series . poster_file_path ,
|
||||||
from series
|
min ( episodes . release_date )
|
||||||
|
from series , seasons , episodes
|
||||||
|
where series . uuid = seasons . series and seasons . uuid = episodes . season
|
||||||
|
group by series . uuid
|
||||||
" ) ?
|
" ) ?
|
||||||
. query (()) ?
|
. query (()) ?
|
||||||
. map (row_to_series)
|
. map (row_to_series_overview)
|
||||||
. collect () ? ;
|
. collect () ? ;
|
||||||
Ok ( Collection { films , series } )
|
|
||||||
} ) . await ? ;
|
|
||||||
|
|
||||||
Ok (collection)
|
Ok ( CollectionOverview { films , series } )
|
||||||
|
} ) . await . unwrap ()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn row_to_film ( row : & Row ) -> rusqlite :: Result <Film> {
|
pub struct CollectionOverview {
|
||||||
|
pub films : Vec <FilmOverview> ,
|
||||||
|
pub series : Vec <SeriesOverview> ,
|
||||||
|
}
|
||||||
|
|
||||||
|
# [ derive (Clone) ] pub struct FilmOverview {
|
||||||
|
pub uuid : String ,
|
||||||
|
pub name : String ,
|
||||||
|
pub original_name : Option <String> ,
|
||||||
|
pub release_date : String , // TODO: Switch to chrono types, I think rusqlite has crate option for it
|
||||||
|
pub runtime_minutes : u32 ,
|
||||||
|
pub poster_file_path : Option <PathBuf> ,
|
||||||
|
}
|
||||||
|
# [ derive (Clone) ] pub struct SeriesOverview {
|
||||||
|
pub uuid : String ,
|
||||||
|
pub name : String ,
|
||||||
|
pub original_name : Option <String> ,
|
||||||
|
pub first_release_date : String , // TODO: Switch to chrono types, I think rusqlite has crate option for it
|
||||||
|
pub poster_file_path : Option <PathBuf> ,
|
||||||
|
}
|
||||||
|
|
||||||
|
fn row_to_film_overview ( row : & Row ) -> rusqlite :: Result <FilmOverview> {
|
||||||
let uuid = row . get (0) ? ;
|
let uuid = row . get (0) ? ;
|
||||||
let name = row . get (1) ? ;
|
let name = row . get (1) ? ;
|
||||||
let original_name = row . get (2) ? ;
|
let original_name = row . get (2) ? ;
|
||||||
|
@ -52,7 +83,7 @@ fn row_to_film ( row : & Row ) -> rusqlite :: Result <Film> {
|
||||||
let poster_file_path = row . get :: < _ , Option <String> > (5) ?
|
let poster_file_path = row . get :: < _ , Option <String> > (5) ?
|
||||||
. map ( PathBuf :: from ) ;
|
. map ( PathBuf :: from ) ;
|
||||||
|
|
||||||
Ok ( Film {
|
Ok ( FilmOverview {
|
||||||
uuid ,
|
uuid ,
|
||||||
name ,
|
name ,
|
||||||
original_name ,
|
original_name ,
|
||||||
|
@ -61,15 +92,15 @@ fn row_to_film ( row : & Row ) -> rusqlite :: Result <Film> {
|
||||||
poster_file_path ,
|
poster_file_path ,
|
||||||
} )
|
} )
|
||||||
}
|
}
|
||||||
|
fn row_to_series_overview ( row : & Row ) -> rusqlite :: Result <SeriesOverview> {
|
||||||
fn row_to_series ( row : & Row ) -> rusqlite :: Result <Series> {
|
|
||||||
let uuid = row . get (0) ? ;
|
let uuid = row . get (0) ? ;
|
||||||
let name = row . get (1) ? ;
|
let name = row . get (1) ? ;
|
||||||
let original_name = row . get (2) ? ;
|
let original_name = row . get (2) ? ;
|
||||||
let poster_file_path = row. get :: < _ , Option <String> > (3) ?
|
let poster_file_path = row. get :: < _ , Option <String> > (3) ?
|
||||||
. map ( PathBuf :: from ) ;
|
. map ( PathBuf :: from ) ;
|
||||||
|
let first_release_date = row . get (4) ? ;
|
||||||
|
|
||||||
Ok ( Series { uuid , name , original_name , poster_file_path } )
|
Ok ( SeriesOverview { uuid , name , original_name , first_release_date , poster_file_path } )
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From <Error> for ZoodexError {
|
impl From <Error> for ZoodexError {
|
||||||
|
|
|
@ -9,21 +9,21 @@ use {
|
||||||
std :: { cell :: * , iter :: * , path :: Path } ,
|
std :: { cell :: * , iter :: * , path :: Path } ,
|
||||||
} ;
|
} ;
|
||||||
|
|
||||||
use crate :: { collection :: * , ui :: { collatable_container :: * , component :: * } } ;
|
use crate :: ui :: { collatable_container :: * , component :: * } ;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
pub struct CollatedFilmsGrid {
|
pub struct CollatedFilmsGrid {
|
||||||
film_widget_pairs : RefCell < Vec < ( Film , Box ) > > ,
|
film_widget_pairs : RefCell < Vec < ( FilmOverview , Box ) > > ,
|
||||||
grid_widget : FlowBox ,
|
grid_widget : FlowBox ,
|
||||||
}
|
}
|
||||||
pub struct CollatedSeriesGrid {
|
pub struct CollatedSeriesGrid {
|
||||||
series_widget_pairs : RefCell < Vec < ( Series , Box ) > > ,
|
series_widget_pairs : RefCell < Vec < ( SeriesOverview , Box ) > > ,
|
||||||
grid_widget : FlowBox ,
|
grid_widget : FlowBox ,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl CollatedFilmsGrid {
|
impl CollatedFilmsGrid {
|
||||||
pub fn new ( films : Vec <Film> , sorting : FilmsSorting ) -> Self {
|
pub fn new ( films : Vec <FilmOverview> , sorting : FilmsSorting ) -> Self {
|
||||||
let grid_widget = flow_box ! (
|
let grid_widget = flow_box ! (
|
||||||
@ orientation : Horizontal ;
|
@ orientation : Horizontal ;
|
||||||
@ homogeneous : true ;
|
@ homogeneous : true ;
|
||||||
|
@ -37,7 +37,7 @@ impl CollatedFilmsGrid {
|
||||||
component
|
component
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_films ( & self , films : Vec <Film> , sorting : FilmsSorting ) {
|
pub fn set_films ( & self , films : Vec <FilmOverview> , sorting : FilmsSorting ) {
|
||||||
let widgets = films . iter ()
|
let widgets = films . iter ()
|
||||||
. map (create_film_entry)
|
. map (create_film_entry)
|
||||||
. collect :: < Vec <_> > () ;
|
. collect :: < Vec <_> > () ;
|
||||||
|
@ -57,7 +57,7 @@ impl CollatedFilmsGrid {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn sort_film_widget_pairs ( & self , sorting : FilmsSorting ) -> Vec < ( Film , Box ) > {
|
fn sort_film_widget_pairs ( & self , sorting : FilmsSorting ) -> Vec < ( FilmOverview , 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 () ) ;
|
||||||
|
|
||||||
|
@ -76,7 +76,7 @@ impl CollatedFilmsGrid {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
impl CollatedSeriesGrid {
|
impl CollatedSeriesGrid {
|
||||||
pub fn new ( series : Vec <Series> , sorting : SeriesSorting ) -> Self {
|
pub fn new ( series : Vec <SeriesOverview> , sorting : SeriesSorting ) -> Self {
|
||||||
let grid_widget = flow_box ! (
|
let grid_widget = flow_box ! (
|
||||||
@ orientation : Horizontal ;
|
@ orientation : Horizontal ;
|
||||||
@ homogeneous : true ;
|
@ homogeneous : true ;
|
||||||
|
@ -90,7 +90,7 @@ impl CollatedSeriesGrid {
|
||||||
component
|
component
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_series ( & self , series : Vec <Series> , sorting : SeriesSorting ) {
|
pub fn set_series ( & self , series : Vec <SeriesOverview> , sorting : SeriesSorting ) {
|
||||||
let widgets = series . iter ()
|
let widgets = series . iter ()
|
||||||
. map (create_series_entry)
|
. map (create_series_entry)
|
||||||
. collect :: < Vec <_> > () ;
|
. collect :: < Vec <_> > () ;
|
||||||
|
@ -110,7 +110,7 @@ impl CollatedSeriesGrid {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn sort_series_widget_pairs ( & self , sorting : SeriesSorting ) -> Vec < ( Series , Box ) > {
|
fn sort_series_widget_pairs ( & self , sorting : SeriesSorting ) -> Vec < ( SeriesOverview , Box ) > {
|
||||||
let mut sorted = Vec :: from (
|
let mut sorted = Vec :: from (
|
||||||
self . series_widget_pairs . borrow () . as_slice () ) ;
|
self . series_widget_pairs . borrow () . as_slice () ) ;
|
||||||
|
|
||||||
|
@ -134,7 +134,7 @@ impl Component <FlowBox> for CollatedSeriesGrid {
|
||||||
fn get_widget ( & self ) -> & FlowBox { & self . grid_widget }
|
fn get_widget ( & self ) -> & FlowBox { & self . grid_widget }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn create_film_entry ( film : & Film ) -> Box {
|
pub fn create_film_entry ( film : & FilmOverview ) -> Box {
|
||||||
create_collection_item (
|
create_collection_item (
|
||||||
film . name . as_str () ,
|
film . name . as_str () ,
|
||||||
film . original_name . as_deref () ,
|
film . original_name . as_deref () ,
|
||||||
|
@ -142,7 +142,7 @@ pub fn create_film_entry ( film : & Film ) -> Box {
|
||||||
& create_film_details (film) ,
|
& create_film_details (film) ,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
pub fn create_series_entry ( series : & Series ) -> Box {
|
pub fn create_series_entry ( series : & SeriesOverview ) -> Box {
|
||||||
create_collection_item (
|
create_collection_item (
|
||||||
series . name . as_str () ,
|
series . name . as_str () ,
|
||||||
series . original_name . as_deref () ,
|
series . original_name . as_deref () ,
|
||||||
|
@ -201,7 +201,7 @@ fn create_collection_item (
|
||||||
container
|
container
|
||||||
}
|
}
|
||||||
|
|
||||||
fn create_film_details ( film : & Film ) -> Box {
|
fn create_film_details ( film : & FilmOverview ) -> Box {
|
||||||
g_box ! (
|
g_box ! (
|
||||||
@ orientation : Horizontal ;
|
@ orientation : Horizontal ;
|
||||||
@ halign : Center ;
|
@ halign : Center ;
|
||||||
|
@ -210,12 +210,11 @@ fn create_film_details ( film : & Film ) -> Box {
|
||||||
label ! ( format ! ( "{}m" , film . runtime_minutes ) . as_str () ) ,
|
label ! ( format ! ( "{}m" , film . runtime_minutes ) . as_str () ) ,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
fn create_series_details ( series : & Series ) -> Box {
|
fn create_series_details ( series : & SeriesOverview ) -> Box {
|
||||||
g_box ! (
|
g_box ! (
|
||||||
@ orientation : Horizontal ;
|
@ orientation : Horizontal ;
|
||||||
@ halign : Center ;
|
@ halign : Center ;
|
||||||
@ spacing : 20 ;
|
@ spacing : 20 ;
|
||||||
// TODO
|
label ! ( series . first_release_date . as_str () ) ,
|
||||||
label ! ("????") ,
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,7 +4,7 @@ mod collation_menu ;
|
||||||
use gtk4 :: { * , Orientation :: * , prelude :: * } ;
|
use gtk4 :: { * , Orientation :: * , prelude :: * } ;
|
||||||
|
|
||||||
use crate :: {
|
use crate :: {
|
||||||
collection :: * ,
|
persistence :: * ,
|
||||||
ui :: {
|
ui :: {
|
||||||
component :: * , utility :: * ,
|
component :: * , utility :: * ,
|
||||||
collatable_container :: { collated_grid :: * , collation_menu :: * } ,
|
collatable_container :: { collated_grid :: * , collation_menu :: * } ,
|
||||||
|
@ -63,7 +63,7 @@ pub struct CollatableSeriesContainer {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl CollatableFilmsContainer {
|
impl CollatableFilmsContainer {
|
||||||
pub fn new ( films : Vec <Film> ) -> Self {
|
pub fn new ( films : Vec <FilmOverview> ) -> Self {
|
||||||
let collated_grid = leak (
|
let collated_grid = leak (
|
||||||
CollatedFilmsGrid :: new ( films , FilmsSorting :: default () ) ) ;
|
CollatedFilmsGrid :: new ( films , FilmsSorting :: default () ) ) ;
|
||||||
let film_collation_menu = FilmCollationMenu :: new ( |sorting|
|
let film_collation_menu = FilmCollationMenu :: new ( |sorting|
|
||||||
|
@ -81,12 +81,12 @@ impl CollatableFilmsContainer {
|
||||||
Self { collated_grid , widget }
|
Self { collated_grid , widget }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_films ( & self , films : Vec <Film> ) {
|
pub fn set_films ( & self , films : Vec <FilmOverview> ) {
|
||||||
self . collated_grid . set_films ( films , FilmsSorting :: default () ) ;
|
self . collated_grid . set_films ( films , FilmsSorting :: default () ) ;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
impl CollatableSeriesContainer {
|
impl CollatableSeriesContainer {
|
||||||
pub fn new ( series : Vec <Series> ) -> Self {
|
pub fn new ( series : Vec <SeriesOverview> ) -> Self {
|
||||||
let collated_grid = leak (
|
let collated_grid = leak (
|
||||||
CollatedSeriesGrid :: new ( series , SeriesSorting :: default () ) ) ;
|
CollatedSeriesGrid :: new ( series , SeriesSorting :: default () ) ) ;
|
||||||
let series_collation_menu = SeriesCollationMenu :: new ( |sorting| {
|
let series_collation_menu = SeriesCollationMenu :: new ( |sorting| {
|
||||||
|
@ -105,7 +105,7 @@ impl CollatableSeriesContainer {
|
||||||
Self { collated_grid , widget }
|
Self { collated_grid , widget }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_series ( & self , series : Vec <Series> ) {
|
pub fn set_series ( & self , series : Vec <SeriesOverview> ) {
|
||||||
self . collated_grid . set_series ( series , SeriesSorting :: default () ) ;
|
self . collated_grid . set_series ( series , SeriesSorting :: default () ) ;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,7 +10,7 @@ use {
|
||||||
} ;
|
} ;
|
||||||
|
|
||||||
use crate :: {
|
use crate :: {
|
||||||
collection :: * ,
|
persistence :: * ,
|
||||||
ui :: {
|
ui :: {
|
||||||
application_header_bar :: * ,
|
application_header_bar :: * ,
|
||||||
collatable_container :: * ,
|
collatable_container :: * ,
|
||||||
|
@ -53,7 +53,7 @@ impl UI {
|
||||||
|
|
||||||
pub fn close_window ( & self ) { self . window . close () }
|
pub fn close_window ( & self ) { self . window . close () }
|
||||||
|
|
||||||
pub fn render_collection ( & self , collection : Collection ) {
|
pub fn render_collection_overview ( & self , collection : CollectionOverview ) {
|
||||||
self . films_component . set_films ( collection . films ) ;
|
self . films_component . set_films ( collection . films ) ;
|
||||||
self . series_component . set_series ( collection . series ) ;
|
self . series_component . set_series ( collection . series ) ;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue