-
Notifications
You must be signed in to change notification settings - Fork 641
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add favorites selection for Android Auto
- Loading branch information
Showing
13 changed files
with
450 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
.../io/homeassistant/companion/android/settings/vehicle/ManageAndroidAutoSettingsFragment.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package io.homeassistant.companion.android.settings.vehicle | ||
|
||
import android.content.Intent | ||
import android.net.Uri | ||
import android.os.Build | ||
import android.os.Bundle | ||
import android.view.LayoutInflater | ||
import android.view.Menu | ||
import android.view.View | ||
import android.view.ViewGroup | ||
import androidx.annotation.RequiresApi | ||
import androidx.compose.ui.platform.ComposeView | ||
import androidx.fragment.app.Fragment | ||
import androidx.fragment.app.viewModels | ||
import com.google.accompanist.themeadapter.material.MdcTheme | ||
import dagger.hilt.android.AndroidEntryPoint | ||
import io.homeassistant.companion.android.R | ||
import io.homeassistant.companion.android.common.data.servers.ServerManager | ||
import io.homeassistant.companion.android.settings.vehicle.views.AndroidAutoFavoritesSettings | ||
import javax.inject.Inject | ||
import io.homeassistant.companion.android.common.R as commonR | ||
|
||
@AndroidEntryPoint | ||
class ManageAndroidAutoSettingsFragment : Fragment() { | ||
|
||
@Inject | ||
lateinit var serverManager: ServerManager | ||
|
||
val viewModel: ManageAndroidAutoViewModel by viewModels() | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
setHasOptionsMenu(true) | ||
} | ||
|
||
@Deprecated("Deprecated in Java") | ||
override fun onPrepareOptionsMenu(menu: Menu) { | ||
super.onPrepareOptionsMenu(menu) | ||
|
||
menu.findItem(R.id.get_help)?.let { | ||
it.isVisible = true | ||
it.intent = Intent(Intent.ACTION_VIEW, Uri.parse("https://companion.home-assistant.io/docs/android-auto")) | ||
} | ||
} | ||
|
||
@RequiresApi(Build.VERSION_CODES.O) | ||
override fun onCreateView( | ||
inflater: LayoutInflater, | ||
container: ViewGroup?, | ||
savedInstanceState: Bundle? | ||
): View { | ||
return ComposeView(requireContext()).apply { | ||
setContent { | ||
MdcTheme { | ||
AndroidAutoFavoritesSettings( | ||
androidAutoViewModel = viewModel, | ||
serversList = serverManager.defaultServers, | ||
defaultServer = serverManager.getServer()?.id ?: 0 | ||
) | ||
} | ||
} | ||
} | ||
} | ||
|
||
override fun onResume() { | ||
super.onResume() | ||
activity?.title = getString(commonR.string.aa_favorites) | ||
} | ||
} |
91 changes: 91 additions & 0 deletions
91
...in/java/io/homeassistant/companion/android/settings/vehicle/ManageAndroidAutoViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package io.homeassistant.companion.android.settings.vehicle | ||
|
||
import android.app.Application | ||
import android.os.Build | ||
import android.util.Log | ||
import androidx.annotation.RequiresApi | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.mutableStateListOf | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.setValue | ||
import androidx.lifecycle.AndroidViewModel | ||
import androidx.lifecycle.viewModelScope | ||
import dagger.hilt.android.lifecycle.HiltViewModel | ||
import io.homeassistant.companion.android.common.data.integration.Entity | ||
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 io.homeassistant.companion.android.vehicle.MainVehicleScreen | ||
import kotlinx.coroutines.async | ||
import kotlinx.coroutines.awaitAll | ||
import kotlinx.coroutines.launch | ||
import org.burnoutcrew.reorderable.ItemPosition | ||
import javax.inject.Inject | ||
|
||
@RequiresApi(Build.VERSION_CODES.O) | ||
@HiltViewModel | ||
class ManageAndroidAutoViewModel @Inject constructor( | ||
private val serverManager: ServerManager, | ||
private val prefsRepository: PrefsRepository, | ||
application: Application | ||
) : AndroidViewModel(application) { | ||
|
||
companion object { | ||
private const val TAG = "AAViewModel" | ||
} | ||
val favoritesList = mutableStateListOf<String>() | ||
|
||
var entitiesLoaded by mutableStateOf(false) | ||
private set | ||
var sortedEntities by mutableStateOf<List<Entity<*>>>(emptyList()) | ||
private set | ||
val entities = mutableMapOf<Int, List<Entity<*>>>() | ||
init { | ||
viewModelScope.launch { | ||
favoritesList.addAll(prefsRepository.getAutoFavorites()) | ||
entitiesLoaded = true | ||
serverManager.defaultServers.map { | ||
async { | ||
entities[it.id] = try { | ||
serverManager.integrationRepository(it.id).getEntities().orEmpty() | ||
.filter { it.domain in MainVehicleScreen.SUPPORTED_DOMAINS } | ||
} catch (e: Exception) { | ||
Log.e(TAG, "Couldn't load entities for server", e) | ||
emptyList() | ||
} | ||
} | ||
}.awaitAll() | ||
loadEntities(serverManager.getServer()?.id ?: 0) | ||
} | ||
} | ||
|
||
fun onMove(fromItem: ItemPosition, toItem: ItemPosition) { | ||
favoritesList.apply { | ||
add( | ||
favoritesList.indexOfFirst { it == toItem.key }, | ||
removeAt(favoritesList.indexOfFirst { it == fromItem.key }) | ||
) | ||
} | ||
} | ||
|
||
fun canDragOver(position: ItemPosition) = favoritesList.any { it == position.key } | ||
|
||
fun saveFavorites() { | ||
viewModelScope.launch { | ||
prefsRepository.setAutoFavorites(favoritesList) | ||
} | ||
} | ||
|
||
fun loadEntities(serverId: Int) { | ||
sortedEntities = entities[serverId] ?: emptyList() | ||
} | ||
|
||
fun onEntitySelected(checked: Boolean, entityId: String, serverId: Int) { | ||
if (checked) { | ||
favoritesList.add("$serverId-$entityId") | ||
} else { | ||
favoritesList.remove("$serverId-$entityId") | ||
} | ||
viewModelScope.launch { prefsRepository.setAutoFavorites(favoritesList) } | ||
} | ||
} |
Oops, something went wrong.