tuner: fall back when the bundled instrument catalogue cannot load
- Commit
67aa4ef2ee3c811d30f499283c1aabedaac474c3- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-19 01:58 UTC (17 days ago)
- Parents
4129a32
TunerViewModel.initialState() ran during ViewModel construction: assets.open threw IOException if instruments.json was missing, InstrumentCatalog.parse threw SerializationException if it was corrupt, and instruments.first() (via byId/initial selection) threw NoSuchElementException if it decoded to an empty list. Any of those crashed the tuner on every launch with no recovery. The parse is now runCatching and an empty catalogue is rejected; on failure a built-in fallback catalogue (the standard guitar from the bundled asset) is used so the tuner still opens. Verified: ./gradlew :tuner:testDevUnitTest (six-module run, BUILD SUCCESSFUL) and ./check todo translate travel tuner vpn (exit 0).
Diff
tuner/src/main/java/com/vayunmathur/tuner/platform/TunerViewModel.kt
diff --git a/tuner/src/main/java/com/vayunmathur/tuner/platform/TunerViewModel.kt b/tuner/src/main/java/com/vayunmathur/tuner/platform/TunerViewModel.ktindex 8812b6119a..1e7a9cd370 100644--- a/tuner/src/main/java/com/vayunmathur/tuner/platform/TunerViewModel.kt+++ b/tuner/src/main/java/com/vayunmathur/tuner/platform/TunerViewModel.kt@@ -7,8 +7,11 @@ import androidx.compose.runtime.setValue import androidx.lifecycle.AndroidViewModel import androidx.lifecycle.viewModelScope import com.vayunmathur.library.util.DataStoreUtils+import com.vayunmathur.tuner.data.AnalysisRange+import com.vayunmathur.tuner.data.Instrument import com.vayunmathur.tuner.data.InstrumentCatalog import com.vayunmathur.tuner.data.InstrumentKind+import com.vayunmathur.tuner.data.StringSpec import com.vayunmathur.tuner.domain.CentsSmoother import com.vayunmathur.tuner.domain.ChordDetector import com.vayunmathur.tuner.domain.ChordDisplay@@ -90,9 +93,15 @@ class TunerViewModel(application: Application) : AndroidViewModel(application) { } private fun initialState(): TunerUiState {- val catalog = InstrumentCatalog.parse(- getApplication<Application>().assets.open(ASSET).bufferedReader().use { it.readText() },- )+ // This runs during ViewModel construction, so a missing/corrupt+ // bundled asset (or one decoding to an empty catalogue, whose+ // first() below would throw) must not crash every launch. Fall+ // back to a built-in guitar so the tuner still opens.+ val catalog = runCatching {+ InstrumentCatalog.parse(+ getApplication<Application>().assets.open(ASSET).bufferedReader().use { it.readText() },+ )+ }.getOrNull()?.takeIf { it.instruments.isNotEmpty() } ?: FALLBACK_CATALOG val savedId = store.getString(KEY_INSTRUMENT) val instrument = savedId?.let { catalog.byId(it) } ?: catalog.instruments.first() return TunerUiState(@@ -292,6 +301,24 @@ class TunerViewModel(application: Application) : AndroidViewModel(application) { } private companion object {+ /** Built-in guitar used only when the bundled catalogue cannot be read. */+ val FALLBACK_CATALOG = InstrumentCatalog(+ listOf(+ Instrument(+ id = "guitar-standard",+ familyId = "guitar",+ kind = InstrumentKind.FRETTED,+ strings = listOf(+ StringSpec(40, "E"), StringSpec(45, "A"), StringSpec(50, "D"),+ StringSpec(55, "G"), StringSpec(59, "B"), StringSpec(64, "E"),+ ),+ fretCount = 15,+ analysis = AnalysisRange(70.0, 1400.0),+ isDefaultForFamily = true,+ ),+ ),+ )+ const val ASSET = "instruments.json" const val KEY_INSTRUMENT = "tuner_instrument"