Skip to content
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

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

Merged
merged 1 commit into from
Jul 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ interface IntegrationRepository {
pipelineId: String? = null,
conversationId: String? = null
): Flow<AssistPipelineEvent>?

suspend fun getLastUsedPipeline(): String?

suspend fun setLastUsedPipeline(pipelineId: String)
}

@AssistedFactory
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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>>? {
val response = webSocketRepository.getStates()

Expand Down
1 change: 1 addition & 0 deletions common/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1100,6 +1100,7 @@
<string name="assist_enter_a_request">Enter a request</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_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_manage_pipelines">Manage assistants</string>
<string name="assist_permission">To use Assist with your voice, allow Home Assistant to access the microphone</string>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,13 @@ class ConversationViewModel @Inject constructor(
}
}

return setPipeline(null)
return setPipeline(
if (useAssistPipeline) {
serverManager.integrationRepository().getLastUsedPipeline()
} else {
null
}
)
}

return false
Expand Down Expand Up @@ -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)
Expand Down