1package soy.iko.opencode.data.repo
3import android.content.Context
4import kotlinx.coroutines.CoroutineScope
5import kotlinx.coroutines.launch
8 * Persists the in-progress message draft for each session so it survives
9 * back-navigation and process death. Backed by SharedPreferences because the
10 * UI needs the initial value synchronously and drafts are low-stakes data.
12 * The prefs file is loaded on a background thread at construction time so the
13 * first [get] call on the main thread doesn't trigger a synchronous disk read.
15class DraftStore(context: Context, scope: CoroutineScope) {
17 private val appContext = context.applicationContext
18 private val prefs by lazy {
19 appContext.getSharedPreferences("drafts", Context.MODE_PRIVATE)
23 // Eagerly load the prefs file on a background thread so the first main-thread
24 // get() doesn't block on disk I/O. The lazy delegate ensures the same instance.
25 scope.launch { prefs.all }
28 fun get(sessionId: String): String = prefs.getString(sessionId, "").orEmpty()
30 fun set(sessionId: String, text: String) {
32 if (text.isBlank()) remove(sessionId) else putString(sessionId, text)
36 /** Remove the draft for a session (call on session deletion to avoid orphaned entries). */
37 fun remove(sessionId: String) {
38 prefs.edit().remove(sessionId).apply()
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list