Bimba.git

ref: 8693f82819b7dc79e894edcff7d67d69c7f68b2b

app/src/main/java/xyz/apiote/bimba/czwek/journeys/Journeys.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
// SPDX-FileCopyrightText: Adam Evyčędo
//
// SPDX-License-Identifier: GPL-3.0-or-later

package xyz.apiote.bimba.czwek.journeys

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.google.android.material.card.MaterialCardView
import xyz.apiote.bimba.czwek.R
import xyz.apiote.bimba.czwek.repo.Journey

class JourneysViewHolder(itemView: View) : ViewHolder(itemView) {
	val root: MaterialCardView = itemView.findViewById(R.id.journey)
	val startTime: TextView = itemView.findViewById(R.id.start_time)
	val lines: TextView = itemView.findViewById(R.id.lines)
	val endTime: TextView = itemView.findViewById(R.id.end_time)
	val legs: LinearLayout = itemView.findViewById(R.id.legs)

	companion object {
		fun bind(
			holder: JourneysViewHolder,
			onClickListener: (Journey) -> Unit,
			journey: Journey,
			context: Context
		) {
			holder.root.setOnClickListener { onClickListener(journey) }
			holder.startTime.text =
				context.getString(R.string.time, journey.startTime.hour, journey.startTime.minute)
			holder.endTime.text =
				context.getString(R.string.time, journey.endTime.hour, journey.endTime.minute)
			holder.lines.text =
				journey.legs.map { it.start.vehicle.Line.name }.filter { it.isNotBlank() }.joinToString()
		}
	}
}

class JourneysAdapter(
	private val inflater: LayoutInflater,
	private val context: Context,
	private var items: List<Journey>,
	private val onClickListener: ((Journey) -> Unit),
) :
	RecyclerView.Adapter<JourneysViewHolder>() {
	override fun onCreateViewHolder(
		parent: ViewGroup,
		viewType: Int
	): JourneysViewHolder {
		val rowView = inflater.inflate(R.layout.journey, parent, false)
		return JourneysViewHolder(rowView)
	}

	override fun onBindViewHolder(
		holder: JourneysViewHolder,
		position: Int
	) {
		JourneysViewHolder.bind(holder, onClickListener, items[position], context)
	}

	override fun getItemCount(): Int = items.size
}