pull/66/head
kunfei 5 years ago
parent cea6795de7
commit dab8cb781b
  1. 26
      app/src/main/java/io/legado/app/ui/main/MainViewModel.kt
  2. 5
      app/src/main/java/io/legado/app/ui/main/rss/RssFragment.kt

@ -4,7 +4,14 @@ import android.app.Application
import io.legado.app.App
import io.legado.app.base.BaseViewModel
import io.legado.app.constant.Bus
import io.legado.app.data.api.IHttpGetApi
import io.legado.app.data.entities.RssSource
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.GSON
import io.legado.app.utils.NetworkUtils
import io.legado.app.utils.fromJsonObject
import io.legado.app.utils.postEvent
import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.delay
@ -48,4 +55,23 @@ class MainViewModel(application: Application) : BaseViewModel(application) {
}
}
fun initRss() {
execute {
val url = "https://gitee.com/alanskycn/yuedu/raw/master/JS/RSS/rssSource"
NetworkUtils.getBaseUrl(url)?.let {
val response = HttpHelper.getApiService<IHttpGetApi>(it).get(url, mapOf()).execute()
response.body()?.let { body ->
val sources = mutableListOf<RssSource>()
val items: List<Map<String, Any>> = Restore.jsonPath.parse(body).read("$")
for (item in items) {
val jsonItem = Restore.jsonPath.parse(item)
GSON.fromJsonObject<RssSource>(jsonItem.jsonString())?.let { source ->
sources.add(source)
}
}
App.db.rssSourceDao().insert(*sources.toTypedArray())
}
}
}
}
}

@ -11,8 +11,10 @@ import io.legado.app.R
import io.legado.app.base.BaseFragment
import io.legado.app.data.entities.RssSource
import io.legado.app.lib.theme.ATH
import io.legado.app.ui.main.MainViewModel
import io.legado.app.ui.rss.article.RssArticlesActivity
import io.legado.app.ui.rss.source.manage.RssSourceActivity
import io.legado.app.utils.getViewModelOfActivity
import io.legado.app.utils.startActivity
import kotlinx.android.synthetic.main.fragment_rss.*
import kotlinx.android.synthetic.main.view_title_bar.*
@ -50,6 +52,9 @@ class RssFragment : BaseFragment(R.layout.fragment_rss),
private fun initData() {
App.db.rssSourceDao().liveEnabled().observe(viewLifecycleOwner, Observer {
if (it.isEmpty()) {
getViewModelOfActivity(MainViewModel::class.java).initRss()
}
adapter.setItems(it)
})
}

Loading…
Cancel
Save