-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7510aa8
commit 56cb38f
Showing
5 changed files
with
80 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
android/demo-app/src/main/java/com/stadiamaps/ferrostar/DemoNavigationViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package com.stadiamaps.ferrostar | ||
|
||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.viewModelScope | ||
import com.stadiamaps.ferrostar.core.LocationProvider | ||
import com.stadiamaps.ferrostar.core.LocationUpdateListener | ||
import com.stadiamaps.ferrostar.core.NavigationUiState | ||
import com.stadiamaps.ferrostar.core.NavigationViewModel | ||
import java.util.concurrent.Executors | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.SharingStarted | ||
import kotlinx.coroutines.flow.map | ||
import kotlinx.coroutines.flow.stateIn | ||
import kotlinx.coroutines.flow.update | ||
import uniffi.ferrostar.Heading | ||
import uniffi.ferrostar.UserLocation | ||
|
||
// NOTE: We are aware that this is not a particularly great ViewModel. | ||
// We are working on improving this. See the discussion on | ||
// https://github.com/stadiamaps/ferrostar/pull/295. | ||
class DemoNavigationViewModel : ViewModel(), NavigationViewModel { | ||
private val locationStateFlow = MutableStateFlow<UserLocation?>(null) | ||
private val executor = Executors.newSingleThreadScheduledExecutor() | ||
|
||
fun startLocationUpdates(locationProvider: LocationProvider) { | ||
locationStateFlow.update { locationProvider.lastLocation } | ||
locationProvider.addListener( | ||
object : LocationUpdateListener { | ||
override fun onLocationUpdated(location: UserLocation) { | ||
locationStateFlow.update { location } | ||
} | ||
|
||
override fun onHeadingUpdated(heading: Heading) { | ||
// TODO: Heading | ||
} | ||
}, | ||
executor) | ||
} | ||
|
||
override val uiState = | ||
locationStateFlow | ||
.map { userLocation -> | ||
// TODO: Heading | ||
NavigationUiState(userLocation, null, null, null, null, null, null, false, null, null) | ||
} | ||
.stateIn( | ||
scope = viewModelScope, | ||
started = SharingStarted.WhileSubscribed(), | ||
// TODO: Heading | ||
initialValue = | ||
NavigationUiState(null, null, null, null, null, null, null, false, null, null)) | ||
|
||
override fun toggleMute() { | ||
// Do nothing | ||
} | ||
|
||
override fun stopNavigation() { | ||
// Do nothing | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters