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

View File

@ -1,6 +1,7 @@
package me.ash.reader.data.repository
import android.content.Context
import android.text.Html
import android.util.Log
import dagger.hilt.android.qualifiers.ApplicationContext
import me.ash.reader.currentAccountId
@ -35,30 +36,7 @@ class RssHelper @Inject constructor(
groupId = "",
accountId = accountId,
)
val articles = mutableListOf<Article>()
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)
return FeedWithArticle(feed, queryRssXml(feed))
}
fun parseDescriptionContent(link: String, content: String): String {
@ -95,13 +73,12 @@ class RssHelper @Inject constructor(
}
suspend fun queryRssXml(
rssNetworkDataSource: RssNetworkDataSource,
accountId: Int,
feed: Feed,
latestLink: String? = null,
): List<Article> {
val a = mutableListOf<Article>()
try {
val accountId = context.currentAccountId
val parseRss = rssNetworkDataSource.parseRss(feed.url)
parseRss.items.forEach {
if (latestLink != null && latestLink == it.link) return a
@ -111,8 +88,14 @@ class RssHelper @Inject constructor(
id = accountId.spacerDollar(UUID.randomUUID().toString()),
accountId = accountId,
feedId = feed.id,
date = Date(it.publishDate.toString()),
title = it.title.toString(),
date = it.publishDate.toString().let {
try {
Date(it)
} catch (e: IllegalArgumentException) {
parseDate(it) ?: Date()
}
},
title = Html.fromHtml(it.title.toString()).toString(),
author = it.author,
rawDescription = 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.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.alpha
import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
@ -132,7 +133,7 @@ fun FlowPage(
Crossfade(targetState = pagingItems) { pagingItems ->
if (pagingItems.loadState.source.refresh is LoadState.NotLoading && pagingItems.itemCount == 0) {
LottieAnimation(
modifier = Modifier.padding(80.dp),
modifier = Modifier.alpha(0.7f).padding(80.dp),
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.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.alpha
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
@ -208,7 +209,7 @@ private fun Content(
if (articleWithFeed == null) {
Spacer(modifier = Modifier.height(64.dp))
LottieAnimation(
modifier = Modifier.padding(80.dp),
modifier = Modifier.alpha(0.7f).padding(80.dp),
url = "https://assets8.lottiefiles.com/packages/lf20_jm7mv1ib.json",
)
} else {