flashcards: detekt clean (169 tests)
- Commit
086459004fc47c280ab0bd11267bcf8d25a1bba6- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-14 23:22 UTC (21 days ago)
- Parents
835a88f
Fix all 47 detekt findings: real KDoc for 45 undocumented public classes (Route, seven DAOs and the database, UiContract action interfaces/states, Grade, companions); DeckIo.splitCsvRecords (CyclomaticComplexMethod 21) extracted into a private CsvParser scanner class and parseCsv LoopWithTooManyJumpStatements rewritten without multiple continues, preserving header and empty-row semantics. Verified: ./gradlew :flashcards:detekt :flashcards:testDevUnitTest :flashcards:lint --max-workers=32 — BUILD SUCCESSFUL, 169 unit tests pass. No @Suppress added.
Diff
flashcards/src/main/java/com/vayunmathur/flashcards/MainActivity.kt
diff --git a/flashcards/src/main/java/com/vayunmathur/flashcards/MainActivity.kt b/flashcards/src/main/java/com/vayunmathur/flashcards/MainActivity.ktindex 76dbbaa26a..7adba3014f 100644--- a/flashcards/src/main/java/com/vayunmathur/flashcards/MainActivity.kt+++ b/flashcards/src/main/java/com/vayunmathur/flashcards/MainActivity.kt@@ -36,6 +36,7 @@ import com.vayunmathur.library.util.NavKey import com.vayunmathur.library.util.rememberNavBackStack import kotlinx.serialization.Serializable +/** Entry activity that owns the [FlashcardsViewModel] and applies the theme from its settings. */ class MainActivity : MaActivity() { private val viewModel: FlashcardsViewModel by viewModels { FlashcardsViewModelFactory(application, FlashcardsRepository.get(application))@@ -58,26 +59,34 @@ class MainActivity : MaActivity() { } } +/** Navigation destinations for decks, notes, note types, review, and stats. */ @Serializable sealed interface Route : NavKey {+ /** The deck-list main screen. */ @Serializable data object Main : Route + /** The review-statistics screen. */ @Serializable data object Stats : Route + /** The note-type management list. */ @Serializable data object NoteTypeList : Route + /** The editor for the note type identified by [noteTypeId]. */ @Serializable data class NoteTypeEdit(val noteTypeId: Long) : Route + /** The note list for the deck identified by [deckId]. */ @Serializable data class CardList(val deckId: Long) : Route + /** The editor for note [noteId] in deck [deckId]. */ @Serializable data class NoteEdit(val deckId: Long, val noteId: Long) : Route + /** A review session for deck [deckId] with its study mode and limits. */ @Serializable data class Review( val deckId: Long,
flashcards/src/main/java/com/vayunmathur/flashcards/data/FlashcardsDatabase.kt
diff --git a/flashcards/src/main/java/com/vayunmathur/flashcards/data/FlashcardsDatabase.kt b/flashcards/src/main/java/com/vayunmathur/flashcards/data/FlashcardsDatabase.ktindex 2ac7e40559..72f96bacce 100644--- a/flashcards/src/main/java/com/vayunmathur/flashcards/data/FlashcardsDatabase.kt+++ b/flashcards/src/main/java/com/vayunmathur/flashcards/data/FlashcardsDatabase.kt@@ -20,6 +20,7 @@ const val DB_NAME = "flashcards-db" fun flashcardsDbConfigs(context: Context): List<Pair<String, String>> = listOf(DB_NAME to DatabaseHelper(context).getPassphrase()) +/** Room access for decks. */ @Dao interface DeckDao { @Query("SELECT * FROM Deck")@@ -38,6 +39,7 @@ interface DeckDao { suspend fun delete(value: Deck): Int } +/** Room access for note types. */ @Dao interface NoteTypeDao { @Query("SELECT * FROM NoteType")@@ -56,6 +58,7 @@ interface NoteTypeDao { suspend fun delete(value: NoteType): Int } +/** Room access for the fields of note types. */ @Dao interface NoteTypeFieldDao { @Query("SELECT * FROM NoteTypeField")@@ -77,6 +80,7 @@ interface NoteTypeFieldDao { suspend fun deleteByNoteType(noteTypeId: Long) } +/** Room access for card templates. */ @Dao interface CardTemplateDao { @Query("SELECT * FROM CardTemplate")@@ -98,6 +102,7 @@ interface CardTemplateDao { suspend fun deleteByNoteType(noteTypeId: Long) } +/** Room access for notes. */ @Dao interface NoteDao { @Query("SELECT * FROM Note")@@ -140,6 +145,7 @@ interface NoteDao { suspend fun deleteByDeck(deckId: Long) } +/** Room access for generated cards. */ @Dao interface CardDao { @Query("SELECT * FROM Card")@@ -200,6 +206,7 @@ interface CardDao { suspend fun deleteRemovedTemplates(noteId: Long, keepOrds: List<Int>) } +/** Room access for review-log entries. */ @Dao interface ReviewLogDao { @Query("SELECT * FROM ReviewLog")@@ -243,6 +250,7 @@ interface ReviewLogDao { version = 4, exportSchema = false, )+/** Room database holding decks, notes, cards, and review history. */ abstract class FlashcardsDatabase : RoomDatabase() { abstract fun deckDao(): DeckDao abstract fun cardDao(): CardDao@@ -252,6 +260,7 @@ abstract class FlashcardsDatabase : RoomDatabase() { abstract fun cardTemplateDao(): CardTemplateDao abstract fun noteDao(): NoteDao + /** Migration definitions upgrading the flashcards database schema. */ companion object : DatabaseMigrations { override val migrations = listOf( Migration(1, 2) { db ->
flashcards/src/main/java/com/vayunmathur/flashcards/data/FlashcardsRepository.kt
diff --git a/flashcards/src/main/java/com/vayunmathur/flashcards/data/FlashcardsRepository.kt b/flashcards/src/main/java/com/vayunmathur/flashcards/data/FlashcardsRepository.ktindex 95ca0c6c39..d7062b25f9 100644--- a/flashcards/src/main/java/com/vayunmathur/flashcards/data/FlashcardsRepository.kt+++ b/flashcards/src/main/java/com/vayunmathur/flashcards/data/FlashcardsRepository.kt@@ -44,6 +44,7 @@ class FlashcardsRepository private constructor(context: Context) : suspend fun importApkg(context: Context, uri: Uri): String = ApkgImport.import(context, uri, deckDao, noteTypeDao, noteTypeFieldDao, cardTemplateDao, noteDao, cardDao) + /** Holds the process-wide [FlashcardsRepository] singleton. */ companion object { @Volatile private var instance: FlashcardsRepository? = null
flashcards/src/main/java/com/vayunmathur/flashcards/util/ApkgImport.kt
diff --git a/flashcards/src/main/java/com/vayunmathur/flashcards/util/ApkgImport.kt b/flashcards/src/main/java/com/vayunmathur/flashcards/util/ApkgImport.ktindex ca9001af99..fdd920f641 100644--- a/flashcards/src/main/java/com/vayunmathur/flashcards/util/ApkgImport.kt+++ b/flashcards/src/main/java/com/vayunmathur/flashcards/util/ApkgImport.kt@@ -26,6 +26,7 @@ import java.util.zip.ZipInputStream * `collection.anki2`) are detected and rejected with a clear message. */ object ApkgImport {+ /** Thrown when an imported .apkg file is not in the supported Anki format. */ class ApkgFormatException(message: String) : Exception(message) /** Cursor columns of `SELECT id, nid, did, ord FROM cards`. */
flashcards/src/main/java/com/vayunmathur/flashcards/util/AppBackupAgent.kt
diff --git a/flashcards/src/main/java/com/vayunmathur/flashcards/util/AppBackupAgent.kt b/flashcards/src/main/java/com/vayunmathur/flashcards/util/AppBackupAgent.ktindex 9c1f8ab0ff..185d225532 100644--- a/flashcards/src/main/java/com/vayunmathur/flashcards/util/AppBackupAgent.kt+++ b/flashcards/src/main/java/com/vayunmathur/flashcards/util/AppBackupAgent.kt@@ -5,6 +5,7 @@ import com.vayunmathur.library.util.BaseBackupAgent import com.vayunmathur.flashcards.data.flashcardsDbConfigs import java.io.File +/** Backup agent that backs up the encrypted flashcards database and its DataStore settings. */ class AppBackupAgent : BaseBackupAgent() { override val dbCodec = SqlCipherDbCodec
flashcards/src/main/java/com/vayunmathur/flashcards/util/DeckIo.kt
diff --git a/flashcards/src/main/java/com/vayunmathur/flashcards/util/DeckIo.kt b/flashcards/src/main/java/com/vayunmathur/flashcards/util/DeckIo.ktindex 1521e903ce..5d5763e031 100644--- a/flashcards/src/main/java/com/vayunmathur/flashcards/util/DeckIo.kt+++ b/flashcards/src/main/java/com/vayunmathur/flashcards/util/DeckIo.kt@@ -30,16 +30,16 @@ object DeckIo { val rows = mutableListOf<Pair<String, String>>() var firstRecord = true for (fields in splitCsvRecords(text)) {- if (fields.size < 2) continue- val front = fields[0].trim()- val back = fields[1].trim()- if (front.isEmpty() && back.isEmpty()) continue- if (firstRecord && front.equals("front", true) && back.equals("back", true)) {- firstRecord = false- continue+ if (fields.size >= 2) {+ val front = fields[0].trim()+ val back = fields[1].trim()+ if (front.isNotEmpty() || back.isNotEmpty()) {+ val isHeader = firstRecord && front.equals("front", true) &&+ back.equals("back", true)+ firstRecord = false+ if (!isHeader) rows.add(front to back)+ } }- firstRecord = false- rows.add(front to back) } return rows }@@ -49,37 +49,51 @@ object DeckIo { * (as [writeCsv] emits for fields containing newlines), so this must scan * the whole text rather than one physical line at a time. */- private fun splitCsvRecords(text: String): List<List<String>> {- val records = mutableListOf<List<String>>()- val record = mutableListOf<String>()- val sb = StringBuilder()- var inQuotes = false- var recordHasContent = false- var i = 0- fun endRecord() {- record.add(sb.toString()); sb.clear()- records.add(record.toList()); record.clear()- recordHasContent = false+ private fun splitCsvRecords(text: String): List<List<String>> = CsvParser(text).parse()++ /** Character scanner for [splitCsvRecords]; quoted fields may span lines. */+ private class CsvParser(private val text: String) {+ private val records = mutableListOf<List<String>>()+ private val record = mutableListOf<String>()+ private val sb = StringBuilder()+ private var inQuotes = false+ private var recordHasContent = false+ private var i = 0++ fun parse(): List<List<String>> {+ while (i < text.length) {+ consume(text[i])+ i+++ }+ if (recordHasContent || record.isNotEmpty() || sb.isNotEmpty()) endRecord()+ return records }- while (i < text.length) {- val c = text[i]++ private fun consume(c: Char) { when { inQuotes && c == '"' && text.getOrNull(i + 1) == '"' -> { sb.append('"'); i++ } c == '"' -> { inQuotes = !inQuotes; recordHasContent = true }- c == ',' && !inQuotes -> {- record.add(sb.toString()); sb.clear(); recordHasContent = true- }- (c == '\n' || c == '\r') && !inQuotes -> {- if (c == '\r' && text.getOrNull(i + 1) == '\n') i++- if (recordHasContent || record.isNotEmpty() || sb.isNotEmpty()) endRecord()- }+ c == ',' && !inQuotes -> endField()+ (c == '\n' || c == '\r') && !inQuotes -> consumeNewline(c) else -> { sb.append(c); recordHasContent = true } }- i++ }- if (recordHasContent || record.isNotEmpty() || sb.isNotEmpty()) endRecord()- return records++ private fun endField() {+ record.add(sb.toString()); sb.clear(); recordHasContent = true+ }++ private fun consumeNewline(c: Char) {+ if (c == '\r' && text.getOrNull(i + 1) == '\n') i+++ if (recordHasContent || record.isNotEmpty() || sb.isNotEmpty()) endRecord()+ }++ private fun endRecord() {+ record.add(sb.toString()); sb.clear()+ records.add(record.toList()); record.clear()+ recordHasContent = false+ } } }
flashcards/src/main/java/com/vayunmathur/flashcards/util/FlashcardsUiContract.kt
diff --git a/flashcards/src/main/java/com/vayunmathur/flashcards/util/FlashcardsUiContract.kt b/flashcards/src/main/java/com/vayunmathur/flashcards/util/FlashcardsUiContract.ktindex 85f02a42c8..934942546b 100644--- a/flashcards/src/main/java/com/vayunmathur/flashcards/util/FlashcardsUiContract.kt+++ b/flashcards/src/main/java/com/vayunmathur/flashcards/util/FlashcardsUiContract.kt@@ -35,6 +35,7 @@ data class DeckListUiState( val decks: List<DeckSummary> = emptyList(), ) +/** Callbacks for the deck list: opening, adding, deleting, reviewing, and reordering decks. */ interface DeckListActions { fun openDeck(id: Long) {} fun addDeck(name: String) {}@@ -42,6 +43,7 @@ interface DeckListActions { fun startReview(deckId: Long) {} fun reorder(decks: List<Deck>) {} + /** Provides the no-op implementation used by previews. */ companion object { val Noop: DeckListActions = object : DeckListActions {} }@@ -70,6 +72,7 @@ data class NoteListUiState( val decks: List<DeckOption> = emptyList(), ) +/** Callbacks for a deck's note list, including bulk selection actions. */ interface NoteListActions { fun back() {} fun openNote(id: Long) {}@@ -89,6 +92,7 @@ interface NoteListActions { fun setSuspended(ids: List<Long>, suspended: Boolean) {} fun resetScheduling(ids: List<Long>) {} + /** Provides the no-op implementation used by previews. */ companion object { val Noop: NoteListActions = object : NoteListActions {} }@@ -118,6 +122,7 @@ data class NoteEditUiState( val decks: List<DeckOption> = emptyList(), ) +/** Callbacks for the note editor: saving, deleting, suspension, and image insertion. */ interface NoteEditActions { fun back() {} fun save(noteTypeId: Long, deckId: Long, fieldValues: List<String>, tags: String) {}@@ -125,6 +130,7 @@ interface NoteEditActions { fun setSuspended(suspended: Boolean) {} fun insertImage(fieldIndex: Int) {} + /** Provides the no-op implementation used by previews. */ companion object { val Noop: NoteEditActions = object : NoteEditActions {} }@@ -144,16 +150,19 @@ data class NoteTypeSummary( val isCloze: Boolean, ) +/** Everything the note-type management list draws. */ data class NoteTypeListUiState( val noteTypes: List<NoteTypeSummary> = emptyList(), ) +/** Callbacks for the note-type management list. */ interface NoteTypeListActions { fun back() {} fun openNoteType(id: Long) {} fun addNoteType() {} fun deleteNoteType(id: Long) {} + /** Provides the no-op implementation used by previews. */ companion object { val Noop: NoteTypeListActions = object : NoteTypeListActions {} }@@ -178,11 +187,13 @@ data class NoteTypeEditUiState( val isNew: Boolean = true, ) +/** Callbacks for the note-type editor: saving and deleting the note type. */ interface NoteTypeEditActions { fun back() {} fun save(name: String, css: String, type: Int, fields: List<String>, templates: List<TemplateDraft>) {} fun delete() {} + /** Provides the no-op implementation used by previews. */ companion object { val Noop: NoteTypeEditActions = object : NoteTypeEditActions {} }@@ -252,6 +263,7 @@ data class ReviewUiState( fun label(grade: Grade): String = intervalLabels[grade] ?: "" } +/** Callbacks for a review session: grading, undo, suspension, and speech. */ interface ReviewActions { fun back() {} fun grade(grade: Grade) {}@@ -259,6 +271,7 @@ interface ReviewActions { fun suspend() {} fun speak(text: String) {} + /** Provides the no-op implementation used by previews. */ companion object { val Noop: ReviewActions = object : ReviewActions {} }@@ -274,6 +287,7 @@ data class DeckOption(val id: Long?, val name: String) /** One day of review history. [epochDay] is days since the Unix epoch (local). */ data class DailyStat(val epochDay: Long, val count: Int) +/** Everything the statistics screen draws, including history, forecast, and retention. */ data class StatsUiState( val deckOptions: List<DeckOption> = emptyList(), val selectedDeckId: Long? = null,@@ -292,10 +306,12 @@ data class StatsUiState( val heatmap: List<DailyStat> = emptyList(), ) +/** Callbacks for the statistics screen. */ interface StatsActions { fun back() {} fun selectDeck(id: Long?) {} + /** Provides the no-op implementation used by previews. */ companion object { val Noop: StatsActions = object : StatsActions {} }@@ -305,6 +321,7 @@ interface StatsActions { // Settings // --------------------------------------------------------------------------- +/** Theme-mode values stored in settings: system, light, or dark. */ object ThemeMode { const val SYSTEM = 0 const val LIGHT = 1@@ -319,6 +336,7 @@ data class DeckPreset( val desiredRetention: Double, ) +/** Everything the settings screen draws, including study limits and reminders. */ data class SettingsUiState( val desiredRetention: Double = 0.9, val newPerDay: Int = 20,@@ -333,6 +351,7 @@ data class SettingsUiState( val canOptimize: Boolean = false, ) +/** Callbacks for the settings screen, including FSRS optimisation and exports. */ interface SettingsActions { fun back() {} fun setDesiredRetention(value: Double) {}@@ -349,6 +368,7 @@ interface SettingsActions { fun manageNoteTypes() {} fun exportCollection() {} + /** Provides the no-op implementation used by previews. */ companion object { val Noop: SettingsActions = object : SettingsActions {} }
flashcards/src/main/java/com/vayunmathur/flashcards/util/MediaStore.kt
diff --git a/flashcards/src/main/java/com/vayunmathur/flashcards/util/MediaStore.kt b/flashcards/src/main/java/com/vayunmathur/flashcards/util/MediaStore.ktindex 2f1ac4ddad..b31be5660d 100644--- a/flashcards/src/main/java/com/vayunmathur/flashcards/util/MediaStore.kt+++ b/flashcards/src/main/java/com/vayunmathur/flashcards/util/MediaStore.kt@@ -83,6 +83,7 @@ class MediaStore(context: Context) { } } + /** Media directory name and the markdown image-reference pattern. */ companion object { private const val DIR = "media"
flashcards/src/main/java/com/vayunmathur/flashcards/util/ReviewReminderWorker.kt
diff --git a/flashcards/src/main/java/com/vayunmathur/flashcards/util/ReviewReminderWorker.kt b/flashcards/src/main/java/com/vayunmathur/flashcards/util/ReviewReminderWorker.ktindex 027ea3cd8a..a25e2e2a65 100644--- a/flashcards/src/main/java/com/vayunmathur/flashcards/util/ReviewReminderWorker.kt+++ b/flashcards/src/main/java/com/vayunmathur/flashcards/util/ReviewReminderWorker.kt@@ -105,6 +105,7 @@ class ReviewReminderWorker( NotificationManagerCompat.from(context).notify(REMINDER_NOTIFICATION_ID, notification) } + /** Notification id for the review reminder. */ companion object { private const val REMINDER_NOTIFICATION_ID = 4201 }
flashcards/src/main/java/com/vayunmathur/flashcards/util/ReviewSession.kt
diff --git a/flashcards/src/main/java/com/vayunmathur/flashcards/util/ReviewSession.kt b/flashcards/src/main/java/com/vayunmathur/flashcards/util/ReviewSession.ktindex d344b0ba71..ca8a1e84fb 100644--- a/flashcards/src/main/java/com/vayunmathur/flashcards/util/ReviewSession.kt+++ b/flashcards/src/main/java/com/vayunmathur/flashcards/util/ReviewSession.kt@@ -142,6 +142,7 @@ class ReviewSession( } } + /** How many cards ahead a failed card is re-queued in a session. */ companion object { private const val RE_QUEUE_GAP = 3 }
flashcards/src/main/java/com/vayunmathur/flashcards/util/Scheduler.kt
diff --git a/flashcards/src/main/java/com/vayunmathur/flashcards/util/Scheduler.kt b/flashcards/src/main/java/com/vayunmathur/flashcards/util/Scheduler.ktindex d121f6ab84..015b4ccd2d 100644--- a/flashcards/src/main/java/com/vayunmathur/flashcards/util/Scheduler.kt+++ b/flashcards/src/main/java/com/vayunmathur/flashcards/util/Scheduler.kt@@ -17,6 +17,7 @@ private const val GRADE_HARD = 2 private const val GRADE_GOOD = 3 private const val GRADE_EASY = 4 +/** The four review grades, mapped onto the FSRS 1..4 scale. */ enum class Grade(val value: Int) { AGAIN(GRADE_AGAIN), HARD(GRADE_HARD),