Skip to content

Commit

Permalink
first draft finished
Browse files Browse the repository at this point in the history
  • Loading branch information
sor-ca committed Nov 16, 2023
1 parent 0dfdf14 commit cb499a5
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 11 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ tokio = { version = "1", features = ["net", "rt-multi-thread"] }
# You only need serde if you want app persistence:
serde = { version = "1", features = ["derive"] }
serde_derive = "*"
serde_json = "1.0"

# native:
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
Expand Down
56 changes: 45 additions & 11 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@ use std::ops::RangeInclusive;
use reqwest::Client;
use serde_derive::{Deserialize, Serialize};
use std::sync::mpsc::{Receiver, Sender};
use std::time::Duration;
use tokio::runtime::Runtime;

#[derive(Deserialize, Serialize, Debug)]
struct SavedTime {
id: usize,
time: String,
}


#[derive(Deserialize, Serialize)]
#[serde(default)] // if we add new fields, give them default values when deserializing old state
Expand Down Expand Up @@ -133,8 +138,8 @@ impl TemplateApp {
fn post_time(msg: String, tx: Sender<String>, ctx: egui::Context) {
tokio::spawn(async move {
// Send a request with an increment value.
let body: HttpbinJson = Client::default()
.post("localhost:8080/time")
let body: usize = Client::default()
.post("http://127.0.0.1:8080/time")
//.json(&Body { incr })
.query(&[("time", msg)])
.send()
Expand All @@ -145,20 +150,43 @@ fn post_time(msg: String, tx: Sender<String>, ctx: egui::Context) {
.expect("Unable to parse response");

// After parsing the response, notify the GUI thread of the increment value.
let _ = tx.send(body.json.msg);
let _ = tx.send(body.to_string());
ctx.request_repaint();
});
}

#[derive(Deserialize, Serialize)]
fn get_time_list(tx: Sender<String>, ctx: egui::Context) {
tokio::spawn(async move {
// Send a request with an increment value.
let body: String = Client::default()
.get("http://127.0.0.1:8080/times")
.send()
.await
.expect("Unable to send request")
.text()
.await
.expect("Unable to parse response");

dbg!(&body);

// After parsing the response, notify the GUI thread of the increment value.
let _ = tx.send(body);
ctx.request_repaint();
});
}



/*#[derive(Deserialize, Serialize)]
struct HttpbinJson {
json: Body,
//json: Body,
json: usize,
}
#[derive(Deserialize, Serialize)]
struct Body {
msg: String,
}
}*/

/*#[derive(Debug, serde::Deserialize, serde::Serialize)]
enum Hour {
Expand All @@ -181,7 +209,9 @@ impl eframe::App for TemplateApp {
let height = ctx.screen_rect().height();

if let Ok(msg) = self.rx.try_recv() {
dbg!(&msg);
//dbg!(&msg);
let list: Vec<SavedTime> = serde_json::from_str(&msg).expect("cant parse msg");
dbg!(&list);
}

use egui::FontFamily::Proportional;
Expand Down Expand Up @@ -226,10 +256,14 @@ impl eframe::App for TemplateApp {
if ui.button("time now").clicked() {
set_local_time = true;
}
ui.label("Press the button to initiate an HTTP request.");
if ui.button("save current time").clicked() {

if ui.button("save time in db").clicked() {
post_time(self.time.to_string(), self.tx.clone(), ctx.clone());
}

if ui.button("time list").clicked() {
get_time_list(self.tx.clone(), ctx.clone());
}

});
});
Expand Down
18 changes: 18 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,29 @@
#![warn(clippy::all, rust_2018_idioms)]
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] // hide console window on Windows in release

use std::time::Duration;
use tokio::runtime::Runtime;

// When compiling natively:
#[cfg(not(target_arch = "wasm32"))]
fn main() -> eframe::Result<()> {
env_logger::init(); // Log to stderr (if you run with `RUST_LOG=debug`).

let rt = Runtime::new().expect("Unable to create Runtime");

// Enter the runtime so that `tokio::spawn` is available immediately.
let _enter = rt.enter();

// Execute the runtime in its own thread.
// The future doesn't have to do anything. In this example, it just sleeps forever.
std::thread::spawn(move || {
rt.block_on(async {
loop {
tokio::time::sleep(Duration::from_secs(3600)).await;
}
})
});

let native_options = eframe::NativeOptions::default();
eframe::run_native(
"clock",
Expand Down

0 comments on commit cb499a5

Please sign in to comment.