FacilityDetailActivity.kt 2.32 KB
Newer Older
1 2
package srct.whatsopen

3 4
import android.annotation.SuppressLint
import android.graphics.BitmapFactory
5
import android.os.Bundle
6
import android.widget.ImageView
7
import androidx.appcompat.app.AppCompatActivity
8 9 10 11
import androidx.appcompat.widget.Toolbar
import io.reactivex.Observable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
12 13

import kotlinx.android.synthetic.main.activity_facility_detail.*
14 15 16
import okhttp3.Request
import srct.whatsopen.model.Facility
import srct.whatsopen.util.WhatsOpenService
17 18 19

class FacilityDetailActivity : AppCompatActivity() {

20 21 22
    private lateinit var facility: Facility

    @SuppressLint("CheckResult")
23 24 25 26 27
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_facility_detail)
        setSupportActionBar(toolbar)

28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
        // ensure service is ready if we were launched by deep link or something
        WhatsOpenService.init(applicationContext)

        // get name
        val slug = intent.getStringExtra("slug")
        // get our facility object and set stuff correctly
        WhatsOpenService.getData().subscribe { data ->
            facility = data.find { facility -> facility.slug == slug }!!
            findViewById<Toolbar>(R.id.toolbar).title = facility.name
            // do the bg image
            if (facility.logoBitmap == null) {
                Observable.defer {
                    Observable.just(
                        WhatsOpenService.okHttpClient.newCall(
                            Request.Builder().url(
                                facility.logoUrl
                            ).build()
                        ).execute()
                    )
                }.subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe { response ->
                        facility.logoBitmap = BitmapFactory.decodeStream(response.body()!!.byteStream())
                        findViewById<ImageView>(R.id.facility_bg_image).setImageBitmap(facility.logoBitmap)
                    }
            } else { // skip the HTTP altogether
                findViewById<ImageView>(R.id.facility_bg_image).setImageBitmap(facility.logoBitmap)
            }
        }

58 59 60
        supportActionBar?.setDisplayHomeAsUpEnabled(true)
    }
}