mirror of
https://github.com/omnivore-app/omnivore.git
synced 2026-03-11 08:54:26 +00:00
load web reader from server before db
This commit is contained in:
parent
075182d515
commit
6393c52a24
2 changed files with 40 additions and 31 deletions
|
|
@ -81,9 +81,16 @@ class LibraryViewModel @Inject constructor(
|
|||
}
|
||||
}
|
||||
|
||||
private suspend fun performItemSync(cursor: String?, since: String, count: Int, startTime: String, fetchContentSlugs: List<String> = listOf()) {
|
||||
private suspend fun performItemSync(cursor: String?, since: String, count: Int, startTime: String, isInitialBatch: Boolean = true) {
|
||||
dataService.syncOfflineItemsWithServerIfNeeded()
|
||||
val result = dataService.sync(since = since, cursor = cursor)
|
||||
val result = dataService.sync(since = since, cursor = cursor, limit = 20)
|
||||
|
||||
// Fetch content for the initial batch only
|
||||
if (isInitialBatch) {
|
||||
for (slug in result.savedItemSlugs) {
|
||||
dataService.syncSavedItemContent(slug)
|
||||
}
|
||||
}
|
||||
|
||||
val totalCount = count + result.count
|
||||
|
||||
|
|
@ -95,15 +102,10 @@ class LibraryViewModel @Inject constructor(
|
|||
since = since,
|
||||
count = totalCount,
|
||||
startTime = startTime,
|
||||
fetchContentSlugs = fetchContentSlugs.ifEmpty { result.savedItemSlugs }
|
||||
|
||||
isInitialBatch = false
|
||||
)
|
||||
} else {
|
||||
datastoreRepo.putString(DatastoreKeys.libraryLastSyncTimestamp, startTime)
|
||||
|
||||
for (slug in result.savedItemSlugs) {
|
||||
dataService.syncSavedItemContent(slug)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -44,36 +44,44 @@ class WebReaderViewModel @Inject constructor(
|
|||
|
||||
var hasTappedExistingHighlight = false
|
||||
var lastTapCoordinates: TapCoordinates? = null
|
||||
|
||||
|
||||
fun loadItem(slug: String) {
|
||||
viewModelScope.launch {
|
||||
// Attempt to load from db first
|
||||
withContext(Dispatchers.IO) {
|
||||
val persistedItem = dataService.db.savedItemDao().getSavedItemWithLabelsAndHighlights(slug)
|
||||
val webReaderParams = loadItemFromServer(slug)
|
||||
|
||||
if (persistedItem?.savedItem?.content != null) {
|
||||
val articleContent = ArticleContent(
|
||||
title = persistedItem.savedItem.title,
|
||||
htmlContent = persistedItem.savedItem.content,
|
||||
highlights = persistedItem.highlights,
|
||||
contentStatus = "SUCCEEDED",
|
||||
objectID = "",
|
||||
labelsJSONString = Gson().toJson(persistedItem.labels)
|
||||
)
|
||||
|
||||
Log.d("sync", "data loaded from db")
|
||||
webReaderParamsLiveData.postValue(WebReaderParams(persistedItem.savedItem, articleContent))
|
||||
} else {
|
||||
loadItemFromServer(slug)
|
||||
}
|
||||
if (webReaderParams != null) {
|
||||
Log.d("sync", "data loaded from server")
|
||||
webReaderParamsLiveData.postValue(webReaderParams)
|
||||
} else {
|
||||
loadItemFromDB(slug)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun loadItemFromServer(slug: String) {
|
||||
private suspend fun loadItemFromDB(slug: String) {
|
||||
withContext(Dispatchers.IO) {
|
||||
val persistedItem = dataService.db.savedItemDao().getSavedItemWithLabelsAndHighlights(slug)
|
||||
|
||||
if (persistedItem?.savedItem?.content != null) {
|
||||
val articleContent = ArticleContent(
|
||||
title = persistedItem.savedItem.title,
|
||||
htmlContent = persistedItem.savedItem.content,
|
||||
highlights = persistedItem.highlights,
|
||||
contentStatus = "SUCCEEDED",
|
||||
objectID = "",
|
||||
labelsJSONString = Gson().toJson(persistedItem.labels)
|
||||
)
|
||||
|
||||
Log.d("sync", "data loaded from db")
|
||||
webReaderParamsLiveData.postValue(WebReaderParams(persistedItem.savedItem, articleContent))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun loadItemFromServer(slug: String): WebReaderParams? {
|
||||
val articleQueryResult = networker.savedItem(slug)
|
||||
|
||||
val article = articleQueryResult.item ?: return
|
||||
val article = articleQueryResult.item ?: return null
|
||||
|
||||
val articleContent = ArticleContent(
|
||||
title = article.title,
|
||||
|
|
@ -84,8 +92,7 @@ class WebReaderViewModel @Inject constructor(
|
|||
labelsJSONString = Gson().toJson(articleQueryResult.labels)
|
||||
)
|
||||
|
||||
Log.d("sync", "data loaded from server")
|
||||
webReaderParamsLiveData.postValue(WebReaderParams(article, articleContent))
|
||||
return WebReaderParams(article, articleContent)
|
||||
}
|
||||
|
||||
fun handleSavedItemAction(itemID: String, action: SavedItemAction) {
|
||||
|
|
|
|||
Loading…
Reference in a new issue