Bimba.git

ref: f42cabb4c00acd643489c8758be0f6d490414ea8

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
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package ml.adamsprogs.bimba.departures

import android.content.Context
import android.content.Intent
import android.content.SharedPreferences
import android.net.ConnectivityManager
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.appcompat.content.res.AppCompatResources
import androidx.core.content.res.ResourcesCompat
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.R
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 preferences: SharedPreferences

	private lateinit var adapter: BimbaDeparturesAdapter

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

		binding.collapsingLayout.apply {
			title = getName()
			val tf = ResourcesCompat.getFont(this@DeparturesActivity, R.font.railway)
			setCollapsedTitleTypeface(tf)
			setExpandedTitleTypeface(tf)
		}

		binding.departuresRecycler.layoutManager = LinearLayoutManager(this)
		adapter = BimbaDeparturesAdapter(layoutInflater, this, listOf()) {
			Log.v("Departure", "clicked: $it")
			val modalBottomSheet = ModalBottomSheet(it).apply {
				show(supportFragmentManager, ModalBottomSheet.TAG)
			}
		}
		binding.departuresRecycler.adapter = adapter
		WindowCompat.setDecorFitsSystemWindows(window, false)

		preferences = getSharedPreferences("shp", MODE_PRIVATE)

		// todo check every 30s
		getDepartures()
	}

	private fun getName(): String {
		return when (intent?.action) {
			Intent.ACTION_VIEW -> getCode()
			null -> intent?.extras?.getString("name") ?: ""
			else -> ""
		}
	}

	private fun getCode(): String {
		@Suppress("SpellCheckingInspection")
		return when (intent?.action) {
			Intent.ACTION_VIEW -> intent?.data?.getQueryParameter("przystanek") ?: ""
			null -> intent?.extras?.getString("code") ?: ""
			else -> ""
		}
	}

	private fun getDepartures() {
		val cm = getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
		val host = preferences.getString("host", "bimba.apiote.xyz")!!
		MainScope().launch {
			val departuresResult = getDepartures(
				cm,
				Server(
					host, preferences.getString("token", "")!!,
					preferences.getString("${host}_feeds", "")!!
				), getCode()
			)
			val response = if (departuresResult.stream != null) {
				unmarshallDepartureResponse(departuresResult.stream)
			} else {
				null
			}
			if (departuresResult.error != null) {
				Log.e("Departures", "$departuresResult")
				Log.e("Departures", "$response")
				showError(departuresResult.error)
			} else {
				updateItems((response as DeparturesSuccess))
			}
		}
	}

	private suspend fun unmarshallDepartureResponse(stream: InputStream): DeparturesResponse {
		return withContext(Dispatchers.IO) {
			DeparturesResponse.unmarshal(stream)
		}
	}

	private fun showError(error: Error) {
		binding.departuresProgress.visibility = View.GONE
		binding.departuresRecycler.visibility = View.GONE
		binding.errorImage.visibility = View.VISIBLE
		binding.errorText.visibility = View.VISIBLE

		binding.errorText.text = getString(error.stringResource)
		binding.errorImage.setImageDrawable(AppCompatResources.getDrawable(this, error.imageResource))
	}

	private fun updateItems(response: DeparturesSuccess) {
		binding.departuresProgress.visibility = View.GONE
		adapter.update(response.departures)
		binding.collapsingLayout.apply {
			title = response.stop.name
		}
		if (response.departures.isEmpty()) {
			binding.errorImage.visibility = View.VISIBLE
			binding.errorText.visibility = View.VISIBLE
			binding.departuresRecycler.visibility = View.GONE

			binding.errorText.text = getString(R.string.no_departures)
			binding.errorImage.setImageDrawable(
				AppCompatResources.getDrawable(
					this,
					R.drawable.error_search
				)
			)
		} else {
			binding.departuresOverlay.visibility = View.GONE
			binding.errorImage.visibility = View.GONE
			binding.errorText.visibility = View.GONE
			binding.departuresRecycler.visibility = View.VISIBLE
		}
		// todo alerts
		// todo stop info
	}
}