add cache, improve code

This commit is contained in:
Daniel Bulant 2024-11-22 23:15:06 +01:00
parent 3f0dde272c
commit 6114b8a25a
No known key found for this signature in database
5 changed files with 33 additions and 15 deletions

2
.gitignore vendored
View file

@ -1,4 +1,4 @@
target target
result result
http-cacache /cache
refresh_token.txt refresh_token.txt

View file

@ -13,13 +13,11 @@ use cushy::{
static FONT: OnceLock<LoadedFont> = OnceLock::new(); static FONT: OnceLock<LoadedFont> = OnceLock::new();
pub fn load_fonts() -> FontCollection { pub fn load_fonts(col: &FontCollection) {
let fonts = FontCollection::default(); let font = col.push_unloadable(
let font = fonts.push_unloadable(
include_bytes!("../fonts/MaterialSymbolsOutlined[FILL,GRAD,opsz,wght].ttf").into(), include_bytes!("../fonts/MaterialSymbolsOutlined[FILL,GRAD,opsz,wght].ttf").into(),
); );
FONT.set(font).map_err(|_| ()).unwrap(); FONT.set(font).map_err(|_| ()).unwrap();
fonts
} }
fn font_component() -> DynamicComponent { fn font_component() -> DynamicComponent {

View file

@ -13,7 +13,7 @@ use librespot_connect::{
spirc::{Spirc, SpircLoadCommand}, spirc::{Spirc, SpircLoadCommand},
state::ConnectStateConfig, state::ConnectStateConfig,
}; };
use librespot_core::{authentication::Credentials, Session, SessionConfig}; use librespot_core::{authentication::Credentials, cache::Cache, Session, SessionConfig};
use librespot_playback::{ use librespot_playback::{
audio_backend, audio_backend,
config::{AudioFormat, PlayerConfig}, config::{AudioFormat, PlayerConfig},
@ -40,21 +40,41 @@ mod widgets;
fn main() -> cushy::Result { fn main() -> cushy::Result {
let args = Args::parse(); let args = Args::parse();
let app = PendingApp::new(TokioRuntime::default()); 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 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 session_config = SessionConfig::default();
let player_config = PlayerConfig::default(); let player_config = PlayerConfig::default();
let audio_format = AudioFormat::default(); let audio_format = AudioFormat::default();
let credentials = Credentials::with_access_token(&token.access_token); 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 backend = audio_backend::find(None).unwrap();
let session; let session;
{ {
let guard = app.cushy().enter_runtime(); let guard = app.cushy().enter_runtime();
session = Session::new(session_config, None); session = Session::new(session_config, cache);
dbg!(session.user_data()); dbg!(session.user_data());
@ -88,13 +108,12 @@ fn main() -> cushy::Result {
tokio::join!(spirc_task, dynplayer2.run(), async move { tokio::join!(spirc_task, dynplayer2.run(), async move {
let user = context.current_user().await.unwrap(); let user = context.current_user().await.unwrap();
dbg!(&user); dbg!(&user);
// let userid = user.id;
let playlists = context.current_user_playlists(None, None).await.unwrap(); let playlists = context.current_user_playlists(None, None).await.unwrap();
let selected_page = Dynamic::new(ActivePage::default()); 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()) .and(LikedSongsPage::new(context.clone()).into_widget())
.into_columns() .into_columns()
.expand() .expand()
@ -102,8 +121,7 @@ fn main() -> cushy::Result {
.into_rows() .into_rows()
.expand() .expand()
.into_window(); .into_window();
let fonts = load_fonts(/*app.cushy()*/); load_fonts(&win.fonts);
win.fonts = fonts;
win.open(&mut app).unwrap(); win.open(&mut app).unwrap();
}); });
}); });

View file

@ -33,7 +33,9 @@ static CLIENT: LazyLock<ClientWithMiddleware> = LazyLock::new(|| {
ClientBuilder::new(Client::new()) ClientBuilder::new(Client::new())
.with(Cache(HttpCache { .with(Cache(HttpCache {
mode: CacheMode::Default, mode: CacheMode::Default,
manager: CACacheManager::default(), manager: CACacheManager {
path: "./cache/http-cacache".into(),
},
options: HttpCacheOptions::default(), options: HttpCacheOptions::default(),
})) }))
.build() .build()

View file

@ -121,7 +121,7 @@ impl LikedSongsPage {
.to_string() .to_string()
.align_right() .align_right()
.size(Size { .size(Size {
width: Dimension::Lp(Lp::points(20)).into(), width: Dimension::Lp(Lp::points(30)).into(),
height: DimensionRange::default(), height: DimensionRange::default(),
}) })
.and({ .and({