diff --git a/app/src/main/java/io/legado/app/data/dao/HttpTTSDao.kt b/app/src/main/java/io/legado/app/data/dao/HttpTTSDao.kt index 971dc4896..f71169bb1 100644 --- a/app/src/main/java/io/legado/app/data/dao/HttpTTSDao.kt +++ b/app/src/main/java/io/legado/app/data/dao/HttpTTSDao.kt @@ -13,6 +13,9 @@ interface HttpTTSDao { @Query("select * from httpTTS order by name") fun observeAll(): LiveData> + @get:Query("select count(*) from httpTTS") + val count: Int + @Query("select * from httpTTS where id = :id") fun get(id: Long): HttpTTS? diff --git a/app/src/main/java/io/legado/app/ui/main/MainViewModel.kt b/app/src/main/java/io/legado/app/ui/main/MainViewModel.kt index 06fd049d3..ed2e070d8 100644 --- a/app/src/main/java/io/legado/app/ui/main/MainViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/main/MainViewModel.kt @@ -6,15 +6,13 @@ import io.legado.app.base.BaseViewModel import io.legado.app.constant.BookType import io.legado.app.constant.EventBus import io.legado.app.data.entities.Book +import io.legado.app.data.entities.HttpTTS import io.legado.app.data.entities.RssSource import io.legado.app.help.AppConfig import io.legado.app.help.http.HttpHelper import io.legado.app.help.storage.Restore import io.legado.app.model.WebBook -import io.legado.app.utils.FileUtils -import io.legado.app.utils.GSON -import io.legado.app.utils.fromJsonObject -import io.legado.app.utils.postEvent +import io.legado.app.utils.* import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.asCoroutineDispatcher import kotlinx.coroutines.delay @@ -97,6 +95,13 @@ class MainViewModel(application: Application) : BaseViewModel(application) { fun postLoad() { execute { FileUtils.deleteFile(FileUtils.getPath(context.cacheDir, "Fonts")) + if (App.db.httpTTSDao().count == 0) { + @Suppress("BlockingMethodInNonBlockingContext") + val json = String(App.INSTANCE.assets.open("httpTTS.json").readBytes()) + GSON.fromJsonArray(json)?.let { + App.db.httpTTSDao().insert(*it.toTypedArray()) + } + } } } } \ No newline at end of file