Skip to content

Commit

Permalink
Add 'Manage assistants' button to native Assist (#3616)
Browse files Browse the repository at this point in the history
  • Loading branch information
jpelgrom committed Jun 30, 2023
1 parent 48a7755 commit 2023268
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ 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.data.servers.ServerManager
import io.homeassistant.companion.android.webview.WebViewActivity

@AndroidEntryPoint
class AssistActivity : BaseActivity() {
Expand Down Expand Up @@ -90,6 +91,8 @@ class AssistActivity : BaseActivity() {
)
}

val fromFrontend = intent.getBooleanExtra(EXTRA_FROM_FRONTEND, false)

WindowCompat.setDecorFitsSystemWindows(window, false)
setContent {
MdcTheme {
Expand All @@ -103,9 +106,25 @@ class AssistActivity : BaseActivity() {
conversation = viewModel.conversation,
pipelines = viewModel.pipelines,
inputMode = viewModel.inputMode,
fromFrontend = intent.getBooleanExtra(EXTRA_FROM_FRONTEND, false),
fromFrontend = fromFrontend,
currentPipeline = viewModel.currentPipeline,
onSelectPipeline = viewModel::changePipeline,
onManagePipelines =
if (fromFrontend && viewModel.userCanManagePipelines()) {
{
startActivity(
WebViewActivity.newInstance(
this,
"config/voice-assistants/assistants"
).apply {
flags += Intent.FLAG_ACTIVITY_NEW_TASK // Delivers data in onNewIntent
}
)
finish()
}
} else {
null
},
onChangeInput = viewModel::onChangeInput,
onTextInput = viewModel::onTextInput,
onMicrophoneInput = viewModel::onMicrophoneInput,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ class AssistViewModel @Inject constructor(
}
}

fun userCanManagePipelines(): Boolean = serverManager.getServer()?.user?.isAdmin == true

fun changePipeline(serverId: Int, id: String) = viewModelScope.launch {
if (serverId == selectedServerId && id == selectedPipeline?.id) return@launch

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.text.KeyboardActions
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material.ButtonDefaults
import androidx.compose.material.Divider
import androidx.compose.material.DropdownMenu
import androidx.compose.material.DropdownMenuItem
import androidx.compose.material.ExperimentalMaterialApi
Expand Down Expand Up @@ -91,6 +92,7 @@ fun AssistSheetView(
currentPipeline: AssistUiPipeline?,
fromFrontend: Boolean,
onSelectPipeline: (Int, String) -> Unit,
onManagePipelines: (() -> Unit)?,
onChangeInput: () -> Unit,
onTextInput: (String) -> Unit,
onMicrophoneInput: () -> Unit,
Expand Down Expand Up @@ -133,7 +135,8 @@ fun AssistSheetView(
pipelines = pipelines,
currentPipeline = currentPipeline,
fromFrontend = fromFrontend,
onSelectPipeline = onSelectPipeline
onSelectPipeline = onSelectPipeline,
onManagePipelines = onManagePipelines
)
LazyColumn(
state = lazyListState,
Expand Down Expand Up @@ -169,7 +172,8 @@ fun AssistSheetHeader(
pipelines: List<AssistUiPipeline>,
currentPipeline: AssistUiPipeline?,
fromFrontend: Boolean,
onSelectPipeline: (Int, String) -> Unit
onSelectPipeline: (Int, String) -> Unit,
onManagePipelines: (() -> Unit)?
) = Column(verticalArrangement = Arrangement.Center) {
Text(
text = stringResource(if (fromFrontend) commonR.string.assist else commonR.string.app_name),
Expand Down Expand Up @@ -219,6 +223,12 @@ fun AssistSheetHeader(
)
}
}
if (onManagePipelines != null) {
Divider()
DropdownMenuItem(onClick = { onManagePipelines() }) {
Text(stringResource(commonR.string.assist_manage_pipelines))
}
}
}
}
}
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 @@ -1078,6 +1078,7 @@
<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_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>
<string name="assist_pipeline">Assistant</string>
<string name="assist_preferred_pipeline">Preferred assistant (%1$s)</string>
Expand Down

0 comments on commit 2023268

Please sign in to comment.