Add navigation entities to favorites and convert nav screen to grid (#3725)

* Add navigation entities to favorites and convert nav screen to grid

* Consistent null checks

* More consistency
This commit is contained in:
Daniel Shokouhi 2023-07-28 10:49:28 -07:00 committed by GitHub
parent 54836c8db2
commit e3b44ac109
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 52 additions and 18 deletions

View file

@ -45,7 +45,14 @@ class ManageAndroidAutoViewModel @Inject constructor(
async {
entities[it.id] = try {
serverManager.integrationRepository(it.id).getEntities().orEmpty()
.filter { it.domain in MainVehicleScreen.SUPPORTED_DOMAINS }
.filter {
it.domain in MainVehicleScreen.SUPPORTED_DOMAINS ||
(
it.domain in MainVehicleScreen.MAP_DOMAINS &&
((it.attributes as? Map<*, *>)?.get("latitude") as? Double != null) &&
((it.attributes as? Map<*, *>)?.get("longitude") as? Double != null)
)
}
} catch (e: Exception) {
Log.e(TAG, "Couldn't load entities for server", e)
emptyList()

View file

@ -55,7 +55,14 @@ fun AndroidAutoFavoritesSettings(
androidAutoViewModel.sortedEntities
.filter {
!favoriteEntities.contains("$selectedServer-${it.entityId}") &&
(it.domain in MainVehicleScreen.SUPPORTED_DOMAINS)
(
it.domain in MainVehicleScreen.SUPPORTED_DOMAINS ||
(
it.domain in MainVehicleScreen.MAP_DOMAINS &&
((it.attributes as? Map<*, *>)?.get("latitude") as? Double != null) &&
((it.attributes as? Map<*, *>)?.get("longitude") as? Double != null)
)
)
}
.toList()
}

View file

@ -1,5 +1,7 @@
package io.homeassistant.companion.android.vehicle
import android.content.Intent
import android.net.Uri
import android.os.Build
import android.util.Log
import androidx.annotation.RequiresApi
@ -23,6 +25,7 @@ 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
@ -160,8 +163,23 @@ class EntityGridVehicleScreen(
gridItem
.setOnClickListener {
Log.i(TAG, "${entity.entityId} clicked")
lifecycleScope.launch {
entity.onPressed(integrationRepository)
if (entity.domain in MainVehicleScreen.MAP_DOMAINS) {
val attrs = entity.attributes as? Map<*, *>
if (attrs != null) {
val lat = attrs["latitude"] as? Double
val lon = attrs["longitude"] as? Double
if (lat != null && lon != null) {
val intent = Intent(
CarContext.ACTION_NAVIGATE,
Uri.parse("geo:$lat,$lon")
)
carContext.startCarApp(intent)
}
}
} else {
lifecycleScope.launch {
entity.onPressed(integrationRepository)
}
}
}
.setImage(

View file

@ -11,9 +11,9 @@ import androidx.car.app.constraints.ConstraintManager
import androidx.car.app.model.Action
import androidx.car.app.model.CarColor
import androidx.car.app.model.CarIcon
import androidx.car.app.model.GridItem
import androidx.car.app.model.GridTemplate
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.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
@ -27,6 +27,7 @@ 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 kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.launch
@ -36,14 +37,14 @@ import io.homeassistant.companion.android.common.R as commonR
class MapVehicleScreen(
carContext: CarContext,
val integrationRepository: IntegrationRepository,
val entitiesFlow: Flow<List<Entity<*>>>
private val entitiesFlow: Flow<List<Entity<*>>>
) : Screen(carContext) {
companion object {
private const val TAG = "MapVehicleScreen"
}
var loading = true
private var loading = true
var entities: Set<Entity<*>> = setOf()
init {
@ -75,8 +76,8 @@ class MapVehicleScreen(
override fun onGetTemplate(): Template {
val manager = carContext.getCarService(ConstraintManager::class.java)
val listLimit = manager.getContentLimit(ConstraintManager.CONTENT_LIMIT_TYPE_LIST)
val listBuilder = ItemList.Builder()
val gridLimit = manager.getContentLimit(ConstraintManager.CONTENT_LIMIT_TYPE_GRID)
val gridBuilder = ItemList.Builder()
entities
.map { // Null checks handled during collection
val attrs = it.attributes as Map<*, *>
@ -86,19 +87,20 @@ class MapVehicleScreen(
}
.sortedBy { it.first.friendlyName }
.forEachIndexed { index, pair ->
if (index >= listLimit) {
Log.i(TAG, "List limit ($listLimit) reached, not adding any more navigation entities (${entities.size})")
if (index >= gridLimit) {
Log.i(TAG, "Grid limit ($gridLimit) reached, not adding any more navigation entities (${entities.size})")
return@forEachIndexed
}
val icon = pair.first.getIcon(carContext) ?: CommunityMaterial.Icon.cmd_account
listBuilder.addItem(
Row.Builder()
gridBuilder.addItem(
GridItem.Builder()
.setTitle(pair.first.friendlyName)
.setText(pair.first.friendlyState(carContext))
.setImage(
CarIcon.Builder(
IconicsDrawable(carContext, icon)
.apply {
sizeDp = 48
sizeDp = 64
}.toAndroidIconCompat()
)
.setTint(CarColor.DEFAULT)
@ -116,15 +118,15 @@ class MapVehicleScreen(
)
}
return ListTemplate.Builder().apply {
return GridTemplate.Builder().apply {
setTitle(carContext.getString(R.string.aa_navigation))
setHeaderAction(Action.BACK)
if (loading) {
setLoading(true)
} else {
setLoading(false)
listBuilder.setNoItemsMessage(carContext.getString(commonR.string.aa_no_entities_with_locations))
setSingleList(listBuilder.build())
gridBuilder.setNoItemsMessage(carContext.getString(commonR.string.aa_no_entities_with_locations))
setSingleList(gridBuilder.build())
}
}.build()
}