From 7848330e2cc8037b23c37216fd0063f477924334 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joris=20Pelgr=C3=B6m?= Date: Mon, 17 Jul 2023 21:02:52 +0200 Subject: [PATCH] Support last_used/preferred pipeline IDs and use last_used by default --- .../companion/android/assist/AssistActivity.kt | 5 +++-- .../companion/android/assist/AssistViewModel.kt | 10 +++++++++- .../android/widgets/assist/AssistShortcutView.kt | 16 +++++++++++++--- .../android/common/assist/AssistViewModelBase.kt | 5 +++++ .../data/integration/IntegrationRepository.kt | 4 ++++ .../impl/IntegrationRepositoryImpl.kt | 8 ++++++++ common/src/main/res/values/strings.xml | 1 + .../conversation/ConversationViewModel.kt | 11 ++++++++++- 8 files changed, 53 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/io/homeassistant/companion/android/assist/AssistActivity.kt b/app/src/main/java/io/homeassistant/companion/android/assist/AssistActivity.kt index 54037902656..8a652adf083 100644 --- a/app/src/main/java/io/homeassistant/companion/android/assist/AssistActivity.kt +++ b/app/src/main/java/io/homeassistant/companion/android/assist/AssistActivity.kt @@ -22,6 +22,7 @@ import com.google.accompanist.themeadapter.material.MdcTheme import dagger.hilt.android.AndroidEntryPoint import io.homeassistant.companion.android.BaseActivity import io.homeassistant.companion.android.assist.ui.AssistSheetView +import io.homeassistant.companion.android.common.assist.AssistViewModelBase import io.homeassistant.companion.android.common.data.servers.ServerManager import io.homeassistant.companion.android.webview.WebViewActivity @@ -79,9 +80,9 @@ class AssistActivity : BaseActivity() { null }, pipelineId = if (intent.hasExtra(EXTRA_PIPELINE)) { - intent.getStringExtra(EXTRA_PIPELINE) + intent.getStringExtra(EXTRA_PIPELINE) ?: AssistViewModelBase.PIPELINE_LAST_USED } else { - null + AssistViewModelBase.PIPELINE_LAST_USED }, startListening = if (intent.hasExtra(EXTRA_START_LISTENING)) { intent.getBooleanExtra(EXTRA_START_LISTENING, true) diff --git a/app/src/main/java/io/homeassistant/companion/android/assist/AssistViewModel.kt b/app/src/main/java/io/homeassistant/companion/android/assist/AssistViewModel.kt index 2a3a02e3b3c..8cdcf0d8daa 100644 --- a/app/src/main/java/io/homeassistant/companion/android/assist/AssistViewModel.kt +++ b/app/src/main/java/io/homeassistant/companion/android/assist/AssistViewModel.kt @@ -83,7 +83,14 @@ class AssistViewModel @Inject constructor( ) ) } else { - setPipeline(pipelineId?.ifBlank { null }) + setPipeline( + when { + pipelineId == PIPELINE_LAST_USED -> serverManager.integrationRepository(selectedServerId).getLastUsedPipeline() + pipelineId == PIPELINE_PREFERRED -> null + pipelineId?.isNotBlank() == true -> pipelineId + else -> null + } + ) } if (serverManager.isRegistered()) { @@ -149,6 +156,7 @@ class AssistViewModel @Inject constructor( id = it.id, name = it.name ) + serverManager.integrationRepository(selectedServerId).setLastUsedPipeline(it.id) _conversation.clear() _conversation.add(startMessage) diff --git a/app/src/main/java/io/homeassistant/companion/android/widgets/assist/AssistShortcutView.kt b/app/src/main/java/io/homeassistant/companion/android/widgets/assist/AssistShortcutView.kt index 64bd9870115..2062a78a6ad 100644 --- a/app/src/main/java/io/homeassistant/companion/android/widgets/assist/AssistShortcutView.kt +++ b/app/src/main/java/io/homeassistant/companion/android/widgets/assist/AssistShortcutView.kt @@ -26,6 +26,7 @@ import androidx.compose.ui.Modifier import androidx.compose.ui.res.colorResource import androidx.compose.ui.res.stringResource import androidx.compose.ui.unit.dp +import io.homeassistant.companion.android.common.assist.AssistViewModelBase import io.homeassistant.companion.android.common.data.websocket.impl.entities.AssistPipelineListResponse import io.homeassistant.companion.android.database.server.Server import io.homeassistant.companion.android.util.compose.ExposedDropdownMenu @@ -79,16 +80,25 @@ fun AssistShortcutView( ExposedDropdownMenu( label = stringResource(commonR.string.assist_pipeline), keys = listOf( + stringResource(commonR.string.assist_last_used_pipeline), stringResource( commonR.string.assist_preferred_pipeline, pipelines.pipelines.first { it.id == pipelines.preferredPipeline }.name ) ) + pipelines.pipelines.map { it.name }, - currentIndex = pipelineId?.let { pid -> 1 + pipelines.pipelines.indexOfFirst { it.id == pid } } - ?: 0, + currentIndex = when { + pipelineId == AssistViewModelBase.PIPELINE_LAST_USED -> 0 + pipelineId == AssistViewModelBase.PIPELINE_PREFERRED -> 1 + pipelineId != null -> 2 + pipelines.pipelines.indexOfFirst { it.id == pipelineId } + else -> 0 + }, onSelected = { - pipelineId = if (it == 0) null else pipelines.pipelines[it - 1].id + pipelineId = when (it) { + 0 -> AssistViewModelBase.PIPELINE_LAST_USED + 1 -> AssistViewModelBase.PIPELINE_PREFERRED + else -> pipelines.pipelines[it - 2].id + } } ) Spacer(modifier = Modifier.height(16.dp)) diff --git a/common/src/main/java/io/homeassistant/companion/android/common/assist/AssistViewModelBase.kt b/common/src/main/java/io/homeassistant/companion/android/common/assist/AssistViewModelBase.kt index 1efd9dfb587..a49606056f1 100644 --- a/common/src/main/java/io/homeassistant/companion/android/common/assist/AssistViewModelBase.kt +++ b/common/src/main/java/io/homeassistant/companion/android/common/assist/AssistViewModelBase.kt @@ -26,6 +26,11 @@ abstract class AssistViewModelBase( application: Application ) : AndroidViewModel(application) { + companion object { + const val PIPELINE_PREFERRED = "preferred" + const val PIPELINE_LAST_USED = "last_used" + } + enum class AssistInputMode { TEXT, TEXT_ONLY, diff --git a/common/src/main/java/io/homeassistant/companion/android/common/data/integration/IntegrationRepository.kt b/common/src/main/java/io/homeassistant/companion/android/common/data/integration/IntegrationRepository.kt index b3dc4acac16..87c46c59ec9 100644 --- a/common/src/main/java/io/homeassistant/companion/android/common/data/integration/IntegrationRepository.kt +++ b/common/src/main/java/io/homeassistant/companion/android/common/data/integration/IntegrationRepository.kt @@ -62,6 +62,10 @@ interface IntegrationRepository { pipelineId: String? = null, conversationId: String? = null ): Flow? + + suspend fun getLastUsedPipeline(): String? + + suspend fun setLastUsedPipeline(pipelineId: String) } @AssistedFactory diff --git a/common/src/main/java/io/homeassistant/companion/android/common/data/integration/impl/IntegrationRepositoryImpl.kt b/common/src/main/java/io/homeassistant/companion/android/common/data/integration/impl/IntegrationRepositoryImpl.kt index 472a2fd4e4f..78690e32b5e 100644 --- a/common/src/main/java/io/homeassistant/companion/android/common/data/integration/impl/IntegrationRepositoryImpl.kt +++ b/common/src/main/java/io/homeassistant/companion/android/common/data/integration/impl/IntegrationRepositoryImpl.kt @@ -65,6 +65,7 @@ class IntegrationRepositoryImpl @AssistedInject constructor( private const val PREF_SESSION_EXPIRE = "session_expire" private const val PREF_TRUSTED = "trusted" private const val PREF_SEC_WARNING_NEXT = "sec_warning_last" + private const val PREF_LAST_USED_PIPELINE = "last_used_pipeline" private const val TAG = "IntegrationRepository" private const val RATE_LIMIT_URL = BuildConfig.RATE_LIMIT_URL @@ -170,6 +171,7 @@ class IntegrationRepositoryImpl @AssistedInject constructor( localStorage.remove("${serverId}_$PREF_SESSION_EXPIRE") localStorage.remove("${serverId}_$PREF_TRUSTED") localStorage.remove("${serverId}_$PREF_SEC_WARNING_NEXT") + localStorage.remove("${serverId}_$PREF_LAST_USED_PIPELINE") // app version and push token is device-specific } @@ -555,6 +557,12 @@ class IntegrationRepositoryImpl @AssistedInject constructor( } } + override suspend fun getLastUsedPipeline(): String? = + localStorage.getString("${serverId}_$PREF_LAST_USED_PIPELINE") + + override suspend fun setLastUsedPipeline(pipelineId: String) = + localStorage.putString("${serverId}_$PREF_LAST_USED_PIPELINE", pipelineId) + override suspend fun getEntities(): List>? { val response = webSocketRepository.getStates() diff --git a/common/src/main/res/values/strings.xml b/common/src/main/res/values/strings.xml index 0ee887eb791..1d5d40b6ceb 100644 --- a/common/src/main/res/values/strings.xml +++ b/common/src/main/res/values/strings.xml @@ -1100,6 +1100,7 @@ Enter a request Oops, an error has occurred How can I assist? + Last used assistant Log in to Home Assistant to start using Assist Manage assistants To use Assist with your voice, allow Home Assistant to access the microphone diff --git a/wear/src/main/java/io/homeassistant/companion/android/conversation/ConversationViewModel.kt b/wear/src/main/java/io/homeassistant/companion/android/conversation/ConversationViewModel.kt index b0da780d8b9..bc7edb4dda3 100755 --- a/wear/src/main/java/io/homeassistant/companion/android/conversation/ConversationViewModel.kt +++ b/wear/src/main/java/io/homeassistant/companion/android/conversation/ConversationViewModel.kt @@ -87,7 +87,13 @@ class ConversationViewModel @Inject constructor( } } - return setPipeline(null) + return setPipeline( + if (useAssistPipeline) { + serverManager.integrationRepository().getLastUsedPipeline() + } else { + null + } + ) } return false @@ -142,6 +148,9 @@ class ConversationViewModel @Inject constructor( useAssistPipelineStt = false if (pipeline != null || !useAssistPipeline) { currentPipeline = pipeline + currentPipeline?.let { + serverManager.integrationRepository().setLastUsedPipeline(it.id) + } _conversation.clear() _conversation.add(startMessage)