StopSpecifyActivity.kt 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. package ml.adamsprogs.bimba.activities
  2. import android.content.Context
  3. import android.content.Intent
  4. import android.os.Bundle
  5. import android.view.LayoutInflater
  6. import android.view.View
  7. import android.view.ViewGroup
  8. import android.widget.TextView
  9. import androidx.appcompat.app.AppCompatActivity
  10. import kotlinx.android.synthetic.main.activity_stop_specify.*
  11. import ml.adamsprogs.bimba.ProviderProxy
  12. import ml.adamsprogs.bimba.R
  13. class StopSpecifyActivity : AppCompatActivity() {
  14. companion object {
  15. const val EXTRA_STOP_NAME = "stopName"
  16. }
  17. override fun onCreate(savedInstanceState: Bundle?) {
  18. super.onCreate(savedInstanceState)
  19. setContentView(R.layout.activity_stop_specify)
  20. val name = intent.getStringExtra(EXTRA_STOP_NAME)
  21. val providerProxy = ProviderProxy(this)
  22. providerProxy.getSheds(name) {
  23. progressBar.visibility = View.GONE
  24. val layoutManager = androidx.recyclerview.widget.LinearLayoutManager(this)
  25. val departuresList: androidx.recyclerview.widget.RecyclerView = list_view
  26. departuresList.adapter = ShedAdapter(this, it, name)
  27. departuresList.layoutManager = layoutManager
  28. }
  29. /*val timetable = Timetable.getTimetable(this)
  30. val headlines = timetable.getHeadlinesForStop(name)*/
  31. setSupportActionBar(toolbar)
  32. supportActionBar?.title = name
  33. }
  34. class ShedAdapter(val context: Context, private val values: Map<String, Set<String>>, private val stopName: String) :
  35. androidx.recyclerview.widget.RecyclerView.Adapter<ShedAdapter.ViewHolder>() {
  36. override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
  37. val context = parent.context
  38. val inflater = LayoutInflater.from(context)
  39. val rowView = inflater.inflate(R.layout.row_shed, parent, false)
  40. return ViewHolder(rowView)
  41. }
  42. override fun getItemCount(): Int = values.size
  43. override fun onBindViewHolder(holder: ViewHolder, position: Int) {
  44. holder.root.setOnClickListener {
  45. val code = values.keys.sorted()[position]
  46. val intent = Intent(context, StopActivity::class.java)
  47. intent.putExtra(StopActivity.SOURCE_TYPE, StopActivity.SOURCE_TYPE_STOP)
  48. intent.putExtra(StopActivity.EXTRA_STOP_CODE, code)
  49. intent.putExtra(StopActivity.EXTRA_STOP_NAME, stopName)
  50. context.startActivity(intent)
  51. }
  52. holder.stopCode.text = values.keys.sorted()[position]
  53. holder.stopHeadlines.text = values.entries.sortedBy { it.key }[position].value
  54. .sortedBy { it.split(" → ")[0].toInt() }
  55. .joinToString()
  56. }
  57. inner class ViewHolder(itemView: View) : androidx.recyclerview.widget.RecyclerView.ViewHolder(itemView) {
  58. val root = itemView.findViewById<View>(R.id.shed_row)!!
  59. val stopCode: TextView = itemView.findViewById(R.id.stop_code)
  60. val stopHeadlines: TextView = itemView.findViewById(R.id.stop_headlines)
  61. }
  62. }
  63. }