Kotlin for Android Developers
Creating a Detail Activity
Download 1.04 Mb. Pdf ko'rish
|
Kotlin for Android Developers Learn Kotlin the Easy Way While Developing an Android App ( PDFDrive )
- Bu sahifa navigatsiya:
- 23.1 Preparing the request
23 Creating a Detail Activity
When we click on an item from the home screen, we would expect to navigate to a detail activity and see some extra info about the forecast for that day. We are currently showing a toast when an item is clicked, but it’s time to change that. 23.1 Preparing the request As we need to know which item we are going to show in the detail activity, logic tells we’ll need to send the id of the forecast to the detail. So the domain model needs a new id property: 1 data class Forecast(val id: Long, val date: Long, val description: String, 2 val high: Int, val low: Int, val iconUrl: String) The ForecastProvider also needs a new function, which returns the requested forecast by id. The DetailActivity will need it to recover the forecast based on the id it will receive. As all the requests always iterate over the sources and return the first non-null result, we can extract that behaviour to another function: 1 private fun 2 = sources.firstResult { f(it) } The function is generified using a non-nullable type. It will receive a function which uses a ForecastDataSource to return an nullable object of the generic type, and will finally return a non- nullable object. We can rewrite the previous request and write the new one this way: 1 fun requestByZipCode(zipCode: Long, days: Int): ForecastList = requestToSources { 2 val res = it.requestForecastByZipCode(zipCode, todayTimeSpan()) 3 if (res != null && res.size() >= days) res else null 4 } 5 6 fun requestForecast(id: Long): Forecast = requestToSources { 7 it.requestDayForecast(id) 8 } Now the data sources need to implement the new function: 103 23 Creating a Detail Activity 104 1 fun requestDayForecast(id: Long): Forecast? The ForecastDb will always have the required value already cached from previous requests, so we can get it from there this way: 1 override fun requestDayForecast(id: Long): Forecast? = forecastDbHelper.use { 2 val forecast = select(DayForecastTable.NAME).byId(id). 3 parseOpt { DayForecast(HashMap(it)) } 4 5 if (forecast != null) dataMapper.convertDayToDomain(forecast) else null 6 } The select query is very similar to the previous one. I created another utility function called byId , because a request by id is so common that a function like that simplifies the process and is easier to read. The implementation of the function is quite simple: 1 fun SelectQueryBuilder.byId(id: Long) 2 = whereSimple("_id = ?", id.toString()) It just makes use of the whereSimple function and implements the search over the _id field. This function is quite generic, but as you can see, you could create as many extension functions as you need based on the structure of your database, and hugely simplify the readability of your code. The DbDataMapper has some slight changes not worth mentioning. You can check them in the repository. On the other hand, the ForecastServer will never be used, because the info will be always cached in the database. We could implement it to defend our code from uncommon edge cases, but we’re not doing it in this case, so it will just throw an exception if it’s called: 1 override fun requestDayForecast(id: Long) 2 = throw UnsupportedOperationException() |
Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling
ma'muriyatiga murojaat qiling