Bimba.git

ref: 21e0cf7e22a8975e88a54e145dae8b2fc51aff45

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
package ml.adamsprogs.bimba.models

import android.os.Parcel
import android.os.Parcelable
import android.util.Log
import ml.adamsprogs.bimba.MessageReceiver
import ml.adamsprogs.bimba.getMode
import java.util.*
import kotlin.collections.ArrayList
import kotlin.collections.HashMap
import kotlin.collections.HashSet

class Favourite : Parcelable, MessageReceiver.OnVmListener {
    private var isRegisteredOnVmListener: Boolean = false
    var name: String
        private set
    var timetables: HashSet<Plate>
        private set
    private val vmDeparturesMap = HashMap<String, ArrayList<Departure>>()
    private var vmDepartures = ArrayList<Departure>()
    val timetable = Timetable.getTimetable()
    val size: Int
        get() = timetables.size

    private val requestValidityNumber = HashMap<String, Int>()
    private val requestValidity = HashMap<String, Boolean>()

    constructor(parcel: Parcel) {
        val array = ArrayList<String>()
        parcel.readStringList(array)
        val timetables = HashSet<Plate>()
        array.mapTo(timetables) { Plate.fromString(it) }
        this.name = parcel.readString()
        this.timetables = timetables
    }

    constructor(name: String, timetables: HashSet<Plate>) {
        this.name = name
        this.timetables = timetables

    }

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

    override fun writeToParcel(dest: Parcel?, flags: Int) {
        val parcel = timetables.map { it.toString() }
        dest?.writeStringList(parcel)
        dest?.writeString(name)
    }

    private fun filterVmDepartures() {
        this.vmDepartures
                .filter { it.timeTill() < 0 }
                .forEach { this.vmDepartures.remove(it) }
    }

    fun delete(plate: Plate) {
        timetables.remove(timetables.find { it.stop == plate.stop && it.line == plate.line })
    }

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

    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? {
        filterVmDepartures()
        if (timetables.isEmpty() && vmDepartures.isEmpty())
            return null

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

        val twoDayDepartures = nowDepartures()

        if (twoDayDepartures.isEmpty())
            return null

        return twoDayDepartures
                .filter { it.timeTill() >= 0 }
                .minBy { it.timeTill() }
    }

    private fun nowDepartures(): ArrayList<Departure> {
        val today = Calendar.getInstance().getMode()
        val tomorrowCal = Calendar.getInstance()
        tomorrowCal.add(Calendar.DAY_OF_MONTH, 1)
        val tomorrow = tomorrowCal.getMode()

        val departures = timetable.getStopDepartures(timetables)
        val todayDepartures = departures[today]!!
        val tomorrowDepartures = ArrayList<Departure>()
        val twoDayDepartures = ArrayList<Departure>()
        departures[tomorrow]!!.mapTo(tomorrowDepartures) { it.copy() }
        tomorrowDepartures.forEach { it.tomorrow = true }

        todayDepartures.forEach { twoDayDepartures.add(it) }
        tomorrowDepartures.forEach { twoDayDepartures.add(it) }
        return twoDayDepartures
    }

    fun allDepartures(): Map<String, List<Departure>> {
        val departures = timetable.getStopDepartures(timetables) as HashMap<String, ArrayList<Departure>>

        Log.i("Fav", "$vmDepartures")
        if (vmDepartures.isNotEmpty()) {
            val today = Calendar.getInstance().getMode()
            departures[today] = vmDepartures
        }

        return Departure.createDepartures(departures)
    }

    fun fullTimetable(): Map<String, List<Departure>>? {
        return timetable.getStopDepartures(timetables)
    }

    override fun onVm(vmDepartures: ArrayList<Departure>?, requester: String, id: String, size: Int) {
        val requesterName = requester.split(";")[0]
        val requesterTimetable: String = try {
            requester.split(";")[1]
        } catch (e: IndexOutOfBoundsException) {
            ""
        }

        if (!requestValidity.containsKey(id)) {
            requestValidity[id] = false
            requestValidityNumber[id] = 0
        }
        if (vmDepartures != null && requesterName == name) {
            vmDeparturesMap[requesterTimetable] = vmDepartures
            this.vmDepartures = vmDeparturesMap.flatMap { it.value } as ArrayList<Departure>
            requestValidity[id] = true
            requestValidityNumber[id] = requestValidityNumber[id]!! + 1
        } else if (requesterName == name) {
            requestValidityNumber[id] = requestValidityNumber[id]!! + 1
        }
        if (requestValidityNumber[id] == size) {
            if (!requestValidity[id]!!) {
                this.vmDepartures = ArrayList()
            }
            requestValidity.remove(id)
            requestValidityNumber.remove(id)
        }
        filterVmDepartures()
    }
}