Bimba.git

ref: 26a0b0890ee5725de05f0093aaf69f77668cbf3b

app/src/main/java/ml/adamsprogs/bimba/ProviderProxy.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
package ml.adamsprogs.bimba

import android.content.Context
import android.content.Intent
import kotlinx.coroutines.*
import kotlinx.coroutines.android.Main
import ml.adamsprogs.bimba.datasources.VmClient
import ml.adamsprogs.bimba.datasources.VmService
import ml.adamsprogs.bimba.models.Departure
import ml.adamsprogs.bimba.models.Plate
import ml.adamsprogs.bimba.models.StopSegment
import ml.adamsprogs.bimba.models.Timetable
import ml.adamsprogs.bimba.models.suggestions.GtfsSuggestion
import ml.adamsprogs.bimba.models.suggestions.StopSuggestion
import java.util.*
import kotlin.collections.HashMap

class ProviderProxy(context: Context? = null) {
    private val vmClient = VmClient.getVmClient()
    private var timetable: Timetable = Timetable.getTimetable(context)
    private var suggestions = emptyList<GtfsSuggestion>()
    private val requests = HashMap<String, Request>()

    var mode = if (timetable.isEmpty()) MODE_VM else MODE_FULL

    companion object {
        const val MODE_FULL = "mode_full"
        const val MODE_VM = "mode_vm"
    }

    fun getSuggestions(query: String = "", callback: (List<GtfsSuggestion>) -> Unit) {
        GlobalScope.launch {
            suggestions = getStopSuggestions(query) //+ getLineSuggestions(query) //todo<p:v+1> + bike stations, train stations, &c
            val filtered = filterSuggestions(query)
            launch(Dispatchers.Main) {
                callback(filtered)
            }
        }
    }

    private suspend fun getStopSuggestions(query: String): List<StopSuggestion> {
        val vmSuggestions = withContext(Dispatchers.Default) {
            vmClient.getStops(query)
        }

        return if (vmSuggestions.isEmpty() and !timetable.isEmpty()) {
            timetable.getStopSuggestions()
        } else {
            vmSuggestions
        }
    }

    private fun filterSuggestions(query: String): List<GtfsSuggestion> {
        return suggestions.filter {
            deAccent(it.name).contains(deAccent(query), true)
        }
    }

    private fun deAccent(str: String): String {
        var result = str.replace('ę', 'e', true)
        result = result.replace('ó', 'o', true)
        result = result.replace('ą', 'a', true)
        result = result.replace('ś', 's', true)
        result = result.replace('ł', 'l', true)
        result = result.replace('ż', 'z', true)
        result = result.replace('ź', 'z', true)
        result = result.replace('ć', 'c', true)
        result = result.replace('ń', 'n', true)
        return result
    }

    fun getSheds(name: String, callback: (Map<String, Set<String>>) -> Unit) {
        GlobalScope.launch {
            val vmSheds = vmClient.getSheds(name)

            val sheds = if (vmSheds.isEmpty() and !timetable.isEmpty()) {
                timetable.getHeadlinesForStop(name)
            } else {
                vmSheds
            }

            launch(Dispatchers.Main) {
                callback(sheds)
            }
        }
    }

    fun subscribeForDepartures(stopSegments: Set<StopSegment>, listener: OnDeparturesReadyListener, context: Context): String {
        stopSegments.forEach {
            val intent = Intent(context, VmService::class.java)
            intent.putExtra("stop", it.stop)
            intent.action = "request"
            context.startService(intent)
        }
        val uuid = UUID.randomUUID().toString()
        requests[uuid] = Request(listener, stopSegments)
        return uuid
    }

    fun subscribeForDepartures(stopCode: String, listener: OnDeparturesReadyListener, context: Context): String {
        val intent = Intent(context, VmService::class.java)
        intent.putExtra("stop", stopCode)
        intent.action = "request"
        context.startService(intent)

        val uuid = UUID.randomUUID().toString()
        requests[uuid] = Request(listener, setOf(StopSegment(stopCode, null)))
        return uuid
    }

    private fun constructSegmentDepartures(stopSegments: Set<StopSegment>): Deferred<Map<String, List<Departure>>> {
        return GlobalScope.async {
            if (timetable.isEmpty())
                emptyMap()
            else {
                timetable.getStopDeparturesBySegments(stopSegments)
            }
        }
    }

