Learnings From Building A Weather App
Tags: android, java
Published: 2020-07-03 22:31:46

# Summary
This post is about learnings from my experience building apps  while following this [Udemy Course](https://www.udemy.com/course/the-complete-android-oreo-developer-course). 
I ended up adding quite a bit to the base app, and I'm glad I did - it was really good learning to experiment with different frameworks and such. It took some time - about 6 hours or so...

## Class Goal
Building a weather app, that fetches and displays information from an existing public API. 

### Class Learning Goal:
* Learn how to talk to public APIs and parse JSON from responses.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/lk0q3ftllig2nlf40fzf.png)

## My personal Goals

As part of trying to build this app, I wanted to learn more about:
* Dependency Injection
* JSON parsing frameworks

Most importantly I wanted to build a beautiful app similar to the [Yahoo Weather app](https://www.google.com/url?sa=i&url=https%3A%2F%2Fwww.technologyreview.com%2F2013%2F04%2F26%2F178661%2Fyahoos-weather-app-has-no-cool-interactions-and-thats-amazing%2F&psig=AOvVaw0nXTLobnHpfv5eCPd64SEa&ust=1593900087465000&source=images&cd=vfe&ved=0CAIQjRxqFwoTCNDVpdeKsuoCFQAAAAAdAAAAABAD), which changed its background based on current location. 

## Result

### github
{% github kuanp/DaWeatherApp %}

### Screenshot / Demo
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/lkyefxesc59yadtvn26l.png)

{% youtube hA_wfRgF0Fk %}

### UX improvements to the base app
* Beautiful backgrounds appropriate to the City and the current weather in the city. 
* Auto completion suggestions for city names; case insensitive search
* Toast messages for incorrect spelling
* Visible Search / Weather panels that doesn't obstruct view of the photos

## Learnings
### APIs
* Leverage Unsplash for great images. Unsplash APIs are quite easy and useful; used the "portrait" mode to search for photos that doesn't require resizing as much
* API tokens can be stored locally via BuildConfigs and git-ignored so they don't appear on the searchable web. See this [guide](https://guides.codepath.com/android/Storing-Secret-Keys-in-Android). Not the most secure for production, but good enough for learning projects. 
* [Retrofit](https://square.github.io/retrofit/) is such a nice library to use when the API use cases are super simple. It's ability to quickly generated a usable client with auto-json parsing abilities is great (make sure to use the converters like GsonConverter). 
* Gson is a lot easier to use than I expected. Good synergy w/ Lombok. 
* OkHttpInterceptors are good but a little bit of a pain. I read that OkHttpClients should be shared generally, but when they need to interface w/ separate services, I think we need to create a client for each service.  

### Architecture learnings
* Dagger takes a little setting up to get going (you have to create an Application Class in addition to the basic activities, and the entry point is for some reason in the activity oncreate). But once it's set up, it's a great framework that makes dependencies super easy to manage. I'll try Hilt next. 
* How to organize package structure for D.I. is still a mystery to me.
* Always use Java8. Streams are just too powerful to not use. 

### Android Learnings
* AsyncTask isn't that bad on the surface... I probably should explore alternatives given it's deprecated. 
* ArrayAdaptors are a lot more finicky that I had expected. They require a layout (either my own or from android) to know how render a TextView. I made the mistake of passing them the activity layout, not realizing that made the dropdown box render the whole activity and crash the app. 
* Animating background changes seems to be a pain. Didn't figure out how to do this. Good next actions. 
* Toast can have gravity and appear in customized locations on the screen.
* Hiding keyboard is hard, [apparently](https://stackoverflow.com/questions/1109022/close-hide-android-soft-keyboard). Also getting Enter key pressed to act like a submit button was interesting - use ```view.setOnKeyListener```.
* Careful when reading from file, because it could cost a lot of latency on startup. I honestly think I should've done it async...
* Semi-Transparent layouts can help display the image underneath and at the same time help with making texts more legible. 

## Further Meaningful Augmentations
* Add Animations/Transitions to photo changes, as I've mentioned
* Add Temperature, humidity, etc
* Save preferences
* Make the TextLayouts that make things legible a color gradient rather than a single grey color. This could also blur the background as well..



That's all! Thanks for reading...