Add Nextcloud initial synchronization
This commit is contained in:
parent
9a63c1a777
commit
5bff505fc3
@ -3,6 +3,6 @@ package com.readrops.api.services.nextcloudnews
|
||||
import com.readrops.api.services.Credentials
|
||||
|
||||
class NextNewsCredentials(login: String?, password: String?, url: String):
|
||||
Credentials((login != null && password != null).let {
|
||||
okhttp3.Credentials.basic(login!!, password!!)
|
||||
}, url)
|
||||
Credentials(if (login != null && password != null) {
|
||||
okhttp3.Credentials.basic(login, password)
|
||||
} else null, url)
|
@ -2,10 +2,15 @@ package com.readrops.app.compose.repositories
|
||||
|
||||
import com.readrops.api.services.Credentials
|
||||
import com.readrops.api.services.SyncResult
|
||||
import com.readrops.api.services.SyncType
|
||||
import com.readrops.api.services.nextcloudnews.NewNextcloudNewsDataSource
|
||||
import com.readrops.api.services.nextcloudnews.NextcloudNewsSyncData
|
||||
import com.readrops.api.utils.AuthInterceptor
|
||||
import com.readrops.app.compose.util.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.account.Account
|
||||
import org.koin.core.component.KoinComponent
|
||||
import org.koin.core.component.get
|
||||
@ -27,12 +32,15 @@ class NextcloudNewsRepository(
|
||||
override suspend fun synchronize(
|
||||
selectedFeeds: List<Feed>,
|
||||
onUpdate: (Feed) -> Unit
|
||||
): Pair<SyncResult, ErrorResult> {
|
||||
TODO("Not yet implemented")
|
||||
}
|
||||
): Pair<SyncResult, ErrorResult> = throw NotImplementedError("This method can't be called here")
|
||||
|
||||
override suspend fun synchronize(): SyncResult {
|
||||
TODO("Not yet implemented")
|
||||
return dataSource.synchronize(SyncType.INITIAL_SYNC, NextcloudNewsSyncData()).apply {
|
||||
insertFolders(folders)
|
||||
insertFeeds(feeds)
|
||||
|
||||
insertItems(items, true)
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun insertNewFeeds(
|
||||
@ -41,4 +49,45 @@ class NextcloudNewsRepository(
|
||||
): ErrorResult {
|
||||
TODO("Not yet implemented")
|
||||
}
|
||||
|
||||
private suspend fun insertFolders(folders: List<Folder>) {
|
||||
folders.forEach { it.accountId = account.id }
|
||||
database.newFolderDao().upsertFolders(folders, account)
|
||||
}
|
||||
|
||||
private suspend fun insertFeeds(feeds: List<Feed>): List<Long> {
|
||||
feeds.forEach { it.accountId = account.id }
|
||||
return database.newFeedDao().upsertFeeds(feeds, account)
|
||||
}
|
||||
|
||||
private suspend fun insertItems(items: List<Item>, initialSync: Boolean) {
|
||||
val itemsToInsert = arrayListOf<Item>()
|
||||
val itemsFeedsIds = mutableMapOf<String?, Int>()
|
||||
|
||||
for (item in items) {
|
||||
val feedId: Int
|
||||
if (itemsFeedsIds.containsKey(item.feedRemoteId)) {
|
||||
feedId = itemsFeedsIds.getValue(item.feedRemoteId)
|
||||
} else {
|
||||
feedId =
|
||||
database.newFeedDao().selectRemoteFeedLocalId(item.feedRemoteId!!, account.id)
|
||||
itemsFeedsIds[item.feedRemoteId] = feedId
|
||||
}
|
||||
|
||||
if (!initialSync && feedId > 0 && database.newItemDao()
|
||||
.itemExists(item.remoteId!!, feedId)
|
||||
) {
|
||||
database.newItemDao()
|
||||
.updateReadAndStarState(item.remoteId!!, item.isRead, item.isStarred)
|
||||
}
|
||||
|
||||
item.feedId = feedId
|
||||
item.readTime = Utils.readTimeFromString(item.content.orEmpty())
|
||||
itemsToInsert += item
|
||||
}
|
||||
|
||||
if (itemsToInsert.isNotEmpty()) {
|
||||
database.newItemDao().insert(itemsToInsert)
|
||||
}
|
||||
}
|
||||
}
|
@ -28,6 +28,9 @@ abstract class NewItemDao : NewBaseDao<Item> {
|
||||
@Query("Update Item Set starred = :starred Where id = :itemId")
|
||||
abstract suspend fun updateStarState(itemId: Int, starred: Boolean)
|
||||
|
||||
@Query("Update Item set read = :read, starred = :starred Where remoteId = :remoteId")
|
||||
abstract suspend fun updateReadAndStarState(remoteId: String, read: Boolean, starred: Boolean)
|
||||
|
||||
@Query("Update Item set read = 1 Where feed_id IN (Select id From Feed Where account_id = :accountId)")
|
||||
abstract suspend fun setAllItemsRead(accountId: Int)
|
||||
|
||||
@ -54,4 +57,7 @@ abstract class NewItemDao : NewBaseDao<Item> {
|
||||
@RawQuery(observedEntities = [Item::class])
|
||||
abstract fun selectFeedUnreadItemsCount(query: SupportSQLiteQuery):
|
||||
Flow<Map<@MapColumn(columnName = "feed_id") Int, @MapColumn(columnName = "item_count") Int>>
|
||||
|
||||
@Query("Select case When :guid In (Select guid From Item Inner Join Feed on Item.feed_id = Feed.id and account_id = :accountId) Then 1 else 0 end")
|
||||
abstract suspend fun itemExists(guid: String, accountId: Int): Boolean
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user