Add app/src/main/java/space/bloggin/scribe/Store.kt
This commit is contained in:
@@ -0,0 +1,106 @@
|
||||
package space.bloggin.scribe
|
||||
|
||||
import android.content.Context
|
||||
import org.json.JSONArray
|
||||
import org.json.JSONObject
|
||||
import java.io.File
|
||||
import java.util.UUID
|
||||
|
||||
data class Note(val id: String, var text: String, val created: Long, var updated: Long)
|
||||
|
||||
object Store {
|
||||
private lateinit var file: File
|
||||
private val notes = ArrayList<Note>()
|
||||
private var loaded = false
|
||||
|
||||
@Synchronized
|
||||
fun init(ctx: Context) {
|
||||
if (loaded) return
|
||||
file = File(ctx.applicationContext.filesDir, "notes.json")
|
||||
if (file.exists()) {
|
||||
try {
|
||||
val arr = JSONArray(file.readText())
|
||||
for (i in 0 until arr.length()) {
|
||||
val o = arr.getJSONObject(i)
|
||||
notes.add(Note(o.getString("id"), o.optString("text"), o.optLong("created"), o.optLong("updated")))
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
file.copyTo(File(file.parentFile, "notes.broken.json"), overwrite = true)
|
||||
}
|
||||
}
|
||||
loaded = true
|
||||
}
|
||||
|
||||
@Synchronized
|
||||
private fun persist() {
|
||||
val arr = JSONArray()
|
||||
for (n in notes) {
|
||||
arr.put(JSONObject().put("id", n.id).put("text", n.text).put("created", n.created).put("updated", n.updated))
|
||||
}
|
||||
val tmp = File(file.parentFile, "notes.json.tmp")
|
||||
tmp.writeText(arr.toString())
|
||||
tmp.renameTo(file)
|
||||
}
|
||||
|
||||
@Synchronized
|
||||
fun all(): List<Note> = notes.sortedByDescending { it.updated }
|
||||
|
||||
@Synchronized
|
||||
fun get(id: String): Note? = notes.firstOrNull { it.id == id }
|
||||
|
||||
@Synchronized
|
||||
fun create(text: String = ""): Note {
|
||||
val now = System.currentTimeMillis()
|
||||
val n = Note(UUID.randomUUID().toString(), text, now, now)
|
||||
notes.add(n)
|
||||
persist()
|
||||
return n
|
||||
}
|
||||
|
||||
@Synchronized
|
||||
fun setText(id: String, text: String) {
|
||||
val n = get(id) ?: return
|
||||
if (n.text == text) return
|
||||
n.text = text
|
||||
n.updated = System.currentTimeMillis()
|
||||
persist()
|
||||
}
|
||||
|
||||
@Synchronized
|
||||
fun delete(id: String) {
|
||||
notes.removeAll { it.id == id }
|
||||
persist()
|
||||
}
|
||||
}
|
||||
|
||||
object Prefs {
|
||||
private fun sp(ctx: Context) = ctx.applicationContext.getSharedPreferences("scribe", Context.MODE_PRIVATE)
|
||||
// The speech model spells it "dummy", so that is what it listens for. We show it as "dumby".
|
||||
const val DEFAULT_WAKE = "hey dummy take a note"
|
||||
fun wakeShown(ctx: Context): String {
|
||||
val w = wake(ctx)
|
||||
return if (w == DEFAULT_WAKE) "Hey Dumby, take a note" else w
|
||||
}
|
||||
fun wake(ctx: Context): String = sp(ctx).getString("wake", DEFAULT_WAKE)!!.trim().lowercase().ifEmpty { DEFAULT_WAKE }
|
||||
fun setWake(ctx: Context, v: String) = sp(ctx).edit().putString("wake", v.trim().lowercase()).apply()
|
||||
fun silence(ctx: Context): Int = sp(ctx).getInt("silence", 6)
|
||||
fun setSilence(ctx: Context, v: Int) = sp(ctx).edit().putInt("silence", v.coerceIn(2, 30)).apply()
|
||||
fun beep(ctx: Context) = sp(ctx).getBoolean("beep", true)
|
||||
fun setBeep(ctx: Context, v: Boolean) = sp(ctx).edit().putBoolean("beep", v).apply()
|
||||
fun vibrate(ctx: Context) = sp(ctx).getBoolean("vibrate", true)
|
||||
fun setVibrate(ctx: Context, v: Boolean) = sp(ctx).edit().putBoolean("vibrate", v).apply()
|
||||
fun cpu(ctx: Context) = sp(ctx).getBoolean("cpu", true)
|
||||
fun setCpu(ctx: Context, v: Boolean) = sp(ctx).edit().putBoolean("cpu", v).apply()
|
||||
fun wanted(ctx: Context) = sp(ctx).getBoolean("wanted", false)
|
||||
fun setWanted(ctx: Context, v: Boolean) = sp(ctx).edit().putBoolean("wanted", v).apply()
|
||||
}
|
||||
|
||||
object Bus {
|
||||
@Volatile var state: String = "Not listening"
|
||||
@Volatile var partial: String = ""
|
||||
@Volatile var activeNoteId: String? = null
|
||||
@Volatile var running: Boolean = false
|
||||
val listeners = java.util.concurrent.CopyOnWriteArrayList<() -> Unit>()
|
||||
private val h = android.os.Handler(android.os.Looper.getMainLooper())
|
||||
fun changed() { h.post { for (l in listeners) l() } }
|
||||
}
|
||||
Reference in New Issue
Block a user