Skip to content

Commit

Permalink
Check for manifest updates periodically while app is open
Browse files Browse the repository at this point in the history
  • Loading branch information
mtkennerly committed Jul 19, 2024
1 parent 13ddaac commit 440883d
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 31 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
you can now disable the primary manifest's entries.
* GUI: On startup, Ludusavi will check if a new version is available and notify you.
This happens at most once per 7 days.
* GUI: When left open,
Ludusavi will automatically check for manifest updates once every 24 hours.
Previously, this check only occurred when the app started.
* Fixed:
* CLI: Some commands would fail with relative path arguments.
* Changed:
Expand Down
55 changes: 30 additions & 25 deletions src/gui/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ use crate::{
lang::TRANSLATOR,
prelude::{app_dir, get_threads_from_env, initialize_rayon, Error, Finality, StrictPath, SyncDirection},
resource::{
cache::Cache,
config::{Config, CustomGame, CustomGameKind, Root},
cache::{self, Cache},
config::{self, Config, CustomGame, CustomGameKind, Root},
manifest::Manifest,
ResourceFile, SaveableResourceFile,
},
Expand Down Expand Up @@ -66,6 +66,7 @@ pub enum SaveKind {

#[derive(Default)]
pub struct App {
flags: Flags,
config: Config,
manifest: Manifest,
cache: Cache,
Expand Down Expand Up @@ -1125,6 +1126,16 @@ impl App {
self.switch_screen(Screen::CustomGames)
}

fn update_manifest(config: config::ManifestConfig, cache: cache::Manifests, force: bool) -> Command<Message> {
Command::perform(
async move { tokio::task::spawn_blocking(move || Manifest::update(config, cache, force)).await },
|join| match join {
Ok(x) => Message::ManifestUpdated(x),
Err(_) => Message::Ignore,
},
)
}

fn open_url(url: String) -> Command<Message> {
let url2 = url.clone();
Command::perform(async { opener::open(url) }, move |res| match res {
Expand Down Expand Up @@ -1249,8 +1260,6 @@ impl Application for App {
}
}

let manifest_config = config.manifest.clone();
let manifest_cache = cache.manifests.clone();
let text_histories = TextHistories::new(&config);

log::debug!("Config on startup: {config:?}");
Expand All @@ -1260,14 +1269,10 @@ impl Application for App {
iced::font::load(std::borrow::Cow::Borrowed(crate::gui::font::ICONS_DATA)).map(|_| Message::Ignore),
];
if flags.update_manifest {
commands.push(Command::perform(
async move {
tokio::task::spawn_blocking(move || Manifest::update(manifest_config, manifest_cache, false)).await
},
|join| match join {
Ok(x) => Message::ManifestUpdated(x),
Err(_) => Message::Ignore,
},
commands.push(Self::update_manifest(
config.manifest.clone(),
cache.manifests.clone(),
false,
));
}

Expand All @@ -1288,6 +1293,7 @@ impl Application for App {
modal,
updating_manifest: flags.update_manifest,
text_histories,
flags,
..Self::default()
},
Command::batch(commands),
Expand Down Expand Up @@ -1361,20 +1367,13 @@ impl Application for App {

Command::none()
}
Message::UpdateManifest => {
Message::UpdateManifest { force } => {
if self.updating_manifest {
return Command::none();
}

self.updating_manifest = true;
let manifest_config = self.config.manifest.clone();
let manifest_cache = self.cache.manifests.clone();
Command::perform(
async move {
tokio::task::spawn_blocking(move || Manifest::update(manifest_config, manifest_cache, true))
.await
},
|join| match join {
Ok(x) => Message::ManifestUpdated(x),
Err(_) => Message::Ignore,
},
)
Self::update_manifest(self.config.manifest.clone(), self.cache.manifests.clone(), force)
}
Message::ManifestUpdated(updates) => {
self.updating_manifest = false;
Expand Down Expand Up @@ -2674,6 +2673,12 @@ impl Application for App {
subscriptions.push(iced::time::every(Duration::from_millis(200)).map(|_| Message::Save));
}

if self.flags.update_manifest {
subscriptions.push(
iced::time::every(Duration::from_secs(60 * 60 * 24)).map(|_| Message::UpdateManifest { force: false }),
);
}

if self.exiting {
subscriptions.push(iced::time::every(Duration::from_millis(50)).map(|_| Message::Exit { user: false }));
}
Expand Down
4 changes: 3 additions & 1 deletion src/gui/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,9 @@ pub enum Message {
PruneNotifications,
AppReleaseToggle(bool),
AppReleaseChecked(Result<crate::metadata::Release, String>),
UpdateManifest,
UpdateManifest {
force: bool,
},
ManifestUpdated(Vec<Result<Option<ManifestUpdate>, Error>>),
Backup(BackupPhase),
Restore(RestorePhase),
Expand Down
5 changes: 4 additions & 1 deletion src/gui/screen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,10 @@ pub fn other<'a>(
Row::new()
.align_items(iced::Alignment::Center)
.push(text(TRANSLATOR.manifest_label()).width(100))
.push(button::refresh(Message::UpdateManifest, updating_manifest)),
.push(button::refresh(
Message::UpdateManifest { force: true },
updating_manifest,
)),
)
.push(editor::manifest(config, cache, histories, modifiers).padding([10, 0, 0, 0])),
)
Expand Down
4 changes: 2 additions & 2 deletions src/resource/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,12 @@ pub struct ManifestConfig {
}

impl ManifestConfig {
pub fn secondary_manifest_urls(&self) -> Vec<&str> {
pub fn secondary_manifest_urls(&self, force: bool) -> Vec<&str> {
self.secondary
.iter()
.filter_map(|x| match x {
SecondaryManifestConfig::Local { .. } => None,
SecondaryManifestConfig::Remote { url, .. } => Some(url.as_str()),
SecondaryManifestConfig::Remote { url, enable } => (*enable || force).then_some(url.as_str()),
})
.collect()
}
Expand Down
6 changes: 4 additions & 2 deletions src/resource/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -391,9 +391,11 @@ impl Manifest {
) -> Vec<Result<Option<ManifestUpdate>, Error>> {
let mut out = vec![];

out.push(Self::update_one(&config.url, &cache, force, true));
if config.enable || force {
out.push(Self::update_one(&config.url, &cache, force, true));
}

for secondary in config.secondary_manifest_urls() {
for secondary in config.secondary_manifest_urls(force) {
out.push(Self::update_one(secondary, &cache, force, false));
}

Expand Down

0 comments on commit 440883d

Please sign in to comment.