Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
Tanja-4732 committed Aug 8, 2023
1 parent 3b6f7a6 commit a33f403
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 26 deletions.
11 changes: 11 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ uuid = { version = "1.3.4", features = ["serde", "v4", "js"] }
gix-hash = "0.11.1"
gix-refspec = "0.13.0"
lazy_static = "1.4.0"
reqwest = "0.11.18"
reqwest = { version = "0.11.18", features = ["blocking"] }
regex = "1.9.3"
wasm-bindgen = "0.2.87"
web-sys = "0.3.64"
Expand Down
73 changes: 49 additions & 24 deletions src/app/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
mod sound_button;

use leptos::{ev::close, *};
use leptos_router::*;

use crate::core::get_sounds;
use crate::app::sound_button::SoundButton;
use crate::core::{get_sounds, get_sounds_strings, parse_sounds, Sound};

async fn load_data() -> i32 {
42
Expand All @@ -11,7 +14,6 @@ async fn load_data() -> i32 {
pub fn App(cx: Scope) -> impl IntoView {
let version_info = env!("CARGO_PKG_VERSION");

// this count is our synchronous, local state
let (count, set_count) = create_signal(cx, 0);

// create_resource takes two arguments after its scope
Expand All @@ -22,51 +24,74 @@ pub fn App(cx: Scope) -> impl IntoView {
// the second is the loader
// it takes the source signal's value as its argument
// and does some async work
|value| async move { get_sounds().await },
|value| async move {
log::info!("loading data...");

let res = get_sounds_strings().await;

log::info!("res: {:?}", res);

let sounds = parse_sounds(&res);

sounds
},
);
// whenever the source signal changes, the loader reloads

// you can also create resources that only load once
// just return the unit type () from the source signal
// that doesn't depend on anything: we just load it once
let stable = create_resource(cx, || (), |_| async move { get_sounds().await });

// we can access the resource values with .read()
// this will reactively return None before the Future has resolved
// and update to Some(T) when it has resolved
let async_result = move || {
async_data
.read(cx)
.map(|value| format!("Server returned {value:?}"))
// This loading state will only show before the first load
.unwrap_or_else(|| "Loading...".into())
.unwrap_or_else(|| vec![])
};

// the resource's loading() method gives us a
// signal to indicate whether it's currently loading
let loading = async_data.loading();
let is_loading = move || if loading() { "Loading..." } else { "Idle." };

let test_sound = Sound {
name: "test_sound".to_string(),
url: "/test_sound.ogg".to_string(),
};

view! { cx,
<div class="h-fit min-h-screen bg-slate-600 text-white">
<div id="topbar" class="sticky top-0 left-0 bg-slate-500 p-1">
"Realraum UI v" {env!("CARGO_PKG_VERSION")}
"Realraum UI v"
{env!("CARGO_PKG_VERSION")}
</div>


<button
on:click=move |_| {
<button on:click=move |_| {
set_count.update(|n| *n += 1);
}
>
"Click me"
</button>
<p>
<code>"async_value"</code>": "
{async_result}
<br/>
{is_loading}
</p>
}>
"Click me"
</button>

// <p>
// <code>"async_value"</code>
// ": "
// {async_result}
// <br/>
// {is_loading}

{
async_result().into_iter()
.map(|n| view! { cx,
<SoundButton sound=n/>
})
.collect_view(cx)
}

{
dbg!(async_result());
}


// </p>

</div>
}
Expand Down
24 changes: 24 additions & 0 deletions src/app/sound_button.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use leptos::*;

use crate::core::{play_sound, Sound};

#[component]
pub fn SoundButton(cx: Scope, sound: Sound) -> impl IntoView {
// let play = async move |_| {
// let _ = play_sound(&url).unwrap();
// };

let Sound { name, url } = sound;

view! { cx,
// <button
// class="bg-slate-500 hover:bg-slate-400 text-white font-bold py-2 px-4 rounded"
// on:click=play
// >
// {name}
// </button>
<div class="bg-slate-500 hover:bg-slate-400 text-white font-bold py-2 px-4 rounded">
{name}", " {url}
</div>
}
}
56 changes: 55 additions & 1 deletion src/core.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
use std::future::Future;

use gloo_net::http::{Request, Response};
use lazy_static::lazy_static;
use regex::Regex;
use reqwest::{Client, RequestBuilder};
use serde::{Deserialize, Serialize};

lazy_static! {
static ref SOUND_RX: Regex = Regex::new(r#"href='([^']*)'.*?>([^<]*)"#).unwrap();
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Sound {
pub name: String,
pub url: String,
}

pub async fn old_get_sounds() -> String {
log::info!("get_sounds");
let url: reqwest::Url = "http://licht.realraum.at:8080".parse().unwrap();
Expand All @@ -32,7 +41,12 @@ pub async fn old_get_sounds() -> String {
txt
}

pub async fn get_sounds() -> String {
pub async fn get_sounds_strings() -> String {
"TEST_TXT".to_string()
// TEST_TXT.to_string()
}

pub async fn _get_sounds_strings() -> String {
let resp = Request::get("http://licht.realraum.at:8080")
.send()
.await
Expand All @@ -41,3 +55,43 @@ pub async fn get_sounds() -> String {

String::new()
}

pub fn parse_sounds(txt: &str) -> Vec<Sound> {
let mut sounds = Vec::new();
for cap in SOUND_RX.captures_iter(txt) {
let name = cap[2].to_string();
let url = cap[1].to_string();
sounds.push(Sound { name, url });
}
sounds
}

pub async fn get_sounds() -> Vec<Sound> {
let txt = get_sounds_strings().await;
parse_sounds(&txt)
}

pub async fn play_sound(url: &str) -> Result<(), String> {
let url = format!("http://licht.realraum.at:8080{}", url);
log::info!("play_sound: {}", url);
let resp = Request::get(&url).send().await.unwrap();
if resp.status() != 200 {
return Err(resp.status_text());
}
Ok(())
}

// const TEST_TXT: &str = include_str!("../data/licht.realraum.at.html");

// #[cfg(test)]
// mod test {
// use super::*;

// #[test]
// fn test_parse_sounds() {
// let sounds = parse_sounds(TEST_TXT);
// // assert_eq!(sounds.len(), 4);
// assert_eq!(sounds[0].name, "sad_trombone.ogg");
// assert_eq!(sounds[0].url, "/sound/sad_trombone.ogg");
// }
// }

0 comments on commit a33f403

Please sign in to comment.