mirror of
https://github.com/omnivore-app/omnivore.git
synced 2026-03-11 08:54:26 +00:00
stub in sync call
This commit is contained in:
parent
2bb2e806ff
commit
87c1800d70
3 changed files with 22 additions and 82 deletions
|
|
@ -1,14 +1,16 @@
|
|||
package app.omnivore.omnivore
|
||||
|
||||
import android.content.Context
|
||||
import android.util.Log
|
||||
import androidx.room.Room
|
||||
import app.omnivore.omnivore.networking.Networker
|
||||
import app.omnivore.omnivore.networking.savedItemUpdates
|
||||
import app.omnivore.omnivore.persistence.AppDatabase
|
||||
import javax.inject.Inject
|
||||
|
||||
class DataService @Inject constructor(
|
||||
context: Context,
|
||||
private val networker: Networker
|
||||
val networker: Networker
|
||||
) {
|
||||
val db = Room.databaseBuilder(
|
||||
context,
|
||||
|
|
@ -16,6 +18,15 @@ class DataService @Inject constructor(
|
|||
).build()
|
||||
}
|
||||
|
||||
//suspend fun DataService.sync(): Boolean {
|
||||
//
|
||||
//}
|
||||
suspend fun DataService.sync(since: String, cursor: String?, limit: Int = 15): Boolean {
|
||||
val syncResult = networker.savedItemUpdates(cursor = cursor, limit = limit, since = since) ?: return false
|
||||
|
||||
Log.d("sync", "count: ${syncResult.totalCount}; sync result: $syncResult")
|
||||
// TODO: Store items in Room DB
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
suspend fun DataService.syncOfflineItemsWithServerIfNeeded() {
|
||||
// TODO: implement this
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,9 +2,6 @@ package app.omnivore.omnivore.networking
|
|||
|
||||
import app.omnivore.omnivore.graphql.generated.SearchQuery
|
||||
import app.omnivore.omnivore.graphql.generated.TypeaheadSearchQuery
|
||||
import app.omnivore.omnivore.graphql.generated.UpdatesSinceQuery
|
||||
import app.omnivore.omnivore.graphql.generated.type.UpdateReason
|
||||
import app.omnivore.omnivore.persistence.entities.SavedItem
|
||||
import app.omnivore.omnivore.persistence.entities.SavedItemCardData
|
||||
import com.apollographql.apollo3.api.Optional
|
||||
|
||||
|
|
@ -13,14 +10,6 @@ data class SearchQueryResponse(
|
|||
val cardsData: List<SavedItemCardData>
|
||||
)
|
||||
|
||||
data class SavedItemUpdatesQueryResponse(
|
||||
val cursor: String?,
|
||||
val hasMoreItems: Boolean,
|
||||
val totalCount: Int,
|
||||
val deletedItemIDs: List<String>,
|
||||
val items: List<SavedItem>
|
||||
)
|
||||
|
||||
suspend fun Networker.typeaheadSearch(
|
||||
query: String
|
||||
): SearchQueryResponse {
|
||||
|
|
@ -87,65 +76,3 @@ suspend fun Networker.search(
|
|||
return SearchQueryResponse(null, listOf())
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun Networker.savedItemUpdates(
|
||||
cursor: String? = null,
|
||||
limit: Int = 15,
|
||||
since: String
|
||||
): SavedItemUpdatesQueryResponse? {
|
||||
try {
|
||||
val result = authenticatedApolloClient().query(
|
||||
UpdatesSinceQuery(
|
||||
after = Optional.presentIfNotNull(cursor),
|
||||
first = Optional.presentIfNotNull(limit),
|
||||
since = since
|
||||
)
|
||||
).execute()
|
||||
|
||||
val payload = result.data?.updatesSince?.onUpdatesSinceSuccess ?: return null
|
||||
val itemNodes: MutableList<UpdatesSinceQuery.Node> = mutableListOf()
|
||||
val deletedItemIDs: MutableList<String> = mutableListOf()
|
||||
|
||||
for (edge in payload.edges) {
|
||||
if (edge.updateReason == UpdateReason.DELETED) {
|
||||
deletedItemIDs.add(edge.itemID)
|
||||
} else if (edge.node != null) {
|
||||
itemNodes.add(edge.node)
|
||||
}
|
||||
}
|
||||
|
||||
val savedItems = itemNodes.map {
|
||||
SavedItem(
|
||||
id = it.id,
|
||||
title = it.title,
|
||||
createdAt = it.createdAt as String,
|
||||
savedAt = it.savedAt as String,
|
||||
readAt = it.readAt as String?,
|
||||
updatedAt = it.updatedAt as String?,
|
||||
readingProgress = it.readingProgressPercent,
|
||||
readingProgressAnchor = it.readingProgressAnchorIndex,
|
||||
imageURLString = it.image,
|
||||
pageURLString = it.url,
|
||||
descriptionText = it.description,
|
||||
publisherURLString = it.originalArticleUrl,
|
||||
siteName = it.siteName,
|
||||
author = it.author,
|
||||
publishDate = it.publishedAt as String?,
|
||||
slug = it.slug,
|
||||
isArchived = it.isArchived,
|
||||
contentReader = it.contentReader.rawValue,
|
||||
content = null
|
||||
)
|
||||
}
|
||||
|
||||
return SavedItemUpdatesQueryResponse(
|
||||
cursor = payload.pageInfo.endCursor,
|
||||
hasMoreItems = payload.pageInfo.hasNextPage,
|
||||
totalCount = savedItems.size,
|
||||
deletedItemIDs = deletedItemIDs,
|
||||
items = savedItems
|
||||
)
|
||||
} catch (e: java.lang.Exception) {
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,9 +7,7 @@ import androidx.compose.runtime.setValue
|
|||
import androidx.lifecycle.MutableLiveData
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import app.omnivore.omnivore.DataService
|
||||
import app.omnivore.omnivore.DatastoreKeys
|
||||
import app.omnivore.omnivore.DatastoreRepository
|
||||
import app.omnivore.omnivore.*
|
||||
import app.omnivore.omnivore.networking.*
|
||||
import app.omnivore.omnivore.persistence.entities.SavedItemCardData
|
||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||
|
|
@ -58,11 +56,14 @@ class LibraryViewModel @Inject constructor(
|
|||
}
|
||||
}
|
||||
|
||||
fun syncItems() {
|
||||
suspend fun syncItems() {
|
||||
val syncStart = LocalDateTime.now()
|
||||
val lastSyncDate = getLastSyncTime() ?: LocalDateTime.MIN
|
||||
|
||||
// try? await dataService.syncOfflineItemsWithServerIfNeeded()
|
||||
withContext(Dispatchers.IO) {
|
||||
dataService.syncOfflineItemsWithServerIfNeeded()
|
||||
dataService.sync(since = lastSyncDate.toString(), cursor = null)
|
||||
}
|
||||
}
|
||||
|
||||
fun load(clearPreviousSearch: Boolean = false) {
|
||||
|
|
@ -71,6 +72,7 @@ class LibraryViewModel @Inject constructor(
|
|||
}
|
||||
|
||||
viewModelScope.launch {
|
||||
syncItems()
|
||||
val thisSearchIdx = searchIdx
|
||||
searchIdx += 1
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue