Просмотр исходного кода

订阅源瀑布流 #121 (@Luoyacheng)

HapeLee 1 год назад
Родитель
Сommit
4a8b218916

+ 1 - 3
app/src/main/java/io/legato/kazusa/ui/main/rss/RssFragment.kt

@@ -136,10 +136,8 @@ class RssFragment() : VMBaseFragment<RssViewModel>(R.layout.fragment_rss),
             if (actionId == EditorInfo.IME_ACTION_SEARCH) {
             if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                 val query = searchView.text.toString()
                 val query = searchView.text.toString()
                 upRssFlowJob(query)
                 upRssFlowJob(query)
-                searchBar.hint = if (query.isEmpty()) {
+                searchBar.hint = query.ifEmpty {
                     getString(R.string.search_rss_source)
                     getString(R.string.search_rss_source)
-                } else {
-                    query
                 }
                 }
                 searchView.hide()
                 searchView.hide()
                 return@setOnEditorActionListener true
                 return@setOnEditorActionListener true

+ 91 - 0
app/src/main/java/io/legato/kazusa/ui/rss/article/RssArticlesAdapter3.kt

@@ -0,0 +1,91 @@
+package io.legato.kazusa.ui.rss.article
+
+import android.annotation.SuppressLint
+import android.content.Context
+import android.graphics.drawable.Drawable
+import android.view.ViewGroup
+import com.bumptech.glide.load.DataSource
+import com.bumptech.glide.load.engine.GlideException
+import com.bumptech.glide.request.RequestListener
+import com.bumptech.glide.request.RequestOptions
+import com.bumptech.glide.request.target.Target
+import io.legato.kazusa.R
+import io.legato.kazusa.base.adapter.ItemViewHolder
+import io.legato.kazusa.data.entities.RssArticle
+import io.legato.kazusa.databinding.ItemRssArticle3Binding
+import io.legato.kazusa.help.glide.ImageLoader
+import io.legato.kazusa.help.glide.OkHttpModelLoader
+import io.legato.kazusa.utils.gone
+import io.legato.kazusa.utils.themeColor
+import io.legato.kazusa.utils.visible
+
+class RssArticlesAdapter3(context: Context, callBack: CallBack) :
+    BaseRssArticlesAdapter<ItemRssArticle3Binding>(context, callBack) {
+
+    override fun getViewBinding(parent: ViewGroup): ItemRssArticle3Binding {
+        return ItemRssArticle3Binding.inflate(inflater, parent, false)
+    }
+
+    @SuppressLint("CheckResult")
+    override fun convert(
+        holder: ItemViewHolder,
+        binding: ItemRssArticle3Binding,
+        item: RssArticle,
+        payloads: MutableList<Any>
+    ) {
+        binding.run {
+            tvTitle.text = item.title
+            tvPubDate.text = item.pubDate
+
+            val imageUrl = item.image
+            if (imageUrl.isNullOrBlank() && !callBack.isGridLayout) {
+                imageView.gone()
+            } else {
+                imageView.visible()
+                imageView.adjustViewBounds = true
+                val options = RequestOptions()
+                    .set(OkHttpModelLoader.sourceOriginOption, item.origin)
+
+                ImageLoader.load(context, imageUrl)
+                    .apply(options)
+                    .listener(object : RequestListener<Drawable> {
+                        override fun onLoadFailed(
+                            e: GlideException?,
+                            model: Any?,
+                            target: Target<Drawable>?,
+                            isFirstResource: Boolean
+                        ): Boolean {
+                            imageView.gone()
+                            return false
+                        }
+
+                        override fun onResourceReady(
+                            resource: Drawable,
+                            model: Any,
+                            target: Target<Drawable>?,
+                            dataSource: DataSource?,
+                            isFirstResource: Boolean
+                        ): Boolean {
+                            return false
+                        }
+                    })
+                    .into(imageView)
+            }
+
+            tvTitle.setTextColor(
+                if (item.read)
+                    context.themeColor(com.google.android.material.R.attr.colorOnSurfaceVariant)
+                else
+                    context.themeColor(com.google.android.material.R.attr.colorOnSurface)
+            )
+        }
+    }
+
+    override fun registerListener(holder: ItemViewHolder, binding: ItemRssArticle3Binding) {
+        holder.itemView.setOnClickListener {
+            getItem(holder.layoutPosition)?.let {
+                callBack.readRss(it)
+            }
+        }
+    }
+}

+ 7 - 3
app/src/main/java/io/legato/kazusa/ui/rss/article/RssArticlesFragment.kt

@@ -11,6 +11,7 @@ import androidx.lifecycle.repeatOnLifecycle
 import androidx.recyclerview.widget.GridLayoutManager
 import androidx.recyclerview.widget.GridLayoutManager
 import androidx.recyclerview.widget.LinearLayoutManager
 import androidx.recyclerview.widget.LinearLayoutManager
 import androidx.recyclerview.widget.RecyclerView
 import androidx.recyclerview.widget.RecyclerView
+import androidx.recyclerview.widget.StaggeredGridLayoutManager
 import io.legato.kazusa.R
 import io.legato.kazusa.R
 import io.legato.kazusa.base.VMBaseFragment
 import io.legato.kazusa.base.VMBaseFragment
 import io.legato.kazusa.constant.AppLog
 import io.legato.kazusa.constant.AppLog
@@ -50,6 +51,7 @@ class RssArticlesFragment() : VMBaseFragment<RssArticlesViewModel>(R.layout.frag
         when (activityViewModel.rssSource?.articleStyle) {
         when (activityViewModel.rssSource?.articleStyle) {
             1 -> RssArticlesAdapter1(requireContext(), this@RssArticlesFragment)
             1 -> RssArticlesAdapter1(requireContext(), this@RssArticlesFragment)
             2 -> RssArticlesAdapter2(requireContext(), this@RssArticlesFragment)
             2 -> RssArticlesAdapter2(requireContext(), this@RssArticlesFragment)
+            3 -> RssArticlesAdapter3(requireContext(), this@RssArticlesFragment)
             else -> RssArticlesAdapter(requireContext(), this@RssArticlesFragment)
             else -> RssArticlesAdapter(requireContext(), this@RssArticlesFragment)
         }
         }
     }
     }
