-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* updated docs and replaced JsValue with String in Results * removed unnecasarry stuff, debugging * fixes bugs * debbuged * upated example
- Loading branch information
Showing
4 changed files
with
386 additions
and
320 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,82 +1,95 @@ | ||
# A wrapper around the Spotify web playback SDK for targeting wasm with rust | ||
|
||
## All the methods now are functions | ||
|
||
Because you only can have only 1 player per page, so there is no need for an explicit class, rust calls all the methods of the class through JS | ||
**Use the `init` function first** this function adds the script to the document, and creates an instance of the `Spotify.Player` class, if you don't call this function all the other functions will be useless | ||
|
||
## [Docs](https://docs.rs/rust_spotify_web_playback_sdk/latest/) | ||
|
||
## [Repo](https://github.com/KOEGlike/rust_spotify_web_playback_sdk) | ||
|
||
## [Crates.io](https://crates.io/crates/rust_spotify_web_playback_sdk) | ||
|
||
# Example in leptos: | ||
# Example in leptos | ||
|
||
```rust | ||
use rust_spotify_web_playback_sdk as sp; | ||
use leptos::*; | ||
#[component] | ||
fn HomePage() -> impl IntoView { | ||
let (is_sp_ready, set_is_sp_ready) = create_signal(false); | ||
if cfg!(any(target_arch = "wasm32", target_arch = "wasm64")) { | ||
let token="[Your token goes here]"; | ||
let oauth_cb = || { | ||
log!("oauth was called"); | ||
token.to_string() | ||
}; | ||
let oauth_cb = Closure::new(oauth_cb); | ||
let update_signal = move || { | ||
set_is_sp_ready(true); | ||
}; | ||
let on_ready = Closure::new(update_signal); | ||
fn Player() -> impl IntoView { | ||
use leptos::logging::log; | ||
use rust_spotify_web_playback_sdk::prelude as sp; | ||
|
||
create_effect(move |_| { | ||
sp::init( | ||
&oauth_cb, | ||
&on_ready, | ||
"example player".to_string(), | ||
1.0, | ||
false, | ||
); | ||
}); | ||
} | ||
let (current_song_name, set_current_song_name) = create_signal(String::new()); | ||
|
||
let token = "BQAdHQqBLczVFdCIM58tVbF0eaztF-83cXczNdz2Aua-U7JyOdIlpiG5M7oEww-dK7jo3qjcpMJ4isuyU2RYy3EoD_SWEOX1uW39bpR-KDbjSYeBPb0Jn4QtwXQw2yjQ33oRzVdyRufKF8o7kwXYW-ij6rtio6oDq0PNYIGIyMsDxKhgM5ijt4LXWz-iWQykftBMXdeSWZuU-Z51VyFOPuznUBQj"; | ||
|
||
let connect = create_action(|_| async { | ||
match sp::connect().await { | ||
Ok(_) => log!("connected"), | ||
Err(e) => log!("error {:?}", e.as_string()), | ||
Err(e) => log!("error {:?}", e), | ||
}; | ||
}); | ||
|
||
let get_state = create_action(|_| async { | ||
log!( | ||
"{:#?}", | ||
sp::get_current_state() | ||
.await | ||
.expect("something went wrong") | ||
.expect("this device is not in use be spotify connect") | ||
create_effect(move |_| { | ||
sp::init( | ||
|| { | ||
log!("oauth was called"); | ||
token.to_string() | ||
}, | ||
move || { | ||
log!("ready"); | ||
connect.dispatch(()); | ||
|
||
sp::add_listener!("player_state_changed", move |state: sp::StateChange| { | ||
log!("state changed, {}", state.track_window.current_track.name); | ||
set_current_song_name(state.track_window.current_track.name); | ||
}); | ||
}, | ||
"example player", | ||
1.0, | ||
false, | ||
); | ||
}); | ||
|
||
let (current_song_name, set_current_song_name) = create_signal(String::new()); | ||
let get_state = create_action(|_| async { | ||
let state = sp::get_current_state().await.unwrap(); | ||
log!("{:#?}", state); | ||
}); | ||
|
||
if cfg!(any(target_arch = "wasm32", target_arch = "wasm64")) { | ||
let cb = Closure::new(move |jsv: JsValue| { | ||
let state: sp::structs::state_change::StateChange = sp::structs::from_js(jsv); | ||
log!("state changed, {}", state.track_window.current_track.name); | ||
set_current_song_name(state.track_window.current_track.name); | ||
}); | ||
create_effect(move |_| { | ||
if is_sp_ready() { | ||
log!("ready"); | ||
connect.dispatch(()); | ||
sp::add_listener("player_state_changed", &cb); | ||
} | ||
}); | ||
} | ||
let activate_player = create_action(|_| async { | ||
sp::activate_element().await | ||
}); | ||
|
||
|
||
view! { | ||
<h1>"Welcome to Leptos!"</h1> | ||
<Suspense fallback=move || view! { <p>"Loading..."</p> }> | ||
<button on:click=move |_| get_state.dispatch(())> | ||
"state" | ||
</button> | ||
<p>"Current song: " {move || current_song_name()}</p> | ||
</Suspense> | ||
<h1>"Welcome to Leptos"</h1> | ||
<button on:click=move |_| activate_player.dispatch(())> | ||
"activate player" | ||
</button> | ||
{ | ||
move || match activate_player.value().get() { | ||
Some(Ok(_)) => { | ||
view! { | ||
<button on:click=move |_| get_state.dispatch(())> | ||
"log state in console" | ||
</button> | ||
<p>"Current song: " {current_song_name}</p> | ||
}.into_view() | ||
} | ||
Some(Err(e)) => { | ||
view! { | ||
<p>"Error activating player: " {e}</p> | ||
}.into_view() | ||
} | ||
None => { | ||
view! { | ||
<p>"Activating player..."</p> | ||
}.into_view() | ||
} | ||
} | ||
} | ||
|
||
} | ||
} | ||
``` |
Oops, something went wrong.