Skip to content

Commit 1fa945f

Browse files
committed
Add Desktop template update action
1 parent 6717d3c commit 1fa945f

2 files changed

Lines changed: 54 additions & 5 deletions

File tree

desktopApp/src/main/kotlin/me/rosuh/easywatermark/desktop/DesktopWindow.kt

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import kotlinx.coroutines.Dispatchers
3737
import kotlinx.coroutines.flow.first
3838
import kotlinx.coroutines.launch
3939
import kotlinx.coroutines.withContext
40+
import kotlin.time.Clock
4041
import me.rosuh.easywatermark.data.db.buildTemplateDatabase
4142
import me.rosuh.easywatermark.data.db.unpackDefaultTemplateSeed
4243
import me.rosuh.easywatermark.data.model.ImageFormat
@@ -179,7 +180,8 @@ private fun resolveDesktopOutputDir(): File {
179180
* onto the window loads it through the same Open-image save spine (`Modifier.dragAndDropTarget` + the AWT
180181
* file-list flavor; updates `lastImage` + `lastSavedFile`; unsupported/empty/while-busy drops fail softly).
181182
* S4d-160: a minimal "Templates" section over the shared Desktop Room path saves the current watermark
182-
* text, lists saved templates, and applies (Use → `WatermarkConfigEditor.updateText`) or deletes them.
183+
* text, lists saved templates, and applies (Use → `WatermarkConfigEditor.updateText`), updates in place
184+
* (Update → `TemplateEditor.update`), or deletes them.
183185
* S4d-198: REACTIVE preview — every successful explicit editor action (Apply text/degree/color/opacity/
184186
* gaps/text-size, the tile/typeface/style buttons, "Use text watermark", and template "Use") now
185187
* auto-refreshes the on-screen preview through the SAME `refreshPreview()` → `runSaveFlow` temp-file spine
@@ -1059,10 +1061,12 @@ fun launchDesktopWindow() = application {
10591061
},
10601062
) { Text("Copy output path") }
10611063
}
1062-
// S4d-160: minimal Templates section over the shared Desktop Room template path. "Save current
1063-
// text" stores the edited watermark text (templateEditor.add); each saved row applies it (Use →
1064-
// WatermarkConfigEditor.updateText + sync the text field) or deletes it. S4d-198: Use auto-refreshes
1065-
// the preview after a successful apply. `content` is the watermark TEXT (S4d-159).
1064+
// S4d-160/S4d-226: minimal Templates section over the shared Desktop Room template path. "Save
1065+
// current text" stores the edited watermark text (templateEditor.add); each saved row applies it
1066+
// (Use → WatermarkConfigEditor.updateText + sync the text field), updates in place from the
1067+
// current watermark text (Update → TemplateEditor.update, preserving id/creationDate), or deletes
1068+
// it. S4d-198: Use auto-refreshes the preview after a successful apply. `content` is the watermark
1069+
// TEXT (S4d-159).
10661070
Text("Templates", style = MaterialTheme.typography.subtitle1)
10671071
Button(
10681072
// S4d-162: require nonblank text so an empty template can't be saved.
@@ -1121,6 +1125,31 @@ fun launchDesktopWindow() = application {
11211125
}
11221126
},
11231127
) { Text("Use") }
1128+
Button(
1129+
// S4d-226: update the existing row from the current watermark text.
1130+
enabled = !busy && watermarkText.isNotBlank(),
1131+
onClick = {
1132+
val text = watermarkText
1133+
scope.launch {
1134+
busy = true
1135+
val next = withContext(Dispatchers.IO) {
1136+
try {
1137+
templateEditor.update(
1138+
template.copy(
1139+
content = text,
1140+
lastModifiedDate = Clock.System.now(),
1141+
)
1142+
)
1143+
"Updated template to: \"$text\""
1144+
} catch (t: Throwable) {
1145+
"Failed: ${t.message}"
1146+
}
1147+
}
1148+
status = next
1149+
busy = false
1150+
}
1151+
},
1152+
) { Text("Update") }
11241153
Button(
11251154
enabled = !busy,
11261155
onClick = {

shared/src/desktopTest/kotlin/me/rosuh/easywatermark/data/db/TemplateRoundtripTest.kt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import kotlin.test.Test
1212
import kotlin.test.assertEquals
1313
import kotlin.test.assertFalse
1414
import kotlin.test.assertTrue
15+
import kotlin.time.Clock
1516

1617
/**
1718
* S4d-142: Desktop (JVM) templates roundtrip over the commonMain Room path, built by the new desktopMain
@@ -51,4 +52,23 @@ class TemplateRoundtripTest {
5152
editor.delete(afterAdd[0])
5253
assertTrue(repo.getAllTemplate().first().isEmpty(), "store is empty again after delete")
5354
}
55+
56+
@Test
57+
fun update_preserves_id_and_creation_date() = runBlocking {
58+
editor.add("original content")
59+
val afterAdd = repo.getAllTemplate().first()
60+
assertEquals(1, afterAdd.size, "exactly one template after add")
61+
62+
val original = afterAdd[0]
63+
val originalId = original.id
64+
val originalCreationDate = original.creationDate
65+
assertTrue(originalId != 0, "autoGenerate assigned a row id")
66+
67+
editor.update(original.copy(content = "updated content", lastModifiedDate = Clock.System.now()))
68+
val afterUpdate = repo.getAllTemplate().first()
69+
assertEquals(1, afterUpdate.size, "still exactly one template after update")
70+
assertEquals(originalId, afterUpdate[0].id, "update preserves the row id")
71+
assertEquals("updated content", afterUpdate[0].content, "update changes the content")
72+
assertEquals(originalCreationDate, afterUpdate[0].creationDate, "update preserves the creation date")
73+
}
5474
}

0 commit comments

Comments
 (0)