diff --git a/api/src/main/java/com/readrops/api/ApiModule.kt b/api/src/main/java/com/readrops/api/ApiModule.kt index ad84d65e..a31ae2ac 100644 --- a/api/src/main/java/com/readrops/api/ApiModule.kt +++ b/api/src/main/java/com/readrops/api/ApiModule.kt @@ -107,7 +107,7 @@ val apiModule = module { single(named("feverMoshi")) { Moshi.Builder() .add(FeverFoldersAdapter()) - .add(FeverFeedsAdapter()) + .add(FeverFeeds::class.java, FeverFeedsAdapter()) .add(FeverItemsAdapter()) .add(FeverFaviconsAdapter()) .add(Boolean::class.java, FeverAPIAdapter()) diff --git a/api/src/main/java/com/readrops/api/services/fever/FeverDataSource.kt b/api/src/main/java/com/readrops/api/services/fever/FeverDataSource.kt index 5f73c4e1..bdbfdfe5 100644 --- a/api/src/main/java/com/readrops/api/services/fever/FeverDataSource.kt +++ b/api/src/main/java/com/readrops/api/services/fever/FeverDataSource.kt @@ -1,6 +1,5 @@ package com.readrops.api.services.fever -import com.readrops.api.services.SyncResult import com.readrops.api.services.fever.adapters.FeverAPIAdapter import com.readrops.api.utils.ApiUtils import com.squareup.moshi.Moshi @@ -26,13 +25,14 @@ class FeverDataSource(private val service: FeverService) { adapter.fromJson(response.source())!! } - suspend fun sync(body: MultipartBody): SyncResult { - return SyncResult( - feeds = service.getFeeds(body), + suspend fun sync(body: MultipartBody): FeverSyncResult { + return FeverSyncResult( + feverFeeds = service.getFeeds(body), folders = service.getFolders(body), items = service.getItems(body), unreadIds = service.getUnreadItemsIds(body), starredIds = service.getStarredItemsIds(body), + favicons = listOf(), ) } } \ No newline at end of file diff --git a/api/src/main/java/com/readrops/api/services/fever/FeverService.kt b/api/src/main/java/com/readrops/api/services/fever/FeverService.kt index b285d229..6600cd6d 100644 --- a/api/src/main/java/com/readrops/api/services/fever/FeverService.kt +++ b/api/src/main/java/com/readrops/api/services/fever/FeverService.kt @@ -1,6 +1,7 @@ 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 @@ -16,7 +17,7 @@ interface FeverService { suspend fun login(@Body body: MultipartBody): ResponseBody @POST("?feeds") - suspend fun getFeeds(@Body body: MultipartBody): List + suspend fun getFeeds(@Body body: MultipartBody): FeverFeeds @POST("?groups") suspend fun getFolders(@Body body: MultipartBody): List diff --git a/api/src/main/java/com/readrops/api/services/fever/FeverSyncResult.kt b/api/src/main/java/com/readrops/api/services/fever/FeverSyncResult.kt new file mode 100644 index 00000000..e099e2a2 --- /dev/null +++ b/api/src/main/java/com/readrops/api/services/fever/FeverSyncResult.kt @@ -0,0 +1,15 @@ +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.Folder +import com.readrops.db.entities.Item + +data class FeverSyncResult( + val feverFeeds: FeverFeeds, + val folders: List, + val items: List, + val unreadIds: List, + val starredIds: List, + val favicons: List, +) \ No newline at end of file diff --git a/api/src/main/java/com/readrops/api/services/fever/adapters/FeverFeedsAdapter.kt b/api/src/main/java/com/readrops/api/services/fever/adapters/FeverFeedsAdapter.kt index cfef228e..10ff863f 100644 --- a/api/src/main/java/com/readrops/api/services/fever/adapters/FeverFeedsAdapter.kt +++ b/api/src/main/java/com/readrops/api/services/fever/adapters/FeverFeedsAdapter.kt @@ -7,24 +7,28 @@ import com.readrops.api.utils.extensions.nextNullableString import com.readrops.api.utils.extensions.skipField import com.readrops.api.utils.extensions.skipToEnd import com.readrops.db.entities.Feed -import com.squareup.moshi.FromJson -import com.squareup.moshi.JsonReader -import com.squareup.moshi.ToJson +import com.squareup.moshi.* -class FeverFeedsAdapter { +data class FeverFeeds( + val feeds: List, + val feedsGroups: Map> +) - @ToJson - fun toJson(feeds: List) = "" +class FeverFeedsAdapter : JsonAdapter() { + + override fun toJson(writer: JsonWriter, value: FeverFeeds?) { + // not useful here + } @SuppressLint("CheckResult") - @FromJson - fun fromJson(reader: JsonReader): List = with(reader) { + override fun fromJson(reader: JsonReader): FeverFeeds = with(reader) { return try { val feeds = arrayListOf() + val feedsGroups = mutableMapOf>() beginObject() - // skip basic fields (api_version, auth, last_refreshed...) + // skip based fields (api_version, auth, last_refreshed...) repeat(3) { skipField() } @@ -38,7 +42,7 @@ class FeverFeedsAdapter { val feed = Feed() while (hasNext()) { with(feed) { - when(selectName(NAMES)) { + when (selectName(NAMES)) { 0 -> remoteId = nextInt().toString() 1 -> name = nextNonEmptyString() 2 -> url = nextNonEmptyString() @@ -53,10 +57,31 @@ class FeverFeedsAdapter { } endArray() - skipToEnd() + + nextName() + beginArray() + + while (hasNext()) { + beginObject() + + var folderId: Int? = null + val feedsIds = mutableListOf() + while (hasNext()) { + when (selectName(JsonReader.Options.of("group_id", "feed_ids"))) { + 0 -> folderId = nextInt() + 1 -> feedsIds += nextNonEmptyString().split(",").map { it.toInt() } + else -> skipValue() + } + } + + folderId?.let { feedsGroups[it] = feedsIds } + endObject() + } + + endArray() endObject() - feeds + FeverFeeds(feeds, feedsGroups) } catch (e: Exception) { throw ParseException(e.message) } diff --git a/api/src/test/java/com/readrops/api/services/fever/adapters/FeverFeedsAdapterTest.kt b/api/src/test/java/com/readrops/api/services/fever/adapters/FeverFeedsAdapterTest.kt index f193eafe..c6f078f8 100644 --- a/api/src/test/java/com/readrops/api/services/fever/adapters/FeverFeedsAdapterTest.kt +++ b/api/src/test/java/com/readrops/api/services/fever/adapters/FeverFeedsAdapterTest.kt @@ -1,9 +1,7 @@ package com.readrops.api.services.fever.adapters import com.readrops.api.TestUtils -import com.readrops.db.entities.Feed import com.squareup.moshi.Moshi -import com.squareup.moshi.Types import okio.Buffer import org.junit.Test import kotlin.test.assertEquals @@ -11,23 +9,28 @@ import kotlin.test.assertEquals class FeverFeedsAdapterTest { val adapter = Moshi.Builder() - .add(FeverFeedsAdapter()) - .build() - .adapter>(Types.newParameterizedType(List::class.java, Feed::class.java))!! + .add(FeverFeeds::class.java, FeverFeedsAdapter()) + .build() + .adapter(FeverFeeds::class.java)!! @Test fun validFeedsTest() { val stream = TestUtils.loadResource("services/fever/feeds.json") - val feeds = adapter.fromJson(Buffer().readFrom(stream))!! + val feverFeeds = adapter.fromJson(Buffer().readFrom(stream))!! - assertEquals(feeds.size, 1) + assertEquals(feverFeeds.feeds.size, 1) - with(feeds[0]) { + with(feverFeeds.feeds[0]) { assertEquals(name, "xda-developers") assertEquals(url, "https://www.xda-developers.com/feed/") assertEquals(siteUrl, "https://www.xda-developers.com/") assertEquals(remoteId, "32") } + + with(feverFeeds.feedsGroups.entries.first()) { + assertEquals(key, 3) + assertEquals(value, listOf(5, 4)) + } } } \ No newline at end of file diff --git a/app/src/main/java/com/readrops/app/repositories/FeverRepository.kt b/app/src/main/java/com/readrops/app/repositories/FeverRepository.kt index 98f5745d..193296a0 100644 --- a/app/src/main/java/com/readrops/app/repositories/FeverRepository.kt +++ b/app/src/main/java/com/readrops/app/repositories/FeverRepository.kt @@ -3,13 +3,16 @@ package com.readrops.app.repositories import android.content.Context import com.readrops.api.services.SyncType import com.readrops.api.services.fever.FeverDataSource +import com.readrops.api.services.fever.adapters.FeverFeeds import com.readrops.api.utils.ApiUtils import com.readrops.app.addfeed.FeedInsertionResult import com.readrops.app.addfeed.ParsingResult +import com.readrops.app.utils.Utils import com.readrops.db.Database import com.readrops.db.entities.Feed import com.readrops.db.entities.Folder import com.readrops.db.entities.Item +import com.readrops.db.entities.ItemState import com.readrops.db.entities.account.Account import io.reactivex.Completable import io.reactivex.Single @@ -19,11 +22,11 @@ import kotlinx.coroutines.rx2.rxCompletable import okhttp3.MultipartBody class FeverRepository( - private val feverDataSource: FeverDataSource, - private val dispatcher: CoroutineDispatcher, - database: Database, - context: Context, - account: Account?, + private val feverDataSource: FeverDataSource, + private val dispatcher: CoroutineDispatcher, + database: Database, + context: Context, + account: Account?, ) : ARepository(database, context, account) { override fun login(account: Account, insert: Boolean): Completable { @@ -33,8 +36,8 @@ class FeverRepository( account.displayedName = account.accountType!!.name database.accountDao().insert(account) - .doOnSuccess { account.id = it.toInt() } - .await() + .doOnSuccess { account.id = it.toInt() } + .await() } catch (e: Exception) { error(e.message!!) } @@ -60,7 +63,10 @@ class FeverRepository( val syncResult = feverDataSource.sync(requestBody) insertFolders(syncResult.folders) - insertFeeds(syncResult.feeds) + insertFeeds(syncResult.feverFeeds) + + //insertItems(syncResult.items) + //insertItemsIds(syncResult.unreadIds!!, syncResult.starredIds!!.toMutableList()) } catch (e: Exception) { error(e.message!!) } @@ -76,7 +82,13 @@ class FeverRepository( database.folderDao().foldersUpsert(folders, account) } - private fun insertFeeds(feeds: List) { + private fun insertFeeds(feverFeeds: FeverFeeds) = with(feverFeeds) { + for (feed in feeds) { + for ((folderId, feedsIds) in feedsGroups) { + if (feedsIds.contains(feed.remoteId!!.toInt())) feed.remoteFolderId = folderId.toString() + } + } + feeds.forEach { it.accountId = account.id } database.feedDao().feedsUpsert(feeds, account) }