Weather is an Android app for Weather with Jetpack Compose. It uses openweathermap API. It is single screen app which displays weather data for a city.
There is a search function in the top app bar to search for any city and display its weather data.
The app is built with the Model-View-ViewModel (MVVM) architecture with Repository pattern which is used to separate the app's user interface, logic, and data.
- Model: Represents the data and the business logic of the app. It's the actual data and data sources, like databases or network requests.
- View: Represents the UI of the app. In your app, it would be the Jetpack Compose components that display the weather data to the user.
- ViewModel: It holds the data that the View needs, exposing this data through StateFlow.
- Repository: This is an additional layer between the ViewModel and the data source (in this case, the OpenWeatherMap API). The Repository centralizes the data fetching mechanism, so if in the future we decide to add another data source, such as a local database for offline caching, the ViewModel wouldn't need to know where the data is coming from. The Repository will handle data retrieval, whether it's from the API, local database, or other sources.
To visualize the flow:
- User opens the app and sees the main weather screen with weather details of the default city - Munich.
- User types a city name into the search bar.
- The app's ViewModel, through the Repository, makes a call to the OpenWeatherMap API.
- The API returns the weather data for the requested city.
- The Repository passes this data to the ViewModel.
- The ViewModel updates the View (the screen) with the fetched weather data, displaying it to the user.
In essence, this architecture ensures a separation of concerns, promoting a more scalable and maintainable codebase. It decouples the UI from the data and business logic, allowing each component to evolve independently.
- Kotlin - Most of the Android community uses Kotlin as their preferred choice of language.
- Jetpack:
- Jetpack Compose - Jetpack Compose is Androidβs modern toolkit for building native UI. It simplifies and accelerates UI development on Android. Quickly bring your app to life with less code, powerful tools, and intuitive Kotlin APIs.
- Android KTX - Android KTX is a set of Kotlin extensions that are included with Android Jetpack and other Android libraries. KTX extensions provide concise, idiomatic Kotlin to Jetpack, Android platform, and other APIs.
- AndroidX - The androidx namespace comprises the Android Jetpack libraries. It's a major improvement to the original Android Support Library, which is no longer maintained.
- Lifecycle - Lifecycle-aware components perform actions in response to a change in the lifecycle status of another component, such as activities and fragments. These components help you produce better-organized, and often lighter-weight code, that is easier to maintain.
- ViewModel - The ViewModel class is a business logic or screen level state holder. It exposes state to the UI and encapsulates related business logic. Its principal advantage is that it caches state and persists it through configuration changes.
- Dagger Hilt - A dependency injection library for Android that reduces the boilerplate of doing manual dependency injection in your project.
- Kotlin Coroutines - A concurrency design pattern that you can use on Android to simplify code that executes asynchronously and it's the recommended way for asynchronous programming on Android.
- Kotlin Flow - In coroutines, a flow is a type that can emit multiple values sequentially, as opposed to suspend functions that return only a single value.
- Retrofit - Retrofit is a REST client for Java/ Kotlin and Android by Square. Its a simple network library that is used for network transactions.
- OkHttp - OkHttp is an HTTP client. It perseveres when the network is troublesome as it will silently recover from common connection problems.
- GSON - JSON Parser, used to parse requests on the data layer for Entities and understands Kotlin non-nullable and default parameters.
- Logging Interceptor - An OkHttp interceptor which logs HTTP request and response data.
- Coil- An image loading library for Android backed by Kotlin Coroutines.
- Timber- A logger with a small, extensible API which provides utility on top of Android's normal Log class.
- Mockk- A mocking library for Kotlin
- Turbine- A small testing library for kotlinx.coroutines Flow