Implement Logs, Plugins, Channels, Keys, System, and Kanban screens with full integration tests and code formatting checks
This commit is contained in:
1 parent
cffb68550e
commit
03916014c0
22 files changed
+2272
-30
No files matched your search
@@ -21,17 +21,23 @@ import androidx.navigation3.ui.NavDisplay
|
||||
import com.m57.hermescontrol.data.local.AuthManager
|
||||
import kotlinx.coroutines.launch
|
||||
import com.m57.hermescontrol.ui.achievements.AchievementsScreen as AchievementsScreenContent
|
||||
import com.m57.hermescontrol.ui.channels.ChannelsScreen as ChannelsScreenContent
|
||||
import com.m57.hermescontrol.ui.chat.ChatScreen as ChatScreenContent
|
||||
import com.m57.hermescontrol.ui.config.ConfigScreen as ConfigScreenContent
|
||||
import com.m57.hermescontrol.ui.connect.ConnectScreen as ConnectScreenContent
|
||||
import com.m57.hermescontrol.ui.cron.CronJobsScreen as CronJobsScreenContent
|
||||
import com.m57.hermescontrol.ui.gateway.GatewayScreen as GatewayScreenContent
|
||||
import com.m57.hermescontrol.ui.kanban.KanbanScreen as KanbanScreenContent
|
||||
import com.m57.hermescontrol.ui.keys.KeysScreen as KeysScreenContent
|
||||
import com.m57.hermescontrol.ui.logs.LogsScreen as LogsScreenContent
|
||||
import com.m57.hermescontrol.ui.mcp.McpServersScreen as McpServersScreenContent
|
||||
import com.m57.hermescontrol.ui.model.ModelScreen as ModelScreenContent
|
||||
import com.m57.hermescontrol.ui.pairing.PairingScreen as PairingScreenContent
|
||||
import com.m57.hermescontrol.ui.plugins.PluginsScreen as PluginsScreenContent
|
||||
import com.m57.hermescontrol.ui.profiles.ProfilesScreen as ProfilesScreenContent
|
||||
import com.m57.hermescontrol.ui.settings.SettingsScreen as SettingsScreenContent
|
||||
import com.m57.hermescontrol.ui.skills.SkillsScreen as SkillsScreenContent
|
||||
import com.m57.hermescontrol.ui.system.SystemScreen as SystemScreenContent
|
||||
import com.m57.hermescontrol.ui.toolsets.ToolsetsScreen as ToolsetsScreenContent
|
||||
import com.m57.hermescontrol.ui.webhooks.WebhooksScreen as WebhooksScreenContent
|
||||
|
||||
@@ -53,7 +59,10 @@ fun MainNavigation() {
|
||||
currentScreen == ToolsetsScreen || currentScreen == AchievementsScreen ||
|
||||
currentScreen == PairingScreen || currentScreen == ConfigScreen ||
|
||||
currentScreen == McpServersScreen || currentScreen == WebhooksScreen ||
|
||||
currentScreen == ModelScreen
|
||||
currentScreen == ModelScreen || currentScreen == LogsScreen ||
|
||||
currentScreen == PluginsScreen || currentScreen == ChannelsScreen ||
|
||||
currentScreen == KeysScreen || currentScreen == SystemScreen ||
|
||||
currentScreen == KanbanScreen
|
||||
val openDrawer = { scope.launch { drawerState.open() } }
|
||||
|
||||
ModalNavigationDrawer(
|
||||
@@ -176,6 +185,60 @@ fun MainNavigation() {
|
||||
},
|
||||
modifier = Modifier.padding(horizontal = 12.dp, vertical = 4.dp),
|
||||
)
|
||||
NavigationDrawerItem(
|
||||
label = { Text("Logs") },
|
||||
selected = currentScreen == LogsScreen,
|
||||
onClick = {
|
||||
scope.launch { drawerState.close() }
|
||||
NavigationController.navigateTo(LogsScreen)
|
||||
},
|
||||
modifier = Modifier.padding(horizontal = 12.dp, vertical = 4.dp),
|
||||
)
|
||||
NavigationDrawerItem(
|
||||
label = { Text("Plugins") },
|
||||
selected = currentScreen == PluginsScreen,
|
||||
onClick = {
|
||||
scope.launch { drawerState.close() }
|
||||
NavigationController.navigateTo(PluginsScreen)
|
||||
},
|
||||
modifier = Modifier.padding(horizontal = 12.dp, vertical = 4.dp),
|
||||
)
|
||||
NavigationDrawerItem(
|
||||
label = { Text("Channels") },
|
||||
selected = currentScreen == ChannelsScreen,
|
||||
onClick = {
|
||||
scope.launch { drawerState.close() }
|
||||
NavigationController.navigateTo(ChannelsScreen)
|
||||
},
|
||||
modifier = Modifier.padding(horizontal = 12.dp, vertical = 4.dp),
|
||||
)
|
||||
NavigationDrawerItem(
|
||||
label = { Text("Keys") },
|
||||
selected = currentScreen == KeysScreen,
|
||||
onClick = {
|
||||
scope.launch { drawerState.close() }
|
||||
NavigationController.navigateTo(KeysScreen)
|
||||
},
|
||||
modifier = Modifier.padding(horizontal = 12.dp, vertical = 4.dp),
|
||||
)
|
||||
NavigationDrawerItem(
|
||||
label = { Text("System") },
|
||||
selected = currentScreen == SystemScreen,
|
||||
onClick = {
|
||||
scope.launch { drawerState.close() }
|
||||
NavigationController.navigateTo(SystemScreen)
|
||||
},
|
||||
modifier = Modifier.padding(horizontal = 12.dp, vertical = 4.dp),
|
||||
)
|
||||
NavigationDrawerItem(
|
||||
label = { Text("Kanban") },
|
||||
selected = currentScreen == KanbanScreen,
|
||||
onClick = {
|
||||
scope.launch { drawerState.close() }
|
||||
NavigationController.navigateTo(KanbanScreen)
|
||||
},
|
||||
modifier = Modifier.padding(horizontal = 12.dp, vertical = 4.dp),
|
||||
)
|
||||
}
|
||||
}
|
||||
},
|
||||
@@ -277,6 +340,42 @@ fun MainNavigation() {
|
||||
onOpenDrawer = { openDrawer() },
|
||||
)
|
||||
}
|
||||
|
||||
entry<LogsScreen> {
|
||||
LogsScreenContent(
|
||||
onOpenDrawer = { openDrawer() },
|
||||
)
|
||||
}
|
||||
|
||||
entry<PluginsScreen> {
|
||||
PluginsScreenContent(
|
||||
onOpenDrawer = { openDrawer() },
|
||||
)
|
||||
}
|
||||
|
||||
entry<ChannelsScreen> {
|
||||
ChannelsScreenContent(
|
||||
onOpenDrawer = { openDrawer() },
|
||||
)
|
||||
}
|
||||
|
||||
entry<KeysScreen> {
|
||||
KeysScreenContent(
|
||||
onOpenDrawer = { openDrawer() },
|
||||
)
|
||||
}
|
||||
|
||||
entry<SystemScreen> {
|
||||
SystemScreenContent(
|
||||
onOpenDrawer = { openDrawer() },
|
||||
)
|
||||
}
|
||||
|
||||
entry<KanbanScreen> {
|
||||
KanbanScreenContent(
|
||||
onOpenDrawer = { openDrawer() },
|
||||
)
|
||||
}
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
@@ -30,3 +30,15 @@ import kotlinx.serialization.Serializable
|
||||
@Serializable data object WebhooksScreen : NavKey
|
||||
|
||||
@Serializable data object ModelScreen : NavKey
|
||||
|
||||
@Serializable data object LogsScreen : NavKey
|
||||
|
||||
@Serializable data object PluginsScreen : NavKey
|
||||
|
||||
@Serializable data object ChannelsScreen : NavKey
|
||||
|
||||
@Serializable data object KeysScreen : NavKey
|
||||
|
||||
@Serializable data object SystemScreen : NavKey
|
||||
|
||||
@Serializable data object KanbanScreen : NavKey
|
||||
@@ -0,0 +1,12 @@
|
||||
package com.m57.hermescontrol.data.model
|
||||
|
||||
data class DoctorResponse(
|
||||
val status: String,
|
||||
val reports: List<DoctorReport>,
|
||||
)
|
||||
|
||||
data class DoctorReport(
|
||||
val category: String,
|
||||
val status: String,
|
||||
val message: String,
|
||||
)
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.m57.hermescontrol.data.model
|
||||
|
||||
data class KanbanBoard(
|
||||
val id: String,
|
||||
val name: String,
|
||||
val description: String?,
|
||||
)
|
||||
|
||||
data class KanbanTask(
|
||||
val id: String,
|
||||
val boardId: String,
|
||||
val title: String,
|
||||
val description: String?,
|
||||
val status: String,
|
||||
val assignedTo: String?,
|
||||
)
|
||||
|
||||
data class MoveTaskRequest(
|
||||
val status: String,
|
||||
)
|
||||
|
||||
data class CreateTaskRequest(
|
||||
val boardId: String,
|
||||
val title: String,
|
||||
val description: String?,
|
||||
val status: String,
|
||||
)
|
||||
@@ -0,0 +1,5 @@
|
||||
package com.m57.hermescontrol.data.model
|
||||
|
||||
data class LogResponse(
|
||||
val logs: List<String>,
|
||||
)
|
||||
@@ -0,0 +1,8 @@
|
||||
package com.m57.hermescontrol.data.model
|
||||
|
||||
data class MessagingPlatform(
|
||||
val name: String,
|
||||
val enabled: Boolean,
|
||||
val configured: Boolean,
|
||||
val details: Map<String, String>?,
|
||||
)
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.m57.hermescontrol.data.model
|
||||
|
||||
data class PluginInfo(
|
||||
val name: String,
|
||||
val description: String?,
|
||||
val version: String?,
|
||||
val enabled: Boolean,
|
||||
val installed: Boolean,
|
||||
)
|
||||
|
||||
data class TogglePluginRequest(
|
||||
val enabled: Boolean,
|
||||
)
|
||||
@@ -2,13 +2,21 @@ package com.m57.hermescontrol.data.remote
|
||||
|
||||
import com.m57.hermescontrol.data.model.AchievementsResponse
|
||||
import com.m57.hermescontrol.data.model.ActiveProfileResponse
|
||||
import com.m57.hermescontrol.data.model.CreateTaskRequest
|
||||
import com.m57.hermescontrol.data.model.CronJob
|
||||
import com.m57.hermescontrol.data.model.DoctorResponse
|
||||
import com.m57.hermescontrol.data.model.KanbanBoard
|
||||
import com.m57.hermescontrol.data.model.KanbanTask
|
||||
import com.m57.hermescontrol.data.model.LogResponse
|
||||
import com.m57.hermescontrol.data.model.McpServerToggleRequest
|
||||
import com.m57.hermescontrol.data.model.McpServersResponse
|
||||
import com.m57.hermescontrol.data.model.MessagingPlatform
|
||||
import com.m57.hermescontrol.data.model.ModelOptionsResponse
|
||||
import com.m57.hermescontrol.data.model.MoveTaskRequest
|
||||
import com.m57.hermescontrol.data.model.PairingApproveRequest
|
||||
import com.m57.hermescontrol.data.model.PairingResponse
|
||||
import com.m57.hermescontrol.data.model.PairingRevokeRequest
|
||||
import com.m57.hermescontrol.data.model.PluginInfo
|
||||
import com.m57.hermescontrol.data.model.ProfileSoulResponse
|
||||
import com.m57.hermescontrol.data.model.ProfilesResponse
|
||||
import com.m57.hermescontrol.data.model.RawConfigResponse
|
||||
@@ -18,6 +26,7 @@ import com.m57.hermescontrol.data.model.SetActiveProfileRequest
|
||||
import com.m57.hermescontrol.data.model.Skill
|
||||
import com.m57.hermescontrol.data.model.StatusResponse
|
||||
import com.m57.hermescontrol.data.model.SystemStatsResponse
|
||||
import com.m57.hermescontrol.data.model.TogglePluginRequest
|
||||
import com.m57.hermescontrol.data.model.ToggleSkillRequest
|
||||
import com.m57.hermescontrol.data.model.Toolset
|
||||
import com.m57.hermescontrol.data.model.ToolsetToggleRequest
|
||||
@@ -192,4 +201,76 @@ interface HermesApiService {
|
||||
|
||||
@GET("api/model/options")
|
||||
suspend fun getModelOptions(): Response<ModelOptionsResponse>
|
||||
|
||||
@GET("api/logs")
|
||||
suspend fun getLogs(): Response<LogResponse>
|
||||
|
||||
@GET("api/plugins")
|
||||
suspend fun getPlugins(): Response<List<PluginInfo>>
|
||||
|
||||
@POST("api/plugins/{name}/install")
|
||||
suspend fun installPlugin(
|
||||
@Path("name") name: String,
|
||||
): Response<Unit>
|
||||
|
||||
@POST("api/plugins/{name}/uninstall")
|
||||
suspend fun uninstallPlugin(
|
||||
@Path("name") name: String,
|
||||
): Response<Unit>
|
||||
|
||||
@POST("api/plugins/{name}/update")
|
||||
suspend fun updatePlugin(
|
||||
@Path("name") name: String,
|
||||
): Response<Unit>
|
||||
|
||||
@PUT("api/plugins/{name}/toggle")
|
||||
suspend fun togglePlugin(
|
||||
@Path("name") name: String,
|
||||
@Body body: TogglePluginRequest,
|
||||
): Response<Unit>
|
||||
|
||||
@GET("api/messaging/platforms")
|
||||
suspend fun getMessagingPlatforms(): Response<List<MessagingPlatform>>
|
||||
|
||||
@POST("api/messaging/platforms/{name}/configure")
|
||||
suspend fun configurePlatform(
|
||||
@Path("name") name: String,
|
||||
@Body config: Map<String, String>,
|
||||
): Response<Unit>
|
||||
|
||||
@GET("api/env")
|
||||
suspend fun getEnvVars(): Response<Map<String, String>>
|
||||
|
||||
@PUT("api/env")
|
||||
suspend fun updateEnvVar(
|
||||
@Body vars: Map<String, String>,
|
||||
): Response<Unit>
|
||||
|
||||
@POST("api/env/reveal")
|
||||
suspend fun revealEnvVars(): Response<Map<String, String>>
|
||||
|
||||
@POST("api/ops/backup")
|
||||
suspend fun triggerBackup(): Response<Unit>
|
||||
|
||||
@GET("api/ops/doctor")
|
||||
suspend fun runDoctor(): Response<DoctorResponse>
|
||||
|
||||
@GET("api/plugins/kanban/boards")
|
||||
suspend fun getKanbanBoards(): Response<List<KanbanBoard>>
|
||||
|
||||
@GET("api/plugins/kanban/boards/{id}/tasks")
|
||||
suspend fun getKanbanTasks(
|
||||
@Path("id") boardId: String,
|
||||
): Response<List<KanbanTask>>
|
||||
|
||||
@POST("api/plugins/kanban/tasks/{id}/move")
|
||||
suspend fun moveKanbanTask(
|
||||
@Path("id") taskId: String,
|
||||
@Body body: MoveTaskRequest,
|
||||
): Response<Unit>
|
||||
|
||||
@POST("api/plugins/kanban/tasks")
|
||||
suspend fun createKanbanTask(
|
||||
@Body task: CreateTaskRequest,
|
||||
): Response<KanbanTask>
|
||||
}
|
||||
@@ -0,0 +1,196 @@
|
||||
package com.m57.hermescontrol.ui.channels
|
||||
|
||||
import android.widget.Toast
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.Box
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.PaddingValues
|
||||
import androidx.compose.foundation.layout.Row
|
||||
import androidx.compose.foundation.layout.Spacer
|
||||
import androidx.compose.foundation.layout.fillMaxSize
|
||||
import androidx.compose.foundation.layout.fillMaxWidth
|
||||
import androidx.compose.foundation.layout.height
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.layout.width
|
||||
import androidx.compose.foundation.lazy.LazyColumn
|
||||
import androidx.compose.foundation.lazy.items
|
||||
import androidx.compose.material.icons.Icons
|
||||
import androidx.compose.material.icons.filled.Menu
|
||||
import androidx.compose.material3.Button
|
||||
import androidx.compose.material3.Card
|
||||
import androidx.compose.material3.CircularProgressIndicator
|
||||
import androidx.compose.material3.ExperimentalMaterial3Api
|
||||
import androidx.compose.material3.Icon
|
||||
import androidx.compose.material3.IconButton
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.OutlinedTextField
|
||||
import androidx.compose.material3.Scaffold
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.material3.TopAppBar
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.LaunchedEffect
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateMapOf
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.lifecycle.compose.collectAsStateWithLifecycle
|
||||
import androidx.lifecycle.viewmodel.compose.viewModel
|
||||
|
||||
@OptIn(ExperimentalMaterial3Api::class)
|
||||
@Composable
|
||||
fun ChannelsScreen(
|
||||
modifier: Modifier = Modifier,
|
||||
onOpenDrawer: (() -> Unit)? = null,
|
||||
viewModel: ChannelsViewModel = viewModel { ChannelsViewModel() },
|
||||
) {
|
||||
val state by viewModel.uiState.collectAsStateWithLifecycle()
|
||||
val context = LocalContext.current
|
||||
|
||||
LaunchedEffect(Unit) {
|
||||
viewModel.loadPlatforms()
|
||||
}
|
||||
|
||||
LaunchedEffect(state.toastMessage) {
|
||||
state.toastMessage?.let { msg ->
|
||||
Toast.makeText(context, msg, Toast.LENGTH_SHORT).show()
|
||||
viewModel.clearToast()
|
||||
}
|
||||
}
|
||||
|
||||
Scaffold(
|
||||
topBar = {
|
||||
TopAppBar(
|
||||
navigationIcon = {
|
||||
if (onOpenDrawer != null) {
|
||||
IconButton(onClick = onOpenDrawer) {
|
||||
Icon(
|
||||
imageVector = Icons.Filled.Menu,
|
||||
contentDescription = "Open Drawer",
|
||||
)
|
||||
}
|
||||
}
|
||||
},
|
||||
title = { Text("Messaging Channels") },
|
||||
actions = {
|
||||
IconButton(onClick = { viewModel.loadPlatforms() }) {
|
||||
Text("Refresh", style = MaterialTheme.typography.bodyMedium)
|
||||
}
|
||||
},
|
||||
)
|
||||
},
|
||||
modifier = modifier,
|
||||
) { paddingValues ->
|
||||
Box(
|
||||
modifier =
|
||||
Modifier
|
||||
.fillMaxSize()
|
||||
.padding(paddingValues),
|
||||
) {
|
||||
if (state.isLoading) {
|
||||
CircularProgressIndicator(modifier = Modifier.align(Alignment.Center))
|
||||
} else if (state.errorMessage != null) {
|
||||
Text(
|
||||
text = state.errorMessage ?: "",
|
||||
color = MaterialTheme.colorScheme.error,
|
||||
modifier =
|
||||
Modifier
|
||||
.align(Alignment.Center)
|
||||
.padding(16.dp),
|
||||
)
|
||||
} else {
|
||||
LazyColumn(
|
||||
modifier = Modifier.fillMaxSize(),
|
||||
contentPadding = PaddingValues(16.dp),
|
||||
verticalArrangement = Arrangement.spacedBy(16.dp),
|
||||
) {
|
||||
items(state.platforms) { platform ->
|
||||
var showConfigureForm by remember { mutableStateOf(false) }
|
||||
|
||||
Card(modifier = Modifier.fillMaxWidth()) {
|
||||
Column(modifier = Modifier.padding(16.dp)) {
|
||||
Row(
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
horizontalArrangement = Arrangement.SpaceBetween,
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
) {
|
||||
Column {
|
||||
Text(text = platform.name, style = MaterialTheme.typography.titleLarge)
|
||||
Text(
|
||||
text = if (platform.enabled) "Active" else "Disabled",
|
||||
color =
|
||||
if (platform.enabled) {
|
||||
MaterialTheme.colorScheme.primary
|
||||
} else {
|
||||
MaterialTheme.colorScheme.error
|
||||
},
|
||||
style = MaterialTheme.typography.bodyMedium,
|
||||
)
|
||||
}
|
||||
Button(onClick = { showConfigureForm = !showConfigureForm }) {
|
||||
Text(if (showConfigureForm) "Cancel" else "Configure")
|
||||
}
|
||||
}
|
||||
|
||||
if (showConfigureForm) {
|
||||
Spacer(modifier = Modifier.height(16.dp))
|
||||
Text(
|
||||
text = "Configuration Settings",
|
||||
style = MaterialTheme.typography.titleMedium,
|
||||
)
|
||||
|
||||
val fields =
|
||||
if (platform.name.lowercase() == "telegram") {
|
||||
listOf("bot_token", "chat_id")
|
||||
} else {
|
||||
listOf("webhook_url", "channel")
|
||||
}
|
||||
|
||||
val inputValues = remember { mutableStateMapOf<String, String>() }
|
||||
|
||||
fields.forEach { field ->
|
||||
OutlinedTextField(
|
||||
value = inputValues[field].orEmpty(),
|
||||
onValueChange = { inputValues[field] = it },
|
||||
label = { Text(field.replace("_", " ").uppercase()) },
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
)
|
||||
Spacer(modifier = Modifier.height(8.dp))
|
||||
}
|
||||
|
||||
Button(
|
||||
onClick = {
|
||||
viewModel.configurePlatform(platform.name, inputValues.toMap())
|
||||
showConfigureForm = false
|
||||
},
|
||||
modifier = Modifier.align(Alignment.End),
|
||||
) {
|
||||
Text("Save Settings")
|
||||
}
|
||||
} else {
|
||||
platform.details?.let { details ->
|
||||
if (details.isNotEmpty()) {
|
||||
Spacer(modifier = Modifier.height(8.dp))
|
||||
Text(text = "Details:", style = MaterialTheme.typography.titleSmall)
|
||||
details.forEach { (key, value) ->
|
||||
Text(
|
||||
text = "$key: $value",
|
||||
style = MaterialTheme.typography.bodyMedium,
|
||||
color = MaterialTheme.colorScheme.onSurfaceVariant,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
package com.m57.hermescontrol.ui.channels
|
||||
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import com.m57.hermescontrol.data.model.MessagingPlatform
|
||||
import com.m57.hermescontrol.data.remote.ApiClient
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.StateFlow
|
||||
import kotlinx.coroutines.flow.asStateFlow
|
||||
import kotlinx.coroutines.flow.update
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withContext
|
||||
|
||||
data class ChannelsUiState(
|
||||
val isLoading: Boolean = false,
|
||||
val platforms: List<MessagingPlatform> = emptyList(),
|
||||
val errorMessage: String? = null,
|
||||
val toastMessage: String? = null,
|
||||
)
|
||||
|
||||
class ChannelsViewModel : ViewModel() {
|
||||
private val _uiState = MutableStateFlow(ChannelsUiState())
|
||||
val uiState: StateFlow<ChannelsUiState> = _uiState.asStateFlow()
|
||||
|
||||
fun loadPlatforms() {
|
||||
_uiState.update { it.copy(isLoading = true, errorMessage = null) }
|
||||
viewModelScope.launch {
|
||||
try {
|
||||
val response =
|
||||
withContext(Dispatchers.IO) {
|
||||
ApiClient.hermesApi.getMessagingPlatforms()
|
||||
}
|
||||
if (response.isSuccessful) {
|
||||
_uiState.update { it.copy(isLoading = false, platforms = response.body().orEmpty()) }
|
||||
} else {
|
||||
_uiState.update {
|
||||
it.copy(
|
||||
isLoading = false,
|
||||
errorMessage = "Failed to load platforms: HTTP ${response.code()}",
|
||||
)
|
||||
}
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
_uiState.update { it.copy(isLoading = false, errorMessage = "Failed to load platforms: ${e.message}") }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun configurePlatform(
|
||||
name: String,
|
||||
config: Map<String, String>,
|
||||
) {
|
||||
viewModelScope.launch {
|
||||
try {
|
||||
val response =
|
||||
withContext(Dispatchers.IO) {
|
||||
ApiClient.hermesApi.configurePlatform(name, config)
|
||||
}
|
||||
if (response.isSuccessful) {
|
||||
_uiState.update { it.copy(toastMessage = "$name configured successfully") }
|
||||
loadPlatforms()
|
||||
} else {
|
||||
_uiState.update { it.copy(toastMessage = "Failed to configure $name: HTTP ${response.code()}") }
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
_uiState.update { it.copy(toastMessage = "Failed to configure $name: ${e.message}") }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun clearToast() {
|
||||
_uiState.update { it.copy(toastMessage = null) }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,309 @@
|
||||
package com.m57.hermescontrol.ui.kanban
|
||||
|
||||
import android.widget.Toast
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.Box
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.PaddingValues
|
||||
import androidx.compose.foundation.layout.Row
|
||||
import androidx.compose.foundation.layout.Spacer
|
||||
import androidx.compose.foundation.layout.fillMaxSize
|
||||
import androidx.compose.foundation.layout.fillMaxWidth
|
||||
import androidx.compose.foundation.layout.height
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.layout.size
|
||||
import androidx.compose.foundation.layout.width
|
||||
import androidx.compose.foundation.lazy.LazyColumn
|
||||
import androidx.compose.foundation.lazy.LazyRow
|
||||
import androidx.compose.foundation.lazy.items
|
||||
import androidx.compose.material.icons.Icons
|
||||
import androidx.compose.material.icons.filled.Add
|
||||
import androidx.compose.material.icons.filled.ArrowBack
|
||||
import androidx.compose.material.icons.filled.ArrowForward
|
||||
import androidx.compose.material.icons.filled.Menu
|
||||
import androidx.compose.material3.Button
|
||||
import androidx.compose.material3.Card
|
||||
import androidx.compose.material3.CircularProgressIndicator
|
||||
import androidx.compose.material3.ExperimentalMaterial3Api
|
||||
import androidx.compose.material3.FloatingActionButton
|
||||
import androidx.compose.material3.Icon
|
||||
import androidx.compose.material3.IconButton
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.OutlinedTextField
|
||||
import androidx.compose.material3.Scaffold
|
||||
import androidx.compose.material3.ScrollableTabRow
|
||||
import androidx.compose.material3.Tab
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.material3.TopAppBar
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.LaunchedEffect
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.compose.ui.text.font.FontWeight
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.lifecycle.compose.collectAsStateWithLifecycle
|
||||
import androidx.lifecycle.viewmodel.compose.viewModel
|
||||
import com.m57.hermescontrol.data.model.KanbanTask
|
||||
|
||||
@OptIn(ExperimentalMaterial3Api::class)
|
||||
@Composable
|
||||
fun KanbanScreen(
|
||||
modifier: Modifier = Modifier,
|
||||
onOpenDrawer: (() -> Unit)? = null,
|
||||
viewModel: KanbanViewModel = viewModel { KanbanViewModel() },
|
||||
) {
|
||||
val state by viewModel.uiState.collectAsStateWithLifecycle()
|
||||
val context = LocalContext.current
|
||||
var showAddTaskDialog by remember { mutableStateOf(false) }
|
||||
|
||||
LaunchedEffect(Unit) {
|
||||
viewModel.loadBoards()
|
||||
}
|
||||
|
||||
LaunchedEffect(state.toastMessage) {
|
||||
state.toastMessage?.let { msg ->
|
||||
Toast.makeText(context, msg, Toast.LENGTH_SHORT).show()
|
||||
viewModel.clearToast()
|
||||
}
|
||||
}
|
||||
|
||||
Scaffold(
|
||||
topBar = {
|
||||
TopAppBar(
|
||||
navigationIcon = {
|
||||
if (onOpenDrawer != null) {
|
||||
IconButton(onClick = onOpenDrawer) {
|
||||
Icon(
|
||||
imageVector = Icons.Filled.Menu,
|
||||
contentDescription = "Open Drawer",
|
||||
)
|
||||
}
|
||||
}
|
||||
},
|
||||
title = { Text(state.selectedBoard?.name ?: "Kanban Boards") },
|
||||
actions = {
|
||||
IconButton(onClick = { viewModel.loadBoards() }) {
|
||||
Text("Refresh", style = MaterialTheme.typography.bodyMedium)
|
||||
}
|
||||
},
|
||||
)
|
||||
},
|
||||
floatingActionButton = {
|
||||
if (state.selectedBoard != null) {
|
||||
FloatingActionButton(onClick = { showAddTaskDialog = true }) {
|
||||
Icon(imageVector = Icons.Filled.Add, contentDescription = "Add Task")
|
||||
}
|
||||
}
|
||||
},
|
||||
modifier = modifier,
|
||||
) { paddingValues ->
|
||||
Box(
|
||||
modifier =
|
||||
Modifier
|
||||
.fillMaxSize()
|
||||
.padding(paddingValues),
|
||||
) {
|
||||
if (state.isLoading) {
|
||||
CircularProgressIndicator(modifier = Modifier.align(Alignment.Center))
|
||||
} else if (state.errorMessage != null) {
|
||||
Text(
|
||||
text = state.errorMessage ?: "",
|
||||
color = MaterialTheme.colorScheme.error,
|
||||
modifier =
|
||||
Modifier
|
||||
.align(Alignment.Center)
|
||||
.padding(16.dp),
|
||||
)
|
||||
} else {
|
||||
Column(modifier = Modifier.fillMaxSize()) {
|
||||
// Board selector tab row
|
||||
if (state.boards.isNotEmpty()) {
|
||||
ScrollableTabRow(
|
||||
selectedTabIndex = state.boards.indexOf(state.selectedBoard).coerceAtLeast(0),
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
) {
|
||||
state.boards.forEach { board ->
|
||||
Tab(
|
||||
selected = board == state.selectedBoard,
|
||||
onClick = { viewModel.selectBoard(board) },
|
||||
text = { Text(board.name) },
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (state.selectedBoard == null) {
|
||||
Box(modifier = Modifier.fillMaxSize(), contentAlignment = Alignment.Center) {
|
||||
Text("No boards found.")
|
||||
}
|
||||
} else {
|
||||
// Columns list: To Do, In Progress, Done
|
||||
val columns = listOf("todo", "in_progress", "done")
|
||||
val columnNames =
|
||||
mapOf(
|
||||
"todo" to "To Do",
|
||||
"in_progress" to "In Progress",
|
||||
"done" to "Done",
|
||||
)
|
||||
|
||||
LazyRow(
|
||||
modifier = Modifier.fillMaxSize(),
|
||||
contentPadding = PaddingValues(16.dp),
|
||||
horizontalArrangement = Arrangement.spacedBy(16.dp),
|
||||
) {
|
||||
items(columns) { col ->
|
||||
val colTasks = state.tasks.filter { it.status.lowercase() == col }
|
||||
|
||||
Column(
|
||||
modifier =
|
||||
Modifier
|
||||
.width(280.dp)
|
||||
.fillMaxSize(),
|
||||
) {
|
||||
Text(
|
||||
text = "${columnNames[col]} (${colTasks.size})",
|
||||
style = MaterialTheme.typography.titleMedium,
|
||||
fontWeight = FontWeight.Bold,
|
||||
modifier = Modifier.padding(bottom = 8.dp),
|
||||
)
|
||||
|
||||
LazyColumn(
|
||||
modifier = Modifier.weight(1f),
|
||||
verticalArrangement = Arrangement.spacedBy(8.dp),
|
||||
) {
|
||||
items(colTasks) { task ->
|
||||
val onMoveLeft: (() -> Unit)? =
|
||||
if (col != "todo") {
|
||||
{
|
||||
val prevStatus = if (col == "done") "in_progress" else "todo"
|
||||
viewModel.moveTask(task, prevStatus)
|
||||
}
|
||||
} else {
|
||||
null
|
||||
}
|
||||
|
||||
val onMoveRight: (() -> Unit)? =
|
||||
if (col != "done") {
|
||||
{
|
||||
val nextStatus = if (col == "todo") "in_progress" else "done"
|
||||
viewModel.moveTask(task, nextStatus)
|
||||
}
|
||||
} else {
|
||||
null
|
||||
}
|
||||
|
||||
TaskCard(
|
||||
task = task,
|
||||
onMoveLeft = onMoveLeft,
|
||||
onMoveRight = onMoveRight,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (showAddTaskDialog) {
|
||||
AddTaskDialog(
|
||||
onDismiss = { showAddTaskDialog = false },
|
||||
onConfirm = { title, desc ->
|
||||
viewModel.createTask(title, desc, "todo")
|
||||
showAddTaskDialog = false
|
||||
},
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
fun TaskCard(
|
||||
task: KanbanTask,
|
||||
onMoveLeft: (() -> Unit)?,
|
||||
onMoveRight: (() -> Unit)?,
|
||||
) {
|
||||
Card(modifier = Modifier.fillMaxWidth()) {
|
||||
Column(modifier = Modifier.padding(12.dp)) {
|
||||
Text(text = task.title, style = MaterialTheme.typography.titleMedium, fontWeight = FontWeight.SemiBold)
|
||||
Spacer(modifier = Modifier.height(4.dp))
|
||||
task.description?.let {
|
||||
Text(text = it, style = MaterialTheme.typography.bodyMedium)
|
||||
}
|
||||
Spacer(modifier = Modifier.height(8.dp))
|
||||
Row(
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
horizontalArrangement = Arrangement.SpaceBetween,
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
) {
|
||||
if (onMoveLeft != null) {
|
||||
IconButton(onClick = onMoveLeft) {
|
||||
Icon(imageVector = Icons.Filled.ArrowBack, contentDescription = "Move Left")
|
||||
}
|
||||
} else {
|
||||
Spacer(modifier = Modifier.size(48.dp))
|
||||
}
|
||||
|
||||
if (onMoveRight != null) {
|
||||
IconButton(onClick = onMoveRight) {
|
||||
Icon(imageVector = Icons.Filled.ArrowForward, contentDescription = "Move Right")
|
||||
}
|
||||
} else {
|
||||
Spacer(modifier = Modifier.size(48.dp))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@OptIn(ExperimentalMaterial3Api::class)
|
||||
@Composable
|
||||
fun AddTaskDialog(
|
||||
onDismiss: () -> Unit,
|
||||
onConfirm: (title: String, desc: String?) -> Unit,
|
||||
) {
|
||||
var title by remember { mutableStateOf("") }
|
||||
var desc by remember { mutableStateOf("") }
|
||||
|
||||
androidx.compose.material3.AlertDialog(
|
||||
onDismissRequest = onDismiss,
|
||||
title = { Text("Add New Task") },
|
||||
text = {
|
||||
Column {
|
||||
OutlinedTextField(
|
||||
value = title,
|
||||
onValueChange = { title = it },
|
||||
label = { Text("Task Title") },
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
)
|
||||
Spacer(modifier = Modifier.height(8.dp))
|
||||
OutlinedTextField(
|
||||
value = desc,
|
||||
onValueChange = { desc = it },
|
||||
label = { Text("Task Description") },
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
)
|
||||
}
|
||||
},
|
||||
confirmButton = {
|
||||
Button(
|
||||
onClick = { if (title.isNotBlank()) onConfirm(title, desc.ifBlank { null }) },
|
||||
enabled = title.isNotBlank(),
|
||||
) {
|
||||
Text("Add")
|
||||
}
|
||||
},
|
||||
dismissButton = {
|
||||
Button(onClick = onDismiss) {
|
||||
Text("Cancel")
|
||||
}
|
||||
},
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,175 @@
|
||||
package com.m57.hermescontrol.ui.kanban
|
||||
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import com.m57.hermescontrol.data.model.CreateTaskRequest
|
||||
import com.m57.hermescontrol.data.model.KanbanBoard
|
||||
import com.m57.hermescontrol.data.model.KanbanTask
|
||||
import com.m57.hermescontrol.data.model.MoveTaskRequest
|
||||
import com.m57.hermescontrol.data.remote.ApiClient
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.StateFlow
|
||||
import kotlinx.coroutines.flow.asStateFlow
|
||||
import kotlinx.coroutines.flow.update
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withContext
|
||||
|
||||
data class KanbanUiState(
|
||||
val isLoading: Boolean = false,
|
||||
val boards: List<KanbanBoard> = emptyList(),
|
||||
val selectedBoard: KanbanBoard? = null,
|
||||
val tasks: List<KanbanTask> = emptyList(),
|
||||
val errorMessage: String? = null,
|
||||
val toastMessage: String? = null,
|
||||
)
|
||||
|
||||
class KanbanViewModel : ViewModel() {
|
||||
private val _uiState = MutableStateFlow(KanbanUiState())
|
||||
val uiState: StateFlow<KanbanUiState> = _uiState.asStateFlow()
|
||||
|
||||
fun loadBoards() {
|
||||
_uiState.update { it.copy(isLoading = true, errorMessage = null) }
|
||||
viewModelScope.launch {
|
||||
try {
|
||||
val response =
|
||||
withContext(Dispatchers.IO) {
|
||||
ApiClient.hermesApi.getKanbanBoards()
|
||||
}
|
||||
if (response.isSuccessful) {
|
||||
val boards = response.body().orEmpty()
|
||||
_uiState.update { it.copy(isLoading = false, boards = boards) }
|
||||
if (boards.isNotEmpty()) {
|
||||
selectBoard(boards.first())
|
||||
}
|
||||
} else {
|
||||
_uiState.update {
|
||||
it.copy(
|
||||
isLoading = false,
|
||||
errorMessage = "Failed to load Kanban boards: HTTP ${response.code()}",
|
||||
)
|
||||
}
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
_uiState.update {
|
||||
it.copy(
|
||||
isLoading = false,
|
||||
errorMessage = "Failed to load Kanban boards: ${e.message}",
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun selectBoard(board: KanbanBoard) {
|
||||
_uiState.update { it.copy(selectedBoard = board, isLoading = true, errorMessage = null) }
|
||||
viewModelScope.launch {
|
||||
try {
|
||||
val response =
|
||||
withContext(Dispatchers.IO) {
|
||||
ApiClient.hermesApi.getKanbanTasks(board.id)
|
||||
}
|
||||
if (response.isSuccessful) {
|
||||
_uiState.update { it.copy(isLoading = false, tasks = response.body().orEmpty()) }
|
||||
} else {
|
||||
_uiState.update {
|
||||
it.copy(
|
||||
isLoading = false,
|
||||
errorMessage = "Failed to load Kanban tasks: HTTP ${response.code()}",
|
||||
)
|
||||
}
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
_uiState.update {
|
||||
it.copy(
|
||||
isLoading = false,
|
||||
errorMessage = "Failed to load Kanban tasks: ${e.message}",
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun createTask(
|
||||
title: String,
|
||||
description: String?,
|
||||
status: String,
|
||||
) {
|
||||
val board = _uiState.value.selectedBoard ?: return
|
||||
viewModelScope.launch {
|
||||
try {
|
||||
val response =
|
||||
withContext(Dispatchers.IO) {
|
||||
ApiClient.hermesApi.createKanbanTask(
|
||||
CreateTaskRequest(
|
||||
boardId = board.id,
|
||||
title = title,
|
||||
description = description,
|
||||
status = status,
|
||||
),
|
||||
)
|
||||
}
|
||||
if (response.isSuccessful) {
|
||||
_uiState.update { it.copy(toastMessage = "Task created successfully") }
|
||||
selectBoard(board)
|
||||
} else {
|
||||
_uiState.update { it.copy(toastMessage = "Failed to create task: HTTP ${response.code()}") }
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
_uiState.update { it.copy(toastMessage = "Failed to create task: ${e.message}") }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun moveTask(
|
||||
task: KanbanTask,
|
||||
newStatus: String,
|
||||
) {
|
||||
val originalStatus = task.status
|
||||
val board = _uiState.value.selectedBoard ?: return
|
||||
|
||||
// Optimistically update
|
||||
_uiState.update { state ->
|
||||
state.copy(
|
||||
tasks =
|
||||
state.tasks.map {
|
||||
if (it.id == task.id) it.copy(status = newStatus) else it
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
viewModelScope.launch {
|
||||
try {
|
||||
val response =
|
||||
withContext(Dispatchers.IO) {
|
||||
ApiClient.hermesApi.moveKanbanTask(task.id, MoveTaskRequest(newStatus))
|
||||
}
|
||||
if (!response.isSuccessful) {
|
||||
revertTaskMove(task.id, originalStatus, "Failed to move task: HTTP ${response.code()}")
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
revertTaskMove(task.id, originalStatus, "Failed to move task: ${e.message}")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun revertTaskMove(
|
||||
taskId: String,
|
||||
originalStatus: String,
|
||||
errorMsg: String,
|
||||
) {
|
||||
_uiState.update { state ->
|
||||
state.copy(
|
||||
tasks =
|
||||
state.tasks.map {
|
||||
if (it.id == taskId) it.copy(status = originalStatus) else it
|
||||
},
|
||||
toastMessage = errorMsg,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
fun clearToast() {
|
||||
_uiState.update { it.copy(toastMessage = null) }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,173 @@
|
||||
package com.m57.hermescontrol.ui.keys
|
||||
|
||||
import android.widget.Toast
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.Box
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.PaddingValues
|
||||
import androidx.compose.foundation.layout.Row
|
||||
import androidx.compose.foundation.layout.Spacer
|
||||
import androidx.compose.foundation.layout.fillMaxSize
|
||||
import androidx.compose.foundation.layout.fillMaxWidth
|
||||
import androidx.compose.foundation.layout.height
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.layout.width
|
||||
import androidx.compose.foundation.lazy.LazyColumn
|
||||
import androidx.compose.foundation.lazy.items
|
||||
import androidx.compose.material.icons.Icons
|
||||
import androidx.compose.material.icons.filled.Menu
|
||||
import androidx.compose.material3.Button
|
||||
import androidx.compose.material3.Card
|
||||
import androidx.compose.material3.CircularProgressIndicator
|
||||
import androidx.compose.material3.ExperimentalMaterial3Api
|
||||
import androidx.compose.material3.Icon
|
||||
import androidx.compose.material3.IconButton
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.OutlinedTextField
|
||||
import androidx.compose.material3.Scaffold
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.material3.TopAppBar
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.LaunchedEffect
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.compose.ui.text.font.FontFamily
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.lifecycle.compose.collectAsStateWithLifecycle
|
||||
import androidx.lifecycle.viewmodel.compose.viewModel
|
||||
|
||||
@OptIn(ExperimentalMaterial3Api::class)
|
||||
@Composable
|
||||
fun KeysScreen(
|
||||
modifier: Modifier = Modifier,
|
||||
onOpenDrawer: (() -> Unit)? = null,
|
||||
viewModel: KeysViewModel = viewModel { KeysViewModel() },
|
||||
) {
|
||||
val state by viewModel.uiState.collectAsStateWithLifecycle()
|
||||
val context = LocalContext.current
|
||||
|
||||
LaunchedEffect(Unit) {
|
||||
viewModel.loadKeys(reveal = false)
|
||||
}
|
||||
|
||||
LaunchedEffect(state.toastMessage) {
|
||||
state.toastMessage?.let { msg ->
|
||||
Toast.makeText(context, msg, Toast.LENGTH_SHORT).show()
|
||||
viewModel.clearToast()
|
||||
}
|
||||
}
|
||||
|
||||
Scaffold(
|
||||
topBar = {
|
||||
TopAppBar(
|
||||
navigationIcon = {
|
||||
if (onOpenDrawer != null) {
|
||||
IconButton(onClick = onOpenDrawer) {
|
||||
Icon(
|
||||
imageVector = Icons.Filled.Menu,
|
||||
contentDescription = "Open Drawer",
|
||||
)
|
||||
}
|
||||
}
|
||||
},
|
||||
title = { Text("Keys & Credentials") },
|
||||
actions = {
|
||||
Button(onClick = { viewModel.loadKeys(reveal = !state.isRevealed) }) {
|
||||
Text(if (state.isRevealed) "Hide Values" else "Reveal Values")
|
||||
}
|
||||
},
|
||||
)
|
||||
},
|
||||
modifier = modifier,
|
||||
) { paddingValues ->
|
||||
Box(
|
||||
modifier =
|
||||
Modifier
|
||||
.fillMaxSize()
|
||||
.padding(paddingValues),
|
||||
) {
|
||||
if (state.isLoading) {
|
||||
CircularProgressIndicator(modifier = Modifier.align(Alignment.Center))
|
||||
} else if (state.errorMessage != null) {
|
||||
Text(
|
||||
text = state.errorMessage ?: "",
|
||||
color = MaterialTheme.colorScheme.error,
|
||||
modifier =
|
||||
Modifier
|
||||
.align(Alignment.Center)
|
||||
.padding(16.dp),
|
||||
)
|
||||
} else {
|
||||
LazyColumn(
|
||||
modifier = Modifier.fillMaxSize(),
|
||||
contentPadding = PaddingValues(16.dp),
|
||||
verticalArrangement = Arrangement.spacedBy(8.dp),
|
||||
) {
|
||||
items(state.envVars.toList()) { (key, value) ->
|
||||
var isEditing by remember { mutableStateOf(false) }
|
||||
var editedValue by remember { mutableStateOf(value) }
|
||||
|
||||
Card(modifier = Modifier.fillMaxWidth()) {
|
||||
Column(modifier = Modifier.padding(16.dp)) {
|
||||
Text(
|
||||
text = key,
|
||||
style = MaterialTheme.typography.titleMedium,
|
||||
fontFamily = FontFamily.Monospace,
|
||||
)
|
||||
Spacer(modifier = Modifier.height(4.dp))
|
||||
if (isEditing) {
|
||||
OutlinedTextField(
|
||||
value = editedValue,
|
||||
onValueChange = { editedValue = it },
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
singleLine = true,
|
||||
)
|
||||
Spacer(modifier = Modifier.height(8.dp))
|
||||
Row(
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
horizontalArrangement = Arrangement.End,
|
||||
) {
|
||||
Button(onClick = {
|
||||
viewModel.updateKey(key, editedValue)
|
||||
isEditing = false
|
||||
}) {
|
||||
Text("Save")
|
||||
}
|
||||
Spacer(modifier = Modifier.width(8.dp))
|
||||
Button(onClick = {
|
||||
isEditing = false
|
||||
editedValue = value
|
||||
}) {
|
||||
Text("Cancel")
|
||||
}
|
||||
}
|
||||
} else {
|
||||
Row(
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
horizontalArrangement = Arrangement.SpaceBetween,
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
) {
|
||||
Text(
|
||||
text = value,
|
||||
style = MaterialTheme.typography.bodyMedium,
|
||||
color = MaterialTheme.colorScheme.onSurfaceVariant,
|
||||
fontFamily = FontFamily.Monospace,
|
||||
)
|
||||
IconButton(onClick = { isEditing = true }) {
|
||||
Text("Edit", style = MaterialTheme.typography.bodySmall)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
package com.m57.hermescontrol.ui.keys
|
||||
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import com.m57.hermescontrol.data.remote.ApiClient
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.StateFlow
|
||||
import kotlinx.coroutines.flow.asStateFlow
|
||||
import kotlinx.coroutines.flow.update
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withContext
|
||||
|
||||
data class KeysUiState(
|
||||
val isLoading: Boolean = false,
|
||||
val envVars: Map<String, String> = emptyMap(),
|
||||
val isRevealed: Boolean = false,
|
||||
val errorMessage: String? = null,
|
||||
val toastMessage: String? = null,
|
||||
)
|
||||
|
||||
class KeysViewModel : ViewModel() {
|
||||
private val _uiState = MutableStateFlow(KeysUiState())
|
||||
val uiState: StateFlow<KeysUiState> = _uiState.asStateFlow()
|
||||
|
||||
fun loadKeys(reveal: Boolean = false) {
|
||||
_uiState.update { it.copy(isLoading = true, errorMessage = null) }
|
||||
viewModelScope.launch {
|
||||
try {
|
||||
val response =
|
||||
withContext(Dispatchers.IO) {
|
||||
if (reveal) {
|
||||
ApiClient.hermesApi.revealEnvVars()
|
||||
} else {
|
||||
ApiClient.hermesApi.getEnvVars()
|
||||
}
|
||||
}
|
||||
if (response.isSuccessful) {
|
||||
_uiState.update {
|
||||
it.copy(
|
||||
isLoading = false,
|
||||
envVars = response.body().orEmpty(),
|
||||
isRevealed = reveal,
|
||||
)
|
||||
}
|
||||
} else {
|
||||
_uiState.update {
|
||||
it.copy(
|
||||
isLoading = false,
|
||||
errorMessage = "Failed to load keys: HTTP ${response.code()}",
|
||||
)
|
||||
}
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
_uiState.update { it.copy(isLoading = false, errorMessage = "Failed to load keys: ${e.message}") }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun updateKey(
|
||||
key: String,
|
||||
value: String,
|
||||
) {
|
||||
viewModelScope.launch {
|
||||
try {
|
||||
val updatedVars =
|
||||
_uiState.value.envVars.toMutableMap().apply {
|
||||
put(key, value)
|
||||
}
|
||||
val response =
|
||||
withContext(Dispatchers.IO) {
|
||||
ApiClient.hermesApi.updateEnvVar(updatedVars)
|
||||
}
|
||||
if (response.isSuccessful) {
|
||||
_uiState.update { it.copy(toastMessage = "Key updated successfully") }
|
||||
loadKeys(_uiState.value.isRevealed)
|
||||
} else {
|
||||
_uiState.update { it.copy(toastMessage = "Failed to update key: HTTP ${response.code()}") }
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
_uiState.update { it.copy(toastMessage = "Failed to update key: ${e.message}") }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun clearToast() {
|
||||
_uiState.update { it.copy(toastMessage = null) }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,129 @@
|
||||
package com.m57.hermescontrol.ui.logs
|
||||
|
||||
import android.widget.Toast
|
||||
import androidx.compose.foundation.background
|
||||
import androidx.compose.foundation.layout.Box
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.PaddingValues
|
||||
import androidx.compose.foundation.layout.fillMaxSize
|
||||
import androidx.compose.foundation.layout.fillMaxWidth
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.lazy.LazyColumn
|
||||
import androidx.compose.foundation.lazy.items
|
||||
import androidx.compose.foundation.lazy.rememberLazyListState
|
||||
import androidx.compose.material.icons.Icons
|
||||
import androidx.compose.material.icons.filled.Menu
|
||||
import androidx.compose.material3.CircularProgressIndicator
|
||||
import androidx.compose.material3.ExperimentalMaterial3Api
|
||||
import androidx.compose.material3.Icon
|
||||
import androidx.compose.material3.IconButton
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.Scaffold
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.material3.TopAppBar
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.LaunchedEffect
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.graphics.Color
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.compose.ui.text.font.FontFamily
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.compose.ui.unit.sp
|
||||
import androidx.lifecycle.compose.collectAsStateWithLifecycle
|
||||
import androidx.lifecycle.viewmodel.compose.viewModel
|
||||
|
||||
@OptIn(ExperimentalMaterial3Api::class)
|
||||
@Composable
|
||||
fun LogsScreen(
|
||||
modifier: Modifier = Modifier,
|
||||
onOpenDrawer: (() -> Unit)? = null,
|
||||
viewModel: LogsViewModel = viewModel { LogsViewModel() },
|
||||
) {
|
||||
val state by viewModel.uiState.collectAsStateWithLifecycle()
|
||||
val context = LocalContext.current
|
||||
val listState = rememberLazyListState()
|
||||
|
||||
LaunchedEffect(Unit) {
|
||||
viewModel.loadLogs()
|
||||
}
|
||||
|
||||
LaunchedEffect(state.toastMessage) {
|
||||
state.toastMessage?.let { msg ->
|
||||
Toast.makeText(context, msg, Toast.LENGTH_SHORT).show()
|
||||
viewModel.clearToast()
|
||||
}
|
||||
}
|
||||
|
||||
// Auto scroll to bottom when new logs arrive
|
||||
LaunchedEffect(state.logs.size) {
|
||||
if (state.logs.isNotEmpty()) {
|
||||
listState.animateScrollToItem(state.logs.size - 1)
|
||||
}
|
||||
}
|
||||
|
||||
Scaffold(
|
||||
topBar = {
|
||||
TopAppBar(
|
||||
navigationIcon = {
|
||||
if (onOpenDrawer != null) {
|
||||
IconButton(onClick = onOpenDrawer) {
|
||||
Icon(
|
||||
imageVector = Icons.Filled.Menu,
|
||||
contentDescription = "Open Drawer",
|
||||
)
|
||||
}
|
||||
}
|
||||
},
|
||||
title = { Text("System Logs") },
|
||||
actions = {
|
||||
IconButton(onClick = { viewModel.loadLogs() }) {
|
||||
Text("Refresh", style = MaterialTheme.typography.bodyMedium)
|
||||
}
|
||||
},
|
||||
)
|
||||
},
|
||||
modifier = modifier,
|
||||
) { paddingValues ->
|
||||
Box(
|
||||
modifier =
|
||||
Modifier
|
||||
.fillMaxSize()
|
||||
.padding(paddingValues)
|
||||
.background(Color.Black),
|
||||
) {
|
||||
if (state.isLoading) {
|
||||
CircularProgressIndicator(modifier = Modifier.align(Alignment.Center), color = Color.Green)
|
||||
} else if (state.errorMessage != null) {
|
||||
Text(
|
||||
text = state.errorMessage ?: "",
|
||||
color = MaterialTheme.colorScheme.error,
|
||||
modifier =
|
||||
Modifier
|
||||
.align(Alignment.Center)
|
||||
.padding(16.dp),
|
||||
)
|
||||
} else {
|
||||
LazyColumn(
|
||||
state = listState,
|
||||
modifier = Modifier.fillMaxSize(),
|
||||
contentPadding = PaddingValues(16.dp),
|
||||
) {
|
||||
items(state.logs) { logLine ->
|
||||
Text(
|
||||
text = logLine,
|
||||
color = Color(0xFF00FF00),
|
||||
fontFamily = FontFamily.Monospace,
|
||||
fontSize = 12.sp,
|
||||
modifier =
|
||||
Modifier
|
||||
.fillMaxWidth()
|
||||
.padding(vertical = 2.dp),
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
package com.m57.hermescontrol.ui.logs
|
||||
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import com.m57.hermescontrol.data.remote.ApiClient
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.StateFlow
|
||||
import kotlinx.coroutines.flow.asStateFlow
|
||||
import kotlinx.coroutines.flow.update
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withContext
|
||||
|
||||
data class LogsUiState(
|
||||
val isLoading: Boolean = false,
|
||||
val logs: List<String> = emptyList(),
|
||||
val errorMessage: String? = null,
|
||||
val toastMessage: String? = null,
|
||||
)
|
||||
|
||||
class LogsViewModel : ViewModel() {
|
||||
private val _uiState = MutableStateFlow(LogsUiState())
|
||||
val uiState: StateFlow<LogsUiState> = _uiState.asStateFlow()
|
||||
|
||||
fun loadLogs() {
|
||||
_uiState.update { it.copy(isLoading = true, errorMessage = null) }
|
||||
viewModelScope.launch {
|
||||
try {
|
||||
val response =
|
||||
withContext(Dispatchers.IO) {
|
||||
ApiClient.hermesApi.getLogs()
|
||||
}
|
||||
if (response.isSuccessful) {
|
||||
_uiState.update { it.copy(isLoading = false, logs = response.body()?.logs.orEmpty()) }
|
||||
} else {
|
||||
_uiState.update {
|
||||
it.copy(
|
||||
isLoading = false,
|
||||
errorMessage = "Failed to load logs: HTTP ${response.code()}",
|
||||
)
|
||||
}
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
_uiState.update { it.copy(isLoading = false, errorMessage = "Failed to load logs: ${e.message}") }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun clearToast() {
|
||||
_uiState.update { it.copy(toastMessage = null) }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,170 @@
|
||||
package com.m57.hermescontrol.ui.plugins
|
||||
|
||||
import android.widget.Toast
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.Box
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.PaddingValues
|
||||
import androidx.compose.foundation.layout.Row
|
||||
import androidx.compose.foundation.layout.Spacer
|
||||
import androidx.compose.foundation.layout.fillMaxSize
|
||||
import androidx.compose.foundation.layout.fillMaxWidth
|
||||
import androidx.compose.foundation.layout.height
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.layout.width
|
||||
import androidx.compose.foundation.lazy.LazyColumn
|
||||
import androidx.compose.foundation.lazy.items
|
||||
import androidx.compose.material.icons.Icons
|
||||
import androidx.compose.material.icons.filled.Menu
|
||||
import androidx.compose.material3.Button
|
||||
import androidx.compose.material3.Card
|
||||
import androidx.compose.material3.CircularProgressIndicator
|
||||
import androidx.compose.material3.ExperimentalMaterial3Api
|
||||
import androidx.compose.material3.Icon
|
||||
import androidx.compose.material3.IconButton
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.OutlinedButton
|
||||
import androidx.compose.material3.Scaffold
|
||||
import androidx.compose.material3.Switch
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.material3.TopAppBar
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.LaunchedEffect
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.lifecycle.compose.collectAsStateWithLifecycle
|
||||
import androidx.lifecycle.viewmodel.compose.viewModel
|
||||
|
||||
@OptIn(ExperimentalMaterial3Api::class)
|
||||
@Composable
|
||||
fun PluginsScreen(
|
||||
modifier: Modifier = Modifier,
|
||||
onOpenDrawer: (() -> Unit)? = null,
|
||||
viewModel: PluginsViewModel = viewModel { PluginsViewModel() },
|
||||
) {
|
||||
val state by viewModel.uiState.collectAsStateWithLifecycle()
|
||||
val context = LocalContext.current
|
||||
|
||||
LaunchedEffect(Unit) {
|
||||
viewModel.loadPlugins()
|
||||
}
|
||||
|
||||
LaunchedEffect(state.toastMessage) {
|
||||
state.toastMessage?.let { msg ->
|
||||
Toast.makeText(context, msg, Toast.LENGTH_SHORT).show()
|
||||
viewModel.clearToast()
|
||||
}
|
||||
}
|
||||
|
||||
Scaffold(
|
||||
topBar = {
|
||||
TopAppBar(
|
||||
navigationIcon = {
|
||||
if (onOpenDrawer != null) {
|
||||
IconButton(onClick = onOpenDrawer) {
|
||||
Icon(
|
||||
imageVector = Icons.Filled.Menu,
|
||||
contentDescription = "Open Drawer",
|
||||
)
|
||||
}
|
||||
}
|
||||
},
|
||||
title = { Text("Plugins Hub") },
|
||||
actions = {
|
||||
IconButton(onClick = { viewModel.loadPlugins() }) {
|
||||
Text("Refresh", style = MaterialTheme.typography.bodyMedium)
|
||||
}
|
||||
},
|
||||
)
|
||||
},
|
||||
modifier = modifier,
|
||||
) { paddingValues ->
|
||||
Box(
|
||||
modifier =
|
||||
Modifier
|
||||
.fillMaxSize()
|
||||
.padding(paddingValues),
|
||||
) {
|
||||
if (state.isLoading) {
|
||||
CircularProgressIndicator(modifier = Modifier.align(Alignment.Center))
|
||||
} else if (state.errorMessage != null) {
|
||||
Text(
|
||||
text = state.errorMessage ?: "",
|
||||
color = MaterialTheme.colorScheme.error,
|
||||
modifier =
|
||||
Modifier
|
||||
.align(Alignment.Center)
|
||||
.padding(16.dp),
|
||||
)
|
||||
} else {
|
||||
LazyColumn(
|
||||
modifier = Modifier.fillMaxSize(),
|
||||
contentPadding = PaddingValues(16.dp),
|
||||
verticalArrangement = Arrangement.spacedBy(8.dp),
|
||||
) {
|
||||
items(state.plugins) { plugin ->
|
||||
Card(modifier = Modifier.fillMaxWidth()) {
|
||||
Column(modifier = Modifier.padding(16.dp)) {
|
||||
Row(
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
horizontalArrangement = Arrangement.SpaceBetween,
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
) {
|
||||
Column(modifier = Modifier.weight(1f)) {
|
||||
Text(text = plugin.name, style = MaterialTheme.typography.titleMedium)
|
||||
plugin.version?.let {
|
||||
Text(
|
||||
text = "v$it",
|
||||
style = MaterialTheme.typography.bodySmall,
|
||||
color = MaterialTheme.colorScheme.onSurfaceVariant,
|
||||
)
|
||||
}
|
||||
}
|
||||
if (plugin.installed) {
|
||||
Switch(
|
||||
checked = plugin.enabled,
|
||||
onCheckedChange = { viewModel.togglePlugin(plugin) },
|
||||
)
|
||||
}
|
||||
}
|
||||
Spacer(modifier = Modifier.height(4.dp))
|
||||
Text(
|
||||
text = plugin.description ?: "No description provided.",
|
||||
style = MaterialTheme.typography.bodyMedium,
|
||||
)
|
||||
Spacer(modifier = Modifier.height(8.dp))
|
||||
Row(
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
horizontalArrangement = Arrangement.End,
|
||||
) {
|
||||
if (plugin.installed) {
|
||||
OutlinedButton(onClick = { viewModel.updatePlugin(plugin.name) }) {
|
||||
Text("Update")
|
||||
}
|
||||
Spacer(modifier = Modifier.width(8.dp))
|
||||
OutlinedButton(
|
||||
onClick = { viewModel.uninstallPlugin(plugin.name) },
|
||||
colors =
|
||||
androidx.compose.material3.ButtonDefaults.outlinedButtonColors(
|
||||
contentColor = MaterialTheme.colorScheme.error,
|
||||
),
|
||||
) {
|
||||
Text("Uninstall")
|
||||
}
|
||||
} else {
|
||||
Button(onClick = { viewModel.installPlugin(plugin.name) }) {
|
||||
Text("Install")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,156 @@
|
||||
package com.m57.hermescontrol.ui.plugins
|
||||
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import com.m57.hermescontrol.data.model.PluginInfo
|
||||
import com.m57.hermescontrol.data.model.TogglePluginRequest
|
||||
import com.m57.hermescontrol.data.remote.ApiClient
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.StateFlow
|
||||
import kotlinx.coroutines.flow.asStateFlow
|
||||
import kotlinx.coroutines.flow.update
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withContext
|
||||
|
||||
data class PluginsUiState(
|
||||
val isLoading: Boolean = false,
|
||||
val plugins: List<PluginInfo> = emptyList(),
|
||||
val errorMessage: String? = null,
|
||||
val toastMessage: String? = null,
|
||||
)
|
||||
|
||||
class PluginsViewModel : ViewModel() {
|
||||
private val _uiState = MutableStateFlow(PluginsUiState())
|
||||
val uiState: StateFlow<PluginsUiState> = _uiState.asStateFlow()
|
||||
|
||||
fun loadPlugins() {
|
||||
_uiState.update { it.copy(isLoading = true, errorMessage = null) }
|
||||
viewModelScope.launch {
|
||||
try {
|
||||
val response =
|
||||
withContext(Dispatchers.IO) {
|
||||
ApiClient.hermesApi.getPlugins()
|
||||
}
|
||||
if (response.isSuccessful) {
|
||||
_uiState.update { it.copy(isLoading = false, plugins = response.body().orEmpty()) }
|
||||
} else {
|
||||
_uiState.update {
|
||||
it.copy(
|
||||
isLoading = false,
|
||||
errorMessage = "Failed to load plugins: HTTP ${response.code()}",
|
||||
)
|
||||
}
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
_uiState.update { it.copy(isLoading = false, errorMessage = "Failed to load plugins: ${e.message}") }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun togglePlugin(plugin: PluginInfo) {
|
||||
val originalEnabled = plugin.enabled
|
||||
val targetEnabled = !originalEnabled
|
||||
|
||||
// Optimistically update
|
||||
_uiState.update { state ->
|
||||
state.copy(
|
||||
plugins =
|
||||
state.plugins.map {
|
||||
if (it.name == plugin.name) it.copy(enabled = targetEnabled) else it
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
viewModelScope.launch {
|
||||
try {
|
||||
val response =
|
||||
withContext(Dispatchers.IO) {
|
||||
ApiClient.hermesApi.togglePlugin(plugin.name, TogglePluginRequest(targetEnabled))
|
||||
}
|
||||
if (!response.isSuccessful) {
|
||||
revertPluginToggle(plugin.name, originalEnabled, "Failed to toggle plugin: HTTP ${response.code()}")
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
revertPluginToggle(plugin.name, originalEnabled, "Failed to toggle plugin: ${e.message}")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun installPlugin(name: String) {
|
||||
viewModelScope.launch {
|
||||
try {
|
||||
val response =
|
||||
withContext(Dispatchers.IO) {
|
||||
ApiClient.hermesApi.installPlugin(name)
|
||||
}
|
||||
if (response.isSuccessful) {
|
||||
_uiState.update { it.copy(toastMessage = "Plugin installed successfully") }
|
||||
loadPlugins()
|
||||
} else {
|
||||
_uiState.update { it.copy(toastMessage = "Failed to install plugin: HTTP ${response.code()}") }
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
_uiState.update { it.copy(toastMessage = "Failed to install plugin: ${e.message}") }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun uninstallPlugin(name: String) {
|
||||
viewModelScope.launch {
|
||||
try {
|
||||
val response =
|
||||
withContext(Dispatchers.IO) {
|
||||
ApiClient.hermesApi.uninstallPlugin(name)
|
||||
}
|
||||
if (response.isSuccessful) {
|
||||
_uiState.update { it.copy(toastMessage = "Plugin uninstalled successfully") }
|
||||
loadPlugins()
|
||||
} else {
|
||||
_uiState.update { it.copy(toastMessage = "Failed to uninstall plugin: HTTP ${response.code()}") }
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
_uiState.update { it.copy(toastMessage = "Failed to uninstall plugin: ${e.message}") }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun updatePlugin(name: String) {
|
||||
viewModelScope.launch {
|
||||
try {
|
||||
val response =
|
||||
withContext(Dispatchers.IO) {
|
||||
ApiClient.hermesApi.updatePlugin(name)
|
||||
}
|
||||
if (response.isSuccessful) {
|
||||
_uiState.update { it.copy(toastMessage = "Plugin updated successfully") }
|
||||
loadPlugins()
|
||||
} else {
|
||||
_uiState.update { it.copy(toastMessage = "Failed to update plugin: HTTP ${response.code()}") }
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
_uiState.update { it.copy(toastMessage = "Failed to update plugin: ${e.message}") }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun revertPluginToggle(
|
||||
name: String,
|
||||
originalEnabled: Boolean,
|
||||
errorMsg: String,
|
||||
) {
|
||||
_uiState.update { state ->
|
||||
state.copy(
|
||||
plugins =
|
||||
state.plugins.map {
|
||||
if (it.name == name) it.copy(enabled = originalEnabled) else it
|
||||
},
|
||||
toastMessage = errorMsg,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
fun clearToast() {
|
||||
_uiState.update { it.copy(toastMessage = null) }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,200 @@
|
||||
package com.m57.hermescontrol.ui.system
|
||||
|
||||
import android.widget.Toast
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.Box
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.PaddingValues
|
||||
import androidx.compose.foundation.layout.Row
|
||||
import androidx.compose.foundation.layout.Spacer
|
||||
import androidx.compose.foundation.layout.fillMaxSize
|
||||
import androidx.compose.foundation.layout.fillMaxWidth
|
||||
import androidx.compose.foundation.layout.height
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.layout.width
|
||||
import androidx.compose.foundation.lazy.LazyColumn
|
||||
import androidx.compose.foundation.lazy.items
|
||||
import androidx.compose.material.icons.Icons
|
||||
import androidx.compose.material.icons.filled.Menu
|
||||
import androidx.compose.material3.Button
|
||||
import androidx.compose.material3.Card
|
||||
import androidx.compose.material3.CircularProgressIndicator
|
||||
import androidx.compose.material3.ExperimentalMaterial3Api
|
||||
import androidx.compose.material3.Icon
|
||||
import androidx.compose.material3.IconButton
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.Scaffold
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.material3.TopAppBar
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.LaunchedEffect
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.compose.ui.text.font.FontWeight
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.lifecycle.compose.collectAsStateWithLifecycle
|
||||
import androidx.lifecycle.viewmodel.compose.viewModel
|
||||
|
||||
@OptIn(ExperimentalMaterial3Api::class)
|
||||
@Composable
|
||||
fun SystemScreen(
|
||||
modifier: Modifier = Modifier,
|
||||
onOpenDrawer: (() -> Unit)? = null,
|
||||
viewModel: SystemViewModel = viewModel { SystemViewModel() },
|
||||
) {
|
||||
val state by viewModel.uiState.collectAsStateWithLifecycle()
|
||||
val context = LocalContext.current
|
||||
|
||||
LaunchedEffect(Unit) {
|
||||
viewModel.loadSystemData()
|
||||
}
|
||||
|
||||
LaunchedEffect(state.toastMessage) {
|
||||
state.toastMessage?.let { msg ->
|
||||
Toast.makeText(context, msg, Toast.LENGTH_SHORT).show()
|
||||
viewModel.clearToast()
|
||||
}
|
||||
}
|
||||
|
||||
Scaffold(
|
||||
topBar = {
|
||||
TopAppBar(
|
||||
navigationIcon = {
|
||||
if (onOpenDrawer != null) {
|
||||
IconButton(onClick = onOpenDrawer) {
|
||||
Icon(
|
||||
imageVector = Icons.Filled.Menu,
|
||||
contentDescription = "Open Drawer",
|
||||
)
|
||||
}
|
||||
}
|
||||
},
|
||||
title = { Text("System Diagnostics") },
|
||||
actions = {
|
||||
IconButton(onClick = { viewModel.loadSystemData() }) {
|
||||
Text("Refresh", style = MaterialTheme.typography.bodyMedium)
|
||||
}
|
||||
},
|
||||
)
|
||||
},
|
||||
modifier = modifier,
|
||||
) { paddingValues ->
|
||||
Box(
|
||||
modifier =
|
||||
Modifier
|
||||
.fillMaxSize()
|
||||
.padding(paddingValues),
|
||||
) {
|
||||
if (state.isLoading) {
|
||||
CircularProgressIndicator(modifier = Modifier.align(Alignment.Center))
|
||||
} else if (state.errorMessage != null) {
|
||||
Text(
|
||||
text = state.errorMessage ?: "",
|
||||
color = MaterialTheme.colorScheme.error,
|
||||
modifier =
|
||||
Modifier
|
||||
.align(Alignment.Center)
|
||||
.padding(16.dp),
|
||||
)
|
||||
} else {
|
||||
LazyColumn(
|
||||
modifier = Modifier.fillMaxSize(),
|
||||
contentPadding = PaddingValues(16.dp),
|
||||
verticalArrangement = Arrangement.spacedBy(16.dp),
|
||||
) {
|
||||
item {
|
||||
Card(modifier = Modifier.fillMaxWidth()) {
|
||||
Column(modifier = Modifier.padding(16.dp)) {
|
||||
Text(
|
||||
text = "System Administration",
|
||||
style = MaterialTheme.typography.titleLarge,
|
||||
fontWeight = FontWeight.Bold,
|
||||
)
|
||||
Spacer(modifier = Modifier.height(8.dp))
|
||||
Button(
|
||||
onClick = { viewModel.triggerBackup() },
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
) {
|
||||
Text("Trigger System Backup")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// System Stats (Mock structure check)
|
||||
state.stats?.let { stats ->
|
||||
item {
|
||||
Card(modifier = Modifier.fillMaxWidth()) {
|
||||
Column(modifier = Modifier.padding(16.dp)) {
|
||||
Text(
|
||||
text = "Performance Stats",
|
||||
style = MaterialTheme.typography.titleLarge,
|
||||
fontWeight = FontWeight.Bold,
|
||||
)
|
||||
Spacer(modifier = Modifier.height(8.dp))
|
||||
stats.cpuPercent?.let {
|
||||
Text(text = "CPU Usage: $it%", style = MaterialTheme.typography.bodyMedium)
|
||||
}
|
||||
stats.memoryPercent?.let {
|
||||
Text(text = "Memory Usage: $it%", style = MaterialTheme.typography.bodyMedium)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Doctor reports
|
||||
state.doctorReport?.let { report ->
|
||||
item {
|
||||
Text(
|
||||
text = "Doctor Diagnostic Reports (Status: ${report.status})",
|
||||
style = MaterialTheme.typography.titleMedium,
|
||||
fontWeight = FontWeight.Bold,
|
||||
modifier = Modifier.padding(horizontal = 4.dp),
|
||||
)
|
||||
}
|
||||
|
||||
items(report.reports) { diagnosis ->
|
||||
Card(modifier = Modifier.fillMaxWidth()) {
|
||||
Row(
|
||||
modifier =
|
||||
Modifier
|
||||
.fillMaxWidth()
|
||||
.padding(16.dp),
|
||||
horizontalArrangement = Arrangement.SpaceBetween,
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
) {
|
||||
Column(modifier = Modifier.weight(1f)) {
|
||||
Text(
|
||||
text = diagnosis.category.uppercase(),
|
||||
style = MaterialTheme.typography.titleMedium,
|
||||
fontWeight = FontWeight.SemiBold,
|
||||
)
|
||||
Spacer(modifier = Modifier.height(4.dp))
|
||||
Text(text = diagnosis.message, style = MaterialTheme.typography.bodyMedium)
|
||||
}
|
||||
Spacer(modifier = Modifier.width(8.dp))
|
||||
Text(
|
||||
text = diagnosis.status.uppercase(),
|
||||
color =
|
||||
if (diagnosis.status.lowercase() == "ok" ||
|
||||
diagnosis.status.lowercase() == "success"
|
||||
) {
|
||||
MaterialTheme.colorScheme.primary
|
||||
} else {
|
||||
MaterialTheme.colorScheme.error
|
||||
},
|
||||
fontWeight = FontWeight.Bold,
|
||||
style = MaterialTheme.typography.titleMedium,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
package com.m57.hermescontrol.ui.system
|
||||
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import com.m57.hermescontrol.data.model.DoctorResponse
|
||||
import com.m57.hermescontrol.data.model.SystemStatsResponse
|
||||
import com.m57.hermescontrol.data.remote.ApiClient
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.StateFlow
|
||||
import kotlinx.coroutines.flow.asStateFlow
|
||||
import kotlinx.coroutines.flow.update
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withContext
|
||||
|
||||
data class SystemUiState(
|
||||
val isLoading: Boolean = false,
|
||||
val stats: SystemStatsResponse? = null,
|
||||
val doctorReport: DoctorResponse? = null,
|
||||
val errorMessage: String? = null,
|
||||
val toastMessage: String? = null,
|
||||
)
|
||||
|
||||
class SystemViewModel : ViewModel() {
|
||||
private val _uiState = MutableStateFlow(SystemUiState())
|
||||
val uiState: StateFlow<SystemUiState> = _uiState.asStateFlow()
|
||||
|
||||
fun loadSystemData() {
|
||||
_uiState.update { it.copy(isLoading = true, errorMessage = null) }
|
||||
viewModelScope.launch {
|
||||
try {
|
||||
val statsResponse =
|
||||
withContext(Dispatchers.IO) {
|
||||
ApiClient.hermesApi.getSystemStats()
|
||||
}
|
||||
val doctorResponse =
|
||||
withContext(Dispatchers.IO) {
|
||||
ApiClient.hermesApi.runDoctor()
|
||||
}
|
||||
|
||||
if (statsResponse.isSuccessful && doctorResponse.isSuccessful) {
|
||||
_uiState.update {
|
||||
it.copy(
|
||||
isLoading = false,
|
||||
stats = statsResponse.body(),
|
||||
doctorReport = doctorResponse.body(),
|
||||
)
|
||||
}
|
||||
} else {
|
||||
_uiState.update {
|
||||
it.copy(
|
||||
isLoading = false,
|
||||
errorMessage =
|
||||
"Failed to load system data: HTTP " +
|
||||
"${statsResponse.code()} / ${doctorResponse.code()}",
|
||||
)
|
||||
}
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
_uiState.update {
|
||||
it.copy(
|
||||
isLoading = false,
|
||||
errorMessage = "Failed to load system data: ${e.message}",
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun triggerBackup() {
|
||||
viewModelScope.launch {
|
||||
try {
|
||||
val response =
|
||||
withContext(Dispatchers.IO) {
|
||||
ApiClient.hermesApi.triggerBackup()
|
||||
}
|
||||
if (response.isSuccessful) {
|
||||
_uiState.update { it.copy(toastMessage = "Backup triggered successfully") }
|
||||
} else {
|
||||
_uiState.update { it.copy(toastMessage = "Failed to trigger backup: HTTP ${response.code()}") }
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
_uiState.update { it.copy(toastMessage = "Failed to trigger backup: ${e.message}") }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun clearToast() {
|
||||
_uiState.update { it.copy(toastMessage = null) }
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user