Bimba.git

ref: 7c62bb46c1b5597393d86720528d20fc6ef4c3dc

app/src/main/java/ml/adamsprogs/bimba/models/adapters/FavouriteEditRowAdapter.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
package ml.adamsprogs.bimba.models.adapters

import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import kotlinx.coroutines.experimental.DefaultDispatcher
import kotlinx.coroutines.experimental.android.UI
import kotlinx.coroutines.experimental.launch
import kotlinx.coroutines.experimental.withContext
import ml.adamsprogs.bimba.ProviderProxy
import ml.adamsprogs.bimba.R
import ml.adamsprogs.bimba.collections.FavouriteStorage
import ml.adamsprogs.bimba.models.Favourite
import ml.adamsprogs.bimba.models.Plate
import ml.adamsprogs.bimba.models.StopSegment


class FavouriteEditRowAdapter(private var favourite: Favourite) :
        RecyclerView.Adapter<FavouriteEditRowAdapter.ViewHolder>() {

    private val segments = HashMap<String, StopSegment>()
    private val providerProxy = ProviderProxy()

    init {
        launch(UI) {
            withContext(DefaultDispatcher) {
                favourite.segments.forEach {
                    segments[it.stop] = providerProxy.fillStopSegment(it) ?: it
                }
            }
            this@FavouriteEditRowAdapter.notifyDataSetChanged()
        }
    }


    override fun getItemCount(): Int {
        return segments.flatMap { it.value.plates ?: emptyList<Plate.ID>() }.size
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        launch(UI) {
            val plates = segments.flatMap { it.value.plates ?: emptyList<Plate.ID>() }
            val favourites = FavouriteStorage.getFavouriteStorage()
            val id = plates.sortedBy { "${it.line}${it.stop}" }[position]
            val favouriteElement = withContext(DefaultDispatcher) {
                providerProxy.getStopName(id.stop).let {
                    "${it ?: ""} (${id.stop}):\n${id.line}${id.headsign}"
                }
            }
            holder.rowTextView.text = favouriteElement
            holder.deleteButton.setOnClickListener {
                launch(UI) {
                    favourite.segments.clear()
                    favourite.segments.addAll(segments.map { it.value })
                    favourites.delete(favourite.name, id)
                    favourite = favourites.favourites[favourite.name]!!
                    notifyDataSetChanged()
                }
            }
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val context = parent.context
        val inflater = LayoutInflater.from(context)

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

    inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val rowTextView: TextView = itemView.findViewById(R.id.favourite_edit_row)
        val deleteButton: ImageView = itemView.findViewById(R.id.favourite_edit_delete)
    }
}