Bimba.git

ref: 161e88895a382409bbb3c332bc2f904026d87e43

app/src/main/java/ml/adamsprogs/bimba/activities/StopSpecifyActivity.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
package ml.adamsprogs.bimba.activities

import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import kotlinx.android.synthetic.main.activity_stop_specify.*
import ml.adamsprogs.bimba.R
import ml.adamsprogs.bimba.models.gtfs.AgencyAndId
import ml.adamsprogs.bimba.models.Timetable
import android.content.Context
import android.widget.TextView
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater


class StopSpecifyActivity : AppCompatActivity() {

    companion object {
        const val EXTRA_STOP_IDS = "stopIds"
        const val EXTRA_STOP_NAME = "stopName"
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_stop_specify)

        val ids = intent.getStringExtra(EXTRA_STOP_IDS).split(",").map { AgencyAndId(it) }.toSet()
        val name = intent.getStringExtra(EXTRA_STOP_NAME)
        val timetable = Timetable.getTimetable()
        val headlines = timetable.getHeadlinesForStop(ids)

        val layoutManager = LinearLayoutManager(this)
        val departuresList: RecyclerView = list_view

        departuresList.adapter = ShedAdapter(this, headlines)
        departuresList.layoutManager = layoutManager

        setSupportActionBar(toolbar)
        supportActionBar?.title = name
    }

    class ShedAdapter(val context: Context, val values: Map<AgencyAndId, Pair<String, Set<String>>>) :
            RecyclerView.Adapter<ShedAdapter.ViewHolder>() {
        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
            val context = parent.context
            val inflater = LayoutInflater.from(context)

            val rowView = inflater.inflate(R.layout.row_shed, parent, false)
            return ViewHolder(rowView)
        }

        override fun getItemCount(): Int = values.size

        override fun onBindViewHolder(holder: ViewHolder, position: Int) {
            holder.root.setOnClickListener {
                val id = values.entries.sortedBy { it.value.first }[position].key
                val intent = Intent(context, StopActivity::class.java)
                intent.putExtra(StopActivity.SOURCE_TYPE, StopActivity.SOURCE_TYPE_STOP)
                intent.putExtra(StopActivity.EXTRA_STOP_ID, id)
                context.startActivity(intent)
            }
            holder.stopCode.text = values.values.sortedBy { it.first }[position].first
            holder.stopHeadlines.text = values.values.sortedBy { it.first }[position].second
                    .sortedBy { it } // fixme<p:1> natural sort
                    .joinToString()
        }

        inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
            val root = itemView.findViewById<View>(R.id.shed_row)!!
            val stopCode: TextView = itemView.findViewById(R.id.stop_code)
            val stopHeadlines: TextView = itemView.findViewById(R.id.stop_headlines)
        }
    }
}