app/src/main/kotlin/space/subread/anki/Store.kt (2843 bytes)
1 package space.subread.anki 2 3 import android.content.Context 4 import androidx.core.content.edit 5 import space.subread.anki.core.Source 6 7 /** What the user chose. It stays when the app stops. */ 8 class Store(context: Context) { 9 10 private val prefs = context.getSharedPreferences("anki", Context.MODE_PRIVATE) 11 12 /** The deck in AnkiDroid. [NONE] until the user chose one: the app then makes "SubRead". */ 13 var deckId: Long 14 get() = prefs.getLong("deck_id", NONE) 15 set(value) = prefs.edit { putLong("deck_id", value) } 16 17 /** The note type in AnkiDroid. [NONE]: the note type that the app makes. */ 18 var modelId: Long 19 get() = prefs.getLong("model_id", NONE) 20 set(value) = prefs.edit { putLong("model_id", value) } 21 22 /** What fills each field of the chosen note type. Empty: the app guesses from the field names. */ 23 var mapping: Map<String, Source> 24 get() = prefs.getString("mapping", "").orEmpty().lineSequence().mapNotNull { line -> 25 val tab = line.indexOf('\t') 26 if (tab < 0) null else line.substring(0, tab) to (runCatching { Source.valueOf(line.substring(tab + 1)) }.getOrNull() ?: Source.NONE) 27 }.toMap() 28 set(value) = prefs.edit { putString("mapping", value.entries.joinToString("\n") { (field, source) -> "$field\t${source.name}" }) } 29 30 /** Anki tags for each card, separated by spaces. */ 31 var tags: String 32 get() = prefs.getString("tags", "subread") ?: "subread" 33 set(value) = prefs.edit { putString("tags", value.trim()) } 34 35 /** 36 * False: a selected text opens the pop-up, and "+ Anki" adds the term. True: the first 37 * term at the start of the selection goes on a card at once, with no pop-up. 38 */ 39 var addAtOnce: Boolean 40 get() = prefs.getBoolean("add_at_once", false) 41 set(value) = prefs.edit { putBoolean("add_at_once", value) } 42 43 /** True: a word that is in the note type already is not added again. */ 44 var skipDuplicates: Boolean 45 get() = prefs.getBoolean("skip_duplicates", true) 46 set(value) = prefs.edit { putBoolean("skip_duplicates", value) } 47 48 /** Sound before and after the subtitle line in the clip, in milliseconds. */ 49 var padMs: Int 50 get() = prefs.getInt("pad_ms", 250) 51 set(value) = prefs.edit { putInt("pad_ms", value.coerceIn(0, 3000)) } 52 53 /** The last card that the app added: a picture shared later goes on it. */ 54 var lastNoteId: Long 55 get() = prefs.getLong("last_note_id", NONE) 56 set(value) = prefs.edit { putLong("last_note_id", value) } 57 58 /** When the last card was added, `System.currentTimeMillis()`. */ 59 var lastNoteAt: Long 60 get() = prefs.getLong("last_note_at", 0) 61 set(value) = prefs.edit { putLong("last_note_at", value) } 62 63 companion object { 64 const val NONE = -1L 65 } 66 }