MainActivity.kt 2.35 KB
Newer Older
Dylan Jones's avatar
Dylan Jones committed
1 2
package srct.whatsopen

Dylan Jones's avatar
Dylan Jones committed
3
import android.annotation.SuppressLint
Dylan Jones's avatar
Dylan Jones committed
4
import android.os.Bundle
Dylan Jones's avatar
Dylan Jones committed
5
import android.util.Log
Dylan Jones's avatar
Dylan Jones committed
6 7
import android.view.Menu
import android.view.MenuItem
Dylan Jones's avatar
Dylan Jones committed
8
import android.widget.Toast
9
import androidx.appcompat.app.AppCompatActivity
Dylan Jones's avatar
Dylan Jones committed
10
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
Dylan Jones's avatar
Dylan Jones committed
11
import kotlinx.android.synthetic.main.activity_main.*
Dylan Jones's avatar
Dylan Jones committed
12
import srct.whatsopen.util.MainViewAdapter
Dylan Jones's avatar
Dylan Jones committed
13
import srct.whatsopen.util.WhatsOpenService
Dylan Jones's avatar
Dylan Jones committed
14 15

class MainActivity : AppCompatActivity() {
16
    private lateinit var refreshLayout: SwipeRefreshLayout
Dylan Jones's avatar
Dylan Jones committed
17 18 19 20 21

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        setSupportActionBar(toolbar)
22 23
        // initialize service
        WhatsOpenService.init(applicationContext)
Dylan Jones's avatar
Dylan Jones committed
24
        // setup refresh listener
Dylan Jones's avatar
Dylan Jones committed
25
        refreshLayout = findViewById(R.id.swipe_refresh)
26
        refreshLayout.setOnRefreshListener {
27
            WhatsOpenService.resetCache() // TODO only evict main API call?
28 29
            this.refresh()
        }
Dylan Jones's avatar
Dylan Jones committed
30
        refresh()
Dylan Jones's avatar
Dylan Jones committed
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
    }

    override fun onCreateOptionsMenu(menu: Menu): Boolean {
        // Inflate the menu; this adds items to the action bar if it is present.
        menuInflater.inflate(R.menu.menu_scrolling, menu)
        return true
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.

        return when (item.itemId) {
            R.id.action_settings -> true
            else -> super.onOptionsItemSelected(item)
        }
    }
Dylan Jones's avatar
Dylan Jones committed
49 50 51

    @SuppressLint("CheckResult")
    private fun refresh() {
52
        refreshLayout.isRefreshing = true
53
        WhatsOpenService.getData()
Dylan Jones's avatar
Dylan Jones committed
54
            .subscribe({ facilities ->
55
                refreshLayout.isRefreshing = false
Dylan Jones's avatar
Dylan Jones committed
56 57
                recycle_view.adapter = MainViewAdapter(facilities)
            }, { err ->
Dylan Jones's avatar
Dylan Jones committed
58 59
                Toast.makeText(
                    applicationContext,
Dylan Jones's avatar
Dylan Jones committed
60
                    "Network Error: $err, retrying",
Dylan Jones's avatar
Dylan Jones committed
61 62
                    Toast.LENGTH_SHORT
                ).show()
Dylan Jones's avatar
Dylan Jones committed
63 64 65
                // TODO exponental retry?
                Log.d("MainActivity.refresh", "network error: $err")
                this.refresh()
Dylan Jones's avatar
Dylan Jones committed
66
            })
Dylan Jones's avatar
Dylan Jones committed
67
    }
Dylan Jones's avatar
Dylan Jones committed
68
}