Skip to content

Commit

Permalink
Applied connection status changes to Car App
Browse files Browse the repository at this point in the history
  • Loading branch information
Ixam97 committed Apr 30, 2024
1 parent 608db32 commit d9f775a
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import androidx.car.app.model.Row
import androidx.car.app.model.SectionedItemList
import androidx.core.graphics.drawable.IconCompat
import com.ixam97.carStatsViewer.R
import com.ixam97.carStatsViewer.liveDataApi.ConnectionStatus

@OptIn(ExperimentalCarApi::class)
internal fun CarStatsViewerScreen.CarStatsList() = ListTemplate.Builder().apply {
Expand All @@ -25,18 +26,18 @@ internal fun CarStatsViewerScreen.CarStatsList() = ListTemplate.Builder().apply
ItemList.Builder().apply {

apiState.forEach { (apiName, status) ->
val apiIconColor = when (status) {
0 -> colorDisconnected
1 -> colorConnected
2 -> colorLimited
val apiIconColor = when (ConnectionStatus.fromInt(status)) {
ConnectionStatus.UNUSED -> colorDisconnected
ConnectionStatus.CONNECTED -> colorConnected
ConnectionStatus.LIMITED -> colorLimited
else -> colorError
}
addItem(Row.Builder().apply {
setTitle(apiName)
addText(when (status) {
0 -> "Disabled"
1 -> "Connected"
2 -> "Limited"
addText(when (ConnectionStatus.fromInt(status)) {
ConnectionStatus.UNUSED -> "Disabled"
ConnectionStatus.CONNECTED -> "Connected"
ConnectionStatus.LIMITED -> "Limited"
else -> "Error"
})
setImage(CarIcon.Builder(IconCompat.createWithResource(carContext, R.drawable.ic_connected)).setTint(apiIconColor).build())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,12 @@ class CarStatsViewerScreen(

override fun onPause(owner: LifecycleOwner) {
super.onPause(owner)
carDataSurfaceCallback.pause()
// carDataSurfaceCallback.pause()
}

override fun onResume(owner: LifecycleOwner) {
super.onResume(owner)
carDataSurfaceCallback.resume()
// carDataSurfaceCallback.resume()
}

override fun onGetTemplate(): Template {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import com.ixam97.carStatsViewer.CarStatsViewer
import com.ixam97.carStatsViewer.R
import com.ixam97.carStatsViewer.database.tripData.DrivingSession
import com.ixam97.carStatsViewer.database.tripData.TripType
import com.ixam97.carStatsViewer.liveDataApi.ConnectionStatus
import com.ixam97.carStatsViewer.liveDataApi.LiveDataApi
import com.ixam97.carStatsViewer.utils.StringFormatters
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
Expand Down Expand Up @@ -54,28 +56,35 @@ internal fun CarStatsViewerScreen.TripDataList(session: DrivingSession?) = ListT
))

var statusString = ""
var index = 0
var apiIconColor = colorDisconnected

apiState.forEach { (apiName, status) ->
statusString += "$apiName: $status "
if (index == CarStatsViewer.appPreferences.mainViewConnectionApi) {
CarStatsViewer.liveDataApis.forEachIndexed { apiIndex, liveDataApi ->
if (liveDataApi.connectionStatus.status == 0) return@forEachIndexed
val name = liveDataApi.apiIdentifier
val status = liveDataApi.connectionStatus
val statusName = when (status) {
ConnectionStatus.UNUSED -> "Disabled"
ConnectionStatus.CONNECTED -> "Connected"
ConnectionStatus.LIMITED -> "Limited"
else -> "Error"
}
if (statusString.isNotEmpty()) statusString += "\n"
statusString += "$name: $statusName"
if (apiIndex == CarStatsViewer.appPreferences.mainViewConnectionApi) {
apiIconColor = when (status) {
0 -> colorDisconnected
1 -> colorConnected
2 -> colorLimited
ConnectionStatus.UNUSED -> colorDisconnected
ConnectionStatus.CONNECTED -> colorConnected
ConnectionStatus.LIMITED -> colorLimited
else -> colorError
}
}

index++
}

if (statusString.isEmpty() || statusString == "") statusString = "No API available"

addItem(createDataRow(
statusString,
"Api Status",
null,
R.drawable.ic_connected,
apiIconColor
))
Expand Down Expand Up @@ -129,8 +138,8 @@ internal fun CarStatsViewerScreen.TripDataList(session: DrivingSession?) = ListT
}.build()

@OptIn(ExperimentalCarApi::class) private
fun CarStatsViewerScreen.createDataRow(value: String, name: String, iconResId: Int, iconColor: CarColor = CarColor.DEFAULT) = Row.Builder().apply {
fun CarStatsViewerScreen.createDataRow(value: String, name: String? = null, iconResId: Int, iconColor: CarColor = CarColor.DEFAULT) = Row.Builder().apply {
setTitle(value)
setImage(CarIcon.Builder(IconCompat.createWithResource(carContext, iconResId)).setTint(iconColor).build())
addText(name)
name?.let{addText(it)}
}.build()

0 comments on commit d9f775a

Please sign in to comment.