Implement Logs, Plugins, Channels, Keys, System, and Kanban screens with full integration tests and code formatting checks

This commit is contained in:
Hy4ri committed 2026-06-16 00:13:18 +03:00
1 parent cffb68550e
commit 03916014c0
22 files changed
+2272 -30

No files matched your search

@@ -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) }
}
}