-
-
Notifications
You must be signed in to change notification settings - Fork 941
Feature/add favorite automotive #6718
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
base: main
Are you sure you want to change the base?
Changes from 5 commits
2657805
65f3651
b9f75c1
ceebc75
03b9cc0
1e42075
251f8a2
74cc6f1
e2cd297
82f2143
ce72bb6
2bb7210
f39eaf4
df516d2
d43b4aa
a473bc0
13129c8
b65ce01
ee5a158
622c276
6da3ba5
62465f1
4af0bf0
2df7f86
51bd2d3
b413653
0ed7c46
3b44395
0a74259
2782057
9c2ca73
d9e331b
80bcfc0
10f6bf9
b622996
e91c50c
1fed001
dea98ca
02dffc4
17cf7ad
9bab495
e395938
00c7879
86b234e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,170 @@ | ||||||||||||||
| package io.homeassistant.companion.android.vehicle | ||||||||||||||
|
|
||||||||||||||
| import android.os.Build | ||||||||||||||
|
github-advanced-security[bot] marked this conversation as resolved.
Fixed
github-advanced-security[bot] marked this conversation as resolved.
Fixed
|
||||||||||||||
| import androidx.annotation.RequiresApi | ||||||||||||||
|
github-advanced-security[bot] marked this conversation as resolved.
Fixed
github-advanced-security[bot] marked this conversation as resolved.
Fixed
|
||||||||||||||
| import androidx.car.app.CarContext | ||||||||||||||
| import androidx.car.app.constraints.ConstraintManager | ||||||||||||||
| 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.car.app.model.Toggle | ||||||||||||||
| import androidx.lifecycle.Lifecycle | ||||||||||||||
| import androidx.lifecycle.lifecycleScope | ||||||||||||||
| import androidx.lifecycle.repeatOnLifecycle | ||||||||||||||
| import io.homeassistant.companion.android.common.R as commonR | ||||||||||||||
| import io.homeassistant.companion.android.common.data.integration.Entity | ||||||||||||||
| import io.homeassistant.companion.android.common.data.prefs.AutoFavorite | ||||||||||||||
| import io.homeassistant.companion.android.common.data.prefs.PrefsRepository | ||||||||||||||
| import io.homeassistant.companion.android.util.vehicle.SUPPORTED_DOMAINS_WITH_STRING | ||||||||||||||
| import io.homeassistant.companion.android.util.vehicle.getHeaderBuilder | ||||||||||||||
| import kotlinx.coroutines.flow.Flow | ||||||||||||||
| import kotlinx.coroutines.flow.StateFlow | ||||||||||||||
| import kotlinx.coroutines.launch | ||||||||||||||
| import timber.log.Timber | ||||||||||||||
|
github-advanced-security[bot] marked this conversation as resolved.
Fixed
github-advanced-security[bot] marked this conversation as resolved.
Fixed
|
||||||||||||||
|
|
||||||||||||||
| /** | ||||||||||||||
| * A Car App screen that allows users to manage their automotive favorites when the vehicle is | ||||||||||||||
| * parked. Each entity from the supported domains is displayed with a toggle to add or remove | ||||||||||||||
| * it from the favorites list. Current favorites are sorted to the top. | ||||||||||||||
| * | ||||||||||||||
| * This screen stays fully within the Car App API, making it compliant with Play Store | ||||||||||||||
| * automotive distribution policies. | ||||||||||||||
| */ | ||||||||||||||
| @RequiresApi(Build.VERSION_CODES.O) | ||||||||||||||
|
github-advanced-security[bot] marked this conversation as resolved.
Fixed
|
||||||||||||||
| class ManageFavoritesVehicleScreen( | ||||||||||||||
| carContext: CarContext, | ||||||||||||||
| private val serverId: StateFlow<Int>, | ||||||||||||||
| private val allEntities: Flow<Map<String, Entity>>, | ||||||||||||||
| private val prefsRepository: PrefsRepository, | ||||||||||||||
| ) : BaseVehicleScreen(carContext) { | ||||||||||||||
|
|
||||||||||||||
| private var entities: List<Entity> = emptyList() | ||||||||||||||
| private var favoritesList: List<AutoFavorite> = emptyList() | ||||||||||||||
| private var isLoaded = false | ||||||||||||||
| private var page = 0 | ||||||||||||||
|
|
||||||||||||||
| init { | ||||||||||||||
| lifecycleScope.launch { | ||||||||||||||
| lifecycle.repeatOnLifecycle(Lifecycle.State.STARTED) { | ||||||||||||||
| favoritesList = prefsRepository.getAutoFavorites() | ||||||||||||||
| allEntities.collect { entityMap -> | ||||||||||||||
| val newEntities = entityMap.values | ||||||||||||||
| .filter { it.domain in SUPPORTED_DOMAINS_WITH_STRING } | ||||||||||||||
| .sortedWith( | ||||||||||||||
| compareByDescending<Entity> { entity -> | ||||||||||||||
| favoritesList.any { | ||||||||||||||
| it.serverId == serverId.value && it.entityId == entity.entityId | ||||||||||||||
| } | ||||||||||||||
|
cddu33 marked this conversation as resolved.
Outdated
|
||||||||||||||
| }.thenBy { it.attributes["friendly_name"]?.toString() ?: it.entityId }, | ||||||||||||||
| ) | ||||||||||||||
| if (newEntities.map { it.entityId } != entities.map { it.entityId }) { | ||||||||||||||
| page = 0 | ||||||||||||||
| } | ||||||||||||||
| entities = newEntities | ||||||||||||||
| isLoaded = true | ||||||||||||||
| invalidate() | ||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| override fun onDrivingOptimizedChanged(newState: Boolean) { | ||||||||||||||
| if (newState) { | ||||||||||||||
| lifecycleScope.launch { | ||||||||||||||
| Timber.i("Fermeture de l'écran car la voiture roule") | ||||||||||||||
| screenManager.pop() | ||||||||||||||
|
cddu33 marked this conversation as resolved.
|
||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
| invalidate() | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| override fun onGetTemplate(): Template { | ||||||||||||||
| val listLimit = carContext.getCarService(ConstraintManager::class.java) | ||||||||||||||
| .getContentLimit(ConstraintManager.CONTENT_LIMIT_TYPE_LIST) | ||||||||||||||
|
|
||||||||||||||
| val itemsPerPage = (listLimit - 2).coerceAtLeast(1) | ||||||||||||||
|
|
||||||||||||||
| val fromIndex = page * itemsPerPage | ||||||||||||||
| val toIndex = minOf(fromIndex + itemsPerPage, entities.size) | ||||||||||||||
| val hasPreviousPage = page > 0 | ||||||||||||||
| val hasNextPage = toIndex < entities.size | ||||||||||||||
|
cddu33 marked this conversation as resolved.
Outdated
|
||||||||||||||
| val pageEntities = if (isLoaded) entities.subList(fromIndex, toIndex) else emptyList() | ||||||||||||||
|
||||||||||||||
| val pageEntities = if (isLoaded) entities.subList(fromIndex, toIndex) else emptyList() | |
| val pageEntities = if (isLoaded && fromIndex < entities.size) { | |
| entities.subList(fromIndex, toIndex) | |
| } else { | |
| emptyList() | |
| } |
Uh oh!
There was an error while loading. Please reload this page.