Add items and items ids insertion for Fever synchronisation
This commit is contained in:
parent
701c188cfa
commit
fe57d5b2d7
@ -40,13 +40,14 @@ class FeverItemsAdapter {
|
||||
with(item) {
|
||||
when (selectName(NAMES)) {
|
||||
0 -> remoteId = nextNonEmptyString()
|
||||
1 -> feedRemoteId = nextInt().toString()
|
||||
1 -> feedRemoteId = nextNonEmptyString()
|
||||
2 -> title = nextNonEmptyString()
|
||||
3 -> author = nextNullableString()
|
||||
4 -> content = nextNullableString()
|
||||
5 -> link = nextNullableString()
|
||||
6 -> isRead = nextInt().toBoolean()
|
||||
7 -> pubDate = LocalDateTime(nextLong() * 1000L)
|
||||
7 -> isStarred = nextInt().toBoolean()
|
||||
8 -> pubDate = LocalDateTime(nextLong() * 1000L)
|
||||
else -> skipValue()
|
||||
}
|
||||
}
|
||||
@ -68,7 +69,7 @@ class FeverItemsAdapter {
|
||||
companion object {
|
||||
val NAMES: JsonReader.Options = JsonReader.Options.of(
|
||||
"id", "feed_id", "title", "author", "html", "url",
|
||||
"is_read", "created_on_time"
|
||||
"is_read", "is_saved", "created_on_time"
|
||||
)
|
||||
}
|
||||
}
|
@ -8,6 +8,7 @@ import okio.Buffer
|
||||
import org.junit.Test
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertNotNull
|
||||
import kotlin.test.assertTrue
|
||||
|
||||
class FeverItemsAdapterTest {
|
||||
|
||||
@ -27,7 +28,8 @@ class FeverItemsAdapterTest {
|
||||
assertEquals(author, "Alkarex")
|
||||
assertEquals(link, "https://github.com/FreshRSS/FreshRSS/releases/tag/1.9.0")
|
||||
assertNotNull(content)
|
||||
assertEquals(isRead, true)
|
||||
assertTrue(isStarred)
|
||||
assertTrue(isRead)
|
||||
assertNotNull(pubDate)
|
||||
assertEquals(remoteId, "1546007484154894")
|
||||
assertEquals(feedRemoteId, "2")
|
||||
|
File diff suppressed because one or more lines are too long
@ -1,6 +1,7 @@
|
||||
package com.readrops.app.repositories
|
||||
|
||||
import android.content.Context
|
||||
import android.util.Log
|
||||
import com.readrops.api.services.SyncType
|
||||
import com.readrops.api.services.fever.FeverDataSource
|
||||
import com.readrops.api.services.fever.adapters.FeverFeeds
|
||||
@ -39,6 +40,7 @@ class FeverRepository(
|
||||
.doOnSuccess { account.id = it.toInt() }
|
||||
.await()
|
||||
} catch (e: Exception) {
|
||||
Log.e(TAG, "login: ${e.message}")
|
||||
error(e.message!!)
|
||||
}
|
||||
}
|
||||
@ -65,9 +67,10 @@ class FeverRepository(
|
||||
insertFolders(syncResult.folders)
|
||||
insertFeeds(syncResult.feverFeeds)
|
||||
|
||||
//insertItems(syncResult.items)
|
||||
//insertItemsIds(syncResult.unreadIds!!, syncResult.starredIds!!.toMutableList())
|
||||
insertItems(syncResult.items)
|
||||
insertItemsIds(syncResult.unreadIds, syncResult.starredIds.toMutableList())
|
||||
} catch (e: Exception) {
|
||||
Log.e(TAG, "sync: ${e.message}")
|
||||
error(e.message!!)
|
||||
}
|
||||
}
|
||||
@ -94,10 +97,60 @@ class FeverRepository(
|
||||
}
|
||||
|
||||
private fun insertItems(items: List<Item>) {
|
||||
val itemsToInsert = arrayListOf<Item>()
|
||||
val itemsFeedsIds = mutableMapOf<String, Int>()
|
||||
|
||||
for (item in items) {
|
||||
var feedId: Int?
|
||||
if (itemsFeedsIds.containsKey(item.feedRemoteId)) {
|
||||
feedId = itemsFeedsIds[item.feedRemoteId]
|
||||
} else {
|
||||
feedId = database.feedDao().getFeedIdByRemoteId(item.feedRemoteId!!, account.id)
|
||||
itemsFeedsIds[item.feedRemoteId!!] = feedId
|
||||
}
|
||||
|
||||
item.feedId = feedId!!
|
||||
item.text?.let { item.readTime = Utils.readTimeFromString(it) }
|
||||
|
||||
itemsToInsert += item
|
||||
}
|
||||
|
||||
if (itemsToInsert.isNotEmpty()) {
|
||||
itemsToInsert.sortWith(Item::compareTo)
|
||||
database.itemDao().insert(itemsToInsert)
|
||||
}
|
||||
}
|
||||
|
||||
private fun insertItemsIds(unreadIds: List<String>, starredIds: List<String>) {
|
||||
private fun insertItemsIds(unreadIds: List<String>, starredIds: MutableList<String>) {
|
||||
database.itemStateDao().deleteItemsStates(account.id)
|
||||
|
||||
database.itemStateDao().insertItemStates(unreadIds.map { unreadId ->
|
||||
val starred = starredIds.any { starredId -> starredId == unreadId }
|
||||
if (starred) starredIds.remove(unreadId)
|
||||
|
||||
ItemState(
|
||||
id = 0,
|
||||
read = false,
|
||||
starred = starred,
|
||||
remoteId = unreadId,
|
||||
accountId = account.id,
|
||||
)
|
||||
})
|
||||
|
||||
if (starredIds.isNotEmpty()) {
|
||||
database.itemStateDao().insertItemStates(starredIds.map { starredId ->
|
||||
ItemState(
|
||||
id = 0,
|
||||
read = true, // if this id wasn't in the unread ids list, it is considered a read
|
||||
starred = true,
|
||||
remoteId = starredId,
|
||||
accountId = account.id,
|
||||
)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
val TAG: String = FeverRepository::class.java.simpleName
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user