Aim of this project is using API endpoint to retrieve information about countries to serve these data to user. User can select favourite countries and see details on detail page. These details are country code, flag and wikipedia information of selected country.
In this project, I used Retrofit, Room, MVVM architecture, Coroutines and Navigation.
- I should prepare to get information from API endpoint(https://rapidapi.com/wirefreethought/api/geodb-cities/).
Firstly, I must subscribe this API and get host and api-key information from this website.
Then, I chose Retrofit to send http request to the endpoint. At
CountriesApi.kt
interface I identified methods that use at Retrofit. These methods as described with keywordsuspend
because http request may take for a while that's why I use coroutines in this interface.
@Headers("X-RapidAPI-Key: ${BuildConfig.API_KEY}",
"X-RapidAPI-Host: wft-geo-db.p.rapidapi.com")
@GET("v1/geo/countries")
suspend fun getCountries(
@Query("limit") limit: Int = 10
): Response<Country>
- At
CountryService.kt
, I generated an object with Retrofit builder. Retrofit implements these methods at background Then, I wrote data classes to fit json files of endpoints. Parsing process is completed from GsonConverterFactory automatically.
- I used Room DB for saving favourites countries. Room DB is wrapper of SQLite.
- Firstly, I created a table with @Entity annotation at
SavedCountryData.kt
. After this, I wrote interfaces of methods for database process and Room implements these methods in background.Also, I usedsuspend
keyword for these methods as the same reason with http requests. In database class, a static database object is generated. I created a repository class that does the database operations.
- In this project, I used an activity and three fragments for UI. With navigation, user can switch between fragments.
loading.value = true
job = CoroutineScope(Dispatchers.IO).launch {
val response = countriesService.getCountries(10)
withContext(Dispatchers.Main) {
if(response.isSuccessful) {
loading.value = false
country.value = response.body()
}
}
}
}
* Adapter is used to make the suitable for RecyclerView.
<img width="334" alt="saved2" src="[https://user-images.githubusercontent.com/23053158/149675091-18eed16b-1a04-4bbb-a514-7302a11b375d.png](https://github.com/hanci1905/Countries/blob/main/Screenshot%202022-09-05%20225812.png)">
## Ongoing
### Bugs
* Home and Saved navigation buttons after clicking wiki data link are invisible at home and saved fragments.
* At detail fragment, flag of country must be shown, but I took 404 http error.
### Features
* Flag will be added to detail fragment.
* Saved button will be added to detail fragment.
* Unit test will be written.