first workting version
This commit is contained in:
1 parent
830765cb13
commit
d2048ea5b7
23 files changed
+292
-86
No files matched your search
@@ -5,13 +5,18 @@ import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.unit.DpSize
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.compose.ui.window.*
|
||||
import ch.ergon.anytask.api.AnyTypeClient
|
||||
import ch.ergon.anytask.api.AnyTypeApi
|
||||
import ch.ergon.anytask.config.loadConfig
|
||||
import ch.ergon.anytask.hotkey.HotkeyManager
|
||||
import ch.ergon.anytask.ui.TaskWindow
|
||||
import org.apache.logging.log4j.LogManager
|
||||
import java.awt.*
|
||||
import kotlin.system.exitProcess
|
||||
|
||||
private val logger = LogManager.getLogger("Main")
|
||||
|
||||
fun main() {
|
||||
// Kein Dock-Icon auf macOS (Agent-App)
|
||||
// No dock icon on macOS (agent app)
|
||||
System.setProperty("apple.awt.UIElement", "true")
|
||||
|
||||
application {
|
||||
@@ -22,11 +27,25 @@ fun main() {
|
||||
size = DpSize(480.dp, 320.dp)
|
||||
)
|
||||
|
||||
val client = remember { AnyTypeClient() }
|
||||
val config = remember { loadConfig() }
|
||||
val client = remember { AnyTypeApi(apiKey = config.apiKey, spaceId = config.spaceId) }
|
||||
|
||||
LaunchedEffect(Unit) {
|
||||
HotkeyManager.start(onHotkey = { isVisible = !isVisible })
|
||||
setupSystemTray(onShow = { isVisible = true }, onExit = { exitApplication() })
|
||||
client.getSpaces()
|
||||
.onSuccess { spaces ->
|
||||
logger.info("Available spaces:")
|
||||
spaces.forEach { logger.info("Space: ${it.id} ${it.name}") }
|
||||
val space = spaces.find { space -> space.id == config.spaceId }
|
||||
if (space == null) {
|
||||
logger.error("Selected space not found: ${config.spaceId}")
|
||||
exitProcess(-1)
|
||||
} else {
|
||||
logger.info("Selected space: ${space.name} (${space.id})")
|
||||
}
|
||||
}
|
||||
.onFailure { logger.error("Failed to load spaces: ${it.message}") }
|
||||
}
|
||||
|
||||
DisposableEffect(Unit) {
|
||||
@@ -57,14 +76,14 @@ private fun setupSystemTray(onShow: () -> Unit, onExit: () -> Unit) {
|
||||
val icon = createTrayIcon(size)
|
||||
|
||||
val menu = PopupMenu().apply {
|
||||
add(MenuItem("Öffnen").also { it.addActionListener { onShow() } })
|
||||
add(MenuItem("Open").also { it.addActionListener { onShow() } })
|
||||
addSeparator()
|
||||
add(MenuItem("Beenden").also { it.addActionListener { onExit() } })
|
||||
add(MenuItem("Quit").also { it.addActionListener { onExit() } })
|
||||
}
|
||||
|
||||
val trayIcon = TrayIcon(icon, "AnyTask", menu).apply {
|
||||
isImageAutoSize = true
|
||||
addActionListener { onShow() } // Doppelklick
|
||||
addActionListener { onShow() } // double click
|
||||
}
|
||||
|
||||
SystemTray.getSystemTray().add(trayIcon)
|
||||
|
||||
@@ -0,0 +1,75 @@
|
||||
package ch.ergon.anytask.api
|
||||
|
||||
import ch.ergon.anytask.api.dto.CreateObjectRequest
|
||||
import ch.ergon.anytask.api.dto.CreateObjectResponse
|
||||
import ch.ergon.anytask.api.dto.Space
|
||||
import ch.ergon.anytask.api.dto.SpacesResponse
|
||||
import io.ktor.client.*
|
||||
import io.ktor.client.call.*
|
||||
import io.ktor.client.engine.cio.*
|
||||
import io.ktor.client.plugins.*
|
||||
import io.ktor.client.plugins.contentnegotiation.*
|
||||
import io.ktor.client.request.*
|
||||
import io.ktor.client.statement.*
|
||||
import io.ktor.http.*
|
||||
import io.ktor.serialization.kotlinx.json.*
|
||||
import kotlinx.serialization.json.Json
|
||||
import org.apache.logging.log4j.LogManager
|
||||
import kotlin.time.DurationUnit.MILLISECONDS
|
||||
import kotlin.time.toDuration
|
||||
|
||||
/**
|
||||
* Client for the local AnyType API (anytype-heart middleware).
|
||||
* The API runs on http://localhost:31007 by default.
|
||||
* apiKey and spaceId are loaded from anytask.yaml.
|
||||
*/
|
||||
class AnyTypeApi(
|
||||
private val baseUrl: String = "http://localhost:31009",
|
||||
private val apiKey: String = "",
|
||||
private val spaceId: String = "",
|
||||
) {
|
||||
private val logger = LogManager.getLogger(AnyTypeApi::class.java)
|
||||
|
||||
private val http = HttpClient(CIO) {
|
||||
install(ContentNegotiation) {
|
||||
json(Json { ignoreUnknownKeys = true })
|
||||
}
|
||||
}.also { client ->
|
||||
client.plugin(HttpSend).intercept { request ->
|
||||
val method = request.method.value
|
||||
val url = request.url.buildString()
|
||||
logger.info("→ $method $url")
|
||||
val start = System.currentTimeMillis()
|
||||
val call = execute(request)
|
||||
val duration = (System.currentTimeMillis() - start).toDuration(MILLISECONDS)
|
||||
val status = call.response.status
|
||||
logger.info("← ${status.value} (${status.description}) $duration")
|
||||
if (!status.isSuccess()) {
|
||||
logger.error("Response body: ${call.response.bodyAsText()}")
|
||||
}
|
||||
call
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun getSpaces(): Result<List<Space>> =
|
||||
runCatching {
|
||||
val response = http.get("$baseUrl/v1/spaces") {
|
||||
header("Anytype-Version", "0.1")
|
||||
bearerAuth(apiKey)
|
||||
}
|
||||
response.body<SpacesResponse>().data
|
||||
}
|
||||
|
||||
suspend fun createTask(title: String, details: String = ""): Result<String> =
|
||||
runCatching {
|
||||
val response = http.post("$baseUrl/v1/spaces/$spaceId/objects") {
|
||||
contentType(ContentType.Application.Json)
|
||||
header("Anytype-Version", "0.1")
|
||||
bearerAuth(apiKey)
|
||||
setBody(CreateObjectRequest(name = title, body = details, typeKey = "ot-task"))
|
||||
}
|
||||
response.body<CreateObjectResponse>().objectId
|
||||
}
|
||||
|
||||
fun close() = http.close()
|
||||
}
|
||||
@@ -1,53 +0,0 @@
|
||||
package ch.ergon.anytask.api
|
||||
|
||||
import io.ktor.client.*
|
||||
import io.ktor.client.call.*
|
||||
import io.ktor.client.engine.cio.*
|
||||
import io.ktor.client.plugins.contentnegotiation.*
|
||||
import io.ktor.client.request.*
|
||||
import io.ktor.http.*
|
||||
import io.ktor.serialization.kotlinx.json.*
|
||||
import kotlinx.serialization.Serializable
|
||||
import kotlinx.serialization.json.Json
|
||||
|
||||
/**
|
||||
* Client für die lokale AnyType-API (anytype-heart Middleware).
|
||||
* Die API läuft standardmässig auf http://localhost:31007.
|
||||
*
|
||||
* TODO: App-Key via Einstellungen konfigurierbar machen.
|
||||
*/
|
||||
class AnyTypeClient(
|
||||
private val baseUrl: String = "http://localhost:31007",
|
||||
private val appKey: String = "", // wird nach erstem Login gesetzt
|
||||
) {
|
||||
private val http = HttpClient(CIO) {
|
||||
install(ContentNegotiation) {
|
||||
json(Json { ignoreUnknownKeys = true })
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun createTask(title: String, details: String = "", spaceId: String = ""): Result<String> =
|
||||
runCatching {
|
||||
val response = http.post("$baseUrl/v1/spaces/$spaceId/objects") {
|
||||
contentType(ContentType.Application.Json)
|
||||
header("Anytype-Version", "0.1")
|
||||
bearerAuth(appKey)
|
||||
setBody(CreateObjectRequest(title = title, details = details, typeKey = "ot-task"))
|
||||
}
|
||||
response.body<CreateObjectResponse>().objectId
|
||||
}
|
||||
|
||||
fun close() = http.close()
|
||||
}
|
||||
|
||||
@Serializable
|
||||
private data class CreateObjectRequest(
|
||||
val title: String,
|
||||
val details: String,
|
||||
val typeKey: String,
|
||||
)
|
||||
|
||||
@Serializable
|
||||
private data class CreateObjectResponse(
|
||||
val objectId: String,
|
||||
)
|
||||
@@ -0,0 +1,11 @@
|
||||
package ch.ergon.anytask.api.dto
|
||||
|
||||
import kotlinx.serialization.SerialName
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
@Serializable
|
||||
data class CreateObjectRequest(
|
||||
val name: String,
|
||||
val body: String,
|
||||
@SerialName("type_key") val typeKey: String,
|
||||
)
|
||||
@@ -0,0 +1,8 @@
|
||||
package ch.ergon.anytask.api.dto
|
||||
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
@Serializable
|
||||
data class CreateObjectResponse(
|
||||
val objectId: String,
|
||||
)
|
||||
@@ -0,0 +1,9 @@
|
||||
package ch.ergon.anytask.api.dto
|
||||
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
@Serializable
|
||||
data class Space(
|
||||
val id: String,
|
||||
val name: String,
|
||||
)
|
||||
@@ -0,0 +1,8 @@
|
||||
package ch.ergon.anytask.api.dto
|
||||
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
@Serializable
|
||||
data class SpacesResponse(
|
||||
val data: List<Space>,
|
||||
)
|
||||
@@ -0,0 +1,32 @@
|
||||
package ch.ergon.anytask.config
|
||||
|
||||
import com.charleskorn.kaml.Yaml
|
||||
import kotlinx.serialization.Serializable
|
||||
import java.io.File
|
||||
|
||||
@Serializable
|
||||
data class AppConfig(
|
||||
val apiKey: String = "",
|
||||
val spaceId: String = "",
|
||||
)
|
||||
|
||||
fun loadConfig(): AppConfig {
|
||||
val yaml = Yaml.default
|
||||
val serializer = AppConfig.serializer()
|
||||
|
||||
// External files take precedence (production use)
|
||||
val externalLocations = listOf(
|
||||
File(System.getProperty("user.home"), ".anytask/anytask.yaml"),
|
||||
File("anytask.yaml"),
|
||||
)
|
||||
externalLocations.firstOrNull { it.exists() }?.let {
|
||||
return yaml.decodeFromString(serializer, it.readText())
|
||||
}
|
||||
|
||||
// Fallback: classpath resource (development)
|
||||
AppConfig::class.java.getResourceAsStream("/anytask.yaml")?.let {
|
||||
return yaml.decodeFromString(serializer, it.bufferedReader().readText())
|
||||
}
|
||||
|
||||
return AppConfig()
|
||||
}
|
||||
@@ -4,20 +4,22 @@ import com.github.kwhat.jnativehook.GlobalScreen
|
||||
import com.github.kwhat.jnativehook.NativeHookException
|
||||
import com.github.kwhat.jnativehook.keyboard.NativeKeyEvent
|
||||
import com.github.kwhat.jnativehook.keyboard.NativeKeyListener
|
||||
import org.apache.logging.log4j.LogManager
|
||||
import java.awt.EventQueue
|
||||
import java.util.logging.Level
|
||||
import java.util.logging.Logger
|
||||
|
||||
/**
|
||||
* Lauscht auf eine systemweite Tastenkombination: Ctrl+Shift+Space
|
||||
* Erfordert auf macOS die Berechtigung "Eingabegeräte überwachen" (Accessibility).
|
||||
* Listens for a system-wide hotkey: Ctrl+Shift+Space.
|
||||
* Requires the Accessibility permission on macOS.
|
||||
*/
|
||||
object HotkeyManager {
|
||||
|
||||
private val logger = LogManager.getLogger(HotkeyManager::class.java)
|
||||
private var listener: NativeKeyListener? = null
|
||||
|
||||
fun start(onHotkey: () -> Unit) {
|
||||
// JNativeHook-Logging unterdrücken
|
||||
// Suppress JNativeHook logging
|
||||
Logger.getLogger(GlobalScreen::class.java.`package`.name).apply {
|
||||
level = Level.OFF
|
||||
useParentHandlers = false
|
||||
@@ -26,14 +28,14 @@ object HotkeyManager {
|
||||
try {
|
||||
GlobalScreen.registerNativeHook()
|
||||
} catch (e: NativeHookException) {
|
||||
System.err.println("JNativeHook konnte nicht registriert werden: ${e.message}")
|
||||
logger.error("Failed to register JNativeHook: ${e.message}")
|
||||
return
|
||||
}
|
||||
|
||||
listener = object : NativeKeyListener {
|
||||
override fun nativeKeyPressed(e: NativeKeyEvent) {
|
||||
if (isHotkey(e)) {
|
||||
// Callback auf den AWT-EDT dispatchen (Compose läuft dort)
|
||||
// Dispatch callback on the AWT EDT (where Compose runs)
|
||||
EventQueue.invokeLater(onHotkey)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,11 +9,11 @@ import androidx.compose.ui.focus.FocusRequester
|
||||
import androidx.compose.ui.focus.focusRequester
|
||||
import androidx.compose.ui.input.key.*
|
||||
import androidx.compose.ui.unit.dp
|
||||
import ch.ergon.anytask.api.AnyTypeClient
|
||||
import ch.ergon.anytask.api.AnyTypeApi
|
||||
import kotlinx.coroutines.launch
|
||||
|
||||
@Composable
|
||||
fun TaskWindow(client: AnyTypeClient, onDismiss: () -> Unit) {
|
||||
fun TaskWindow(client: AnyTypeApi, onDismiss: () -> Unit) {
|
||||
val scope = rememberCoroutineScope()
|
||||
var title by remember { mutableStateOf("") }
|
||||
var details by remember { mutableStateOf("") }
|
||||
@@ -58,12 +58,12 @@ fun TaskWindow(client: AnyTypeClient, onDismiss: () -> Unit) {
|
||||
},
|
||||
verticalArrangement = Arrangement.spacedBy(12.dp),
|
||||
) {
|
||||
Text("Neue Aufgabe", style = MaterialTheme.typography.h6)
|
||||
Text("New Task", style = MaterialTheme.typography.h6)
|
||||
|
||||
OutlinedTextField(
|
||||
value = title,
|
||||
onValueChange = { title = it },
|
||||
label = { Text("Titel") },
|
||||
label = { Text("Title") },
|
||||
singleLine = true,
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
@@ -86,10 +86,10 @@ fun TaskWindow(client: AnyTypeClient, onDismiss: () -> Unit) {
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
horizontalArrangement = Arrangement.spacedBy(8.dp, Alignment.End),
|
||||
) {
|
||||
TextButton(onClick = onDismiss) { Text("Abbrechen") }
|
||||
TextButton(onClick = onDismiss) { Text("Cancel") }
|
||||
Button(onClick = ::submit, enabled = title.isNotBlank() && !isLoading) {
|
||||
if (isLoading) CircularProgressIndicator(modifier = Modifier.size(16.dp), strokeWidth = 2.dp)
|
||||
else Text("Erstellen ⌘↵")
|
||||
else Text("Create ⌘↵")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
apiKey: 2tkMSzFegWwzsQ+n9SpGwVjychEkCESWjV1/ftytXG4=
|
||||
spaceId: bafyreiafzvfsrurvx4gvr2wwysgp5uugujzmbdg5hxnjlahfmx3ww6kxfy.172mmtcppmjtt
|
||||
@@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<Configuration status="WARN">
|
||||
<Appenders>
|
||||
<Console name="Console" target="SYSTEM_OUT">
|
||||
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
|
||||
</Console>
|
||||
</Appenders>
|
||||
<Loggers>
|
||||
<Root level="info">
|
||||
<AppenderRef ref="Console"/>
|
||||
</Root>
|
||||
</Loggers>
|
||||
</Configuration>
|
||||