From 794d8985149644a2ccd5d39a88a19c70a96a3f8a Mon Sep 17 00:00:00 2001 From: Ash Date: Tue, 29 Mar 2022 21:00:58 +0800 Subject: [PATCH] Merge the workflow of search feed --- .../data/repository/LocalRssRepository.kt | 58 ++++++------------- .../ash/reader/data/repository/RssHelper.kt | 39 ++++--------- .../ash/reader/ui/page/home/flow/FlowPage.kt | 3 +- .../ash/reader/ui/page/home/read/ReadPage.kt | 3 +- 4 files changed, 34 insertions(+), 69 deletions(-) diff --git a/app/src/main/java/me/ash/reader/data/repository/LocalRssRepository.kt b/app/src/main/java/me/ash/reader/data/repository/LocalRssRepository.kt index e995ed5..503502d 100644 --- a/app/src/main/java/me/ash/reader/data/repository/LocalRssRepository.kt +++ b/app/src/main/java/me/ash/reader/data/repository/LocalRssRepository.kt @@ -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
() - 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) } diff --git a/app/src/main/java/me/ash/reader/data/repository/RssHelper.kt b/app/src/main/java/me/ash/reader/data/repository/RssHelper.kt index cc7aaaf..f19fe8d 100644 --- a/app/src/main/java/me/ash/reader/data/repository/RssHelper.kt +++ b/app/src/main/java/me/ash/reader/data/repository/RssHelper.kt @@ -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
() - 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
{ val a = mutableListOf
() 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()) diff --git a/app/src/main/java/me/ash/reader/ui/page/home/flow/FlowPage.kt b/app/src/main/java/me/ash/reader/ui/page/home/flow/FlowPage.kt index 3b0a5bb..2d94cd6 100644 --- a/app/src/main/java/me/ash/reader/ui/page/home/flow/FlowPage.kt +++ b/app/src/main/java/me/ash/reader/ui/page/home/flow/FlowPage.kt @@ -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", ) } diff --git a/app/src/main/java/me/ash/reader/ui/page/home/read/ReadPage.kt b/app/src/main/java/me/ash/reader/ui/page/home/read/ReadPage.kt index c8bd3ed..2686e3e 100644 --- a/app/src/main/java/me/ash/reader/ui/page/home/read/ReadPage.kt +++ b/app/src/main/java/me/ash/reader/ui/page/home/read/ReadPage.kt @@ -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 {