35 lines
1.1 KiB
Kotlin

package tab.list
import cafe.adriel.voyager.core.model.StateScreenModel
import data.EGroup
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.launch
import kotlinx.serialization.json.Json
import org.jetbrains.compose.resources.ExperimentalResourceApi
import veganenumbers.composeapp.generated.resources.Res.readBytes
class ListScreenModel : StateScreenModel<ListScreenModel.State>(State.Loading) {
sealed class State {
object Loading : State()
data class Result(val eNumbers: List<EGroup>) : State()
}
/**
* Reads JSON file containing all e numbers
* @return List of grouped E Numbers in `EGroup` type
*/
@OptIn(ExperimentalResourceApi::class)
suspend fun getENumbers() {
coroutineScope {
launch {
mutableState.value = State.Loading
val bytes = readBytes("files/enumbers.json")
val eNumbers = Json.decodeFromString<List<EGroup>>(bytes.decodeToString())
mutableState.value = State.Result(eNumbers = eNumbers)
}
}
}
}