From 1be4779df2cb610314ad0e6a2328fa402374dbdd Mon Sep 17 00:00:00 2001 From: Ashinch Date: Wed, 1 Jun 2022 17:29:01 +0800 Subject: [PATCH] Fix the synchronization bug when importing OPML files after version 0.8.0 (#92) --- .../java/me/ash/reader/data/dao/ArticleDao.kt | 75 +++++++------------ .../data/repository/LocalRssRepository.kt | 4 +- .../ash/reader/ui/page/home/feeds/FeedItem.kt | 6 +- .../reader/ui/page/home/feeds/FeedsPage.kt | 3 +- .../reader/ui/page/home/feeds/GroupItem.kt | 3 +- .../home/feeds/subscribe/SubscribeDialog.kt | 2 +- .../settings/color/feeds/FeedsPagePreview.kt | 3 +- 7 files changed, 37 insertions(+), 59 deletions(-) diff --git a/app/src/main/java/me/ash/reader/data/dao/ArticleDao.kt b/app/src/main/java/me/ash/reader/data/dao/ArticleDao.kt index b04dd1d..7505620 100644 --- a/app/src/main/java/me/ash/reader/data/dao/ArticleDao.kt +++ b/app/src/main/java/me/ash/reader/data/dao/ArticleDao.kt @@ -494,6 +494,20 @@ interface ArticleDao { ) suspend fun queryLatestByFeedId(accountId: Int, feedId: String): Article? + @Query( + """ + SELECT * from article + WHERE link = :link + AND feedId = :feedId + AND accountId = :accountId + """ + ) + suspend fun queryArticleByLink( + link: String, + feedId: String, + accountId: Int, + ): Article? + @Transaction @Query( """ @@ -509,56 +523,17 @@ interface ArticleDao { @Update suspend fun update(vararg article: Article) - @RewriteQueriesToDropUnusedColumns @Transaction - @Query( - """ - INSERT INTO article - SELECT :id, :date, :title, :author, :rawDescription, - :shortDescription, :fullContent, :link, :feedId, - :accountId, :isUnread, :isStarred, :isReadLater, :img - WHERE NOT EXISTS(SELECT 1 FROM article WHERE link = :link AND accountId = :accountId) - """ - ) - suspend fun insertIfNotExist( - id: String, - date: Date, - title: String, - author: String? = null, - rawDescription: String, - shortDescription: String, - fullContent: String? = null, - img: String? = null, - link: String, - feedId: String, - accountId: Int, - isUnread: Boolean = true, - isStarred: Boolean = false, - isReadLater: Boolean = false, - ): Long - - @Transaction - suspend fun insertIfNotExist(article: Article): Long { - return insertIfNotExist( - article.id, - article.date, - article.title, - article.author, - article.rawDescription, - article.shortDescription, - article.fullContent, - article.img, - article.link, - article.feedId, - article.accountId, - article.isUnread, - article.isStarred, - article.isReadLater, - ) - } - - @Transaction - suspend fun insertIfNotExist(articles: List
): List
{ - return articles.mapNotNull { if (insertIfNotExist(it) > 0) it else null } + suspend fun insertListIfNotExist(articles: List
): List
{ + return articles.mapNotNull { + if (queryArticleByLink( + link = it.link, + feedId = it.feedId, + accountId = it.accountId + ) == null + ) it else null + }.also { + insertList(it) + } } } \ No newline at end of file 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 b09d0ad..ef60fe6 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 @@ -83,11 +83,11 @@ class LocalRssRepository @Inject constructor( notificationHelper.notify( FeedWithArticle( it.feedWithArticle.feed, - articleDao.insertIfNotExist(it.feedWithArticle.articles) + articleDao.insertListIfNotExist(it.feedWithArticle.articles) ) ) } else { - articleDao.insertIfNotExist(it.feedWithArticle.articles) + articleDao.insertListIfNotExist(it.feedWithArticle.articles) } } Log.i("RlOG", "onCompletion: ${System.currentTimeMillis() - preTime}") diff --git a/app/src/main/java/me/ash/reader/ui/page/home/feeds/FeedItem.kt b/app/src/main/java/me/ash/reader/ui/page/home/feeds/FeedItem.kt index b854541..a6020e2 100644 --- a/app/src/main/java/me/ash/reader/ui/page/home/feeds/FeedItem.kt +++ b/app/src/main/java/me/ash/reader/ui/page/home/feeds/FeedItem.kt @@ -33,7 +33,7 @@ fun FeedItem( feed: Feed, alpha: Float = 1f, badgeAlpha: Float = 1f, - isEnded: Boolean = false, + isEnded: () -> Boolean, isExpanded: () -> Boolean, feedOptionViewModel: FeedOptionViewModel = hiltViewModel(), onClick: () -> Unit = {}, @@ -48,7 +48,7 @@ fun FeedItem( .padding(horizontal = 16.dp) .background( color = MaterialTheme.colorScheme.secondary.copy(alpha = alpha), - shape = if (isEnded) ShapeBottom32 else RectangleShape, + shape = if (isEnded()) ShapeBottom32 else RectangleShape, ) .combinedClickable( onClick = { @@ -60,7 +60,7 @@ fun FeedItem( } ) .padding(horizontal = 14.dp) - .padding(top = 14.dp, bottom = if (isEnded) 22.dp else 14.dp), + .padding(top = 14.dp, bottom = if (isEnded()) 22.dp else 14.dp), ) { Row( modifier = Modifier diff --git a/app/src/main/java/me/ash/reader/ui/page/home/feeds/FeedsPage.kt b/app/src/main/java/me/ash/reader/ui/page/home/feeds/FeedsPage.kt index b144f93..fb9dbc0 100644 --- a/app/src/main/java/me/ash/reader/ui/page/home/feeds/FeedsPage.kt +++ b/app/src/main/java/me/ash/reader/ui/page/home/feeds/FeedsPage.kt @@ -218,6 +218,7 @@ fun FeedsPage( group = groupWithFeed.group, alpha = groupAlpha, indicatorAlpha = groupIndicatorAlpha, + isEnded = { index == feedsUiState.groupWithFeedList.lastIndex }, onExpanded = { groupsVisible[groupWithFeed.group.id] = !(groupsVisible[groupWithFeed.group.id] ?: false) @@ -238,7 +239,7 @@ fun FeedsPage( feed = groupWithFeed.feed, alpha = groupAlpha, badgeAlpha = feedBadgeAlpha, - isEnded = index != feedsUiState.groupWithFeedList.lastIndex && feedsUiState.groupWithFeedList[index + 1] is GroupFeedsView.Group, + isEnded = { index == feedsUiState.groupWithFeedList.lastIndex || feedsUiState.groupWithFeedList[index + 1] is GroupFeedsView.Group }, isExpanded = { groupsVisible[groupWithFeed.feed.groupId] ?: false }, ) { filterChange( diff --git a/app/src/main/java/me/ash/reader/ui/page/home/feeds/GroupItem.kt b/app/src/main/java/me/ash/reader/ui/page/home/feeds/GroupItem.kt index e41ef52..13f20b6 100644 --- a/app/src/main/java/me/ash/reader/ui/page/home/feeds/GroupItem.kt +++ b/app/src/main/java/me/ash/reader/ui/page/home/feeds/GroupItem.kt @@ -35,6 +35,7 @@ fun GroupItem( group: Group, alpha: Float = 1f, indicatorAlpha: Float = 1f, + isEnded: () -> Boolean, isExpanded: () -> Boolean, groupOptionViewModel: GroupOptionViewModel = hiltViewModel(), onExpanded: () -> Unit = {}, @@ -48,7 +49,7 @@ fun GroupItem( .animateContentSize() .fillMaxWidth() .padding(horizontal = 16.dp) - .clip(if (isExpanded()) ShapeTop32 else Shape32) + .clip(if (isExpanded() && !isEnded()) ShapeTop32 else Shape32) .background( MaterialTheme.colorScheme.secondary.copy(alpha = alpha) ) diff --git a/app/src/main/java/me/ash/reader/ui/page/home/feeds/subscribe/SubscribeDialog.kt b/app/src/main/java/me/ash/reader/ui/page/home/feeds/subscribe/SubscribeDialog.kt index 9d122b2..4e44430 100644 --- a/app/src/main/java/me/ash/reader/ui/page/home/feeds/subscribe/SubscribeDialog.kt +++ b/app/src/main/java/me/ash/reader/ui/page/home/feeds/subscribe/SubscribeDialog.kt @@ -45,7 +45,7 @@ fun SubscribeDialog( val groupsState = subscribeUiState.groups.collectAsState(initial = emptyList()) val launcher = rememberLauncherForActivityResult(ActivityResultContracts.GetContent()) { it?.let { uri -> - context.contentResolver.openInputStream(uri)?.use { inputStream -> + context.contentResolver.openInputStream(uri)?.let { inputStream -> subscribeViewModel.importFromInputStream(inputStream) } } diff --git a/app/src/main/java/me/ash/reader/ui/page/settings/color/feeds/FeedsPagePreview.kt b/app/src/main/java/me/ash/reader/ui/page/settings/color/feeds/FeedsPagePreview.kt index 1408f40..f5e945c 100644 --- a/app/src/main/java/me/ash/reader/ui/page/settings/color/feeds/FeedsPagePreview.kt +++ b/app/src/main/java/me/ash/reader/ui/page/settings/color/feeds/FeedsPagePreview.kt @@ -94,6 +94,7 @@ fun FeedsPagePreview( ) Spacer(modifier = Modifier.height(12.dp)) GroupItem( + isEnded = { false }, isExpanded = { groupListExpand.value }, group = generateGroupPreview(), alpha = groupAlpha, @@ -103,7 +104,7 @@ fun FeedsPagePreview( feed = generateFeedPreview(), alpha = groupAlpha, badgeAlpha = feedBadgeAlpha, - isEnded = true, + isEnded = { true }, isExpanded = { true }, ) Spacer(modifier = Modifier.height(12.dp))