Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Android auto: Switch to GridTemplate for home screen and default to favorites if defined #3694

Merged
merged 16 commits into from
Jul 27, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package io.homeassistant.companion.android.vehicle

import android.os.Build
import androidx.annotation.RequiresApi
import androidx.car.app.CarContext
import androidx.car.app.Screen
import androidx.car.app.model.Action
import androidx.car.app.model.ListTemplate
import androidx.car.app.model.Template
import androidx.lifecycle.lifecycleScope
import io.homeassistant.companion.android.BuildConfig
import io.homeassistant.companion.android.common.R
import io.homeassistant.companion.android.common.data.integration.Entity
import io.homeassistant.companion.android.common.data.integration.IntegrationRepository
import io.homeassistant.companion.android.common.data.integration.domain
import io.homeassistant.companion.android.common.data.prefs.PrefsRepository
import io.homeassistant.companion.android.common.data.servers.ServerManager
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.launch

@RequiresApi(Build.VERSION_CODES.O)
class DomainListScreen(
carContext: CarContext,
val serverManager: ServerManager,
val integrationRepository: IntegrationRepository,
private val serverId: StateFlow<Int>,
private val allEntities: Flow<Map<String, Entity<*>>>,
private val prefsRepository: PrefsRepository
) : Screen(carContext) {

private val domains = mutableSetOf<String>()

init {
lifecycleScope.launch {
allEntities.collect { entities ->
val newDomains = entities.values
.map { it.domain }
.distinct()
.filter { it in MainVehicleScreen.SUPPORTED_DOMAINS }
.toSet()
if (newDomains.size != domains.size || newDomains != domains) {
domains.clear()
domains.addAll(newDomains)
invalidate()
}
}
}
}
override fun onGetTemplate(): Template {
val screen = MainVehicleScreen(
dshokouhi marked this conversation as resolved.
Show resolved Hide resolved
carContext,
serverManager,
serverId,
allEntities,
prefsRepository
) { }
val domainList = screen.addDomainList(domains)

return ListTemplate.Builder().apply {
setTitle(carContext.getString(R.string.all_entities))
setHeaderAction(Action.BACK)
if (screen.isAutomotive && !screen.iDrivingOptimized && BuildConfig.FLAVOR != "full") {
setActionStrip(screen.nativeModeActionStrip())
}
if (domainList.build().items.isEmpty()) {
dshokouhi marked this conversation as resolved.
Show resolved Hide resolved
setLoading(true)
} else {
setLoading(false)
setSingleList(domainList.build())
}
}.build()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,30 +20,42 @@ import com.mikepenz.iconics.IconicsDrawable
import com.mikepenz.iconics.typeface.library.community.material.CommunityMaterial
import com.mikepenz.iconics.utils.sizeDp
import com.mikepenz.iconics.utils.toAndroidIconCompat
import io.homeassistant.companion.android.common.R
import io.homeassistant.companion.android.common.data.integration.Entity
import io.homeassistant.companion.android.common.data.integration.IntegrationRepository
import io.homeassistant.companion.android.common.data.integration.domain
import io.homeassistant.companion.android.common.data.integration.friendlyName
import io.homeassistant.companion.android.common.data.integration.friendlyState
import io.homeassistant.companion.android.common.data.integration.getIcon
import io.homeassistant.companion.android.common.data.integration.isExecuting
import io.homeassistant.companion.android.common.data.integration.onPressed
import io.homeassistant.companion.android.common.data.prefs.PrefsRepository
import io.homeassistant.companion.android.common.data.servers.ServerManager
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.launch

@RequiresApi(Build.VERSION_CODES.O)
class EntityGridVehicleScreen(
carContext: CarContext,
val serverManager: ServerManager,
val serverId: StateFlow<Int>,
val prefsRepository: PrefsRepository,
val integrationRepository: IntegrationRepository,
val title: String,
val entitiesFlow: Flow<List<Entity<*>>>
private val entitiesFlow: Flow<List<Entity<*>>>,
private val allEntities: Flow<Map<String, Entity<*>>>,
private val onChangeServer: (Int) -> Unit
) : Screen(carContext) {

companion object {
private const val TAG = "EntityGridVehicleScreen"
}

var loading = true
private var loading = true
var entities: List<Entity<*>> = listOf()
private val isFavorites = title == carContext.getString(R.string.favorites)

init {
lifecycleScope.launch {
Expand All @@ -58,7 +70,7 @@ class EntityGridVehicleScreen(
}
}

override fun onGetTemplate(): Template {
fun getEntityGridItems(entities: List<Entity<*>>): ItemList.Builder {
val manager = carContext.getCarService(ConstraintManager::class.java)
val gridLimit = manager.getContentLimit(ConstraintManager.CONTENT_LIMIT_TYPE_GRID)

Expand Down Expand Up @@ -98,14 +110,109 @@ class EntityGridVehicleScreen(
listBuilder.addItem(gridItem.build())
}

if (isFavorites) {
val navGridItem = GridItem.Builder().apply {
setTitle(carContext.getString(R.string.aa_navigation))
setImage(
CarIcon.Builder(
IconicsDrawable(
carContext,
CommunityMaterial.Icon3.cmd_map_outline
).apply {
sizeDp = 48
}.toAndroidIconCompat()
)
.setTint(CarColor.DEFAULT)
.build()
)
setOnClickListener {
Log.i(TAG, "Navigation clicked")
screenManager.push(
MapVehicleScreen(
carContext,
integrationRepository,
allEntities.map { it.values.filter { entity -> entity.domain in MainVehicleScreen.MAP_DOMAINS } }
)
)
}
}
listBuilder.addItem(navGridItem.build())
val categoryItem = GridItem.Builder().apply {
setTitle(carContext.getString(R.string.all_entities))
setImage(
CarIcon.Builder(
IconicsDrawable(
carContext,
CommunityMaterial.Icon3.cmd_view_list
).apply {
sizeDp = 48
}.toAndroidIconCompat()
)
.setTint(CarColor.DEFAULT)
.build()
)
setOnClickListener {
Log.i(TAG, "Categories clicked")
screenManager.push(
DomainListScreen(
carContext,
serverManager,
integrationRepository,
serverId,
allEntities,
prefsRepository
)
)
}
}
listBuilder.addItem(categoryItem.build())
if (serverManager.defaultServers.size > 1) {
val changeServerItem = GridItem.Builder().apply {
setTitle(carContext.getString(R.string.aa_change_server))
setImage(
CarIcon.Builder(
IconicsDrawable(
carContext,
CommunityMaterial.Icon2.cmd_home_switch
).apply {
sizeDp = 48
}.toAndroidIconCompat()
)
.setTint(CarColor.DEFAULT)
.build()
)
setOnClickListener {
Log.i(TAG, "Change server clicked")
screenManager.pushForResult(
ChangeServerScreen(
carContext,
serverManager,
serverId
)
) {
it?.toString()?.toIntOrNull()?.let { serverId ->
onChangeServer(serverId)
}
}
}
}
listBuilder.addItem(changeServerItem.build())
}
}
return listBuilder
}

override fun onGetTemplate(): Template {
val entityGrid = getEntityGridItems(entities)

return GridTemplate.Builder().apply {
setTitle(title)
setHeaderAction(Action.BACK)
if (loading) {
setLoading(true)
} else {
setLoading(false)
setSingleList(listBuilder.build())
setSingleList(entityGrid.build())
}
}.build()
}
Expand Down
Loading