Implement Message Content Copy to Clipboard #148

Closed
opened 2025-12-25 13:54:40 +00:00 by rwachters · 0 comments
rwachters commented 2025-12-25 13:54:40 +00:00 (Migrated from github.com)

Overview

Wire up the existing "Copy" button in the message actions menu to actually copy message content to the clipboard. This requires creating a custom ClipboardService with platform-specific implementations and extending the notification system to show success messages.

Implementation Plan

1. Extend Notification System

  • Create GenericAppSuccess event class in app/src/commonMain/kotlin/eu/torvian/chatbot/app/domain/events/GenericAppSuccess.kt extending AppSuccess, following the same pattern as GenericAppWarning
  • Rename ErrorNotifier to NotificationService in app/src/commonMain/kotlin/eu/torvian/chatbot/app/viewmodel/common/ErrorNotifier.kt
  • Add genericSuccess(shortMessage: String) method that emits GenericAppSuccess events via EventBus
  • Update all references to ErrorNotifier throughout the codebase (Koin modules, ViewModels, repositories) to use the new NotificationService name

2. Create ClipboardService

  • Create ClipboardService interface in app/src/commonMain/kotlin/eu/torvian/chatbot/app/service/clipboard/ClipboardService.kt with a suspend fun copyToClipboard(text: String) method
  • Implement ClipboardServiceDesktop.kt in desktopMain using java.awt.Toolkit.getDefaultToolkit().systemClipboard
  • Implement ClipboardServiceAndroid.kt in androidMain using Android's ClipboardManager
  • Implement ClipboardServiceWasmJs.kt in wasmJsMain using browser's navigator.clipboard.writeText

3. Register in Koin DI

  • Register ClipboardService in Koin DI modules using expect/actual pattern in app/src/commonMain/kotlin/eu/torvian/chatbot/app/koin/ServiceModule.kt and corresponding platform-specific modules

4. Wire to ViewModel

  • Add copyMessageToClipboard(message: ChatMessage) method to ChatViewModel
  • Inject ClipboardService and NotificationService into the ViewModel
  • Launch a coroutine calling clipboardService.copyToClipboard(message.content) with try-catch for error handling
  • Show "Message copied to clipboard" on success or "Failed to copy to clipboard" on error

5. Connect UI

  • Wire chatViewModel::copyMessageToClipboard to ChatAreaActions.onCopyMessage in the anonymous object implementation around line 183 of ChatScreen.kt
  • Update ChatArea.kt line 135 to pass the action instead of null to enable the Copy button

Notes

  • Keep it simple for now - no string constants or special Android permission checks
  • Use try-catch for error handling across all platforms
  • Success/error messages can be hardcoded strings initially
  • MessageActionRow.kt - Contains the existing Copy button UI
  • MessageActions.kt - Contains the action data class with onCopyMessage callback
  • ChatArea.kt - Currently passes null for onCopyMessage (line 135)
  • ChatScreen.kt - Where actions are wired to the ViewModel
## Overview Wire up the existing "Copy" button in the message actions menu to actually copy message content to the clipboard. This requires creating a custom `ClipboardService` with platform-specific implementations and extending the notification system to show success messages. ## Implementation Plan ### 1. Extend Notification System - Create `GenericAppSuccess` event class in `app/src/commonMain/kotlin/eu/torvian/chatbot/app/domain/events/GenericAppSuccess.kt` extending `AppSuccess`, following the same pattern as `GenericAppWarning` - Rename `ErrorNotifier` to `NotificationService` in `app/src/commonMain/kotlin/eu/torvian/chatbot/app/viewmodel/common/ErrorNotifier.kt` - Add `genericSuccess(shortMessage: String)` method that emits `GenericAppSuccess` events via `EventBus` - Update all references to `ErrorNotifier` throughout the codebase (Koin modules, ViewModels, repositories) to use the new `NotificationService` name ### 2. Create ClipboardService - Create `ClipboardService` interface in `app/src/commonMain/kotlin/eu/torvian/chatbot/app/service/clipboard/ClipboardService.kt` with a `suspend fun copyToClipboard(text: String)` method - Implement `ClipboardServiceDesktop.kt` in `desktopMain` using `java.awt.Toolkit.getDefaultToolkit().systemClipboard` - Implement `ClipboardServiceAndroid.kt` in `androidMain` using Android's `ClipboardManager` - Implement `ClipboardServiceWasmJs.kt` in `wasmJsMain` using browser's `navigator.clipboard.writeText` ### 3. Register in Koin DI - Register `ClipboardService` in Koin DI modules using `expect`/`actual` pattern in `app/src/commonMain/kotlin/eu/torvian/chatbot/app/koin/ServiceModule.kt` and corresponding platform-specific modules ### 4. Wire to ViewModel - Add `copyMessageToClipboard(message: ChatMessage)` method to `ChatViewModel` - Inject `ClipboardService` and `NotificationService` into the ViewModel - Launch a coroutine calling `clipboardService.copyToClipboard(message.content)` with try-catch for error handling - Show "Message copied to clipboard" on success or "Failed to copy to clipboard" on error ### 5. Connect UI - Wire `chatViewModel::copyMessageToClipboard` to `ChatAreaActions.onCopyMessage` in the anonymous object implementation around line 183 of `ChatScreen.kt` - Update `ChatArea.kt` line 135 to pass the action instead of `null` to enable the Copy button ## Notes - Keep it simple for now - no string constants or special Android permission checks - Use try-catch for error handling across all platforms - Success/error messages can be hardcoded strings initially ## Related Files - `MessageActionRow.kt` - Contains the existing Copy button UI - `MessageActions.kt` - Contains the action data class with `onCopyMessage` callback - `ChatArea.kt` - Currently passes `null` for `onCopyMessage` (line 135) - `ChatScreen.kt` - Where actions are wired to the ViewModel
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
Torvian.eu/chatbot#148
No description provided.