Kimvais
Kimvais

Reputation: 39548

Is there a clean DRY way to update multiple textViews from HTTP request JSON?

I have the following Kotlin / Anko / Android activity.

import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.widget.TextView
import com.fasterxml.jackson.module.kotlin.readValue
import com.github.kittinunf.fuel.Fuel
import eu.gwapi.laaketilaus.util.JSON
import eu.gwapi.laaketilaus.util.Order
import org.jetbrains.anko.find
import org.jetbrains.anko.textView
import org.jetbrains.anko.toast
import org.jetbrains.anko.verticalLayout

class OrderDetailsActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val order_id: Long = intent.extras.getLong("order_id")
        verticalLayout {
            textView {
                id = R.id.order_detail_customer
            }
            textView {
                id = R.id.order_detail_address
            }
            textView {
                id = R.id.order_detail_postal_code
            }
            textView {
                id = R.id.order_detail_phone
            }
        }
        getOrder(order_id)
    }

    fun getOrder(order_id: Long) {
        Fuel.get("https://my.api.endpoint/" + order_id.toString()).responseString { request, response, result ->
            val (json, err) = result
            if (err != null) {
                toast(err.toString())
            } else {
                val order: Order = JSON.readValue(json!!)
                find<TextView>(R.id.order_detail_customer).text = order.customer
                find<TextView>(R.id.order_detail_address).text = order.address
                find<TextView>(R.id.order_detail_postal_code).text = order.postal_code
                find<TextView>(R.id.order_detail_phone).text = order.phone
            }
        }
    }
}

To a die-hard pythonista like me, that seems awfully static and verbose way of doing this.

Is there a better way?

Upvotes: 1

Views: 150

Answers (2)

Geoffrey Marizy
Geoffrey Marizy

Reputation: 5521

If you don't need to refresh with new data often, you don't need to keep references to TextView. I don't use Anko, but it could look like:

val order: Order = JSON.readValue(json!!)
verticalLayout {
    arrayOf(order.customer, order.address, order.postal_code, order.phone)
            .map {
                textView {
                    text = it
                }
            }
}

Upvotes: 1

hotkey
hotkey

Reputation: 147991

Since there are only TextViews and you only need to change their text, you can simplify your code in the following way:

  • Add the mapping for the Order properties storing the IDs:

    private val orderPropertyToTextViewId = mapOf(
            Order::customer to R.id.order_detail_customer,
            Order::address to R.id.order_detail_address,
            Order::postalCode to R.id.order_detail_postal_code,
            Order::phone to R.id.order_detail_phone
    )
    
  • Create the views iterating over the map:

    verticalLayout {
        for ((property, textViewId) in orderPropertyToTextViewId) {
            textView { id = textViewId }
        }
    }
    
  • Update the text iterating over the map:

    for ((property, textViewId) in orderPropertyToTextViewId) {
        findViewById<TextView>(textViewId).text = property.get(order)
    }
    

You can go even further and get rid of the IDs and findViewById<TextView>(...) if you store the TextViews returned by the textView { ... } calls instead of the IDs in the map, but that needs further experimenting.

Upvotes: 1

Related Questions