Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(map): create the viewmodel for the list of hike routes #13

Merged
merged 2 commits into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,9 @@ dependencies {
// ---------- FireBase ------------
implementation(platform(libs.firebase.bom))

// ---------- OpenStreetMap ------------
implementation(libs.osmdroid)

// Adds a remote binary dependency only for local tests.
testImplementation(libs.junit)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package ch.hikemate.app.model.map

import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.osmdroid.util.BoundingBox

/**
* ViewModel for the list of hike routes
*
* @param TODO: should take a repository as a parameter
*/
open class ListOfHikeRoutesViewModel() : ViewModel() {
// List of all routes in the database
private val hikeRoutes_ =
MutableStateFlow<List<String>>(emptyList()) // TODO: should be a list of Route objects
val hikeRoutes: StateFlow<List<String>> = hikeRoutes_.asStateFlow()

// Selected route, i.e the route for the detail view
private val selectedHikeRoute_ = MutableStateFlow<String?>(null) // TODO: should be a Route object
open val selectedHikeRoute: StateFlow<String?> = selectedHikeRoute_.asStateFlow()

private val area_ = MutableStateFlow<BoundingBox?>(null)

// Creates a factory
companion object {
val Factory: ViewModelProvider.Factory =
object : ViewModelProvider.Factory {
@Suppress("UNCHECKED_CAST")
override fun <T : ViewModel> create(modelClass: Class<T>): T {
return ListOfHikeRoutesViewModel() as T
}
}
}

private suspend fun getRoutesAsync() {
// TODO: Should call the API repository to get all the routes filtered by the area
withContext(Dispatchers.IO) { Thread.sleep(100) }
hikeRoutes_.value = listOf("Route 1", "Route 2", "Route 3")
}

/** Gets all the routes from the database and updates the routes_ variable */
fun getRoutes() {
viewModelScope.launch(Dispatchers.IO) { getRoutesAsync() }
}

/**
* Sets the current displayed area on the map and updates the list of routes displayed in the
* list.
*
* @param area The area to be displayed
*/
fun setArea(area: BoundingBox) {
area_.value = area
getRoutes()
}

/**
* Selects a route to be displayed in the detail view
*
* @param hikeRoute The route to be displayed
*/
fun selectRoute(hikeRoute: String) { // TODO: should take a Route object
selectedHikeRoute_.value = hikeRoute
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package ch.hikemate.app.model.map

import org.junit.Assert.*
import org.junit.Before
import org.junit.Test
import org.osmdroid.util.BoundingBox

/** Testing the ListOfRoutesViewModel class */
class ListOfHikeRoutesViewModelTest {
private lateinit var listOfHikeRoutesViewModel: ListOfHikeRoutesViewModel

@Before
fun setUp() {
listOfHikeRoutesViewModel = ListOfHikeRoutesViewModel()
}

@Test
fun canBeCreatedAsFactory() {
val factory = ListOfHikeRoutesViewModel.Factory
val viewModel = factory.create(ListOfHikeRoutesViewModel::class.java)
assertNotNull(viewModel)
}

@Test
fun canGetRoutes() {
listOfHikeRoutesViewModel.getRoutes()
// Wait for the coroutine to finish
Thread.sleep(500)
assertNotEquals(listOfHikeRoutesViewModel.hikeRoutes.value.size, 0)
}

@Test
fun canSelectRoute() {
listOfHikeRoutesViewModel.selectRoute("Route 1")
assertEquals(listOfHikeRoutesViewModel.selectedHikeRoute.value, "Route 1")
}

@Test
fun canSetArea() {
listOfHikeRoutesViewModel.setArea(BoundingBox(0.0, 0.0, 0.0, 0.0))
// Wait for the coroutine to finish
Thread.sleep(500)
assertNotEquals(listOfHikeRoutesViewModel.hikeRoutes.value.size, 0)
}
}
1 change: 1 addition & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ kaspresso = { group = "com.kaspersky.android-components", name = "kaspresso", ve
kaspresso-compose = { group = "com.kaspersky.android-components", name = "kaspresso-compose-support", version.ref = "kaspresso" }

robolectric = { module = "org.robolectric:robolectric", version.ref = "robolectric" }
osmdroid = { module = "org.osmdroid:osmdroid-android", version = "6.1.14" }

# Firebase Libraries
firebase-auth = { module = "com.google.firebase:firebase-auth", version.ref = "firebaseAuth" }
Expand Down