Skip to content

Commit

Permalink
[feature] Support download enclosures in article screen
Browse files Browse the repository at this point in the history
  • Loading branch information
SkyD666 committed Apr 16, 2024
1 parent 1c5e6c9 commit 5dd78f3
Show file tree
Hide file tree
Showing 10 changed files with 137 additions and 72 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ android {
minSdk = 24
targetSdk = 34
versionCode = 10
versionName = "1.1-beta06"
versionName = "1.1-beta07"

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"

Expand Down
6 changes: 3 additions & 3 deletions app/src/main/java/com/skyd/anivu/model/db/dao/ArticleDao.kt
Original file line number Diff line number Diff line change
Expand Up @@ -101,15 +101,15 @@ interface ArticleDao {
ORDER BY ${ArticleBean.DATE_COLUMN} DESC
"""
)
fun getArticlePagingSource(feedUrl: String): PagingSource<Int, ArticleBean>
fun getArticlePagingSource(feedUrl: String): PagingSource<Int, ArticleWithEnclosureBean>

@Transaction
@RawQuery(observedEntities = [ArticleBean::class])
fun getArticlePagingSource(sql: SupportSQLiteQuery): PagingSource<Int, ArticleBean>
fun getArticlePagingSource(sql: SupportSQLiteQuery): PagingSource<Int, ArticleWithEnclosureBean>

@Transaction
@RawQuery(observedEntities = [ArticleBean::class])
fun getArticleList(sql: SupportSQLiteQuery): List<ArticleBean>
fun getArticleList(sql: SupportSQLiteQuery): List<ArticleWithEnclosureBean>

@Transaction
@Query(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import androidx.paging.PagingConfig
import androidx.paging.PagingData
import com.skyd.anivu.base.BaseRepository
import com.skyd.anivu.model.bean.ArticleBean
import com.skyd.anivu.model.bean.ArticleWithEnclosureBean
import com.skyd.anivu.model.db.dao.ArticleDao
import com.skyd.anivu.model.db.dao.FeedDao
import kotlinx.coroutines.Dispatchers
Expand All @@ -19,7 +20,7 @@ class ArticleRepository @Inject constructor(
private val rssHelper: RssHelper,
private val pagingConfig: PagingConfig,
) : BaseRepository() {
fun requestArticleList(feedUrl: String): Flow<PagingData<ArticleBean>> {
fun requestArticleList(feedUrl: String): Flow<PagingData<ArticleWithEnclosureBean>> {
return Pager(pagingConfig) {
articleDao.getArticlePagingSource(feedUrl)
}.flow.flowOn(Dispatchers.IO)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import com.skyd.anivu.ext.dataStore
import com.skyd.anivu.ext.getOrDefault
import com.skyd.anivu.model.bean.ARTICLE_TABLE_NAME
import com.skyd.anivu.model.bean.ArticleBean
import com.skyd.anivu.model.bean.ArticleWithEnclosureBean
import com.skyd.anivu.model.bean.FEED_TABLE_NAME
import com.skyd.anivu.model.bean.FeedBean
import com.skyd.anivu.model.db.dao.ArticleDao
Expand Down Expand Up @@ -92,7 +93,7 @@ class SearchRepository @Inject constructor(
fun requestSearchArticle(
feedUrl: String? = null,
query: String,
): Flow<PagingData<ArticleBean>> {
): Flow<PagingData<ArticleWithEnclosureBean>> {
return flow {
emit(
genSql(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,68 +3,102 @@ package com.skyd.anivu.ui.adapter.variety.proxy

import android.os.Bundle
import android.view.LayoutInflater
import android.view.MenuItem
import android.view.ViewGroup
import androidx.appcompat.widget.PopupMenu
import androidx.fragment.app.FragmentActivity
import com.skyd.anivu.R
import com.skyd.anivu.databinding.ItemArticle1Binding
import com.skyd.anivu.ext.findMainNavController
import com.skyd.anivu.ext.gone
import com.skyd.anivu.ext.readable
import com.skyd.anivu.ext.toDateTimeString
import com.skyd.anivu.ext.toHtml
import com.skyd.anivu.ext.tryActivity
import com.skyd.anivu.ext.tryAddIcon
import com.skyd.anivu.ext.visible
import com.skyd.anivu.model.bean.ArticleBean
import com.skyd.anivu.model.bean.ArticleWithEnclosureBean
import com.skyd.anivu.ui.adapter.variety.Article1ViewHolder
import com.skyd.anivu.ui.adapter.variety.VarietyAdapter
import com.skyd.anivu.ui.fragment.read.EnclosureBottomSheet
import com.skyd.anivu.ui.fragment.read.ReadFragment
import com.skyd.anivu.util.CoilUtil.loadImage

class Article1Proxy : VarietyAdapter.Proxy<ArticleBean, ItemArticle1Binding, Article1ViewHolder>() {
class Article1Proxy :
VarietyAdapter.Proxy<ArticleWithEnclosureBean, ItemArticle1Binding, Article1ViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) = Article1ViewHolder(
ItemArticle1Binding.inflate(LayoutInflater.from(parent.context), parent, false)
)

override fun onBindViewHolder(
holder: Article1ViewHolder,
data: ArticleBean,
data: ArticleWithEnclosureBean,
index: Int,
action: ((Any?) -> Unit)?
) {
val activity = holder.itemView.tryActivity
val article = data.article
holder.binding.apply {
tvArticle1Title.text = data.title?.toHtml()
data.description?.readable().let { description ->
tvArticle1Title.text = article.title?.toHtml()
article.description?.readable().let { description ->
if (description.isNullOrBlank()) {
tvArticle1Desc.gone()
} else {
tvArticle1Desc.visible()
tvArticle1Desc.text = description
}
}
data.date?.toDateTimeString().let { dateTime ->
article.date?.toDateTimeString().let { dateTime ->
if (dateTime.isNullOrBlank()) {
tvArticle1Date.gone()
} else {
tvArticle1Date.visible()
tvArticle1Date.text = dateTime
}
}
data.author.let { author ->
article.author.let { author ->
if (author.isNullOrBlank()) {
tvArticle1Author.gone()
} else {
tvArticle1Author.visible()
tvArticle1Author.text = author
}
}
if (data.image.isNullOrBlank()) {
if (article.image.isNullOrBlank()) {
cvArticle1Image.gone()
} else {
cvArticle1Image.visible()
ivArticle1Image.loadImage(data.image)
ivArticle1Image.loadImage(article.image)
}

btnArticle1Options.setOnClickListener { v ->
val popup = PopupMenu(v.context, v)
popup.menuInflater.inflate(R.menu.menu_article_item, popup.menu)
popup.setOnMenuItemClickListener { menuItem: MenuItem ->
when (menuItem.itemId) {
R.id.action_article_item_enclosures -> {
EnclosureBottomSheet().apply {
show(
(activity as FragmentActivity).supportFragmentManager,
EnclosureBottomSheet.TAG
)
updateData(
ReadFragment.getEnclosuresList(holder.itemView.context, data)
)
}
true
}

else -> false
}
}
popup.menu.tryAddIcon(v.context)
popup.show()
}
}
holder.itemView.setOnClickListener {
val bundle = Bundle().apply {
putString(ReadFragment.ARTICLE_ID_KEY, data.articleId)
putString(ReadFragment.ARTICLE_ID_KEY, article.articleId)
}
it.findMainNavController().navigate(R.id.action_to_read_fragment, bundle)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.skyd.anivu.ui.fragment.article

import androidx.paging.PagingData
import com.skyd.anivu.model.bean.ArticleBean
import com.skyd.anivu.model.bean.ArticleWithEnclosureBean


internal sealed interface ArticlePartialStateChange {
Expand Down Expand Up @@ -34,7 +34,9 @@ internal sealed interface ArticlePartialStateChange {
}
}

data class Success(val articlePagingData: PagingData<ArticleBean>) : ArticleList
data class Success(val articlePagingData: PagingData<ArticleWithEnclosureBean>) :
ArticleList

data class Failed(val msg: String) : ArticleList
data object Loading : ArticleList
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package com.skyd.anivu.ui.fragment.article

import androidx.paging.PagingData
import com.skyd.anivu.base.mvi.MviViewState
import com.skyd.anivu.model.bean.ArticleBean
import com.skyd.anivu.model.bean.ArticleWithEnclosureBean

data class ArticleState(
val articleListState: ArticleListState,
Expand All @@ -17,7 +17,7 @@ data class ArticleState(
}

sealed class ArticleListState(var loading: Boolean = false) {
class Success(val articlePagingData: PagingData<ArticleBean>) : ArticleListState()
class Success(val articlePagingData: PagingData<ArticleWithEnclosureBean>) : ArticleListState()
data object Init : ArticleListState()
data class Failed(val msg: String) : ArticleListState()
}
45 changes: 23 additions & 22 deletions app/src/main/java/com/skyd/anivu/ui/fragment/read/ReadFragment.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.skyd.anivu.ui.fragment.read

import android.content.Context
import android.os.Bundle
import android.text.method.LinkMovementMethod
import android.view.LayoutInflater
Expand All @@ -22,6 +23,7 @@ import com.skyd.anivu.ext.openBrowser
import com.skyd.anivu.ext.popBackStackWithLifecycle
import com.skyd.anivu.ext.startWith
import com.skyd.anivu.ext.toHtml
import com.skyd.anivu.model.bean.ArticleWithEnclosureBean
import com.skyd.anivu.model.bean.LinkEnclosureBean
import com.skyd.anivu.model.preference.rss.ParseLinkTagAsEnclosurePreference
import com.skyd.anivu.model.worker.download.doIfMagnetOrTorrentLink
Expand All @@ -38,6 +40,23 @@ import kotlinx.coroutines.flow.onEach
class ReadFragment : BaseFragment<FragmentReadBinding>() {
companion object {
const val ARTICLE_ID_KEY = "articleId"

fun getEnclosuresList(
context: Context,
articleWithEnclosureBean: ArticleWithEnclosureBean,
): List<Any> {
val dataList: MutableList<Any> = articleWithEnclosureBean.enclosures.toMutableList()
if (context.dataStore.getOrDefault(ParseLinkTagAsEnclosurePreference)) {
articleWithEnclosureBean.article.link?.let { link ->
doIfMagnetOrTorrentLink(
link = link,
onMagnet = { dataList += LinkEnclosureBean(link = link) },
onTorrent = { dataList += LinkEnclosureBean(link = link) },
)
}
}
return dataList
}
}

private val feedViewModel by viewModels<ReadViewModel>()
Expand Down Expand Up @@ -88,17 +107,7 @@ class ReadFragment : BaseFragment<FragmentReadBinding>() {
)
}

val dataList: MutableList<Any> = article.enclosures.toMutableList()
if (requireContext().dataStore.getOrDefault(ParseLinkTagAsEnclosurePreference)) {
article.article.link?.let { link ->
doIfMagnetOrTorrentLink(
link = link,
onMagnet = { dataList += LinkEnclosureBean(link = link) },
onTorrent = { dataList += LinkEnclosureBean(link = link) },
)
}
}
enclosureBottomSheet?.updateData(dataList)
enclosureBottomSheet?.updateData(getEnclosuresList(requireContext(), article))
}
}
}
Expand Down Expand Up @@ -164,17 +173,9 @@ class ReadFragment : BaseFragment<FragmentReadBinding>() {
)
val articleState = feedViewModel.viewState.value.articleState
if (articleState is ArticleState.Success) {
val dataList: MutableList<Any> = articleState.article.enclosures.toMutableList()
if (requireContext().dataStore.getOrDefault(ParseLinkTagAsEnclosurePreference)) {
articleState.article.article.link?.let { link ->
doIfMagnetOrTorrentLink(
link = link,
onMagnet = { dataList += LinkEnclosureBean(link = link) },
onTorrent = { dataList += LinkEnclosureBean(link = link) },
)
}
}
enclosureBottomSheet?.updateData(dataList)
enclosureBottomSheet?.updateData(
getEnclosuresList(requireContext(), articleState.article)
)
}
}
}
Expand Down
Loading

0 comments on commit 5dd78f3

Please sign in to comment.