Fixing Ktlin LInt.

This commit is contained in:
Diego Valenzuela 2023-04-29 11:48:54 -07:00
parent 6b33cd727a
commit 975eaf1ab2
2 changed files with 62 additions and 63 deletions

View file

@ -31,74 +31,73 @@ import kotlinx.coroutines.launch
@RequiresApi(Build.VERSION_CODES.O) @RequiresApi(Build.VERSION_CODES.O)
class EntityGridVehicleScreen( class EntityGridVehicleScreen(
carContext: CarContext, carContext: CarContext,
val integrationRepository: IntegrationRepository, val integrationRepository: IntegrationRepository,
val title: String, val title: String,
val entitiesFlow: Flow<List<Entity<*>>>, val entitiesFlow: Flow<List<Entity<*>>>
) : Screen(carContext) { ) : Screen(carContext) {
companion object { companion object {
private const val TAG = "EntityGridVehicleScreen" private const val TAG = "EntityGridVehicleScreen"
}
var loading = true
var entities: List<Entity<*>> = listOf()
init {
lifecycleScope.launch {
lifecycle.repeatOnLifecycle(Lifecycle.State.STARTED) {
entitiesFlow.collect {
loading = false
entities = it
invalidate()
}
}
} }
}
override fun onGetTemplate(): Template { var loading = true
val listBuilder = ItemList.Builder() var entities: List<Entity<*>> = listOf()
entities.forEach { entity ->
val icon = entity.getIcon(carContext) ?: CommunityMaterial.Icon.cmd_cloud_question
val gridItem =
GridItem.Builder()
.setLoading(false)
.setTitle(entity.friendlyName)
.setText(entity.friendlyState(carContext))
init {
if (entity.isExecuting()) { lifecycleScope.launch {
gridItem.setLoading(entity.isExecuting()) lifecycle.repeatOnLifecycle(Lifecycle.State.STARTED) {
} else { entitiesFlow.collect {
gridItem loading = false
.setOnClickListener { entities = it
Log.i(TAG, "${entity.entityId} clicked") invalidate()
lifecycleScope.launch { }
entity.onPressed(integrationRepository)
} }
} }
.setImage(
CarIcon.Builder(
IconicsDrawable(carContext, icon).apply {
sizeDp = 64
}.toAndroidIconCompat()
)
.setTint(CarColor.DEFAULT)
.build()
)
}
listBuilder.addItem(gridItem.build())
} }
return GridTemplate.Builder().apply { override fun onGetTemplate(): Template {
setTitle(title) val listBuilder = ItemList.Builder()
setHeaderAction(Action.BACK) entities.forEach { entity ->
if (loading) { val icon = entity.getIcon(carContext) ?: CommunityMaterial.Icon.cmd_cloud_question
setLoading(true) val gridItem =
} else { GridItem.Builder()
setLoading(false) .setLoading(false)
setSingleList(listBuilder.build()) .setTitle(entity.friendlyName)
} .setText(entity.friendlyState(carContext))
}.build()
} if (entity.isExecuting()) {
gridItem.setLoading(entity.isExecuting())
} else {
gridItem
.setOnClickListener {
Log.i(TAG, "${entity.entityId} clicked")
lifecycleScope.launch {
entity.onPressed(integrationRepository)
}
}
.setImage(
CarIcon.Builder(
IconicsDrawable(carContext, icon).apply {
sizeDp = 64
}.toAndroidIconCompat()
)
.setTint(CarColor.DEFAULT)
.build()
)
}
listBuilder.addItem(gridItem.build())
}
return GridTemplate.Builder().apply {
setTitle(title)
setHeaderAction(Action.BACK)
if (loading) {
setLoading(true)
} else {
setLoading(false)
setSingleList(listBuilder.build())
}
}.build()
}
} }

View file

@ -643,4 +643,4 @@ fun <T> Entity<T>.friendlyState(context: Context): String {
return friendlyState return friendlyState
} }
fun <T> Entity<T>.isExecuting()= state.endsWith("ing") fun <T> Entity<T>.isExecuting() = state.endsWith("ing")