Bimba.git

ref: c10e9e44bcd05997920b2b8bc94f7817535773da

app/src/main/java/ml/adamsprogs/bimba/models/Favourite.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
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package ml.adamsprogs.bimba.models

import android.content.*
import android.os.*
import ml.adamsprogs.bimba.*
import ml.adamsprogs.bimba.datasources.VmClient
import ml.adamsprogs.bimba.models.gtfs.AgencyAndId
import java.io.File
import java.math.BigInteger
import java.security.SecureRandom
import java.util.Calendar
import kotlin.collections.*

class Favourite : Parcelable, MessageReceiver.OnVmListener {
    private var isRegisteredOnVmListener: Boolean = false
    private val cacheDir: File
    var name: String
        private set
    var segments: HashSet<StopSegment>
        private set
    private var vmDepartures = HashMap<Plate.ID, List<Departure>>()
    var fullDepartures: Map<AgencyAndId, List<Departure>> = HashMap()
        private set
    val timetable = Timetable.getTimetable()

    val size
        get() = segments.sumBy {
            it.size
        }
    val isBackedByVm
        get() = vmDepartures.isNotEmpty()

    private val onVmPreparedListeners = HashSet<OnVmPreparedListener>()

    fun addOnVmPreparedListener(listener: OnVmPreparedListener) {
        onVmPreparedListeners.add(listener)
    }

    fun removeOnVmPreparedListener(listener: OnVmPreparedListener) {
        onVmPreparedListeners.remove(listener)
    }

    constructor(parcel: Parcel) {
        this.name = parcel.readString()
        @Suppress("UNCHECKED_CAST")
        val set = HashSet<StopSegment>()
        val array = parcel.readParcelableArray(StopSegment::class.java.classLoader)
        array.forEach {
            set.add(it as StopSegment)
        }
        this.segments = set
        this.cacheDir = File(parcel.readString())
        val mapDir = File(parcel.readString())

        val mapString = mapDir.readText()
        val ile = mapString.length / 1024

        val map = HashMap<AgencyAndId, List<Departure>>()
        mapString.safeSplit("%").forEach {
            val (k, v) = it.split("#")
            map[AgencyAndId(k)] = v.split("&").map { Departure.fromString(it) }
        }
        this.fullDepartures = map
        mapDir.delete()
    }

    constructor(name: String, segments: HashSet<StopSegment>, cache: Map<AgencyAndId, List<Departure>>, context: Context) {
        this.fullDepartures = cache
        this.name = name
        this.segments = segments
        this.cacheDir = context.cacheDir
    }

    constructor(name: String, timetables: HashSet<StopSegment>, context: Context) {
        this.name = name
        this.segments = timetables
        this.cacheDir = context.cacheDir

    }

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

    override fun writeToParcel(dest: Parcel?, flags: Int) {
        dest?.writeString(name)
        val parcelableSegments = segments.map { it }.toTypedArray()
        dest?.writeParcelableArray(parcelableSegments, flags)
        dest?.writeString(cacheDir.absolutePath)

        val bytes = ByteArray(4) { 0 }
        SecureRandom().nextBytes(bytes)
        val mapFile = File(cacheDir, BigInteger(1, bytes).toString(16))
        dest?.writeString(mapFile.absolutePath)

        var isFirst = true
        var map = ""
        fullDepartures.forEach {
            if (isFirst)
                isFirst = false
            else
                map += '%'

            map += "${it.key}#${it.value.joinToString("&") { it.toString() }}"
        }
        mapFile.writeText(map)
    }

    private fun filterVmDepartures() {
        val now = Calendar.getInstance().secondsAfterMidnight()
        this.vmDepartures.forEach {
            val newVms = it.value
                    .filter { it.timeTill(now) >= 0 }.sortedBy { it.timeTill(now) }
            this.vmDepartures[it.key] = newVms
        }
    }

    fun delete(plateId: Plate.ID) {
        segments.forEach {
            it.remove(plateId)
        }
        removeFromCache(plateId)
    }

    fun registerOnVm(receiver: MessageReceiver, context: Context) {
        if (!isRegisteredOnVmListener) {
            receiver.addOnVmListener(this)
            isRegisteredOnVmListener = true


            segments.forEach {
                val intent = Intent(context, VmClient::class.java)
                intent.putExtra("stop", it)
                intent.action = "request"
                context.startService(intent)
            }
        }
    }

    fun deregisterOnVm(receiver: MessageReceiver, context: Context) {
        if (isRegisteredOnVmListener) {
            receiver.removeOnVmListener(this)
            isRegisteredOnVmListener = false

            segments.forEach {
                val intent = Intent(context, VmClient::class.java)
                intent.putExtra("stop", it)
                intent.action = "remove"
                context.startService(intent)
            }
        }
    }

    fun rename(newName: String) {
        name = newName
    }

    companion object CREATOR : Parcelable.Creator<Favourite> {
        override fun createFromParcel(parcel: Parcel): Favourite {
            return Favourite(parcel)
        }

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

    fun nextDeparture(): Departure? {
        val now = Calendar.getInstance().secondsAfterMidnight()
        filterVmDepartures()
        if (segments.isEmpty() && vmDepartures.isEmpty())
            return null

        if (vmDepartures.isNotEmpty()) {
            return vmDepartures.flatMap { it.value }
                    .minBy {
                        it.timeTill(now)
                    }
        }

        val full = fullTimetable()

        val twoDayDepartures = try {
            Departure.rollDepartures(full)[timetable.getServiceForToday()]
        } catch (e: IllegalArgumentException) {
            listOf<Departure>()
        }

        if (twoDayDepartures?.isEmpty() != false)
            return null

        return twoDayDepartures[0]
    }

    fun allDepartures(): Map<AgencyAndId, List<Departure>> {
        if (vmDepartures.isNotEmpty()) {
            val now = Calendar.getInstance().secondsAfterMidnight()
            val departures = HashMap<AgencyAndId, List<Departure>>()
            val today = timetable.getServiceForToday()
            departures[today] = vmDepartures.flatMap { it.value }.sortedBy { it.timeTill(now) }
            return departures
        }

        val departures = fullTimetable()
        return Departure.rollDepartures(departures)
    }

    fun fullTimetable() =
            if (fullDepartures.isNotEmpty())
                fullDepartures
            else {
                fullDepartures = timetable.getStopDeparturesBySegments(segments)
                fullDepartures

            }


    override fun onVm(vmDepartures: Set<Departure>?, plateId: Plate.ID) {
        val now = Calendar.getInstance().secondsAfterMidnight()
        if (segments.any { it.contains(plateId) }) {
            if (vmDepartures == null)
                this.vmDepartures.remove(plateId)
            else
                this.vmDepartures[plateId] = vmDepartures.sortedBy { it.timeTill(now) }
        }
        filterVmDepartures()
        onVmPreparedListeners.forEach {
            it.onVmPrepared()
        }
    }

    private fun removeFromCache(plate: Plate.ID) {
        val map = HashMap<AgencyAndId, List<Departure>>()
        fullDepartures
        fullDepartures.forEach {
            map[it.key] = it.value.filter { plate.line != it.line || plate.headsign != it.headsign }
        }
        fullDepartures = map
    }

    interface OnVmPreparedListener {
        fun onVmPrepared()
    }
}