diff options
Diffstat (limited to 'examples/pokedex')
| -rw-r--r-- | examples/pokedex/Cargo.toml | 12 | ||||
| -rw-r--r-- | examples/pokedex/src/main.rs | 43 | 
2 files changed, 38 insertions, 17 deletions
| diff --git a/examples/pokedex/Cargo.toml b/examples/pokedex/Cargo.toml index 76a3a82f..c1e3edb5 100644 --- a/examples/pokedex/Cargo.toml +++ b/examples/pokedex/Cargo.toml @@ -6,9 +6,13 @@ edition = "2018"  publish = false  [dependencies] -iced = { path = "../..", features = ["image"] } -iced_futures = { path = "../../futures", features = ["async-std"] } -surf = "1.0" -rand = "0.7" +iced = { path = "../..", features = ["image", "debug", "tokio"] }  serde = { version = "1.0", features = ["derive"] }  serde_json = "1.0" +rand = { version = "0.7", features = ["wasm-bindgen"] } + +[dependencies.reqwest] +version = "0.10" +git = "https://github.com/hecrj/reqwest.git" +branch = "feature/wasm-deserialize-json" +features = ["json"] 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      } | 
