39 lines
1020 B
Rust
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;
|
|
}
|
|
}
|