ref: 85f4ee806ced6e075630cfb0cb52cead4c4a0f25
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 |
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 class Favourite : Parcelable, MessageReceiver.OnVmListener { override fun onVm(vmDepartures: ArrayList<Departure>?, requester: String) { val requesterName = requester.split(";")[0] var requesterTimetable: String try { requesterTimetable = requester.split(";")[1] } catch (e: IndexOutOfBoundsException) { requesterTimetable = "" } Log.i("VM", "got vm for $requesterName and my name is $name") if (vmDepartures != null && requesterName == name) { Log.i("VM", "so I’m adding") vmDeparturesMap[requesterTimetable] = vmDepartures this.vmDepartures = vmDeparturesMap.flatMap { it.value } as ArrayList<Departure> } Log.i("VM", "so I’m not adding") filterVmDepartures() } private var isRegisteredOnVmListener: Boolean = false var name: String private set var timetables: ArrayList<HashMap<String, String>> private set private var oneDayDepartures: ArrayList<HashMap<String, ArrayList<Departure>>>? = null private val vmDeparturesMap = HashMap<String, ArrayList<Departure>>() private var vmDepartures = ArrayList<Departure>() constructor(parcel: Parcel) { val array = ArrayList<String>() parcel.readStringList(array) val timetables = ArrayList<HashMap<String, String>>() for (row in array) { val element = HashMap<String, String>() element[TAG_STOP] = row.split("|")[0] element[TAG_LINE] = row.split("|")[1] timetables.add(element) } this.name = parcel.readString() this.timetables = timetables } constructor(name: String, timetables: ArrayList<HashMap<String, String>>) { this.name = name this.timetables = timetables } override fun describeContents(): Int { return 105 } override fun writeToParcel(dest: Parcel?, flags: Int) { val parcel = timetables.map { "${it[TAG_STOP]}|${it[TAG_LINE]}" } dest?.writeStringList(parcel) dest?.writeString(name) } val timetable = Timetable.getTimetable() val size: Int get() = timetables.size var nextDeparture: Departure? = null get() { filterVmDepartures() if (timetables.isEmpty() && vmDepartures.isEmpty()) return null Log.i("FAV", "vmDeps is empty? ${vmDepartures.isEmpty()} so") if (vmDepartures.isNotEmpty()) { val d = vmDepartures.minBy { it.timeTill() } Log.i("FAV", "using vm: ${d.toString()}") return d } Log.i("FAV", "using offline") val twoDayDepartures = ArrayList<Departure>() val today = Calendar.getInstance().getMode() val tomorrowCal = Calendar.getInstance() tomorrowCal.add(Calendar.DAY_OF_MONTH, 1) val tomorrow = tomorrowCal.getMode() if (oneDayDepartures == null) { oneDayDepartures = ArrayList<HashMap<String, ArrayList<Departure>>>() timetables.mapTo(oneDayDepartures!!) { timetable.getStopDepartures(it[TAG_STOP] as String, it[TAG_LINE]) } } oneDayDepartures!!.forEach { it[today]!!.forEach { twoDayDepartures.add(it.copy()) } } oneDayDepartures!!.forEach { it[tomorrow]!!.forEach { val d = it.copy() d.tomorrow = true twoDayDepartures.add(d) } } if (twoDayDepartures.isEmpty()) return null return twoDayDepartures .filter { it.timeTill() >= 0 } .minBy { it.timeTill() } } private set fun filterVmDepartures() { this.vmDepartures .filter { it.timeTill() < 0 } .forEach { this.vmDepartures.remove(it) } } fun delete(stop: String, line: String) { Log.i("ROW", "Favourite deleting $stop, $line") timetables.remove(timetables.find { it[TAG_STOP] == stop && it[TAG_LINE] == line }) Log.i("ROW", timetables.toString()) } fun registerOnVm(receiver: MessageReceiver) { Log.i("FAV", "Shall register? ${!isRegisteredOnVmListener}") if (!isRegisteredOnVmListener) { Log.i("FAV", "registering") 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) } val TAG_STOP = "stop" val TAG_LINE = "line" } } |