Skip to content

Commit

Permalink
Play count & sorting
Browse files Browse the repository at this point in the history
  • Loading branch information
Tanja-4732 committed Feb 3, 2024
1 parent 4f6d046 commit e92a91c
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/app/projector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ pub fn Projector(cx: Scope) -> impl IntoView {
<p>
"Powered by full-stack "
<a class="text-blue-500" href="https://www.rust-lang.org/">"Rust 🦀"</a>
" without any JavaScript."
" without any (written) JavaScript (only WASM)."
</p>
</div>
}
Expand Down
18 changes: 14 additions & 4 deletions src/app/sound_button.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,27 @@ pub fn SoundButton(cx: Scope, sound: Sound) -> impl IntoView {
let sound_2 = sound.clone();
// let play = |_| play_action.dispatch(&sound_2);

let Sound { name, url } = sound.clone();
let Sound {
name,
url: _,
play_count,
} = sound.clone();

view! { cx,
<button
class="bg-slate-500 hover:bg-slate-400 text-white font-bold py-2 px-4 rounded overflow-x-auto"
class="bg-slate-500 hover:bg-slate-400 text-white py-2 px-4 rounded overflow-x-auto"
on:click=move |_| {
log::info!("on:click");
play_action.dispatch(sound_2.clone());
}
>
{name}
>
<p class="font-bold">
{name}
</p>
<p class="text-sm">
{play_count}
{if play_count == 1 { " play" } else { " plays" }}
</p>
// ", " {url}
</button>
// <div class="bg-slate-500 hover:bg-slate-400 text-white font-bold py-2 px-4 rounded">
Expand Down
3 changes: 2 additions & 1 deletion src/app/sounds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ use crate::{
#[component]
pub fn Sounds(cx: Scope) -> impl IntoView {
let test_sound = Sound {
name: "test_sound".to_string(),
name: "Loading...".to_string(),
url: "/test_sound.ogg".to_string(),
play_count: -420,
};
let (count, set_count) = create_signal(cx, 0);
let (show_hl_sounds, set_show_hl_sounds) = create_signal(cx, false);
Expand Down
15 changes: 11 additions & 4 deletions src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ lazy_static! {
pub struct Sound {
pub name: String,
pub url: String,
pub play_count: i64,
}

pub async fn _get_sounds_strings() -> String {
Expand All @@ -22,7 +23,7 @@ pub async fn _get_sounds_strings() -> String {
}

pub async fn get_sounds_strings() -> Result<String, gloo_net::Error> {
let req = RequestBuilder::new("http://licht.realraum.at:4242/api/v1/sounds")
let req = RequestBuilder::new("http://127.0.0.1:4242/api/v1/sounds")
.method(Method::GET)
.mode(RequestMode::Cors)
.build()?;
Expand All @@ -46,6 +47,7 @@ pub async fn get_sounds_strings() -> Result<String, gloo_net::Error> {
struct ServerSound {
name: String,
path: String,
play_count: i64,
}

#[derive(Debug, Serialize)]
Expand All @@ -60,6 +62,7 @@ pub fn parse_sounds(txt: &str) -> Vec<Sound> {
.map(|sound| Sound {
name: sound.name,
url: sound.path,
play_count: sound.play_count,
})
.collect()
}
Expand Down Expand Up @@ -97,8 +100,11 @@ pub async fn kill_mplayer() -> Result<(), gloo_net::Error> {
}

fn sort_sounds(sounds: &mut Vec<Sound>) {
// // Sort alphabetically by name
// sounds.sort_by(|a, b| a.name.cmp(&b.name));
// Sort alphabetically by name
sounds.sort_by(|a, b| a.name.cmp(&b.name));

// Sort by play count
sounds.sort_by(|a, b| b.play_count.cmp(&a.play_count));

let mut hl_sounds = Vec::new();

Expand All @@ -111,10 +117,11 @@ fn sort_sounds(sounds: &mut Vec<Sound>) {
true
}
});

sounds.append(&mut hl_sounds);
}

// const TEST_TXT: &str = include_str!("../data/licht.realraum.at.html");
// const TEST_TXT: &str = include_str!("../data/127.0.0.1.html");
pub const HL_SOUNDS_STRING: &str = "hl-sounds";

// #[cfg(test)]
Expand Down

0 comments on commit e92a91c

Please sign in to comment.