Bimba.git

ref: 0f3ae9e13096218a22641ba4684cd48d77f987e3

app/src/main/java/ml/adamsprogs/bimba/departures/DeparturesActivity.kt


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package ml.adamsprogs.bimba.departures

import android.content.Context
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.Toast
import androidx.core.view.WindowCompat
import androidx.recyclerview.widget.LinearLayoutManager
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import ml.adamsprogs.bimba.api.*
import ml.adamsprogs.bimba.databinding.ActivityDeparturesBinding
import java.io.InputStream

class DeparturesActivity : AppCompatActivity() {
	private var _binding: ActivityDeparturesBinding? = null

	private val binding get() = _binding!!

	private lateinit var adapter: BimbaDeparturesAdapter

	override fun onCreate(savedInstanceState: Bundle?) {
		super.onCreate(savedInstanceState)
		_binding = ActivityDeparturesBinding.inflate(layoutInflater)
		setContentView(binding.root)

//		setSupportActionBar(binding.departuresAppBar)
//		supportActionBar?.title = "Półwiejska"
		binding.collapsingLayout.title = intent?.extras?.getString("name")
		binding.departuresRecycler.layoutManager = LinearLayoutManager(this)
		adapter = BimbaDeparturesAdapter(layoutInflater, this, listOf()) {
			Log.v("Departure", "clicked: $it")
			// todo show bottom sheet
		}
		binding.departuresRecycler.adapter = adapter
		WindowCompat.setDecorFitsSystemWindows(window, false)

		val shp = getSharedPreferences("shp", MODE_PRIVATE)
		val host = shp.getString("host", "bimba.apiote.xyz")!!

		// todo check every 30s

		MainScope().launch {
			intent?.extras?.getString("code")?.let {
				val departuresStream = getDepartures(
					Server(
						host, shp.getString("token", "")!!,
						shp.getString("${host}_feeds", "")!!
					), it, null
				)
				if (departuresStream == null) {
					// todo show empty state
					Toast.makeText(
						this@DeparturesActivity as Context,
						"Couldn't get response",
						Toast.LENGTH_SHORT
					).show()
				} else {
					updateItems(unmarshallDepartureResponse(departuresStream))
				}
			}
		}
	}

	private suspend fun unmarshallDepartureResponse(stream: InputStream): DeparturesSuccess {
		return withContext(Dispatchers.IO) {
			when (val response = DeparturesResponse.unmarshall(stream)) {
				is DeparturesSuccess -> {
					return@withContext response
				}
				else -> {
					TODO("Error response")
				}
			}
		}
	}

	private fun updateItems(response: DeparturesSuccess) {
		binding.departuresProgress.visibility = View.GONE
		binding.departuresRecycler.visibility = View.VISIBLE
		adapter.update(response.departures)
		// todo alerts
		// todo stop info
	}
}