Command Templates and Task/Log UI
Overview
This area covers the Compose screens that let users manage custom command templates and launch/inspect command executions. The persistent template shape is CommandTemplate in app/…/CommandTemplate.kt: a Room entity and serializable data class with an auto-generated id, a user-facing name, and the raw command template string.
Template management has two entry styles:
- app/…/TemplateListPage.kt is the full settings page for custom commands. It reads
PreferenceUtil.templateListStateFlowfor the template list, reads/writes the selected template throughTEMPLATE_ID, and toggles the globalCUSTOM_COMMANDpreference. EachTemplateItemcan open the edit route, mark a template as selected, or enter multi-select mode. The page also exports/imports templates through clipboard JSON viaBackupUtil.exportTemplatesToJson()andDatabaseUtil.importTemplatesFromJson(), and deletes selected templates viaDatabaseUtil.deleteTemplates(). - app/…/TemplateEditPage.kt is the full-screen editor. It resolves
templateIdagainstPreferenceUtil.templateListStateFlow;templateId <= 0creates a blankCommandTemplate(0, "", ""), while an existing id loads that template. The top-bar done action writes throughDatabaseUtil.insertTemplate()when the entity id is0, otherwiseDatabaseUtil.updateTemplate(). The command body field is monospace, supports paste/clear, and can be extended by tapping shortcut chips loaded fromDatabaseUtil.getShortcuts().
app/…/CommandTemplateDialog.kt provides the modal editor used from the task creation sheet. CommandTemplateDialog() mirrors the full editor but stays inside an AlertDialog: a blank/default CommandTemplate(0, "", "") creates a new row, an existing template updates in place, and confirmationCallback receives the inserted or updated template id so callers can immediately select it. The same file owns OptionChipsDialog(), which edits reusable command option shortcuts by collecting DatabaseUtil.getShortcuts(), inserting non-duplicate OptionShortcut rows, and deleting individual shortcuts from chips.
Task creation and execution state live in app/…/TaskListPage.kt. TaskListPage(onNavigateBack, onNavigateToDetail) renders Downloader.mutableTaskList.values, sorted by mapped TaskStatus, as CustomCommandTaskItems. The floating action button opens a bottom sheet that pre-fills the URL from clipboard using matchUrlFromString(), shows the currently selected template from PreferenceUtil.getTemplate(), and offers three template actions: pick an existing template (TemplatePickerDialog()), create a template (CommandTemplateDialog() with a callback that updates TEMPLATE_ID), or edit the current template (CommandTemplateDialog(commandTemplate = template)). Starting a task calls Downloader.executeCommandWithUrl(url) and dismisses the sheet.
Task log display is handled by app/…/TaskLogPage.kt. TaskLogPage(onNavigateBack, taskHashCode) finds the matching Downloader.mutableTaskList entry by hashCode() and renders task.output in a selectable monospace text area that auto-scrolls to the bottom. Its bottom action row delegates task operations to the task object: copy log, copy error report for error states, cancel for running states, restart for canceled/error states, and an expand toggle that enables horizontal scrolling for long log lines.
Path routing guidance: route the settings template list to TemplateListPage, and pass onNavigateToEditPage(id) to a route backed by TemplateEditPage(templateId = id); use -1 for “new template” because the editor treats non-positive ids as a blank entity. Route the running-task list to TaskListPage, and pass onNavigateToDetail(task.hashCode()) to a route backed by TaskLogPage(taskHashCode = value). The task log route is intentionally keyed by the in-memory task hash rather than a persisted database id, so it only works while the task remains in Downloader.mutableTaskList.
Coupling
- If
CommandTemplatein app/…/CommandTemplate.kt changes shape, update both persistence/import paths and every editor that constructsCommandTemplate(0, "", ""): TemplateEditPage.kt, CommandTemplateDialog.kt, and TaskListPage.kt all assumeid == 0means “new row” and that onlynameis required before save. - If template selection semantics change, verify TemplateListPage.kt,
TemplatePickerDialog()in TaskListPage.kt, and the task creation bottom sheet together. They share theTEMPLATE_IDpreference, but the full list usesPreferenceUtil.encodeInt()while picker/dialog callbacks useTEMPLATE_ID.updateInt(). - If command execution task identity changes, update the navigation contract between TaskListPage.kt and TaskLogPage.kt. The list currently navigates with
hashCode()and the log page searchesDownloader.mutableTaskList.values.find { it.hashCode() == taskHashCode }; replacing tasks or changing equality/hash behavior can make the detail route fail silently. - If shortcut storage or display changes, check both TemplateEditPage.kt and CommandTemplateDialog.kt. The full editor appends shortcut options into the template text, while
OptionChipsDialog()in the dialog file owns inserting/deleting the underlying shortcut rows.
Risks
- app/…/TaskLogPage.kt returns immediately when no task matches
taskHashCode. If a task is removed fromDownloader.mutableTaskListbefore the log route opens, the user sees no fallback UI or navigation recovery. - app/…/TaskListPage.kt remembers the bottom-sheet template only when template dialogs open/close. If
PreferenceUtil.getTemplate()changes from another path while the sheet remains open, the visible template chip may not refresh until one of those dialog state keys changes.