|
| 1 | +package ch.hikemate.app.model.map |
| 2 | + |
| 3 | +import androidx.lifecycle.ViewModel |
| 4 | +import androidx.lifecycle.ViewModelProvider |
| 5 | +import kotlinx.coroutines.flow.MutableStateFlow |
| 6 | +import kotlinx.coroutines.flow.StateFlow |
| 7 | +import kotlinx.coroutines.flow.asStateFlow |
| 8 | + |
| 9 | +/** |
| 10 | + * ViewModel for the list of hike routes |
| 11 | + * |
| 12 | + * @param TODO: should take a repository as a parameter |
| 13 | + */ |
| 14 | +open class ListOfHikeRoutesViewModel() : ViewModel() { |
| 15 | + // List of all routes in the database |
| 16 | + private val hikeRoutes_ = |
| 17 | + MutableStateFlow<List<String>>(emptyList()) // TODO: should be a list of Route objects |
| 18 | + val hikeRoutes: StateFlow<List<String>> = hikeRoutes_.asStateFlow() |
| 19 | + |
| 20 | + // Selected route, i.e the route for the detail view |
| 21 | + private val selectedHikeRoute_ = MutableStateFlow<String?>(null) // TODO: should be a Route object |
| 22 | + open val selectedHikeRoute: StateFlow<String?> = selectedHikeRoute_.asStateFlow() |
| 23 | + |
| 24 | + // Creates a factory |
| 25 | + companion object { |
| 26 | + val Factory: ViewModelProvider.Factory = |
| 27 | + object : ViewModelProvider.Factory { |
| 28 | + @Suppress("UNCHECKED_CAST") |
| 29 | + override fun <T : ViewModel> create(modelClass: Class<T>): T { |
| 30 | + return ListOfHikeRoutesViewModel() as T |
| 31 | + } |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + /** Gets all the routes from the database and updates the routes_ variable */ |
| 36 | + fun getRoutes() { |
| 37 | + // TODO: should call the repository to get all the routes |
| 38 | + // repository.getRoutes(onSuccess = { routes_.value = it }, onFailure = {}) |
| 39 | + |
| 40 | + hikeRoutes_.value = listOf("Route 1", "Route 2", "Route 3") |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Selects a route to be displayed in the detail view |
| 45 | + * |
| 46 | + * @param hikeRoute The route to be displayed |
| 47 | + */ |
| 48 | + fun selectRoute(hikeRoute: String) { // TODO: should take a Route object |
| 49 | + selectedHikeRoute_.value = hikeRoute |
| 50 | + } |
| 51 | +} |
0 commit comments