From 83c2be867d218854b4f3c49690de59d34c387094 Mon Sep 17 00:00:00 2001 From: kunfei Date: Tue, 4 Feb 2020 21:38:19 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ui/rss/favorites/RssFavoritesActivity.kt | 44 ++++++++++++- .../ui/rss/favorites/RssFavoritesAdapter.kt | 64 +++++++++++++++++++ .../res/layout/activity_rss_favorites.xml | 16 ++--- 3 files changed, 113 insertions(+), 11 deletions(-) create mode 100644 app/src/main/java/io/legado/app/ui/rss/favorites/RssFavoritesAdapter.kt diff --git a/app/src/main/java/io/legado/app/ui/rss/favorites/RssFavoritesActivity.kt b/app/src/main/java/io/legado/app/ui/rss/favorites/RssFavoritesActivity.kt index 96bc3f99a..0188fe104 100644 --- a/app/src/main/java/io/legado/app/ui/rss/favorites/RssFavoritesActivity.kt +++ b/app/src/main/java/io/legado/app/ui/rss/favorites/RssFavoritesActivity.kt @@ -1,16 +1,58 @@ package io.legado.app.ui.rss.favorites import android.os.Bundle +import androidx.core.content.ContextCompat +import androidx.lifecycle.LiveData +import androidx.lifecycle.Observer +import androidx.recyclerview.widget.DividerItemDecoration +import androidx.recyclerview.widget.LinearLayoutManager +import io.legado.app.App import io.legado.app.R import io.legado.app.base.BaseActivity +import io.legado.app.data.entities.RssStar +import io.legado.app.lib.theme.ATH +import io.legado.app.ui.rss.read.ReadRssActivity +import kotlinx.android.synthetic.main.view_refresh_recycler.* +import org.jetbrains.anko.startActivity -class RssFavoritesActivity : BaseActivity(R.layout.activity_rss_favorites) { +class RssFavoritesActivity : BaseActivity(R.layout.activity_rss_favorites), + RssFavoritesAdapter.CallBack { + private var liveData: LiveData>? = null + private lateinit var adapter: RssFavoritesAdapter override fun onActivityCreated(savedInstanceState: Bundle?) { + initView() + initData() + } + private fun initView() { + ATH.applyEdgeEffectColor(recycler_view) + recycler_view.layoutManager = LinearLayoutManager(this) + recycler_view.addItemDecoration( + DividerItemDecoration(this, DividerItemDecoration.VERTICAL).apply { + ContextCompat.getDrawable(baseContext, R.drawable.ic_divider)?.let { + this.setDrawable(it) + } + }) + adapter = RssFavoritesAdapter(this, this) + recycler_view.adapter = adapter } + private fun initData() { + liveData?.removeObservers(this) + liveData = App.db.rssStarDao().liveAll() + liveData?.observe(this, Observer { + adapter.setItems(it) + }) + } + override fun readRss(rssStar: RssStar) { + startActivity( + Pair("title", rssStar.title), + Pair("origin", rssStar.origin), + Pair("link", rssStar.link) + ) + } } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/rss/favorites/RssFavoritesAdapter.kt b/app/src/main/java/io/legado/app/ui/rss/favorites/RssFavoritesAdapter.kt new file mode 100644 index 000000000..2cb607df5 --- /dev/null +++ b/app/src/main/java/io/legado/app/ui/rss/favorites/RssFavoritesAdapter.kt @@ -0,0 +1,64 @@ +package io.legado.app.ui.rss.favorites + +import android.content.Context +import android.graphics.drawable.Drawable +import com.bumptech.glide.load.DataSource +import com.bumptech.glide.load.engine.GlideException +import com.bumptech.glide.request.RequestListener +import com.bumptech.glide.request.target.Target +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.RssStar +import io.legado.app.help.ImageLoader +import io.legado.app.utils.gone +import io.legado.app.utils.visible +import kotlinx.android.synthetic.main.item_rss_article.view.* +import org.jetbrains.anko.sdk27.listeners.onClick + +class RssFavoritesAdapter(context: Context, val callBack: CallBack) : + SimpleRecyclerAdapter(context, R.layout.item_rss_article) { + + override fun convert(holder: ItemViewHolder, item: RssStar, payloads: MutableList) { + with(holder.itemView) { + tv_title.text = item.title + tv_pub_date.text = item.pubDate + onClick { + callBack.readRss(item) + } + if (item.image.isNullOrBlank()) { + image_view.gone() + } else { + ImageLoader.load(context, item.image) + .addListener(object : RequestListener { + override fun onLoadFailed( + e: GlideException?, + model: Any?, + target: Target?, + isFirstResource: Boolean + ): Boolean { + image_view.gone() + return false + } + + override fun onResourceReady( + resource: Drawable?, + model: Any?, + target: Target?, + dataSource: DataSource?, + isFirstResource: Boolean + ): Boolean { + image_view.visible() + return false + } + + }) + .into(image_view) + } + } + } + + interface CallBack { + fun readRss(rssStar: RssStar) + } +} \ No newline at end of file diff --git a/app/src/main/res/layout/activity_rss_favorites.xml b/app/src/main/res/layout/activity_rss_favorites.xml index 5b80c0714..7740c1981 100644 --- a/app/src/main/res/layout/activity_rss_favorites.xml +++ b/app/src/main/res/layout/activity_rss_favorites.xml @@ -1,6 +1,7 @@ @@ -12,18 +13,13 @@ app:title="@string/favorites" app:layout_constraintTop_toTopOf="parent" /> - - - - - + app:layout_constraintTop_toBottomOf="@id/title_bar" + app:layout_constraintVertical_bias="0.0" + tools:layout_editor_absoluteX="0dp" /> \ No newline at end of file