    private fun filterDepartures(departures: Map<String, List<Departure>>): List<Departure> {
        val now = Calendar.getInstance().secondsAfterMidnight()
        val lines = HashMap<String, Int>()
        val twoDayDepartures = (timetable.getServiceForToday()?.let {
            departures[it]
        } ?: emptyList()) +
                (timetable.getServiceForTomorrow()?.let { service ->
                    departures[service]!!.map { it.copy().apply { tomorrow = true } }
                } ?: emptyList())

        return twoDayDepartures
                .filter { it.timeTill(now) >= 0 }
                .filter {
                    val existed = lines[it.line] ?: 0
                    if (existed < 3) {
                        lines[it.line] = existed + 1
                        true
                    } else false
                }
    }

    fun unsubscribeFromDepartures(uuid: String, context: Context) {
        requests[uuid]?.unsubscribe(context)
        requests.remove(uuid)
    }

    fun refreshTimetable(context: Context) {
        timetable = Timetable.getTimetable(context, true)
        mode = MODE_FULL
    }

    fun getFullTimetable(stopCode: String): Map<String, List<Departure>> {
        return if (timetable.isEmpty())
            emptyMap()
        else
            timetable.getStopDepartures(stopCode)

    }

    fun getFullTimetable(stopSegments: Set<StopSegment>): Map<String, List<Departure>> {
        return if (timetable.isEmpty())
            emptyMap()
        else
            timetable.getStopDeparturesBySegments(stopSegments)

    }

    fun fillStopSegment(stopSegment: StopSegment, callback: (StopSegment?) -> Unit) {
        GlobalScope.launch {
            callback(fillStopSegment(stopSegment))
        }
    }

    suspend fun fillStopSegment(stopSegment: StopSegment): StopSegment? {
        if (stopSegment.plates != null)
            return stopSegment

        return if (timetable.isEmpty())
            vmClient.getDirections(stopSegment.stop)
        else
            timetable.getHeadlinesForStopCode(stopSegment.stop)
    }

    fun getStopName(stopCode: String, callback: (String?) -> Unit) {
        GlobalScope.launch {
            callback(getStopName(stopCode))
        }
    }

    suspend fun getStopName(stopCode: String): String? {
        return if (timetable.isEmpty())
            vmClient.getName(stopCode)
        else
            timetable.getStopName(stopCode)
    }

    fun describeService(service: String, context: Context): String? {
        return if (timetable.isEmpty())
            null
        else
            timetable.getServiceDescription(service, context)
    }

    fun getServiceFirstDay(service: String): Int {
        return timetable.getServiceFirstDay(service)
    }

    interface OnDeparturesReadyListener {
        fun onDeparturesReady(departures: List<Departure>, plateId: Plate.ID?, code: Int)
    }

    inner class Request(private val listener: OnDeparturesReadyListener, private val segments: Set<StopSegment>) : MessageReceiver.OnVmListener {
        private val receiver = MessageReceiver.getMessageReceiver()
        private val receivedPlates = HashSet<Plate.ID>()

        private var cache: Deferred<Map<String, List<Departure>>>? = null

        init {
            receiver.addOnVmListener(this@Request)
            GlobalScope.launch {
                cache = constructSegmentDepartures(segments)
            }
        }

        override fun onVm(vmDepartures: Set<Departure>?, plateId: Plate.ID?, stopCode: String, code: Int) {
            GlobalScope.launch(Dispatchers.Main) {
                if ((plateId == null || vmDepartures == null) and (timetable.isEmpty())) {
                    listener.onDeparturesReady(emptyList(), null, code)
                    return@launch
                }
                if (plateId == null) {
                    listener.onDeparturesReady(filterDepartures(cache!!.await()), null, code)
                } else {
                    if (segments.any { plateId in it }) {
                        if (vmDepartures != null) {
                            listener.onDeparturesReady(vmDepartures.toList(), plateId, code)
                            if (plateId !in receivedPlates)
                                receivedPlates.add(plateId)
                        } else {
                            receivedPlates.remove(plateId)
                            if (receivedPlates.isEmpty()) {
                                listener.onDeparturesReady(filterDepartures(cache!!.await()), null, code)
                            }
                        }
                    }
                }
            }
        }

        fun unsubscribe(context: Context) {
            segments.forEach {
                val intent = Intent(context, VmService::class.java)
                intent.putExtra("stop", it.stop)
                intent.action = "remove"
                context.startService(intent)
            }
            receiver.removeOnVmListener(this)
        }
    }
}