Bimba.git

ref: 2c61d7b87d057c7833f0974dfe17e2e70e12443b

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

import android.os.Parcel
import android.os.Parcelable


data class Route(val id: AgencyAndId, val agency: AgencyAndId, val shortName: String,
                 val longName: String, val description: String, val type: Int, val colour: Int,
                 val textColour: Int, val modifications: Map<String, String>) : Parcelable {
    companion object CREATOR : Parcelable.Creator<Route> {
        const val TYPE_BUS = 3
        const val TYPE_TRAM = 0

        override fun createFromParcel(parcel: Parcel): Route {
            return Route(parcel)
        }

        override fun newArray(size: Int): Array<Route?> {
            return arrayOfNulls(size)
        }

        fun create(id: String, agency: String, shortName: String, longName: String,
                                desc: String, type: Int, colour: Int, textColour: Int): Route {
            return if (desc.contains("|")) {
                val (to, from) = desc.split("|")
                val fromSplit = from.split("^")
                val toSplit = to.split("^")
                val description = "${toSplit[0]}|${fromSplit[0]}"
                val modifications = createModifications(desc)
                Route(AgencyAndId(id), AgencyAndId(agency), shortName, longName, description,
                        type, colour, textColour, modifications)
            } else {
                val toSplit = desc.split("^")
                val description = toSplit[0]
                val modifications = createModifications(desc)
                Route(AgencyAndId(id), AgencyAndId(agency), shortName, longName, description,
                        type, colour, textColour, modifications)
            }
        }

        fun createModifications(desc: String): Map<String, String> {
            val (to, from) = if(desc.contains('|')) desc.split("|") else listOf(desc, null)
            val toSplit = to!!.split("^")
            val fromSplit = from?.split("^")
            val modifications = HashMap<String, String>()
            toSplit.slice(1 until toSplit.size).forEach {
                val (k, v) = it.split(" - ")
                modifications[k] = v
            }
            fromSplit?.slice(1 until fromSplit.size)?.forEach {
                val (k,v) = it.split(" - ")
                modifications[k] = v
            }
            return modifications
        }
    }

    @Suppress("UNCHECKED_CAST")
    constructor(parcel: Parcel) : this(
            AgencyAndId(parcel.readString()),
            AgencyAndId(parcel.readString()),
            parcel.readString(),
            parcel.readString(),
            parcel.readString(),
            parcel.readInt(),
            parcel.readInt(),
            parcel.readInt(),
            parcel.readSerializable() as HashMap<String, String>)

    override fun writeToParcel(parcel: Parcel, flags: Int) {
        parcel.writeString(id.id)
        parcel.writeString(agency.id)
        parcel.writeString(shortName)
        parcel.writeString(longName)
        parcel.writeString(description)
        parcel.writeInt(type)
        parcel.writeInt(colour)
        parcel.writeInt(textColour)
        parcel.writeSerializable(modifications as HashMap)
    }

    override fun describeContents(): Int {
        return Parcelable.CONTENTS_FILE_DESCRIPTOR
    }
}