@@ -75,13 +77,15 @@ class RssArticlesFragment() : VMBaseFragment<RssArticlesViewModel>(R.layout.frag
                 scrollToBottom(true)
                 scrollToBottom(true)
             }
             }
         }
         }
-        recyclerView.layoutManager = if (activityViewModel.isGridLayout) {
-            recyclerView.setPadding(8, 0, 8, 0)
+        val layoutManager = if (activityViewModel.isWaterLayout) {
+            recyclerView.itemAnimator = null
+            StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL)
+        } else if (activityViewModel.isGridLayout) {
             GridLayoutManager(requireContext(), 2)
             GridLayoutManager(requireContext(), 2)
         } else {
         } else {
-            recyclerView.addItemDecoration(VerticalDivider(requireContext()))
             LinearLayoutManager(requireContext())
             LinearLayoutManager(requireContext())
         }
         }
+        recyclerView.layoutManager = layoutManager
         recyclerView.adapter = adapter
         recyclerView.adapter = adapter
         adapter.addFooterView {
         adapter.addFooterView {
             ViewLoadMoreBinding.bind(loadMoreView)
             ViewLoadMoreBinding.bind(loadMoreView)

+ 2 - 1
app/src/main/java/io/legato/kazusa/ui/rss/article/RssSortViewModel.kt

@@ -17,6 +17,7 @@ class RssSortViewModel(application: Application) : BaseViewModel(application) {
     val titleLiveData = MutableLiveData<String>()
     val titleLiveData = MutableLiveData<String>()
     var order = System.currentTimeMillis()
     var order = System.currentTimeMillis()
     val isGridLayout get() = rssSource?.articleStyle == 2
     val isGridLayout get() = rssSource?.articleStyle == 2
+    val isWaterLayout get() = rssSource?.articleStyle == 3
 
 
     fun initData(intent: Intent, finally: () -> Unit) {
     fun initData(intent: Intent, finally: () -> Unit) {
         execute {
         execute {
@@ -36,7 +37,7 @@ class RssSortViewModel(application: Application) : BaseViewModel(application) {
 
 
     fun switchLayout() {
     fun switchLayout() {
         rssSource?.let {
         rssSource?.let {
-            if (it.articleStyle < 2) {
+            if (it.articleStyle < 3) {
                 it.articleStyle += 1
                 it.articleStyle += 1
             } else {
             } else {
                 it.articleStyle = 0
                 it.articleStyle = 0

+ 40 - 31
app/src/main/res/layout/item_rss.xml

@@ -1,40 +1,49 @@
 <?xml version="1.0" encoding="utf-8"?>
 <?xml version="1.0" encoding="utf-8"?>
-<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
+<com.google.android.material.card.MaterialCardView
+    xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:app="http://schemas.android.com/apk/res-auto"
     xmlns:app="http://schemas.android.com/apk/res-auto"
     xmlns:tools="http://schemas.android.com/tools"
     xmlns:tools="http://schemas.android.com/tools"
     android:layout_width="match_parent"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:layout_height="wrap_content"
-    android:background="@drawable/bg_item_focused_on_tv"
-    android:focusable="true"
-    android:foreground="?android:attr/selectableItemBackground"
-    android:padding="16dp"
-    android:scrollbars="none">
+    app:strokeWidth="0dp"
+    app:cardBackgroundColor="@android:color/transparent"
+    android:layout_margin="4dp">
 
 
-    <io.legato.kazusa.ui.widget.image.FilletImageView
-        android:id="@+id/iv_icon"
-        android:layout_width="50dp"
-        android:layout_height="50dp"
-        android:scaleType="centerCrop"
-        android:scrollbars="none"
-        app:layout_constraintLeft_toLeftOf="parent"
-        app:layout_constraintRight_toRightOf="parent"
-        app:layout_constraintTop_toTopOf="parent"
-        app:radius="12dp" />
-
-    <TextView
-        android:id="@+id/tv_name"
-        android:layout_width="wrap_content"
+    <androidx.constraintlayout.widget.ConstraintLayout
+        android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:layout_height="wrap_content"
-        android:layout_marginTop="16dp"
-        android:ellipsize="end"
-        android:gravity="top|center_horizontal"
-        android:lines="2"
-        android:scrollbars="none"
-        android:textSize="14sp"
-        app:layout_constraintLeft_toLeftOf="parent"
-        app:layout_constraintRight_toRightOf="parent"
-        app:layout_constraintTop_toBottomOf="@+id/iv_icon"
-        tools:text="RSS" />
+        android:focusable="true"
+        android:foreground="?android:attr/selectableItemBackground"
+        android:padding="12dp"
+        android:scrollbars="none">
+
+        <io.legato.kazusa.ui.widget.image.FilletImageView
+            android:id="@+id/iv_icon"
+            android:layout_width="50dp"
+            android:layout_height="50dp"
+            android:scaleType="centerCrop"
+            android:scrollbars="none"
+            app:layout_constraintLeft_toLeftOf="parent"
+            app:layout_constraintRight_toRightOf="parent"
+            app:layout_constraintTop_toTopOf="parent"
+            app:radius="12dp" />
+
+        <TextView
+            android:id="@+id/tv_name"
+            android:layout_width="wrap_content"
+            android:layout_height="wrap_content"
+            android:layout_marginTop="8dp"
+            android:ellipsize="end"
+            android:gravity="top|center_horizontal"
+            android:lines="2"
+            android:scrollbars="none"
+            android:textSize="12sp"
+            android:textStyle="bold"
+            app:layout_constraintLeft_toLeftOf="parent"
+            app:layout_constraintRight_toRightOf="parent"
+            app:layout_constraintTop_toBottomOf="@+id/iv_icon"
+            tools:text="RSS" />
 
 
+    </androidx.constraintlayout.widget.ConstraintLayout>
 
 
-</androidx.constraintlayout.widget.ConstraintLayout>
+</com.google.android.material.card.MaterialCardView>

+ 12 - 5
app/src/main/res/layout/item_rss_article.xml

@@ -1,11 +1,17 @@
 <?xml version="1.0" encoding="utf-8"?>
 <?xml version="1.0" encoding="utf-8"?>
-<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
+<com.google.android.material.card.MaterialCardView xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:app="http://schemas.android.com/apk/res-auto"
     xmlns:app="http://schemas.android.com/apk/res-auto"
     xmlns:tools="http://schemas.android.com/tools"
     xmlns:tools="http://schemas.android.com/tools"
+    android:layout_margin="8dp"
     android:layout_width="match_parent"
     android:layout_width="match_parent"
-    android:background="?attr/selectableItemBackground"
-    android:layout_height="100dp"
-    android:padding="16dp">
+    android:layout_height="wrap_content"
+    app:strokeWidth="0dp"
+    app:cardBackgroundColor="?attr/colorSurfaceContainer">
+    <androidx.constraintlayout.widget.ConstraintLayout
+        android:padding="8dp"
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content"
+        android:background="?attr/selectableItemBackground">
 
 
     <TextView
     <TextView
         android:id="@+id/tv_title"
         android:id="@+id/tv_title"
@@ -46,4 +52,5 @@
         app:layout_constraintTop_toTopOf="parent"
         app:layout_constraintTop_toTopOf="parent"
         tools:ignore="RtlHardcoded,RtlSymmetry" />
         tools:ignore="RtlHardcoded,RtlSymmetry" />
 
 
-</androidx.constraintlayout.widget.ConstraintLayout>
+    </androidx.constraintlayout.widget.ConstraintLayout>
+</com.google.android.material.card.MaterialCardView>

+ 11 - 4
app/src/main/res/layout/item_rss_article_1.xml

@@ -1,11 +1,17 @@
 <?xml version="1.0" encoding="utf-8"?>
 <?xml version="1.0" encoding="utf-8"?>
-<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
+<com.google.android.material.card.MaterialCardView xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:app="http://schemas.android.com/apk/res-auto"
     xmlns:app="http://schemas.android.com/apk/res-auto"
     xmlns:tools="http://schemas.android.com/tools"
     xmlns:tools="http://schemas.android.com/tools"
+    android:layout_margin="8dp"
     android:layout_width="match_parent"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:layout_height="wrap_content"
-    android:padding="16dp"
-    android:background="?attr/selectableItemBackground">
+    app:strokeWidth="0dp"
+    app:cardBackgroundColor="?attr/colorSurfaceContainer">
+    <androidx.constraintlayout.widget.ConstraintLayout
+        android:padding="8dp"
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content"
+        android:background="?attr/selectableItemBackground">
 
 
     <ImageView
     <ImageView
         android:id="@+id/image_view"
         android:id="@+id/image_view"
@@ -46,4 +52,5 @@
         app:layout_constraintRight_toRightOf="parent"
         app:layout_constraintRight_toRightOf="parent"
         tools:ignore="SmallSp" />
         tools:ignore="SmallSp" />
 
 
-</androidx.constraintlayout.widget.ConstraintLayout>
+    </androidx.constraintlayout.widget.ConstraintLayout>
+</com.google.android.material.card.MaterialCardView>

+ 48 - 41
app/src/main/res/layout/item_rss_article_2.xml

@@ -1,51 +1,58 @@
 <?xml version="1.0" encoding="utf-8"?>
 <?xml version="1.0" encoding="utf-8"?>
-<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
+<com.google.android.material.card.MaterialCardView xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:app="http://schemas.android.com/apk/res-auto"
     xmlns:app="http://schemas.android.com/apk/res-auto"
     xmlns:tools="http://schemas.android.com/tools"
     xmlns:tools="http://schemas.android.com/tools"
-    android:padding="8dp"
+    android:layout_margin="8dp"
     android:layout_width="match_parent"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:layout_height="wrap_content"
+    app:strokeWidth="0dp"
     android:background="?attr/selectableItemBackground">
     android:background="?attr/selectableItemBackground">
+    <androidx.constraintlayout.widget.ConstraintLayout
+        android:padding="8dp"
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content"
+        android:background="?attr/colorSurfaceContainer">
 
 
-    <ImageView
-        android:id="@+id/image_view"
-        android:layout_width="0dp"
-        android:layout_height="272dp"
-        android:contentDescription="@string/loading"
-        android:visibility="visible"
-        android:scaleType="centerCrop"
-        android:background="@drawable/bg_img_border"
-        app:layout_constraintTop_toTopOf="parent"
-        app:layout_constraintLeft_toLeftOf="parent"
-        app:layout_constraintRight_toRightOf="parent"
-        tools:ignore="RtlHardcoded,RtlSymmetry" />
+        <ImageView
+            android:id="@+id/image_view"
+            android:layout_width="0dp"
+            android:layout_height="240dp"
+            android:contentDescription="@string/loading"
+            android:visibility="visible"
+            android:scaleType="centerCrop"
+            android:background="@drawable/bg_img_border"
+            app:layout_constraintTop_toTopOf="parent"
+            app:layout_constraintLeft_toLeftOf="parent"
+            app:layout_constraintRight_toRightOf="parent"
+            tools:ignore="RtlHardcoded,RtlSymmetry" />
 
 
-    <TextView
-        android:id="@+id/tv_title"
-        android:layout_width="0dp"
-        android:layout_height="wrap_content"
-        android:maxLines="2"
-        android:ellipsize="end"
-        android:text="@string/app_name"
-        android:textSize="13sp"
-        android:paddingTop="8dp"
-        app:layout_constraintTop_toBottomOf="@+id/image_view"
-        app:layout_constraintLeft_toLeftOf="parent"
-        app:layout_constraintRight_toRightOf="parent" />
+        <TextView
+            android:id="@+id/tv_title"
+            android:layout_width="0dp"
+            android:layout_height="wrap_content"
+            android:maxLines="2"
+            android:ellipsize="end"
+            android:text="@string/app_name"
+            android:textSize="13sp"
+            android:paddingTop="8dp"
+            app:layout_constraintTop_toBottomOf="@+id/image_view"
+            app:layout_constraintLeft_toLeftOf="parent"
+            app:layout_constraintRight_toRightOf="parent" />
 
 
-    <TextView
-        android:id="@+id/tv_pub_date"
-        android:layout_width="0dp"
-        android:layout_height="wrap_content"
-        android:layout_gravity="center"
-        android:text="@string/app_name"
-        android:textSize="11sp"
-        android:maxLines="1"
-        android:paddingTop="4dp"
-        android:paddingBottom="2dp"
-        app:layout_constraintTop_toBottomOf="@+id/tv_title"
-        app:layout_constraintLeft_toLeftOf="parent"
-        app:layout_constraintRight_toRightOf="parent"
-        tools:ignore="SmallSp" />
+        <TextView
+            android:id="@+id/tv_pub_date"
+            android:layout_width="0dp"
+            android:layout_height="wrap_content"
+            android:layout_gravity="center"
+            android:text="@string/app_name"
+            android:textSize="11sp"
+            android:maxLines="1"
+            android:paddingTop="4dp"
+            android:paddingBottom="2dp"
+            app:layout_constraintTop_toBottomOf="@+id/tv_title"
+            app:layout_constraintLeft_toLeftOf="parent"
+            app:layout_constraintRight_toRightOf="parent"
+            tools:ignore="SmallSp" />
 
 
-</androidx.constraintlayout.widget.ConstraintLayout>
+    </androidx.constraintlayout.widget.ConstraintLayout>
+</com.google.android.material.card.MaterialCardView>

+ 58 - 0
app/src/main/res/layout/item_rss_article_3.xml

@@ -0,0 +1,58 @@
+<?xml version="1.0" encoding="utf-8"?>
+<com.google.android.material.card.MaterialCardView xmlns:android="http://schemas.android.com/apk/res/android"
+    xmlns:app="http://schemas.android.com/apk/res-auto"
+    xmlns:tools="http://schemas.android.com/tools"
+    android:layout_margin="8dp"
+    android:layout_width="match_parent"
+    android:layout_height="wrap_content"
+    app:strokeWidth="0dp"
+    app:cardBackgroundColor="?attr/colorSurfaceContainer">
+    <androidx.constraintlayout.widget.ConstraintLayout
+        android:padding="8dp"
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content"
+        android:background="?attr/selectableItemBackground">
+
+        <ImageView
+            android:id="@+id/image_view"
+            android:layout_width="match_parent"
+            android:layout_height="wrap_content"
+            android:contentDescription="@string/loading"
+            android:visibility="visible"
+            android:scaleType="centerCrop"
+            android:background="@drawable/bg_img_border"
+            app:layout_constraintTop_toTopOf="parent"
+            app:layout_constraintLeft_toLeftOf="parent"
+            app:layout_constraintRight_toRightOf="parent"
+            tools:ignore="RtlHardcoded,RtlSymmetry" />
+
+        <TextView
+            android:id="@+id/tv_title"
+            android:layout_width="0dp"
+            android:layout_height="wrap_content"
+            android:maxLines="3"
+            android:ellipsize="end"
+            android:text="@string/app_name"
+            android:textSize="13sp"
+            android:textStyle="bold"
+            android:paddingTop="8dp"
+            app:layout_constraintTop_toBottomOf="@+id/image_view"
+            app:layout_constraintLeft_toLeftOf="parent"
+            app:layout_constraintRight_toRightOf="parent" />
+
+        <TextView
+            android:id="@+id/tv_pub_date"
+            android:layout_width="0dp"
+            android:layout_height="wrap_content"
+            android:text="@string/app_name"
+            android:textSize="11sp"
+            android:maxLines="2"
+            android:paddingTop="4dp"
+            android:paddingBottom="2dp"
+            app:layout_constraintTop_toBottomOf="@+id/tv_title"
+            app:layout_constraintLeft_toLeftOf="parent"
+            app:layout_constraintRight_toRightOf="parent"
+            tools:ignore="SmallSp" />
+
+    </androidx.constraintlayout.widget.ConstraintLayout>
+</com.google.android.material.card.MaterialCardView>