Files
oe1-player/src/state.rs
Philipp Hofer 19c9b7739b
Some checks failed
CI/CD Pipeline / test (push) Successful in 1m49s
CI/CD Pipeline / deploy (push) Has been cancelled
generate rss feed
2025-10-13 10:11:06 +02:00

39 lines
1020 B
Rust

use chrono::{Local, NaiveDate};
use std::sync::Arc;
use tokio::sync::RwLock;
pub struct AppState {
pub urls: RwLock<Vec<String>>,
pub last_download_on_day: RwLock<Option<NaiveDate>>,
}
impl AppState {
pub fn new() -> Self {
Self {
urls: RwLock::new(Vec::new()),
last_download_on_day: RwLock::new(None),
}
}
pub async fn check_update(self: Arc<Self>) {
let today = Local::now().date_naive();
if let Some(downloaded_on_day) = *self.last_download_on_day.read().await
&& today == downloaded_on_day
{
return;
}
*self.last_download_on_day.write().await = Some(today);
let latest_url = player::newest_morning_journal_streaming_url()
.await
.unwrap();
let mut old = self.urls.read().await.clone();
old.push(latest_url);
let new = old.into_iter().rev().take(10).collect(); // only keep last 10
*self.urls.write().await = new;
}
}