From 6114b8a25a4339ee2985f1b2e242c8c1e88cb203 Mon Sep 17 00:00:00 2001 From: Daniel Bulant Date: Fri, 22 Nov 2024 23:15:06 +0100 Subject: [PATCH] add cache, improve code --- .gitignore | 4 ++-- src/icons.rs | 6 ++---- src/main.rs | 32 +++++++++++++++++++++++++------- src/widgets/image.rs | 4 +++- src/widgets/pages/liked.rs | 2 +- 5 files changed, 33 insertions(+), 15 deletions(-) diff --git a/.gitignore b/.gitignore index efe9773..f707b5f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ target result -http-cacache -refresh_token.txt \ No newline at end of file +/cache +refresh_token.txt diff --git a/src/icons.rs b/src/icons.rs index fbf0cca..cf01383 100644 --- a/src/icons.rs +++ b/src/icons.rs @@ -13,13 +13,11 @@ use cushy::{ static FONT: OnceLock = OnceLock::new(); -pub fn load_fonts() -> FontCollection { - let fonts = FontCollection::default(); - let font = fonts.push_unloadable( +pub fn load_fonts(col: &FontCollection) { + let font = col.push_unloadable( include_bytes!("../fonts/MaterialSymbolsOutlined[FILL,GRAD,opsz,wght].ttf").into(), ); FONT.set(font).map_err(|_| ()).unwrap(); - fonts } fn font_component() -> DynamicComponent { diff --git a/src/main.rs b/src/main.rs index 365fa2c..eb0ee64 100644 --- a/src/main.rs +++ b/src/main.rs @@ -13,7 +13,7 @@ use librespot_connect::{ spirc::{Spirc, SpircLoadCommand}, state::ConnectStateConfig, }; -use librespot_core::{authentication::Credentials, Session, SessionConfig}; +use librespot_core::{authentication::Credentials, cache::Cache, Session, SessionConfig}; use librespot_playback::{ audio_backend, config::{AudioFormat, PlayerConfig}, @@ -40,21 +40,41 @@ mod widgets; fn main() -> cushy::Result { let args = Args::parse(); let app = PendingApp::new(TokioRuntime::default()); + // doesn't load fonts correctly yet, cushy bug + // load_fonts(app.cushy().fonts()); let token = get_token().unwrap(); + let cache = match Cache::new(None, Some("./cache/volume"), Some("./cache/audio"), None) { + Ok(cache) => Some(cache), + Err(e) => { + eprintln!("Failed to create cache: {}", e); + None + } + }; let session_config = SessionConfig::default(); let player_config = PlayerConfig::default(); let audio_format = AudioFormat::default(); let credentials = Credentials::with_access_token(&token.access_token); - let connect_config = ConnectStateConfig::default(); + let default_connect_config = ConnectStateConfig::default(); + let connect_config = ConnectStateConfig { + name: "Despot".to_string(), + device_type: librespot_core::config::DeviceType::Computer, + volume_steps: 256, + initial_volume: cache + .as_ref() + .and_then(Cache::volume) + .map(Into::into) + .unwrap_or(default_connect_config.initial_volume), + ..Default::default() + }; let backend = audio_backend::find(None).unwrap(); let session; { let guard = app.cushy().enter_runtime(); - session = Session::new(session_config, None); + session = Session::new(session_config, cache); dbg!(session.user_data()); @@ -88,13 +108,12 @@ fn main() -> cushy::Result { tokio::join!(spirc_task, dynplayer2.run(), async move { let user = context.current_user().await.unwrap(); dbg!(&user); - // let userid = user.id; let playlists = context.current_user_playlists(None, None).await.unwrap(); let selected_page = Dynamic::new(ActivePage::default()); - let mut win = playlists_widget(playlists.items, selected_page) + let win = playlists_widget(playlists.items, selected_page) .and(LikedSongsPage::new(context.clone()).into_widget()) .into_columns() .expand() @@ -102,8 +121,7 @@ fn main() -> cushy::Result { .into_rows() .expand() .into_window(); - let fonts = load_fonts(/*app.cushy()*/); - win.fonts = fonts; + load_fonts(&win.fonts); win.open(&mut app).unwrap(); }); }); diff --git a/src/widgets/image.rs b/src/widgets/image.rs index 0d5fb44..7b84340 100644 --- a/src/widgets/image.rs +++ b/src/widgets/image.rs @@ -33,7 +33,9 @@ static CLIENT: LazyLock = LazyLock::new(|| { ClientBuilder::new(Client::new()) .with(Cache(HttpCache { mode: CacheMode::Default, - manager: CACacheManager::default(), + manager: CACacheManager { + path: "./cache/http-cacache".into(), + }, options: HttpCacheOptions::default(), })) .build() diff --git a/src/widgets/pages/liked.rs b/src/widgets/pages/liked.rs index 893d52b..90bc588 100644 --- a/src/widgets/pages/liked.rs +++ b/src/widgets/pages/liked.rs @@ -121,7 +121,7 @@ impl LikedSongsPage { .to_string() .align_right() .size(Size { - width: Dimension::Lp(Lp::points(20)).into(), + width: Dimension::Lp(Lp::points(30)).into(), height: DimensionRange::default(), }) .and({