-
-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added more functionality to compose trip history
- Loading branch information
Showing
9 changed files
with
378 additions
and
120 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
93 changes: 93 additions & 0 deletions
93
automotive/src/main/java/com/ixam97/carStatsViewer/compose/TripDetailsViewModel.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,93 @@ | ||
package com.ixam97.carStatsViewer.compose | ||
|
||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.setValue | ||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.viewModelScope | ||
import com.ixam97.carStatsViewer.CarStatsViewer | ||
import com.ixam97.carStatsViewer.database.tripData.DrivingSession | ||
import com.ixam97.carStatsViewer.map.Mapbox | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.Job | ||
import kotlinx.coroutines.delay | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.asStateFlow | ||
import kotlinx.coroutines.flow.update | ||
import kotlinx.coroutines.launch | ||
|
||
class TripDetailsViewModel: ViewModel() { | ||
|
||
data class TripDetailsState( | ||
val drivingSession: DrivingSession? = null, | ||
val selectedSection: Int = 0, | ||
val startLocation: String? = null, | ||
val endLocation: String? = null, | ||
) | ||
|
||
// private var _tripDetailsState = MutableStateFlow<TripDetailsState>(TripDetailsState()) | ||
// val tripDetailsState = _tripDetailsState.asStateFlow() | ||
|
||
var tripDetailsState by mutableStateOf(TripDetailsState()) | ||
private set | ||
|
||
init { | ||
} | ||
|
||
fun setSelectedSection(section: Int) { | ||
// _tripDetailsState.update { _tripDetailsState.value.copy(selectedSection = section) } | ||
tripDetailsState = tripDetailsState.copy( | ||
selectedSection = section | ||
) | ||
} | ||
|
||
fun loadLocationStrings(): Job { | ||
return viewModelScope.launch(Dispatchers.IO) { | ||
delay(2000) | ||
tripDetailsState.drivingSession?.let { trip -> | ||
if (!trip.drivingPoints.isNullOrEmpty()) { | ||
val coordinates = trip.drivingPoints!!.filter { it.lat != null } | ||
val startAddr = Mapbox.getAddress( | ||
coordinates.first().lon!!.toDouble(), | ||
coordinates.first().lat!!.toDouble() | ||
) | ||
val destAddr = Mapbox.getAddress( | ||
coordinates.last().lon!!.toDouble(), | ||
coordinates.last().lat!!.toDouble() | ||
) | ||
// _tripDetailsState.update { | ||
// _tripDetailsState.value.copy( | ||
// startLocation = startAddr, | ||
// endLocation = destAddr | ||
// ) | ||
// } | ||
tripDetailsState = tripDetailsState.copy( | ||
startLocation = startAddr, | ||
endLocation = destAddr | ||
) | ||
} else { | ||
// _tripDetailsState.update { | ||
// _tripDetailsState.value.copy( | ||
// startLocation = "Location not available", | ||
// endLocation = "Location not available" | ||
// ) | ||
// } | ||
tripDetailsState = tripDetailsState.copy( | ||
startLocation = "Location not available", | ||
endLocation = "Location not available" | ||
) | ||
} | ||
} | ||
} | ||
} | ||
|
||
fun loadDrivingSession(sessionId: Long) { | ||
viewModelScope.launch(Dispatchers.IO) { | ||
delay(2000) | ||
tripDetailsState = tripDetailsState.copy( | ||
drivingSession = CarStatsViewer.tripDataSource.getFullDrivingSession(sessionId) | ||
) | ||
loadLocationStrings().join() | ||
} | ||
} | ||
} |
Oops, something went wrong.