mirror of https://github.com/readrops/Readrops.git
Add Fever initial and classic synchronisation logic
This commit is contained in:
parent
fe57d5b2d7
commit
dcf58a537e
|
@ -1,7 +1,9 @@
|
|||
package com.readrops.api.services.fever
|
||||
|
||||
import com.readrops.api.services.SyncType
|
||||
import com.readrops.api.services.fever.adapters.FeverAPIAdapter
|
||||
import com.readrops.api.utils.ApiUtils
|
||||
import com.readrops.db.entities.Item
|
||||
import com.squareup.moshi.Moshi
|
||||
import okhttp3.MultipartBody
|
||||
|
||||
|
@ -25,14 +27,56 @@ class FeverDataSource(private val service: FeverService) {
|
|||
adapter.fromJson(response.source())!!
|
||||
}
|
||||
|
||||
suspend fun sync(body: MultipartBody): FeverSyncResult {
|
||||
suspend fun sync(syncType: SyncType, syncData: FeverSyncData, body: MultipartBody): FeverSyncResult {
|
||||
if (syncType == SyncType.INITIAL_SYNC) {
|
||||
val unreadIds = service.getUnreadItemsIds(body)
|
||||
.reversed()
|
||||
.subList(0, MAX_ITEMS_IDS)
|
||||
|
||||
var lastId = unreadIds.first()
|
||||
val items = arrayListOf<Item>()
|
||||
repeat(INITIAL_SYNC_ITEMS_REQUESTS_COUNT) {
|
||||
val newItems = service.getItems(body, lastId, null)
|
||||
|
||||
lastId = newItems.last().remoteId!!
|
||||
items += newItems
|
||||
}
|
||||
|
||||
return FeverSyncResult(
|
||||
feverFeeds = service.getFeeds(body),
|
||||
folders = service.getFolders(body),
|
||||
items = service.getItems(body),
|
||||
items = items,
|
||||
unreadIds = unreadIds,
|
||||
starredIds = service.getStarredItemsIds(body),
|
||||
favicons = listOf(),
|
||||
sinceId = unreadIds.first().toLong(),
|
||||
)
|
||||
} else {
|
||||
val items = arrayListOf<Item>()
|
||||
var sinceId = syncData.sinceId
|
||||
|
||||
while (true) {
|
||||
val newItems = service.getItems(body, null, sinceId)
|
||||
|
||||
if (newItems.isEmpty()) break
|
||||
sinceId = newItems.first().remoteId!!
|
||||
items += newItems
|
||||
}
|
||||
|
||||
return FeverSyncResult(
|
||||
feverFeeds = service.getFeeds(body),
|
||||
folders = service.getFolders(body),
|
||||
items = items,
|
||||
unreadIds = service.getUnreadItemsIds(body),
|
||||
starredIds = service.getStarredItemsIds(body),
|
||||
favicons = listOf(),
|
||||
sinceId = if (items.isNotEmpty()) items.first().remoteId!!.toLong() else sinceId.toLong(),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
private const val MAX_ITEMS_IDS = 5000
|
||||
private const val INITIAL_SYNC_ITEMS_REQUESTS_COUNT = 10
|
||||
}
|
||||
}
|
|
@ -2,14 +2,13 @@ package com.readrops.api.services.fever
|
|||
|
||||
import com.readrops.api.services.fever.adapters.Favicon
|
||||
import com.readrops.api.services.fever.adapters.FeverFeeds
|
||||
import com.readrops.db.entities.Feed
|
||||
import com.readrops.db.entities.Folder
|
||||
import com.readrops.db.entities.Item
|
||||
import okhttp3.MultipartBody
|
||||
import okhttp3.ResponseBody
|
||||
import retrofit2.http.Body
|
||||
import retrofit2.http.GET
|
||||
import retrofit2.http.POST
|
||||
import retrofit2.http.Query
|
||||
|
||||
interface FeverService {
|
||||
|
||||
|
@ -26,7 +25,8 @@ interface FeverService {
|
|||
suspend fun getFavicons(@Body body: MultipartBody): List<Favicon>
|
||||
|
||||
@POST("?items")
|
||||
suspend fun getItems(@Body body: MultipartBody): List<Item>
|
||||
suspend fun getItems(@Body body: MultipartBody, @Query("max_id") maxId: String?,
|
||||
@Query("since_id") sinceId: String?): List<Item>
|
||||
|
||||
@POST("?unread_item_ids")
|
||||
suspend fun getUnreadItemsIds(@Body body: MultipartBody): List<String>
|
||||
|
|
|
@ -0,0 +1,5 @@
|
|||
package com.readrops.api.services.fever
|
||||
|
||||
data class FeverSyncData(
|
||||
val sinceId: String,
|
||||
)
|
|
@ -12,4 +12,5 @@ data class FeverSyncResult(
|
|||
val unreadIds: List<String>,
|
||||
val starredIds: List<String>,
|
||||
val favicons: List<Favicon>,
|
||||
val sinceId: Long = 0,
|
||||
)
|
|
@ -4,6 +4,7 @@ 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.FeverSyncData
|
||||
import com.readrops.api.services.fever.adapters.FeverFeeds
|
||||
import com.readrops.api.utils.ApiUtils
|
||||
import com.readrops.app.addfeed.FeedInsertionResult
|
||||
|
@ -62,13 +63,17 @@ class FeverRepository(
|
|||
.addFormDataPart("api_key", credentials)
|
||||
.build()
|
||||
|
||||
val syncResult = feverDataSource.sync(requestBody)
|
||||
val syncResult = feverDataSource.sync(syncType,
|
||||
FeverSyncData(account.lastModified.toString()), requestBody)
|
||||
|
||||
insertFolders(syncResult.folders)
|
||||
insertFeeds(syncResult.feverFeeds)
|
||||
|
||||
insertItems(syncResult.items)
|
||||
insertItemsIds(syncResult.unreadIds, syncResult.starredIds.toMutableList())
|
||||
|
||||
// We store the id to use for the next synchronisation even if it's not a timestamp
|
||||
database.accountDao().updateLastModified(account.id, syncResult.sinceId)
|
||||
} catch (e: Exception) {
|
||||
Log.e(TAG, "sync: ${e.message}")
|
||||
error(e.message!!)
|
||||
|
|
Loading…
Reference in New Issue