Skip to content

Commit

Permalink
Added compose activity for testing
Browse files Browse the repository at this point in the history
  • Loading branch information
Ixam97 committed May 27, 2024
1 parent 4bd88b7 commit 8b17593
Show file tree
Hide file tree
Showing 12 changed files with 322 additions and 5 deletions.
16 changes: 13 additions & 3 deletions automotive/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ android {
defaultConfig {
minSdkVersion 29
targetSdkVersion 33
versionCode 236
versionName "0.27.0.0030"
versionCode 238
versionName "0.27.0.0031"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
Expand All @@ -30,7 +30,7 @@ android {

carapp {
dimension "aaos"
applicationId "de.ixam97.carStatsViewer.play"
applicationId "de.ixam97.carStatsViewer.carApp"
}

dev {
Expand Down Expand Up @@ -73,6 +73,11 @@ android {
// android.car exists since Android 10 (API level 29) Revision 5.
useLibrary 'android.car'
namespace 'com.ixam97.carStatsViewer'


buildFeatures {
compose true
}
}

aboutLibraries {
Expand Down Expand Up @@ -101,6 +106,11 @@ dependencies {
implementation "androidx.car.app:app:1.7.0-alpha02"
implementation "androidx.car.app:app-automotive:1.7.0-alpha02"

implementation 'androidx.activity:activity-compose:1.7.2'
implementation 'androidx.compose.ui:ui:1.6.7'
implementation 'androidx.compose.material:material:1.6.7'
implementation 'androidx.lifecycle:lifecycle-viewmodel-compose:2.8.0'

implementation("androidx.room:room-ktx:2.5.0")
kapt("androidx.room:room-compiler:2.5.0")

Expand Down
1 change: 1 addition & 0 deletions automotive/src/carapp/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
<activity android:name=".ui.activities.AboutActivity" android:exported="false" />
<activity android:name=".ui.activities.SettingsApisActivity" android:exported="false" />
<activity android:name=".ui.activities.DebugActivity" android:exported="false" />
<activity android:name=".compose.ComposeActivity" android:exported="false" />

<meta-data android:name="com.android.automotive"
android:resource="@xml/automotive_app_desc"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ class CarStatsViewerScreen(

}

private var lifecycle = getLifecycle()
// private var lifecycle = getLifecycle()


init {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
package com.ixam97.carStatsViewer.carApp

import android.car.Car
import android.content.Intent
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.ParkedOnlyOnClickListener
import androidx.car.app.model.Row
import androidx.car.app.model.Toggle
import androidx.core.graphics.drawable.IconCompat
import com.ixam97.carStatsViewer.BuildConfig
import com.ixam97.carStatsViewer.CarStatsViewer
import com.ixam97.carStatsViewer.R
import com.ixam97.carStatsViewer.compose.ComposeActivity
import com.ixam97.carStatsViewer.ui.activities.DebugActivity
import com.ixam97.carStatsViewer.ui.activities.HistoryActivity
import com.ixam97.carStatsViewer.ui.activities.MainActivity
Expand All @@ -29,10 +32,12 @@ internal fun CarStatsViewerScreen.MenuList() = ListTemplate.Builder().apply {
val historyActivityIntent = Intent(carContext, HistoryActivity::class.java).apply {
flags = Intent.FLAG_ACTIVITY_NEW_TASK
}

val debugActivityIntent = Intent(carContext, DebugActivity::class.java).apply {
flags = Intent.FLAG_ACTIVITY_NEW_TASK
}
val composeActivityIntent = Intent(carContext, ComposeActivity::class.java).apply {
flags = Intent.FLAG_ACTIVITY_NEW_TASK
}

setSingleList(ItemList.Builder().apply {
addItem(Row.Builder().apply{
Expand Down Expand Up @@ -68,6 +73,14 @@ internal fun CarStatsViewerScreen.MenuList() = ListTemplate.Builder().apply {
}.setChecked(appPreferences.carAppRealTimeData).build())
}.build())
if (BuildConfig.FLAVOR_version == "dev") {
addItem(Row.Builder().apply {
setTitle("Compose Test UI")
setImage(CarIcon.Builder(IconCompat.createWithResource(carContext, R.drawable.ic_car_app_debug)).build())
setBrowsable(true)
setOnClickListener(ParkedOnlyOnClickListener.create {
carContext.startActivity(composeActivityIntent)
})
}.build())
addItem(Row.Builder().apply{
setTitle("Debug")
setImage(CarIcon.Builder(IconCompat.createWithResource(carContext, R.drawable.ic_car_app_debug)).build())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package com.ixam97.carStatsViewer.compose

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.Divider
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Text
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
import com.ixam97.carStatsViewer.CarStatsViewer
import com.ixam97.carStatsViewer.R
import com.ixam97.carStatsViewer.compose.components.CarIconButton
import com.ixam97.carStatsViewer.compose.components.CarSwitchRow
import com.ixam97.carStatsViewer.compose.theme.ComposeTestTheme
import com.ixam97.carStatsViewer.compose.theme.Typography
import com.ixam97.carStatsViewer.compose.theme.darkBackground
import com.ixam97.carStatsViewer.compose.theme.headerBackground
import com.ixam97.carStatsViewer.utils.InAppLogger

class ComposeActivity: ComponentActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setTheme(R.style.AppTheme)

val brand = CarStatsViewer.dataProcessor.staticVehicleData.vehicleMake

InAppLogger.d("brand: $brand")

setContent {

ComposeTestTheme(brand) {

val composeViewModel: ComposeViewModel = viewModel()

val composeActivityState by composeViewModel.composeActivityState.collectAsState()

Box(modifier = Modifier
.fillMaxSize()
.padding(15.dp)
.clip(RoundedCornerShape(10.dp))
.background(color = darkBackground)
) {
Column(
modifier = Modifier
.fillMaxWidth()
) {
Row(
modifier = Modifier
.fillMaxWidth()
.background(color = headerBackground),
verticalAlignment = Alignment.CenterVertically
) {
CarIconButton(onCLick = { finish() }, iconResId = R.drawable.ic_arrow_back)
Spacer(modifier = Modifier.width(10.dp))
Text(text = "Hello World!", style = Typography.h1)
}
Divider(
modifier = Modifier.height(3.dp),
color = MaterialTheme.colors.secondary
)

Column(
modifier = Modifier
.fillMaxSize()
) {
CarSwitchRow(
switchState = composeActivityState.switchStates[0],
onClick = { composeViewModel.setSwitch(0, !composeActivityState.switchStates[0]) }
) {
Text(text = "Row 1")
}
CarSwitchRow(
switchState = composeActivityState.switchStates[1],
onClick = { composeViewModel.setSwitch(1, !composeActivityState.switchStates[1]) }
) {
Text(text = "Row 2")
}
CarSwitchRow(
switchState = composeActivityState.switchStates[2],
onClick = { composeViewModel.setSwitch(2, !composeActivityState.switchStates[2]) }
) {
Text(text = "Row 3")
}
}
}
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.ixam97.carStatsViewer.compose

import androidx.lifecycle.ViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.update

class ComposeViewModel: ViewModel() {

data class ComposeActivityState(
val switchStates: List<Boolean> = listOf(false, false, false)
)

private val _composeActivityState = MutableStateFlow(ComposeActivityState())
val composeActivityState = _composeActivityState.asStateFlow()

fun setSwitch(switchIndex: Int, value: Boolean) {
if (switchIndex >= _composeActivityState.value.switchStates.size || switchIndex < 0) return

_composeActivityState.update {
val newSwitchStates = it.switchStates.toMutableList()
newSwitchStates[switchIndex] = value
it.copy(switchStates = newSwitchStates)
}
}

}

Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.ixam97.carStatsViewer.compose.components

import androidx.annotation.DrawableRes
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.material.Icon
import androidx.compose.material.IconButton
import androidx.compose.material.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.dp

@Composable
fun CarIconButton(onCLick: () -> Unit, @DrawableRes iconResId: Int) {
IconButton(
modifier = Modifier
.padding(10.dp)
.size(70.dp),
onClick = onCLick
) {
Icon(
painterResource(id = iconResId),
tint = MaterialTheme.colors.secondary,
contentDescription = null
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.ixam97.carStatsViewer.compose.components

import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.Switch
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.draw.scale
import androidx.compose.ui.unit.dp

@Composable
fun CarSwitchRow(
switchState: Boolean,
onClick: () -> Unit,
content: @Composable () -> Unit
) {
Row(
modifier = Modifier
.height(100.dp)
.clickable { onClick() }
.padding(horizontal = 30.dp),
verticalAlignment = Alignment.CenterVertically
) {
content()
Spacer(modifier = Modifier.weight(1f))
Switch(
modifier = Modifier
.scale(2f)
.padding(horizontal = 10.dp),
checked = switchState,
onCheckedChange = { onClick() }
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.ixam97.carStatsViewer.compose.theme

import androidx.compose.ui.graphics.Color

val polestarOrange = Color(0xFFD96C00)
val volvoBlue = Color(0xFF2F6093)

val darkBackground = Color(0xFF1B1B1B)
val mainBackground = Color(0xFF1F1F1F)
val headerBackground = Color(0xFF282A2D)
val primaryButtonGray = Color(0xFF3E4146)
val primaryGray = Color(0xFF7B858A)
val secondaryGray = Color(0xFFA3B1B8)
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.ixam97.carStatsViewer.compose.theme

import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.Shapes
import androidx.compose.ui.unit.dp

val Shapes = Shapes(
small = RoundedCornerShape(4.dp),
medium = RoundedCornerShape(4.dp),
large = RoundedCornerShape(0.dp)
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.ixam97.carStatsViewer.compose.theme

import androidx.compose.material.MaterialTheme
import androidx.compose.material.darkColors
import androidx.compose.runtime.Composable
import androidx.compose.ui.graphics.Color

private fun carAppColorPalate(primaryColor: Color? = null) = darkColors(
primary = primaryColor ?: primaryGray,
secondary = primaryColor ?: secondaryGray,
background = darkBackground,
onBackground = Color.White,
surface = darkBackground,
onSurface = Color.White,
onPrimary = Color.White
)

@Composable
fun ComposeTestTheme(carMake: String? = null, content: @Composable () -> Unit) {
val colors = carAppColorPalate(
primaryColor = when (carMake) {
"Polestar" -> polestarOrange
"Volvo" -> volvoBlue
else -> null
}
)

MaterialTheme(
colors = colors,
typography = Typography,
shapes = Shapes,
content = content
)
}
Loading

0 comments on commit 8b17593

Please sign in to comment.