@@ -6,6 +6,7 @@ import kotlinx.coroutines.Dispatchers
66import kotlinx.coroutines.flow.MutableStateFlow
77import kotlinx.coroutines.flow.StateFlow
88import kotlinx.coroutines.flow.asStateFlow
9+import kotlinx.coroutines.flow.update
910import kotlinx.coroutines.launch
1011import kotlinx.coroutines.withContext
1112
@@ -51,9 +52,11 @@ class DraftStore(context: Context, scope: CoroutineScope) {
5152 fun get(sessionId: String): String = _drafts.value[sessionId].orEmpty()
5253
5354 suspend fun set(sessionId: String, text: String) {
54− val current = _drafts.value.toMutableMap()
55− if (text.isBlank()) current.remove(sessionId) else current[sessionId] = text
56− _drafts.value = current
55+ _drafts.update { current ->
56+ val updated = current.toMutableMap()
57+ if (text.isBlank()) updated.remove(sessionId) else updated[sessionId] = text
58+ updated
59+ }
5760 withContext(Dispatchers.IO) {
5861 prefs.edit().apply {
5962 if (text.isBlank()) remove(sessionId) else putString(sessionId, text)
@@ -63,9 +66,11 @@ class DraftStore(context: Context, scope: CoroutineScope) {
6366
6467 /** Remove the draft for a session (call on session deletion to avoid orphaned entries). */
6568 suspend fun remove(sessionId: String) {
66− val current = _drafts.value.toMutableMap()
67− current.remove(sessionId)
68− _drafts.value = current
69+ _drafts.update { current ->
70+ val updated = current.toMutableMap()
71+ updated.remove(sessionId)
72+ updated
73+ }
6974 withContext(Dispatchers.IO) {
7075 prefs.edit().remove(sessionId).apply()
7176 }