diff options
author | 2020-02-06 10:21:52 -0600 | |
---|---|---|
committer | 2020-02-06 10:21:52 -0600 | |
commit | 97c308076ff93d09eb874f8e954aae4c7c5deaf7 (patch) | |
tree | c3a4ec76931c8153c932c364fa393e25d39d74f0 /examples/pokedex/src | |
parent | 7b892eb3e11596925a2993bcc4175ac09ff3768a (diff) | |
parent | 36e617ae70cc7a86ce998cbd61f6aa702bb42933 (diff) | |
download | iced-97c308076ff93d09eb874f8e954aae4c7c5deaf7.tar.gz iced-97c308076ff93d09eb874f8e954aae4c7c5deaf7.tar.bz2 iced-97c308076ff93d09eb874f8e954aae4c7c5deaf7.zip |
Merge pull request #180 from hecrj/feature/web-styling
Custom styling for `iced_web`
Diffstat (limited to 'examples/pokedex/src')
-rw-r--r-- | examples/pokedex/src/main.rs | 43 |
1 files changed, 30 insertions, 13 deletions
diff --git a/examples/pokedex/src/main.rs b/examples/pokedex/src/main.rs index 283437b2..4449b901 100644 --- a/examples/pokedex/src/main.rs +++ b/examples/pokedex/src/main.rs @@ -27,7 +27,7 @@ enum Message { } impl Application for Pokedex { - type Executor = iced_futures::executor::AsyncStd; + type Executor = iced::executor::Default; type Message = Message; fn new() -> (Pokedex, Command<Message>) { @@ -79,6 +79,7 @@ impl Application for Pokedex { fn view(&mut self) -> Element<Message> { let content = match self { Pokedex::Loading => Column::new() + .width(Length::Shrink) .push(Text::new("Searching for Pokémon...").size(40)), Pokedex::Loaded { pokemon, search } => Column::new() .max_width(500) @@ -166,19 +167,21 @@ impl Pokemon { } let id = { - let mut rng = rand::thread_rng(); + let mut rng = rand::rngs::OsRng::default(); rng.gen_range(0, Pokemon::TOTAL) }; - let url = format!("https://pokeapi.co/api/v2/pokemon-species/{}", id); - let sprite = format!("https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/{}.png", id); + let fetch_entry = async { + let url = + format!("https://pokeapi.co/api/v2/pokemon-species/{}", id); - let (entry, sprite): (Entry, _) = futures::future::try_join( - surf::get(&url).recv_json(), - surf::get(&sprite).recv_bytes(), - ) - .await?; + reqwest::get(&url).await?.json().await + }; + + let (entry, image): (Entry, _) = + futures::future::try_join(fetch_entry, Self::fetch_image(id)) + .await?; let description = entry .flavor_text_entries @@ -195,9 +198,23 @@ impl Pokemon { .chars() .map(|c| if c.is_control() { ' ' } else { c }) .collect(), - image: image::Handle::from_memory(sprite), + image, }) } + + async fn fetch_image(id: u16) -> Result<image::Handle, reqwest::Error> { + let url = format!("https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/{}.png", id); + + #[cfg(not(target_arch = "wasm32"))] + { + let bytes = reqwest::get(&url).await?.bytes().await?; + + Ok(image::Handle::from_memory(bytes.as_ref().to_vec())) + } + + #[cfg(target_arch = "wasm32")] + Ok(image::Handle::from_path(url)) + } } #[derive(Debug, Clone)] @@ -206,9 +223,9 @@ enum Error { LanguageError, } -impl From<surf::Exception> for Error { - fn from(exception: surf::Exception) -> Error { - dbg!(&exception); +impl From<reqwest::Error> for Error { + fn from(error: reqwest::Error) -> Error { + dbg!(&error); Error::APIError } |