pull/35/head^2
parent
d6fc96ca19
commit
8a284c2b04
@ -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<List<RssArticle>> |
||||
|
||||
|
||||
} |
@ -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<RssArticle>(), this) |
||||
listView { |
||||
this.adapter = adapter |
||||
onItemClick { p0, p1, p2, p3 -> |
||||
startActivity<ReadRssActivity>("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<RssArticlesViewModel>(R.layout.activity_rss_artivles) { |
||||
|
||||
class ArticleAdapter(var articles: MutableList<RssArticle>, 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<List<RssArticle>>? = 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) |
||||
}) |
||||
} |
||||
} |
@ -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<RssArticle>(context, R.layout.item_rss_article) { |
||||
|
||||
override fun convert(holder: ItemViewHolder, item: RssArticle, payloads: MutableList<Any>) { |
||||
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 |
||||
} |
||||
} |
||||
} |
||||
|
||||
|
||||
} |
@ -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) { |
||||
|
||||
|
||||
} |
@ -0,0 +1,26 @@ |
||||
<?xml version="1.0" encoding="utf-8"?> |
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" |
||||
xmlns:app="http://schemas.android.com/apk/res-auto" |
||||
android:layout_width="match_parent" |
||||
android:layout_height="match_parent" |
||||
android:orientation="vertical"> |
||||
|
||||
<io.legado.app.ui.widget.TitleBar |
||||
android:id="@+id/title_bar" |
||||
android:layout_width="match_parent" |
||||
android:layout_height="wrap_content" |
||||
app:contentInsetStartWithNavigation="0dp" |
||||
app:displayHomeAsUp="true" /> |
||||
|
||||
<FrameLayout |
||||
android:layout_width="match_parent" |
||||
android:layout_height="match_parent"> |
||||
|
||||
<io.legado.app.ui.widget.recycler.scroller.FastScrollRecyclerView |
||||
android:id="@+id/recycler_view" |
||||
android:layout_width="match_parent" |
||||
android:layout_height="match_parent" /> |
||||
|
||||
</FrameLayout> |
||||
|
||||
</LinearLayout> |
Loading…
Reference in new issue