Skip to content

Commit

Permalink
style: reformat code
Browse files Browse the repository at this point in the history
  • Loading branch information
JunkFood02 committed Mar 8, 2024
1 parent d40743d commit 6b29a81
Showing 1 changed file with 119 additions and 112 deletions.
231 changes: 119 additions & 112 deletions app/src/main/java/me/ash/reader/domain/service/FeverRssService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -133,142 +133,149 @@ class FeverRssService @Inject constructor(
* 3. Fetch the Fever articles
* 4. Synchronize read/unread and starred/un-starred items
*/
override suspend fun sync(coroutineWorker: CoroutineWorker): ListenableWorker.Result = supervisorScope {
coroutineWorker.setProgress(SyncWorker.setIsSyncing(true))
override suspend fun sync(coroutineWorker: CoroutineWorker): ListenableWorker.Result =
supervisorScope {
coroutineWorker.setProgress(SyncWorker.setIsSyncing(true))

try {
val preTime = System.currentTimeMillis()
val preDate = Date(preTime)
val accountId = context.currentAccountId
val account = accountDao.queryById(accountId)!!
val feverAPI = getFeverAPI()
try {
val preTime = System.currentTimeMillis()
val preDate = Date(preTime)
val accountId = context.currentAccountId
val account = accountDao.queryById(accountId)!!
val feverAPI = getFeverAPI()

// 1. Fetch the Fever groups
val groups = feverAPI.getGroups().groups?.map {
Group(
id = accountId.spacerDollar(it.id!!),
name = it.title ?: context.getString(R.string.empty),
accountId = accountId,
)
} ?: emptyList()
groupDao.insertOrUpdate(groups)

// 2. Fetch the Fever feeds
val feedsBody = feverAPI.getFeeds()
val feedsGroupsMap = mutableMapOf<String, String>()
feedsBody.feeds_groups?.forEach { feedsGroups ->
feedsGroups.group_id?.toString()?.let { groupId ->
feedsGroups.feed_ids?.split(",")?.forEach { feedId ->
feedsGroupsMap[feedId] = groupId
}
}
}

// Fetch the Fever favicons
val faviconsById = feverAPI.getFavicons().favicons?.associateBy { it.id } ?: emptyMap()
feedDao.insertOrUpdate(
feedsBody.feeds?.map {
Feed(
// 1. Fetch the Fever groups
val groups = feverAPI.getGroups().groups?.map {
Group(
id = accountId.spacerDollar(it.id!!),
name = it.title.decodeHTML() ?: context.getString(R.string.empty),
url = it.url!!,
groupId = accountId.spacerDollar(feedsGroupsMap[it.id.toString()]!!),
name = it.title ?: context.getString(R.string.empty),
accountId = accountId,
icon = faviconsById[it.favicon_id]?.data
)
} ?: emptyList()
)
groupDao.insertOrUpdate(groups)

// Handle empty icon for feeds
val noIconFeeds = feedDao.queryNoIcon(accountId)
noIconFeeds.forEach {
it.icon = rssHelper.queryRssIconLink(it.url)
}
feedDao.update(*noIconFeeds.toTypedArray())
// 2. Fetch the Fever feeds
val feedsBody = feverAPI.getFeeds()
val feedsGroupsMap = mutableMapOf<String, String>()
feedsBody.feeds_groups?.forEach { feedsGroups ->
feedsGroups.group_id?.toString()?.let { groupId ->
feedsGroups.feed_ids?.split(",")?.forEach { feedId ->
feedsGroupsMap[feedId] = groupId
}
}
}

// 3. Fetch the Fever articles (up to unlimited counts)
var sinceId = account.lastArticleId?.dollarLast() ?: ""
var itemsBody = feverAPI.getItemsSince(sinceId)
while (itemsBody.items?.isNotEmpty() == true) {
articleDao.insert(
*itemsBody.items?.map {
Article(
// Fetch the Fever favicons
val faviconsById =
feverAPI.getFavicons().favicons?.associateBy { it.id } ?: emptyMap()
feedDao.insertOrUpdate(
feedsBody.feeds?.map {
Feed(
id = accountId.spacerDollar(it.id!!),
date = it.created_on_time
?.run { Date(this * 1000) }
?.takeIf { !it.isFuture(preDate) }
?: preDate,
title = it.title.decodeHTML() ?: context.getString(R.string.empty),
author = it.author,
rawDescription = it.html ?: "",
shortDescription = Readability.parseToText(it.html, it.url).take(110),
fullContent = it.html,
img = rssHelper.findImg(it.html ?: ""),
link = it.url ?: "",
feedId = accountId.spacerDollar(it.feed_id!!),
name = it.title.decodeHTML() ?: context.getString(R.string.empty),
url = it.url!!,
groupId = accountId.spacerDollar(feedsGroupsMap[it.id.toString()]!!),
accountId = accountId,
isUnread = (it.is_read ?: 0) <= 0,
isStarred = (it.is_saved ?: 0) > 0,
updateAt = preDate,
).also {
sinceId = it.id.dollarLast()
}
}?.toTypedArray() ?: emptyArray()
icon = faviconsById[it.favicon_id]?.data
)
} ?: emptyList()
)
if (itemsBody.items?.size!! >= 50) {
itemsBody = feverAPI.getItemsSince(sinceId)
} else {
break

// Handle empty icon for feeds
val noIconFeeds = feedDao.queryNoIcon(accountId)
noIconFeeds.forEach {
it.icon = rssHelper.queryRssIconLink(it.url)
}
}
feedDao.update(*noIconFeeds.toTypedArray())

// 4. Synchronize read/unread and starred/un-starred
val unreadArticleIds = feverAPI.getUnreadItems().unread_item_ids?.split(",")
val starredArticleIds = feverAPI.getSavedItems().saved_item_ids?.split(",")
val articleMeta = articleDao.queryMetadataAll(accountId)
for (meta: ArticleMeta in articleMeta) {
val articleId = meta.id.dollarLast()
val shouldBeUnread = unreadArticleIds?.contains(articleId)
val shouldBeStarred = starredArticleIds?.contains(articleId)
if (meta.isUnread != shouldBeUnread) {
articleDao.markAsReadByArticleId(accountId, meta.id, shouldBeUnread ?: true)
// 3. Fetch the Fever articles (up to unlimited counts)
var sinceId = account.lastArticleId?.dollarLast() ?: ""
var itemsBody = feverAPI.getItemsSince(sinceId)
while (itemsBody.items?.isNotEmpty() == true) {
articleDao.insert(
*itemsBody.items?.map {
Article(
id = accountId.spacerDollar(it.id!!),
date = it.created_on_time
?.run { Date(this * 1000) }
?.takeIf { !it.isFuture(preDate) }
?: preDate,
title = it.title.decodeHTML() ?: context.getString(R.string.empty),
author = it.author,
rawDescription = it.html ?: "",
shortDescription = Readability.parseToText(it.html, it.url)
.take(110),
fullContent = it.html,
img = rssHelper.findImg(it.html ?: ""),
link = it.url ?: "",
feedId = accountId.spacerDollar(it.feed_id!!),
accountId = accountId,
isUnread = (it.is_read ?: 0) <= 0,
isStarred = (it.is_saved ?: 0) > 0,
updateAt = preDate,
).also {
sinceId = it.id.dollarLast()
}
}?.toTypedArray() ?: emptyArray()
)
if (itemsBody.items?.size!! >= 50) {
itemsBody = feverAPI.getItemsSince(sinceId)
} else {
break
}
}
if (meta.isStarred != shouldBeStarred) {
articleDao.markAsStarredByArticleId(accountId, meta.id, shouldBeStarred ?: false)

// 4. Synchronize read/unread and starred/un-starred
val unreadArticleIds = feverAPI.getUnreadItems().unread_item_ids?.split(",")
val starredArticleIds = feverAPI.getSavedItems().saved_item_ids?.split(",")
val articleMeta = articleDao.queryMetadataAll(accountId)
for (meta: ArticleMeta in articleMeta) {
val articleId = meta.id.dollarLast()
val shouldBeUnread = unreadArticleIds?.contains(articleId)
val shouldBeStarred = starredArticleIds?.contains(articleId)
if (meta.isUnread != shouldBeUnread) {
articleDao.markAsReadByArticleId(accountId, meta.id, shouldBeUnread ?: true)
}
if (meta.isStarred != shouldBeStarred) {
articleDao.markAsStarredByArticleId(
accountId,
meta.id,
shouldBeStarred ?: false
)
}
}
}

// Remove orphaned groups and feeds, after synchronizing the starred/un-starred
val groupIds = groups.map { it.id }
groupDao.queryAll(accountId).forEach {
if (!groupIds.contains(it.id)) {
super.deleteGroup(it, true)
// Remove orphaned groups and feeds, after synchronizing the starred/un-starred
val groupIds = groups.map { it.id }
groupDao.queryAll(accountId).forEach {
if (!groupIds.contains(it.id)) {
super.deleteGroup(it, true)
}
}
}

feedDao.queryAll(accountId).forEach {
if (!feedsGroupsMap.contains(it.id.dollarLast())) {
super.deleteFeed(it, true)
feedDao.queryAll(accountId).forEach {
if (!feedsGroupsMap.contains(it.id.dollarLast())) {
super.deleteFeed(it, true)
}
}
}


Log.i("RLog", "onCompletion: ${System.currentTimeMillis() - preTime}")
accountDao.update(account.apply {
updateAt = Date()
if (sinceId.isNotEmpty()) {
lastArticleId = accountId.spacerDollar(sinceId)
Log.i("RLog", "onCompletion: ${System.currentTimeMillis() - preTime}")
accountDao.update(account.apply {
updateAt = Date()
if (sinceId.isNotEmpty()) {
lastArticleId = accountId.spacerDollar(sinceId)
}
})
ListenableWorker.Result.success(SyncWorker.setIsSyncing(false))
} catch (e: Exception) {
Log.e("RLog", "On sync exception: ${e.message}", e)
withContext(mainDispatcher) {
context.showToast(e.message)
}
})
ListenableWorker.Result.success(SyncWorker.setIsSyncing(false))
} catch (e: Exception) {
Log.e("RLog", "On sync exception: ${e.message}", e)
withContext(mainDispatcher) {
context.showToast(e.message)
ListenableWorker.Result.failure(SyncWorker.setIsSyncing(false))
}
ListenableWorker.Result.failure(SyncWorker.setIsSyncing(false))
}
}

override suspend fun markAsRead(
groupId: String?,
Expand Down

0 comments on commit 6b29a81

Please sign in to comment.