package com.example.aiapp import android.content.Context import androidx.core.content.edit private const val DRAFTS = "session-drafts" /** * A message typed into a session and not sent yet. * * On this device rather than on the backend, which is where this app otherwise keeps state so that * every device sees it. A draft is the case that rule is not about: it is the contents of a text * box on the phone somebody is holding, and half a sentence surfacing on another device would be a * surprise. What has been *sent* is the server's. * * Kept per session id: one shared box would hand a message meant for one session to whichever was * opened next. */ fun loadDraft(context: Context, sessionId: String): String = context.getSharedPreferences(DRAFTS, Context.MODE_PRIVATE).getString(sessionId, "").orEmpty() /** * Records [text] as the draft for [sessionId], or forgets it when there is nothing left to keep. * * The path out is emptying the box, which is what sending does. A session *deleted* while it held a * draft does leave its key behind: pruning those means a pass over the live session list, and the * residue is a few bytes per session ever abandoned mid-sentence. */ fun saveDraft(context: Context, sessionId: String, text: String) { context.getSharedPreferences(DRAFTS, Context.MODE_PRIVATE).edit { if (text.isEmpty()) remove(sessionId) else putString(sessionId, text) } }