From 8a284c2b04a34bc106f2485dffcdc2831e7cfbd1 Mon Sep 17 00:00:00 2001 From: kunfei Date: Wed, 2 Oct 2019 16:50:09 +0800 Subject: [PATCH] up --- .../java/io/legado/app/data/AppDatabase.kt | 3 +- .../io/legado/app/data/dao/RssArticleDao.kt | 14 +++ .../app/ui/rss/article/RssArticlesActivity.kt | 91 ++++++------------- .../app/ui/rss/article/RssArticlesAdapter.kt | 27 ++++++ .../ui/rss/article/RssArticlesViewModel.kt | 10 ++ .../main/res/layout/activity_rss_artivles.xml | 26 ++++++ 6 files changed, 109 insertions(+), 62 deletions(-) create mode 100644 app/src/main/java/io/legado/app/data/dao/RssArticleDao.kt create mode 100644 app/src/main/java/io/legado/app/ui/rss/article/RssArticlesAdapter.kt create mode 100644 app/src/main/java/io/legado/app/ui/rss/article/RssArticlesViewModel.kt create mode 100644 app/src/main/res/layout/activity_rss_artivles.xml diff --git a/app/src/main/java/io/legado/app/data/AppDatabase.kt b/app/src/main/java/io/legado/app/data/AppDatabase.kt index d58a5ccb7..3b17435d5 100644 --- a/app/src/main/java/io/legado/app/data/AppDatabase.kt +++ b/app/src/main/java/io/legado/app/data/AppDatabase.kt @@ -12,7 +12,7 @@ import io.legado.app.data.entities.* @Database( entities = [Book::class, BookGroup::class, BookSource::class, BookChapter::class, ReplaceRule::class, SearchBook::class, - SearchKeyword::class, SourceCookie::class, RssSource::class, Bookmark::class], + SearchKeyword::class, SourceCookie::class, RssSource::class, Bookmark::class, RssArticle::class], version = 1, exportSchema = true ) @@ -58,4 +58,5 @@ abstract class AppDatabase : RoomDatabase() { abstract fun sourceCookieDao(): SourceCookieDao abstract fun rssSourceDao(): RssSourceDao abstract fun bookmarkDao(): BookmarkDao + abstract fun rssArtivleDao(): RssArticleDao } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/data/dao/RssArticleDao.kt b/app/src/main/java/io/legado/app/data/dao/RssArticleDao.kt new file mode 100644 index 000000000..5935d76ce --- /dev/null +++ b/app/src/main/java/io/legado/app/data/dao/RssArticleDao.kt @@ -0,0 +1,14 @@ +package io.legado.app.data.dao + +import androidx.lifecycle.LiveData +import androidx.room.Query +import io.legado.app.data.entities.RssArticle + +interface RssArticleDao { + + + @Query("select * from rssArticles where origin = :origin") + fun liveByOrigin(origin: String): LiveData> + + +} \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/rss/article/RssArticlesActivity.kt b/app/src/main/java/io/legado/app/ui/rss/article/RssArticlesActivity.kt index 3590627a1..9df7a3aeb 100644 --- a/app/src/main/java/io/legado/app/ui/rss/article/RssArticlesActivity.kt +++ b/app/src/main/java/io/legado/app/ui/rss/article/RssArticlesActivity.kt @@ -1,75 +1,44 @@ package io.legado.app.ui.rss.article -import android.app.ProgressDialog -import android.content.Context import android.os.Bundle -import android.view.LayoutInflater -import android.view.View -import android.view.ViewGroup -import android.widget.BaseAdapter -import androidx.appcompat.app.AppCompatActivity +import androidx.lifecycle.LiveData +import androidx.lifecycle.Observer +import androidx.recyclerview.widget.LinearLayoutManager +import io.legado.app.App import io.legado.app.R +import io.legado.app.base.VMBaseActivity import io.legado.app.data.entities.RssArticle -import io.legado.app.model.rss.RssParser -import io.legado.app.ui.rss.read.ReadRssActivity -import kotlinx.android.synthetic.main.item_rss_article.view.* -import org.jetbrains.anko.listView -import org.jetbrains.anko.sdk27.listeners.onItemClick -import org.jetbrains.anko.startActivity -import java.net.URL +import io.legado.app.lib.theme.ATH +import io.legado.app.utils.getViewModel +import kotlinx.android.synthetic.main.activity_rss_artivles.* -class RssArticlesActivity : AppCompatActivity() { - override fun onCreate(savedInstanceState: Bundle?) { - super.onCreate(savedInstanceState) - if (intent.hasExtra("url")) { - // 处理从 快速添加并预览 跳转到这个页面 - val urlString = intent.getStringExtra("url") - val adapter = ArticleAdapter(mutableListOf(), this) - listView { - this.adapter = adapter - onItemClick { p0, p1, p2, p3 -> - startActivity("description" to adapter.articles[p2].description) - } - } - val loading = ProgressDialog(this@RssArticlesActivity) - loading.setMessage("加载中...") - loading.show() - Thread { - val xml = URL(urlString).readText() - val articles = RssParser.parseXML(xml) - runOnUiThread { - adapter.articles = articles - adapter.notifyDataSetChanged() - loading.dismiss() - } - }.start() - } - } -} +class RssArticlesActivity : VMBaseActivity(R.layout.activity_rss_artivles) { -class ArticleAdapter(var articles: MutableList, var context: Context) : BaseAdapter() { - override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View { - val item_rss = LayoutInflater.from(context).inflate(R.layout.item_rss_article, null) - val article = articles[position] - item_rss.title.text = article.title - item_rss.pub_date.text = article.pubDate - if (article.author != null && article.author != "") { - item_rss.author.text = article.author - } else { - item_rss.author.text = article.link - } - return item_rss - } + override val viewModel: RssArticlesViewModel + get() = getViewModel(RssArticlesViewModel::class.java) + + private var adapter: RssArticlesAdapter? = null + private var rssArticlesData: LiveData>? = null - override fun getItem(position: Int): Any { - return articles[position] + override fun onActivityCreated(savedInstanceState: Bundle?) { + initView() + intent.getStringExtra("url")?.let { + initData(it) + } } - override fun getItemId(position: Int): Long { - return 1 + private fun initView() { + ATH.applyEdgeEffectColor(recycler_view) + recycler_view.layoutManager = LinearLayoutManager(this) + adapter = RssArticlesAdapter(this) + recycler_view.adapter = adapter } - override fun getCount(): Int { - return articles.size + private fun initData(origin: String) { + rssArticlesData?.removeObservers(this) + rssArticlesData = App.db.rssArtivleDao().liveByOrigin(origin) + rssArticlesData?.observe(this, Observer { + adapter?.setItems(it) + }) } } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/rss/article/RssArticlesAdapter.kt b/app/src/main/java/io/legado/app/ui/rss/article/RssArticlesAdapter.kt new file mode 100644 index 000000000..0e185ab22 --- /dev/null +++ b/app/src/main/java/io/legado/app/ui/rss/article/RssArticlesAdapter.kt @@ -0,0 +1,27 @@ +package io.legado.app.ui.rss.article + +import android.content.Context +import io.legado.app.R +import io.legado.app.base.adapter.ItemViewHolder +import io.legado.app.base.adapter.SimpleRecyclerAdapter +import io.legado.app.data.entities.RssArticle +import kotlinx.android.synthetic.main.item_rss_article.view.* + + +class RssArticlesAdapter(context: Context) : + SimpleRecyclerAdapter(context, R.layout.item_rss_article) { + + override fun convert(holder: ItemViewHolder, item: RssArticle, payloads: MutableList) { + with(holder.itemView) { + title.text = item.title + pub_date.text = item.pubDate + if (item.author.isNullOrBlank()) { + author.text = item.link + } else { + author.text = item.author + } + } + } + + +} \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/rss/article/RssArticlesViewModel.kt b/app/src/main/java/io/legado/app/ui/rss/article/RssArticlesViewModel.kt new file mode 100644 index 000000000..344b1d098 --- /dev/null +++ b/app/src/main/java/io/legado/app/ui/rss/article/RssArticlesViewModel.kt @@ -0,0 +1,10 @@ +package io.legado.app.ui.rss.article + +import android.app.Application +import io.legado.app.base.BaseViewModel + + +class RssArticlesViewModel(application: Application) : BaseViewModel(application) { + + +} \ No newline at end of file diff --git a/app/src/main/res/layout/activity_rss_artivles.xml b/app/src/main/res/layout/activity_rss_artivles.xml new file mode 100644 index 000000000..5665c8a24 --- /dev/null +++ b/app/src/main/res/layout/activity_rss_artivles.xml @@ -0,0 +1,26 @@ + + + + + + + + + + + + \ No newline at end of file