Skip to content

Commit

Permalink
Tab Screen rearrangement
Browse files Browse the repository at this point in the history
  • Loading branch information
Ixam97 committed Apr 17, 2024
1 parent e3d2bcc commit b2220ff
Show file tree
Hide file tree
Showing 17 changed files with 1,140 additions and 181 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ class CarStatsViewer : Application() {
context,
0,
serviceIntent,
PendingIntent.FLAG_UPDATE_CURRENT
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_MUTABLE
)
alarmManager.cancel(pendingIntent)
if (cancel) return
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.ixam97.carStatsViewer.carApp

import android.car.Car
import androidx.annotation.OptIn
import androidx.car.app.annotations.ExperimentalCarApi
import androidx.car.app.model.CarIcon
import androidx.car.app.model.ItemList
import androidx.car.app.model.ListTemplate
import androidx.car.app.model.Row
import androidx.car.app.model.SectionedItemList
import androidx.core.graphics.drawable.IconCompat
import com.ixam97.carStatsViewer.CarStatsViewer
import com.ixam97.carStatsViewer.R

@OptIn(ExperimentalCarApi::class)
internal fun CarStatsViewerScreen.CarStatsList() = ListTemplate.Builder().apply {
addSectionedList(SectionedItemList.create(
ItemList.Builder().apply {
addItem(Row.Builder().apply {
setTitle("Not yet supported!")
setImage(CarIcon.Builder(IconCompat.createWithResource(carContext, R.drawable.ic_construction)).build())
}.build())
}.build(),
"Real time data"
))
addSectionedList(SectionedItemList.create(
ItemList.Builder().apply {

apiState.forEach { (apiName, status) ->
val apiIconColor = when (status) {
0 -> colorDisconnected
1 -> colorConnected
2 -> colorLimited
else -> colorError
}
addItem(Row.Builder().apply {
setTitle(apiName)
addText(when (status) {
0 -> "Disabled"
1 -> "Connected"
2 -> "Limited"
else -> "Error"
})
setImage(CarIcon.Builder(IconCompat.createWithResource(carContext, R.drawable.ic_connected)).setTint(apiIconColor).build())
}.build())
}
}.build(),
"APIs"
))
}.build()
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import androidx.car.app.model.TabTemplate.TabCallback
import androidx.car.app.model.Template
import androidx.core.content.ContextCompat
import androidx.core.graphics.drawable.IconCompat
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.LifecycleOwner
import com.ixam97.carStatsViewer.CarStatsViewer
import com.ixam97.carStatsViewer.R
import com.ixam97.carStatsViewer.database.tripData.DrivingSession
Expand All @@ -30,6 +32,10 @@ import kotlinx.coroutines.launch
@ExperimentalCarApi
class CarStatsViewerScreen(carContext: CarContext) : Screen(carContext) {

private val CID_TRIP_DATA = "cid_trip_data"
private val CID_MENU = "cid_menu"
private val CID_STATUS = "cid_status"

internal var dataUpdate = false
internal var apiState: Map<String, Int> = mapOf()
internal var session : DrivingSession? = null
Expand All @@ -41,7 +47,7 @@ class CarStatsViewerScreen(carContext: CarContext) : Screen(carContext) {

internal var resetFlag = false

internal var selectedTabContentID = "trip_data"
internal var selectedTabContentID = CID_TRIP_DATA

private val settingsActivityIntent = Intent(carContext, SettingsActivity::class.java).apply {
flags = Intent.FLAG_ACTIVITY_NEW_TASK
Expand All @@ -53,7 +59,16 @@ class CarStatsViewerScreen(carContext: CarContext) : Screen(carContext) {
flags = Intent.FLAG_ACTIVITY_NEW_TASK
}

private var lifecycle = getLifecycle()


init {
lifecycle.addObserver(object : DefaultLifecycleObserver {
override fun onResume(owner: LifecycleOwner) {
super.onResume(owner)
invalidate()
}
})
setupListeners()
}

Expand All @@ -69,6 +84,7 @@ class CarStatsViewerScreen(carContext: CarContext) : Screen(carContext) {
invalidate()
}
}

override fun onGetTemplate(): Template {

// updateLiveData()
Expand All @@ -84,23 +100,29 @@ class CarStatsViewerScreen(carContext: CarContext) : Screen(carContext) {

private fun createTabTemplate() = TabTemplate.Builder(object : TabCallback {
override fun onTabSelected(tabContentId: String) {
when (tabContentId) {
"settings" -> carContext.startActivity(settingsActivityIntent)
"dashboard" -> carContext.startActivity(mainActivityIntent)
"trip_history" -> carContext.startActivity(historyActivityIntent)
}
selectedTabContentID = "trip_data"
// when (tabContentId) {
// "settings" -> carContext.startActivity(settingsActivityIntent)
// "dashboard" -> carContext.startActivity(mainActivityIntent)
// "trip_history" -> carContext.startActivity(historyActivityIntent)
// }

selectedTabContentID = tabContentId
invalidate()
}
}).apply {
setHeaderAction(Action.APP_ICON)
addTab(createTab(R.string.car_app_trip_data, "trip_data", R.drawable.ic_list))
addTab(createTab(R.string.history_title, "trip_history", R.drawable.ic_history))
addTab(createTab(R.string.car_app_dashboard, "dashboard", R.drawable.ic_diagram))
addTab(createTab(R.string.settings_title, "settings", R.drawable.ic_settings))
addTab(createTab(R.string.car_app_trip_data, CID_TRIP_DATA, R.drawable.ic_car_app_list))
addTab(createTab(R.string.car_app_status, CID_STATUS, R.drawable.ic_car_app_status))
addTab(createTab(R.string.car_app_menu, CID_MENU, R.drawable.ic_car_app_menu))
setTabContents(TabContents.Builder(
// createTripDataList()
createTripDataPane()
// TripDataList()
// createTripDataPane()
when (selectedTabContentID) {
CID_TRIP_DATA -> TripDataList()
CID_STATUS -> CarStatsList()
CID_MENU -> MenuList()
else -> throw Exception("Unsupported Content ID!")
}
).build())
setActiveTabContentId(selectedTabContentID)
}.build()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.ixam97.carStatsViewer.carApp

import androidx.car.app.CarContext
import androidx.car.app.Screen
import androidx.car.app.model.Action
import androidx.car.app.model.ActionStrip
import androidx.car.app.model.CarIcon
import androidx.car.app.model.MessageTemplate
import androidx.car.app.model.Template
import androidx.core.graphics.drawable.IconCompat
import com.ixam97.carStatsViewer.CarStatsViewer
import com.ixam97.carStatsViewer.R
import com.ixam97.carStatsViewer.database.tripData.TripType
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

class ConfirmResetScreen(carContext: CarContext) : Screen(carContext) {

override fun onGetTemplate(): Template {
return dialogTemplate()
}

private fun dialogTemplate() = MessageTemplate.Builder(
carContext.getString(R.string.dialog_reset_message)
).apply {
// addAction(Action.BACK)
setHeaderAction(Action.BACK)
setTitle(carContext.getString(R.string.dialog_reset_title))
setIcon(CarIcon.Builder(IconCompat.createWithResource(carContext, R.drawable.ic_reset)).build())
addAction(Action.Builder().apply {
setTitle(carContext.getString(R.string.dialog_reset_confirm))
setFlags(Action.FLAG_PRIMARY)
setOnClickListener {
CoroutineScope(Dispatchers.IO).launch {
CarStatsViewer.dataProcessor.resetTrip(
TripType.MANUAL,
CarStatsViewer.dataProcessor.realTimeData.drivingState
)
}
screenManager.popToRoot()
}
}.build())
addAction(Action.Builder().apply {
setTitle(carContext.getString(R.string.dialog_reset_cancel))
setOnClickListener {
screenManager.pop()
}
}.build())
}.build()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.ixam97.carStatsViewer.carApp

import android.content.Intent
import androidx.car.app.CarContext
import androidx.car.app.Screen
import androidx.car.app.model.CarIcon
import androidx.car.app.model.ItemList
import androidx.car.app.model.ListTemplate
import androidx.car.app.model.Row
import androidx.car.app.model.Template
import androidx.core.graphics.drawable.IconCompat
import com.ixam97.carStatsViewer.R
import com.ixam97.carStatsViewer.ui.activities.HistoryActivity
import com.ixam97.carStatsViewer.ui.activities.MainActivity
import com.ixam97.carStatsViewer.ui.activities.SettingsActivity

internal fun CarStatsViewerScreen.MenuList() = ListTemplate.Builder().apply {

val settingsActivityIntent = Intent(carContext, SettingsActivity::class.java).apply {
flags = Intent.FLAG_ACTIVITY_NEW_TASK
}
val mainActivityIntent = Intent(carContext, MainActivity::class.java).apply {
flags = Intent.FLAG_ACTIVITY_NEW_TASK
}
val historyActivityIntent = Intent(carContext, HistoryActivity::class.java).apply {
flags = Intent.FLAG_ACTIVITY_NEW_TASK
}

setSingleList(ItemList.Builder().apply {
addItem(Row.Builder().apply{
setTitle(carContext.getString(R.string.car_app_dashboard))
setImage(CarIcon.Builder(IconCompat.createWithResource(carContext, R.drawable.ic_diagram)).build())
setBrowsable(true)
setOnClickListener {
carContext.startActivity(mainActivityIntent)
}
}.build())
addItem(Row.Builder().apply{
setTitle(carContext.getString(R.string.history_title))
setImage(CarIcon.Builder(IconCompat.createWithResource(carContext, R.drawable.ic_history)).build())
setBrowsable(true)
setOnClickListener {
carContext.startActivity(historyActivityIntent)
}
}.build())
addItem(Row.Builder().apply{
setTitle(carContext.getString(R.string.settings_title))
setImage(CarIcon.Builder(IconCompat.createWithResource(carContext, R.drawable.ic_settings)).build())
setBrowsable(true)
setOnClickListener {
carContext.startActivity(settingsActivityIntent)
}
}.build())
}.build())
}.build()
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package com.ixam97.carStatsViewer.carApp

import androidx.car.app.CarContext
import androidx.car.app.Screen
import androidx.car.app.model.Action
import androidx.car.app.model.ActionStrip
import androidx.car.app.model.CarIcon
import androidx.car.app.model.ItemList
import androidx.car.app.model.ListTemplate
import androidx.car.app.model.Row
import androidx.car.app.model.SectionedItemList
import androidx.car.app.model.Template
import androidx.core.graphics.drawable.IconCompat
import com.ixam97.carStatsViewer.CarStatsViewer
import com.ixam97.carStatsViewer.R

class TripDataSettingsScreen(carContext: CarContext) : Screen(carContext) {

override fun onGetTemplate(): Template {
return tripDataSettingsList()
}

private fun tripDataSettingsList() = ListTemplate.Builder().apply {
setTitle(carContext.getString(R.string.car_app_trip_data))
setHeaderAction(Action.BACK)
if (CarStatsViewer.appPreferences.mainViewTrip == 0) {
setActionStrip(ActionStrip.Builder().apply {
addAction(Action.Builder().apply {
// setIcon(CarIcon.Builder(IconCompat.createWithResource(carContext, R.drawable.ic_reset)).build())
setTitle(carContext.getString(R.string.dialog_reset_confirm))
setOnClickListener {
screenManager.push(ConfirmResetScreen(carContext))
}
}.build())
}.build())
}
addSectionedList(SectionedItemList.create(ItemList.Builder().apply {
setSelectedIndex(CarStatsViewer.appPreferences.mainViewTrip)
addItem(tripTypeRow(0))
addItem(tripTypeRow(1))
addItem(tripTypeRow(2))
addItem(tripTypeRow(3))
setOnSelectedListener{ changeSelectedTrip(it) }
}.build(), carContext.getString(R.string.settings_select_trip)))
// addSectionedList(SectionedItemList.create(ItemList.Builder().apply {
// addItem(Row.Builder().apply {
// setTitle(carContext.getString(R.string.summary_button_reset_trip))
// setImage(CarIcon.Builder(IconCompat.createWithResource(carContext, R.drawable.ic_reset)).build())
// setOnClickListener {
// screenManager.push(ConfirmResetScreen(carContext))
// }
// }.build())
// }.build(), carContext.getString(R.string.CurrentTripData)))
}.build()

private fun changeSelectedTrip(index: Int) {
CarStatsViewer.dataProcessor.changeSelectedTrip(index + 1)
CarStatsViewer.appPreferences.mainViewTrip = index
screenManager.popToRoot()
}
private fun tripTypeRow(tripType: Int) = Row.Builder().apply {
when (tripType) {
0 -> {
setTitle(carContext.getString(R.string.CurrentTripData))
setImage(CarIcon.Builder(IconCompat.createWithResource(carContext, R.drawable.ic_hand)).build())
}
1 -> {
setTitle(carContext.getString(R.string.SinceChargeData))
setImage(CarIcon.Builder(IconCompat.createWithResource(carContext, R.drawable.ic_charger)).build())
}
2 -> {
setTitle(carContext.getString(R.string.AutoTripData))
setImage(CarIcon.Builder(IconCompat.createWithResource(carContext, R.drawable.ic_day)).build())
}
3 -> {
setTitle(carContext.getString(R.string.CurrentMonthData))
setImage(CarIcon.Builder(IconCompat.createWithResource(carContext, R.drawable.ic_month)).build())
}
else -> throw Exception("Unknown Trip Type!")
}
}.build()
}
Loading

0 comments on commit b2220ff

Please sign in to comment.