Bimba.git

ref: 93a6775f0368bca54e65214a198aad04e8cff92a

app/src/main/java/xyz/apiote/bimba/czwek/dashboard/ui/home/HomeViewModel.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
// SPDX-FileCopyrightText: Adam Evyčędo
//
// SPDX-License-Identifier: GPL-3.0-or-later

package xyz.apiote.bimba.czwek.dashboard.ui.home

import android.content.Context
import android.os.Handler
import android.os.Looper
import android.text.Editable
import android.text.TextWatcher
import android.util.Log
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.async
import kotlinx.coroutines.awaitAll
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.launch
import xyz.apiote.bimba.czwek.repo.Departure
import xyz.apiote.bimba.czwek.repo.Favourite
import xyz.apiote.bimba.czwek.repo.FeedInfo
import xyz.apiote.bimba.czwek.repo.OfflineRepository
import xyz.apiote.bimba.czwek.repo.OnlineRepository
import xyz.apiote.bimba.czwek.repo.Queryable
import xyz.apiote.bimba.czwek.repo.TrafficResponseException
import xyz.apiote.bimba.czwek.settings.feeds.FeedsSettings
import java.sql.SQLException
import java.util.Optional

class HomeViewModel : ViewModel() {
	private val mutableQueryables = MutableLiveData<List<Queryable>>()
	val queryables: LiveData<List<Queryable>> = mutableQueryables
	var feeds: Map<String, FeedInfo>? = null
	var feedsSettings: FeedsSettings? = null
	private val mutableFavourites = MutableLiveData<List<Favourite>>()
	val favourites: LiveData<List<Favourite>> = mutableFavourites
	private val mutableDepartures = MutableLiveData<Map<String, Optional<Departure>>>()
	val departures: LiveData<Map<String, Optional<Departure>>> = mutableDepartures

	fun getQueryables(query: String, context: Context) {
		viewModelScope.launch {
			try {
				getFeeds(context)
				mutableQueryables.value = OnlineRepository().queryQueryables(query, context) ?: emptyList()
			} catch (e: TrafficResponseException) {
				// XXX intentionally no error showing in suggestions
				Log.e("Suggestion", "$e")
			}
		}
	}

	fun getFavourites(context: Context) {
		viewModelScope.launch {
			try {
				getFeeds(context)
				val repository = OfflineRepository(context)
				mutableFavourites.value =
					repository.getFavourites(feedsSettings?.activeFeeds() ?: emptySet())
				repository.close()
			} catch (e: SQLException) {
				Log.w("FavouritesForFavourite", "$e")
			}
			getDeparturesOnly(context)
		}
	}

	fun getDepartures(context: Context) {
		viewModelScope.launch {
			getDeparturesOnly(context)
		}
	}

	private suspend fun getDeparturesOnly(context: Context) {
		coroutineScope {
			if (favourites.value == null)
				return@coroutineScope
			mutableDepartures.value = favourites.value!!.map { favourite ->
				async {
					try {
						val repository = OnlineRepository()
						val stopDepartures =
							repository.getDepartures(
								favourite.feedID,
								favourite.stopCode,
								null,
								context,
								12  // XXX heuristics
							)
							stopDepartures?.let { sDs ->
								if (sDs.departures.isEmpty()) {
									Pair(favourite.feedID+favourite.stopCode, Optional.empty())
								} else {
									Pair(favourite.feedID+favourite.stopCode, Optional.ofNullable(sDs.departures.find { departure ->
											favourite.lines.isEmpty() or favourite.lines.contains(
												departure.vehicle.Line.name
											)
										}))
								}
						} ?: Pair(favourite.feedID+favourite.stopCode, Optional.empty())
					} catch (e: TrafficResponseException) {
						Log.w("DeparturesForFavourite", "$e")
						Pair(favourite.feedID+favourite.stopCode, Optional.empty())
					}
				}
			}.awaitAll().associate { it }
		}
	}

	private suspend fun getFeeds(context: Context) {
		val repository = OfflineRepository(context)
		feeds = repository.getFeeds(context)
		repository.close()
		feedsSettings = FeedsSettings.load(context)
	}

	fun saveFavourites(newFavourites: List<Favourite>, context: Context) {
		viewModelScope.launch {
			try {
				val repository = OfflineRepository(context)
				repository.saveFavourites(newFavourites.toSet())
				mutableFavourites.value = newFavourites
				repository.close()
			} catch (e: SQLException) {
				Log.w("FavouritesForFavourite", "$e")
			}
		}
	}


	inner class SearchBarWatcher(private val context: Context) : TextWatcher {
		private val handler = Handler(Looper.getMainLooper())
		private var workRunnable = Runnable {}

		override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
		}

		override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
		}

		override fun afterTextChanged(s: Editable?) {
			handler.removeCallbacks(workRunnable)
			workRunnable = Runnable {
				val text = s.toString()
				getQueryables(text, context)
			}
			handler.postDelayed(workRunnable, 750)
		}
	}
}