@@ -6,6 +6,7 @@ import kotlinx.coroutines.Dispatchers
6import kotlinx.coroutines.flow.MutableStateFlow
6import kotlinx.coroutines.flow.MutableStateFlow
7import kotlinx.coroutines.flow.StateFlow
7import kotlinx.coroutines.flow.StateFlow
8import kotlinx.coroutines.flow.asStateFlow
8import kotlinx.coroutines.flow.asStateFlow
9+import kotlinx.coroutines.flow.update
9import kotlinx.coroutines.launch
10import kotlinx.coroutines.launch
10import kotlinx.coroutines.withContext
11import kotlinx.coroutines.withContext
@@ -51,9 +52,11 @@ class DraftStore(context: Context, scope: CoroutineScope) {
51 fun get(sessionId: String): String = _drafts.value[sessionId].orEmpty()
52 fun get(sessionId: String): String = _drafts.value[sessionId].orEmpty()
53 suspend fun set(sessionId: String, text: String) {
54 suspend fun set(sessionId: String, text: String) {
55+ _drafts.update { current ->
54− val current = _drafts.value.toMutableMap()
56+ val updated = current.toMutableMap()
55− if (text.isBlank()) current.remove(sessionId) else current[sessionId] = text
57+ if (text.isBlank()) updated.remove(sessionId) else updated[sessionId] = text
56− _drafts.value = current
58+ updated
57 withContext(Dispatchers.IO) {
60 withContext(Dispatchers.IO) {
58 prefs.edit().apply {
61 prefs.edit().apply {
59 if (text.isBlank()) remove(sessionId) else putString(sessionId, text)
62 if (text.isBlank()) remove(sessionId) else putString(sessionId, text)
@@ -63,9 +66,11 @@ class DraftStore(context: Context, scope: CoroutineScope) {
64 /** Remove the draft for a session (call on session deletion to avoid orphaned entries). */
67 /** Remove the draft for a session (call on session deletion to avoid orphaned entries). */
65 suspend fun remove(sessionId: String) {
68 suspend fun remove(sessionId: String) {
69+ _drafts.update { current ->
66− val current = _drafts.value.toMutableMap()
70+ val updated = current.toMutableMap()
67− current.remove(sessionId)
71+ updated.remove(sessionId)
68− _drafts.value = current
72+ updated
69 withContext(Dispatchers.IO) {
74 withContext(Dispatchers.IO) {
70 prefs.edit().remove(sessionId).apply()
75 prefs.edit().remove(sessionId).apply()