Breaking change: support last_used/preferred pipeline IDs and use last_used by default (#3669)

Support last_used/preferred pipeline IDs and use last_used by default
This commit is contained in:
Joris Pelgröm 2023-07-19 00:42:36 +02:00 committed by GitHub
parent 00c2ef6d9e
commit ef916f606b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 53 additions and 7 deletions

View file

@ -22,6 +22,7 @@ import com.google.accompanist.themeadapter.material.MdcTheme
import dagger.hilt.android.AndroidEntryPoint import dagger.hilt.android.AndroidEntryPoint
import io.homeassistant.companion.android.BaseActivity import io.homeassistant.companion.android.BaseActivity
import io.homeassistant.companion.android.assist.ui.AssistSheetView 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.common.data.servers.ServerManager
import io.homeassistant.companion.android.webview.WebViewActivity import io.homeassistant.companion.android.webview.WebViewActivity
@ -79,9 +80,9 @@ class AssistActivity : BaseActivity() {
null null
}, },
pipelineId = if (intent.hasExtra(EXTRA_PIPELINE)) { pipelineId = if (intent.hasExtra(EXTRA_PIPELINE)) {
intent.getStringExtra(EXTRA_PIPELINE) intent.getStringExtra(EXTRA_PIPELINE) ?: AssistViewModelBase.PIPELINE_LAST_USED
} else { } else {
null AssistViewModelBase.PIPELINE_LAST_USED
}, },
startListening = if (intent.hasExtra(EXTRA_START_LISTENING)) { startListening = if (intent.hasExtra(EXTRA_START_LISTENING)) {
intent.getBooleanExtra(EXTRA_START_LISTENING, true) intent.getBooleanExtra(EXTRA_START_LISTENING, true)

View file

@ -83,7 +83,14 @@ class AssistViewModel @Inject constructor(
) )
) )
} else { } 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()) { if (serverManager.isRegistered()) {
@ -149,6 +156,7 @@ class AssistViewModel @Inject constructor(
id = it.id, id = it.id,
name = it.name name = it.name
) )
serverManager.integrationRepository(selectedServerId).setLastUsedPipeline(it.id)
_conversation.clear() _conversation.clear()
_conversation.add(startMessage) _conversation.add(startMessage)

View file

@ -26,6 +26,7 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.res.colorResource import androidx.compose.ui.res.colorResource
import androidx.compose.ui.res.stringResource import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp 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.common.data.websocket.impl.entities.AssistPipelineListResponse
import io.homeassistant.companion.android.database.server.Server import io.homeassistant.companion.android.database.server.Server
import io.homeassistant.companion.android.util.compose.ExposedDropdownMenu import io.homeassistant.companion.android.util.compose.ExposedDropdownMenu
@ -79,16 +80,25 @@ fun AssistShortcutView(
ExposedDropdownMenu( ExposedDropdownMenu(
label = stringResource(commonR.string.assist_pipeline), label = stringResource(commonR.string.assist_pipeline),
keys = listOf( keys = listOf(
stringResource(commonR.string.assist_last_used_pipeline),
stringResource( stringResource(
commonR.string.assist_preferred_pipeline, commonR.string.assist_preferred_pipeline,
pipelines.pipelines.first { it.id == pipelines.preferredPipeline }.name pipelines.pipelines.first { it.id == pipelines.preferredPipeline }.name
) )
) + ) +
pipelines.pipelines.map { it.name }, pipelines.pipelines.map { it.name },
currentIndex = pipelineId?.let { pid -> 1 + pipelines.pipelines.indexOfFirst { it.id == pid } } currentIndex = when {
?: 0, pipelineId == AssistViewModelBase.PIPELINE_LAST_USED -> 0
pipelineId == AssistViewModelBase.PIPELINE_PREFERRED -> 1
pipelineId != null -> 2 + pipelines.pipelines.indexOfFirst { it.id == pipelineId }
else -> 0
},
onSelected = { 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)) Spacer(modifier = Modifier.height(16.dp))

View file

@ -26,6 +26,11 @@ abstract class AssistViewModelBase(
application: Application application: Application
) : AndroidViewModel(application) { ) : AndroidViewModel(application) {
companion object {
const val PIPELINE_PREFERRED = "preferred"
const val PIPELINE_LAST_USED = "last_used"
}
enum class AssistInputMode { enum class AssistInputMode {
TEXT, TEXT,
TEXT_ONLY, TEXT_ONLY,

View file

@ -62,6 +62,10 @@ interface IntegrationRepository {
pipelineId: String? = null, pipelineId: String? = null,
conversationId: String? = null conversationId: String? = null
): Flow<AssistPipelineEvent>? ): Flow<AssistPipelineEvent>?
suspend fun getLastUsedPipeline(): String?
suspend fun setLastUsedPipeline(pipelineId: String)
} }
@AssistedFactory @AssistedFactory

View file

@ -65,6 +65,7 @@ class IntegrationRepositoryImpl @AssistedInject constructor(
private const val PREF_SESSION_EXPIRE = "session_expire" private const val PREF_SESSION_EXPIRE = "session_expire"
private const val PREF_TRUSTED = "trusted" private const val PREF_TRUSTED = "trusted"
private const val PREF_SEC_WARNING_NEXT = "sec_warning_last" 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 TAG = "IntegrationRepository"
private const val RATE_LIMIT_URL = BuildConfig.RATE_LIMIT_URL 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_SESSION_EXPIRE")
localStorage.remove("${serverId}_$PREF_TRUSTED") localStorage.remove("${serverId}_$PREF_TRUSTED")
localStorage.remove("${serverId}_$PREF_SEC_WARNING_NEXT") localStorage.remove("${serverId}_$PREF_SEC_WARNING_NEXT")
localStorage.remove("${serverId}_$PREF_LAST_USED_PIPELINE")
// app version and push token is device-specific // 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<Entity<Any>>? { override suspend fun getEntities(): List<Entity<Any>>? {
val response = webSocketRepository.getStates() val response = webSocketRepository.getStates()

View file

@ -1100,6 +1100,7 @@
<string name="assist_enter_a_request">Enter a request</string> <string name="assist_enter_a_request">Enter a request</string>
<string name="assist_error">Oops, an error has occurred</string> <string name="assist_error">Oops, an error has occurred</string>
<string name="assist_how_can_i_assist">How can I assist?</string> <string name="assist_how_can_i_assist">How can I assist?</string>
<string name="assist_last_used_pipeline">Last used assistant</string>
<string name="assist_log_in">Log in to Home Assistant to start using Assist</string> <string name="assist_log_in">Log in to Home Assistant to start using Assist</string>
<string name="assist_manage_pipelines">Manage assistants</string> <string name="assist_manage_pipelines">Manage assistants</string>
<string name="assist_permission">To use Assist with your voice, allow Home Assistant to access the microphone</string> <string name="assist_permission">To use Assist with your voice, allow Home Assistant to access the microphone</string>

View file

@ -87,7 +87,13 @@ class ConversationViewModel @Inject constructor(
} }
} }
return setPipeline(null) return setPipeline(
if (useAssistPipeline) {
serverManager.integrationRepository().getLastUsedPipeline()
} else {
null
}
)
} }
return false return false
@ -142,6 +148,9 @@ class ConversationViewModel @Inject constructor(
useAssistPipelineStt = false useAssistPipelineStt = false
if (pipeline != null || !useAssistPipeline) { if (pipeline != null || !useAssistPipeline) {
currentPipeline = pipeline currentPipeline = pipeline
currentPipeline?.let {
serverManager.integrationRepository().setLastUsedPipeline(it.id)
}
_conversation.clear() _conversation.clear()
_conversation.add(startMessage) _conversation.add(startMessage)