Skip to content

Commit

Permalink
Added more functionality to compose trip history
Browse files Browse the repository at this point in the history
  • Loading branch information
Ixam97 committed Aug 31, 2024
1 parent 6ac9df2 commit 910da80
Show file tree
Hide file tree
Showing 9 changed files with 378 additions and 120 deletions.
4 changes: 2 additions & 2 deletions automotive/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ android {
defaultConfig {
minSdkVersion 29
targetSdkVersion 34
versionCode 270
versionName "0.28.0.0003"
versionCode 271
versionName "0.28.0.0004"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,6 @@ internal fun TabsScreen.miscList() = ListTemplate.Builder().apply {
val composeSettingsActivityIntent = Intent(carContext, ComposeSettingsActivity::class.java).apply {
flags = Intent.FLAG_ACTIVITY_NEW_TASK
}
val composeTripDetailsScreen = Intent(carContext, ComposeTripDetailsActivity::class.java).apply {
flags = Intent.FLAG_ACTIVITY_NEW_TASK
}
val mainActivityIntent = Intent(carContext, MainActivity::class.java).apply {
flags = Intent.FLAG_ACTIVITY_NEW_TASK
}
Expand Down Expand Up @@ -73,14 +70,6 @@ internal fun TabsScreen.miscList() = ListTemplate.Builder().apply {
carContext.startActivity(composeSettingsActivityIntent)
})
}.build())
addItem(Row.Builder().apply {
setTitle("Compose Trip Details")
setImage(CarIcon.Builder(IconCompat.createWithResource(carContext, R.drawable.ic_car_app_debug)).build())
setBrowsable(true)
setOnClickListener(ParkedOnlyOnClickListener.create {
carContext.startActivity(composeTripDetailsScreen)
})
}.build())
addItem(Row.Builder().apply{
setTitle("Debug")
setImage(CarIcon.Builder(IconCompat.createWithResource(carContext, R.drawable.ic_car_app_debug)).build())
Expand Down
1 change: 1 addition & 0 deletions automotive/src/legacy/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
<meta-data android:name="distractionOptimized" android:value="true"/>
</activity>
<activity android:name=".compose.ComposeSettingsActivity" android:exported="false" />
<activity android:name=".compose.ComposeTripDetailsActivity" android:exported="false" />

<service android:name=".dataCollector.DataCollector"
android:foregroundServiceType="location"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ import android.os.Build
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material.CircularProgressIndicator
import androidx.compose.runtime.collectAsState
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.lifecycle.viewmodel.compose.viewModel
import com.ixam97.carStatsViewer.CarStatsViewer
import com.ixam97.carStatsViewer.R
Expand All @@ -17,10 +22,31 @@ class ComposeTripDetailsActivity: ComponentActivity() {
super.onCreate(savedInstanceState)
setTheme(R.style.AppTheme)

setContent {
if (!intent.hasExtra("SessionId")) {
finish()
} else {
val sessionId = intent.getLongExtra("SessionId", 0)
setContent {
val viewModel: TripDetailsViewModel = viewModel()

CarTheme(Build.BRAND) {
TripDetailsPortraitScreen(CarStatsViewer.dataProcessor.selectedSessionData)
val tripDetailsState = viewModel.tripDetailsState

CarTheme(Build.BRAND) {
if (tripDetailsState.drivingSession != null) {
TripDetailsPortraitScreen(
viewModel = viewModel
)
} else {
viewModel.loadDrivingSession(sessionId)
Box(
modifier = Modifier
.fillMaxSize(),
contentAlignment = Alignment.Center
) {
CircularProgressIndicator()
}
}
}
}
}
}
Expand Down
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()
}
}
}
Loading

0 comments on commit 910da80

Please sign in to comment.