Skip to content

Commit

Permalink
version check
Browse files Browse the repository at this point in the history
  • Loading branch information
robtfm committed Sep 3, 2024
1 parent c81d2a1 commit d00b827
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 13 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ bevy_kira_audio = { git = "https://github.com/robtfm/bevy_kira_audio", branch =
bevy_simple_text_input = { git = "https://github.com/robtfm/bevy_simple_text_input", features=["clipboard"], branch="multiline" }
directories = "5"
uuid = { version = "1.7", features = ["v4"] }
build-time = "0.1.3"

[dependencies]
analytics = { workspace = true }
Expand Down Expand Up @@ -140,7 +141,7 @@ uuid = { workspace = true }

pico-args = "0.5.0"
mimalloc = { version = "*", default-features = false }
build-time = "0.1.3"
build-time = { workspace = true }
chrono = { workspace = true }
tracing-appender = "0.2.3"
log-panics = { version = "2.1.0", features = ["with-backtrace"] }
Expand Down
14 changes: 14 additions & 0 deletions assets/ui/update.dui
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<define-template id="update-available">
<dialog title="Update Available" buttons="@buttons">
<div style="flex-direction: column; align-items: center; justify-content: center; margin: 4vmin;">
<div style="flex-direction: column;">
<med-text text="@body" />
</div>
<hr />
<div>
<med-link label="Download" color="#8888ff" href="@download" />
</div>
</div>
</dialog>
</define-template>

12 changes: 10 additions & 2 deletions crates/common/src/structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,7 @@ impl ActiveDialog {
.clone()
.try_acquire_owned()
.ok()
.map(|p| DialogPermit { _p: p })
.map(|p| DialogPermit { _p: Some(p) })
}

pub fn in_use(&self) -> bool {
Expand All @@ -547,7 +547,15 @@ impl ActiveDialog {

#[derive(Component)]
pub struct DialogPermit {
_p: OwnedSemaphorePermit,
_p: Option<OwnedSemaphorePermit>,
}

impl DialogPermit {
pub fn take(&mut self) -> Self {
Self {
_p: Some(self._p.take().unwrap()),
}
}
}

#[derive(Component, Default, Clone, Copy, PartialEq, Eq)]
Expand Down
1 change: 1 addition & 0 deletions crates/system_ui/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ chrono = { workspace = true }
clap = { workspace = true }
opener = { workspace = true }
urlencoding = { workspace = true }
build-time = { workspace = true }

copypasta = "0.10"
shlex = "1"
1 change: 1 addition & 0 deletions crates/system_ui/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub mod profile_detail;
pub mod sysinfo;
pub mod toasts;
pub mod tooltip;
pub mod version_check;
pub mod wearables;

use bevy::prelude::*;
Expand Down
50 changes: 40 additions & 10 deletions crates/system_ui/src/login.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use bevy::{
use bevy_dui::{DuiCommandsExt, DuiEntityCommandsExt, DuiProps, DuiRegistry};
use common::{
profile::SerializedProfile,
structs::{ActiveDialog, AppConfig, ChainLink, PreviousLogin},
structs::{ActiveDialog, AppConfig, ChainLink, DialogPermit, PreviousLogin},
util::{project_directories, TaskExt},
};
use comms::{
Expand All @@ -32,6 +32,8 @@ use wallet::{
Wallet,
};

use crate::version_check::check_update;

pub struct LoginPlugin;

impl Plugin for LoginPlugin {
Expand Down Expand Up @@ -81,16 +83,44 @@ fn login(
mut motd_shown: Local<bool>,
) {
if !*motd_shown {
let update = check_update();
let permit = active_dialog.try_acquire().unwrap();
let components = commands
.spawn_template(
&dui,
"motd",
DuiProps::default()
.with_prop("buttons", vec![DuiButton::new_enabled("Ok", close_ui)]),
)
.unwrap();
commands.entity(components.root).insert(permit);

if let Some((desc, url)) = update {
let components = commands
.spawn_template(
&dui,
"update-available",
DuiProps::new()
.with_prop("download", url)
.with_prop("body", desc)
.with_prop("buttons", vec![DuiButton::new_enabled("Ok", (|mut commands: Commands, dui: Res<DuiRegistry>, mut permit: Query<&mut DialogPermit>| {
let mut permit = permit.single_mut();
let permit = permit.take();
let components = commands
.spawn_template(
&dui,
"motd",
DuiProps::default()
.with_prop("buttons", vec![DuiButton::new_enabled("Ok", close_ui)]),
)
.unwrap();
commands.entity(components.root).insert(permit);
}).pipe(close_ui))]),
)
.unwrap();
commands.entity(components.root).insert(permit);
} else {
let components = commands
.spawn_template(
&dui,
"motd",
DuiProps::default()
.with_prop("buttons", vec![DuiButton::new_enabled("Ok", close_ui)]),
)
.unwrap();
commands.entity(components.root).insert(permit);
}
*motd_shown = true;
return;
}
Expand Down
35 changes: 35 additions & 0 deletions crates/system_ui/src/version_check.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use isahc::ReadResponseExt;
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize)]
struct GitData {
tag_name: String,
html_url: String,
body: String,
}

pub fn build_date() -> chrono::NaiveDate {
chrono::NaiveDate::parse_from_str(build_time::build_time_utc!("%Y-%m-%d"), "%Y-%m-%d").unwrap()
}

pub fn check_update() -> Option<(String, String)> {
let latest: GitData =
isahc::get("https://api.github.com/repos/decentraland/bevy-explorer/releases/latest")
.ok()?
.json()
.ok()?;
let latest_date = latest
.tag_name
.split('-')
.skip(1)
.take(3)
.collect::<Vec<_>>()
.join("-");
let latest_date = chrono::NaiveDate::parse_from_str(&latest_date, "%Y-%m-%d").ok()?;

if latest_date > build_date() {
Some((latest.body, latest.html_url))
} else {
None
}
}

0 comments on commit d00b827

Please sign in to comment.