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

Add 'Manage assistants' button to native Assist #3616

Merged
merged 1 commit into from
Jun 30, 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 @@ -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 @@ -152,6 +152,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 @@ -1065,6 +1065,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_send_text">Send text</string>
<string name="assist_start_listening">Start listening</string>
Expand Down