Merge the workflow of search feed

This commit is contained in:
Ash 2022-03-29 21:00:58 +08:00
parent 8cefff6a83
commit 794d898514
4 changed files with 34 additions and 69 deletions

View File

@ -6,7 +6,6 @@ import android.app.NotificationManager
import android.app.PendingIntent import android.app.PendingIntent
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import android.os.Build
import android.util.Log import android.util.Log
import androidx.core.app.NotificationCompat import androidx.core.app.NotificationCompat
import androidx.core.content.ContextCompat.getSystemService import androidx.core.content.ContextCompat.getSystemService
@ -52,7 +51,6 @@ class LocalRssRepository @Inject constructor(
context, context,
NotificationManager::class.java NotificationManager::class.java
) as NotificationManager).also { ) as NotificationManager).also {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
it.createNotificationChannel( it.createNotificationChannel(
NotificationChannel( NotificationChannel(
NotificationGroupName.ARTICLE_UPDATE, NotificationGroupName.ARTICLE_UPDATE,
@ -61,7 +59,6 @@ class LocalRssRepository @Inject constructor(
) )
) )
} }
}
override suspend fun updateArticleInfo(article: Article) { override suspend fun updateArticleInfo(article: Article) {
articleDao.update(article) articleDao.update(article)
@ -92,17 +89,11 @@ class LocalRssRepository @Inject constructor(
val preTime = System.currentTimeMillis() val preTime = System.currentTimeMillis()
val accountId = context.currentAccountId val accountId = context.currentAccountId
val articles = mutableListOf<Article>() val articles = mutableListOf<Article>()
feedDao.queryAll(accountId).also { feed -> feedDao.queryAll(accountId)
updateSyncState { .also { feed -> updateSyncState { it.copy(feedCount = feed.size) } }
it.copy( .map { feed -> async { syncFeed(feed) } }
feedCount = feed.size, .awaitAll()
) .forEach {
}
}.map { feed ->
async {
syncFeed(accountId, feed)
}
}.awaitAll().forEach {
if (it.isNotify) { if (it.isNotify) {
notify(it.articles) notify(it.articles)
} }
@ -110,10 +101,7 @@ class LocalRssRepository @Inject constructor(
} }
articleDao.insertList(articles) articleDao.insertList(articles)
Log.i( Log.i("RlOG", "onCompletion: ${System.currentTimeMillis() - preTime}")
"RlOG",
"onCompletion: ${System.currentTimeMillis() - preTime}"
)
accountDao.queryById(accountId)?.let { account -> accountDao.queryById(accountId)?.let { account ->
accountDao.update( accountDao.update(
account.apply { account.apply {
@ -137,17 +125,9 @@ class LocalRssRepository @Inject constructor(
val isNotify: Boolean, val isNotify: Boolean,
) )
private suspend fun syncFeed( private suspend fun syncFeed(feed: Feed): ArticleNotify {
accountId: Int, val latest = articleDao.queryLatestByFeedId(context.currentAccountId, feed.id)
feed: Feed val articles = rssHelper.queryRssXml(feed, latest?.link).also {
): ArticleNotify {
val latest = articleDao.queryLatestByFeedId(accountId, feed.id)
val articles = rssHelper.queryRssXml(
rssNetworkDataSource,
accountId,
feed,
latest?.link,
).also {
if (feed.icon == null && it.isNotEmpty()) { if (feed.icon == null && it.isNotEmpty()) {
rssHelper.queryRssIcon(feedDao, feed, it.first().link) rssHelper.queryRssIcon(feedDao, feed, it.first().link)
} }

View File

@ -1,6 +1,7 @@
package me.ash.reader.data.repository package me.ash.reader.data.repository
import android.content.Context import android.content.Context
import android.text.Html
import android.util.Log import android.util.Log
import dagger.hilt.android.qualifiers.ApplicationContext import dagger.hilt.android.qualifiers.ApplicationContext
import me.ash.reader.currentAccountId import me.ash.reader.currentAccountId
@ -35,30 +36,7 @@ class RssHelper @Inject constructor(
groupId = "", groupId = "",
accountId = accountId, accountId = accountId,
) )
val articles = mutableListOf<Article>() return FeedWithArticle(feed, queryRssXml(feed))
parseRss.items.forEach {
articles.add(
Article(
id = accountId.spacerDollar(UUID.randomUUID().toString()),
accountId = accountId,
feedId = feed.id,
date = it.publishDate.toString().let {
try {
Date(it)
} catch (e: IllegalArgumentException) {
parseDate(it) ?: Date()
}
},
title = it.title.toString(),
author = it.author,
rawDescription = it.description.toString(),
shortDescription = (Readability4JExtended("", it.description.toString())
.parse().textContent ?: "").take(100).trim(),
link = it.link ?: "",
)
)
}
return FeedWithArticle(feed, articles)
} }
fun parseDescriptionContent(link: String, content: String): String { fun parseDescriptionContent(link: String, content: String): String {
@ -95,13 +73,12 @@ class RssHelper @Inject constructor(
} }
suspend fun queryRssXml( suspend fun queryRssXml(
rssNetworkDataSource: RssNetworkDataSource,
accountId: Int,
feed: Feed, feed: Feed,
latestLink: String? = null, latestLink: String? = null,
): List<Article> { ): List<Article> {
val a = mutableListOf<Article>() val a = mutableListOf<Article>()
try { try {
val accountId = context.currentAccountId
val parseRss = rssNetworkDataSource.parseRss(feed.url) val parseRss = rssNetworkDataSource.parseRss(feed.url)
parseRss.items.forEach { parseRss.items.forEach {
if (latestLink != null && latestLink == it.link) return a if (latestLink != null && latestLink == it.link) return a
@ -111,8 +88,14 @@ class RssHelper @Inject constructor(
id = accountId.spacerDollar(UUID.randomUUID().toString()), id = accountId.spacerDollar(UUID.randomUUID().toString()),
accountId = accountId, accountId = accountId,
feedId = feed.id, feedId = feed.id,
date = Date(it.publishDate.toString()), date = it.publishDate.toString().let {
title = it.title.toString(), try {
Date(it)
} catch (e: IllegalArgumentException) {
parseDate(it) ?: Date()
}
},
title = Html.fromHtml(it.title.toString()).toString(),
author = it.author, author = it.author,
rawDescription = it.description.toString(), rawDescription = it.description.toString(),
shortDescription = (Readability4JExtended("", it.description.toString()) shortDescription = (Readability4JExtended("", it.description.toString())

View File

@ -13,6 +13,7 @@ import androidx.compose.material.icons.rounded.Search
import androidx.compose.material3.* import androidx.compose.material3.*
import androidx.compose.runtime.* import androidx.compose.runtime.*
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.alpha
import androidx.compose.ui.input.pointer.pointerInput import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource import androidx.compose.ui.res.stringResource
@ -132,7 +133,7 @@ fun FlowPage(
Crossfade(targetState = pagingItems) { pagingItems -> Crossfade(targetState = pagingItems) { pagingItems ->
if (pagingItems.loadState.source.refresh is LoadState.NotLoading && pagingItems.itemCount == 0) { if (pagingItems.loadState.source.refresh is LoadState.NotLoading && pagingItems.itemCount == 0) {
LottieAnimation( LottieAnimation(
modifier = Modifier.padding(80.dp), modifier = Modifier.alpha(0.7f).padding(80.dp),
url = "https://assets7.lottiefiles.com/packages/lf20_l4ny0jjm.json", url = "https://assets7.lottiefiles.com/packages/lf20_l4ny0jjm.json",
) )
} }

View File

@ -14,6 +14,7 @@ import androidx.compose.material3.*
import androidx.compose.runtime.* import androidx.compose.runtime.*
import androidx.compose.ui.Alignment import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.alpha
import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
@ -208,7 +209,7 @@ private fun Content(
if (articleWithFeed == null) { if (articleWithFeed == null) {
Spacer(modifier = Modifier.height(64.dp)) Spacer(modifier = Modifier.height(64.dp))
LottieAnimation( LottieAnimation(
modifier = Modifier.padding(80.dp), modifier = Modifier.alpha(0.7f).padding(80.dp),
url = "https://assets8.lottiefiles.com/packages/lf20_jm7mv1ib.json", url = "https://assets8.lottiefiles.com/packages/lf20_jm7mv1ib.json",
) )
} else { } else {