Skip to content

Commit 1650d68

Browse files
author
Ruslan
committed
Get the coordinates storing and fetching
1 parent 777944d commit 1650d68

7 files changed

Lines changed: 37 additions & 10 deletions

File tree

.devcontainer/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,4 @@ RUN rustup component add rustfmt
2020
RUN rustup target add wasm32-unknown-unknown
2121

2222
RUN curl -L --proto '=https' --tlsv1.2 -sSf https://raw.githubusercontent.com/cargo-bins/cargo-binstall/main/install-from-binstall-release.sh | bash
23-
RUN cargo binstall -y wasm-pack
23+
RUN cargo binstall -y trunk

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ serde_json = "1.0.140"
2020
serde_urlencoded = "0.7.1"
2121
slint = { version = "1.12.1", features = ["gettext"] }
2222
wasm-bindgen = "0.2.100"
23+
wasm-bindgen-futures = "0.4.50"
2324
web-sys = { version = "0.3.77", features = ["HtmlFormElement", "Storage"] }
2425
yew = { version = "0.21.0", features = ["csr"] }
2526
yew-hooks = "0.3.3"

src/location.rs

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
use serde::Deserialize;
2-
use web_sys::{Storage, Window};
1+
use serde::{Deserialize, Serialize};
2+
use web_sys::Window;
33

44
use crate::{Api, Coordinates};
55

@@ -9,6 +9,12 @@ struct GeoLocationApiData {
99
longitude: f32,
1010
}
1111

12+
#[derive(Debug, Clone, PartialEq, Deserialize, Default, Serialize)]
13+
struct LocationStorage {
14+
latitude: f32,
15+
longitude: f32,
16+
}
17+
1218
const STORAGE_KEY: &str = "coordinates";
1319

1420
async fn fetch_location() -> GeoLocationApiData {
@@ -30,11 +36,15 @@ fn fetch_from_local(key: &str) -> Option<Coordinates> {
3036
if let Some(storage) = maybe_storage {
3137
let maybe_data = storage.get_item(key).unwrap();
3238
if let Some(data) = maybe_data {
33-
let coordinates_result: Result<Coordinates, serde_json::Error> =
39+
let coordinates_result: Result<LocationStorage, serde_json::Error> =
3440
serde_json::from_str(&data);
3541

3642
if coordinates_result.is_ok() {
37-
return Some(coordinates_result.unwrap());
43+
let location_storage = coordinates_result.unwrap();
44+
return Some(Coordinates {
45+
latitude: location_storage.latitude,
46+
longitude: location_storage.longitude,
47+
});
3848
}
3949
}
4050
}
@@ -45,7 +55,12 @@ fn store_into_local(key: &str, coordinates: Coordinates) {
4555
let window: Window = web_sys::window().unwrap();
4656
let maybe_storage = window.local_storage().unwrap();
4757
if let Some(storage) = maybe_storage {
48-
let data_string = serde_json::to_string(&coordinates).unwrap();
58+
let location_storage = LocationStorage {
59+
latitude: coordinates.latitude,
60+
longitude: coordinates.longitude,
61+
};
62+
63+
let data_string = serde_json::to_string(&location_storage).unwrap();
4964
storage.set_item(key, &data_string).unwrap();
5065
}
5166
}
@@ -56,6 +71,7 @@ pub async fn set_location(api: Api<'_>) {
5671

5772
if let Some(local) = from_location {
5873
api.set_coordinates(local);
74+
return;
5975
}
6076

6177
let data = fetch_location().await;

src/main.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use slint::{Timer, TimerMode};
22
use std::sync::Arc;
33
use wasm_bindgen::prelude::wasm_bindgen;
44

5-
use crate::{bins::set_bins, clock::set_time};
5+
use crate::{bins::set_bins, clock::set_time, location::set_location};
66

77
mod bins;
88
mod clock;
@@ -12,7 +12,7 @@ mod weather;
1212
slint::include_modules!();
1313

1414
#[wasm_bindgen(main)]
15-
pub fn main() {
15+
pub async fn main() {
1616
// This provides better error messages in debug mode.
1717
// It's disabled in release mode so it doesn't bloat up the file size.
1818
#[cfg(all(debug_assertions, target_arch = "wasm32"))]
@@ -50,6 +50,7 @@ pub fn main() {
5050
let app_now = Arc::clone(&app_arc);
5151
if let Some(app) = app_now.upgrade() {
5252
set_bins(app.global::<Api>());
53+
set_location(app.global::<Api>()).await;
5354
}
5455

5556
app.run().expect("AppWindow::run() failed");

src/weather.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use serde::Deserialize;
22

3-
use crate::{location::Coordinates, Api};
3+
use crate::{Api, Coordinates};
44

55
// Easier to deal with a single 'variable'
66
#[derive(Debug, PartialEq, Clone)]

ui/app.slint

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,14 @@ export component App inherits Window {
2828
}
2929
}
3030

31-
HorizontalLayout { }
31+
HorizontalLayout {
32+
33+
Text {
34+
text: "longitude: " +Api.coordinates.longitude;
35+
}
36+
Text {
37+
text: "latitude: " +Api.coordinates.latitude;
38+
}
39+
}
3240
}
3341
}

0 commit comments

Comments
 (0)