-
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.
Refactor core to be pure functional; first movement on an Android Vie…
…wModel
- Loading branch information
1 parent
32ff425
commit b080b89
Showing
21 changed files
with
397 additions
and
391 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
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
58 changes: 58 additions & 0 deletions
58
android/core/src/main/java/com/stadiamaps/ferrostar/core/NavigationViewModel.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,58 @@ | ||
package com.stadiamaps.ferrostar.core | ||
|
||
import androidx.lifecycle.ViewModel | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import uniffi.ferrostar.Disposable | ||
import uniffi.ferrostar.NavigationControllerInterface | ||
import uniffi.ferrostar.TripState | ||
import uniffi.ferrostar.UserLocation | ||
import java.util.concurrent.Executors | ||
|
||
data class NavigationUiState( | ||
val snappedLocation: Location, | ||
val heading: Float? | ||
) | ||
|
||
/** | ||
* A view model for integrating state into an Android application. | ||
* | ||
* Uses [androidx.lifecycle.ViewModel]. | ||
* Note that it is assumed that the passed in [navigationController] | ||
* either requires no finalization OR that it conforms to [Disposable]. | ||
* In the case that it conforms to [Disposable], | ||
* the [navigationController] will be automatically destroyed in [onCleared]. | ||
*/ | ||
class NavigationViewModel( | ||
private val navigationController: NavigationControllerInterface, | ||
private val locationProvider: LocationProvider, | ||
initialUserLocation: UserLocation, | ||
) : ViewModel(), LocationUpdateListener { | ||
// TODO: Is this the best executor? | ||
private val _executor = Executors.newSingleThreadExecutor() | ||
private var _state = navigationController.getInitialState(initialUserLocation) | ||
// TODO: UI state flow? | ||
// private val _uiState = MutableStateFlow(NavigationUiState(snappedLocation = navigationController.)) | ||
|
||
init { | ||
locationProvider.addListener(this, _executor) | ||
} | ||
|
||
override fun onLocationUpdated(location: Location) { | ||
_state = navigationController.updateUserLocation(location = location.userLocation(), state = _state) | ||
// TODO: Update view model | ||
} | ||
|
||
override fun onHeadingUpdated(heading: Float) { | ||
// TODO: Update view model | ||
TODO("Not yet implemented") | ||
} | ||
|
||
override fun onCleared() { | ||
locationProvider.removeListener(this) | ||
_executor.shutdown() | ||
|
||
if (navigationController is Disposable) { | ||
navigationController.destroy() | ||
} | ||
} | ||
} |
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
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
Oops, something went wrong.