fetch highlights and labels with library sync

This commit is contained in:
Jackson Harper 2023-04-21 13:36:26 +08:00
parent 52f302f5b0
commit 0f64f3c102
6 changed files with 86 additions and 80 deletions

View file

@ -7,9 +7,15 @@ import app.omnivore.omnivore.persistence.entities.*
suspend fun DataService.librarySearch(cursor: String?, query: String): SearchResult {
val searchResult = networker.search(cursor = cursor, limit = 10, query = query)
val savedItems = searchResult.items.map { it.item }
val savedItems = searchResult.items.map {
SavedItemWithLabelsAndHighlights(
savedItem = it.item,
labels = it.labels,
highlights = it.highlights,
)
}
db.savedItemDao().insertAll(savedItems)
db.savedItemDao().insertAll(savedItems.map { it.savedItem })
val labels: MutableList<SavedItemLabel> = mutableListOf()
val crossRefs: MutableList<SavedItemAndSavedItemLabelCrossRef> = mutableListOf()
@ -156,7 +162,7 @@ data class SearchResult(
val hasError: Boolean,
val hasMoreItems: Boolean,
val count: Int,
val savedItems: List<SavedItem>,
val savedItems: List<SavedItemWithLabelsAndHighlights>,
val cursor: String?
) {
companion object {

View file

@ -1,18 +1,12 @@
package app.omnivore.omnivore.networking
import androidx.room.PrimaryKey
import app.omnivore.omnivore.graphql.generated.SearchQuery
import app.omnivore.omnivore.graphql.generated.TypeaheadSearchQuery
import app.omnivore.omnivore.persistence.entities.SavedItem
import app.omnivore.omnivore.persistence.entities.SavedItemCardData
import app.omnivore.omnivore.persistence.entities.SavedItemLabel
import app.omnivore.omnivore.persistence.entities.TypeaheadCardData
import app.omnivore.omnivore.models.ServerSyncStatus
import app.omnivore.omnivore.persistence.entities.*
import com.apollographql.apollo3.api.Optional
data class SearchQueryResponse(
val cursor: String?,
val cardsData: List<TypeaheadCardData>
)
data class LibrarySearchQueryResponse(
val cursor: String?,
val items: List<LibrarySearchItem>
@ -20,34 +14,10 @@ data class LibrarySearchQueryResponse(
data class LibrarySearchItem(
val item: SavedItem,
val labels: List<SavedItemLabel>
val labels: List<SavedItemLabel>,
val highlights: List<Highlight>
)
suspend fun Networker.typeaheadSearch(
query: String
): SearchQueryResponse {
try {
val result = authenticatedApolloClient().query(
TypeaheadSearchQuery(query)
).execute()
val itemList = result.data?.typeaheadSearch?.onTypeaheadSearchSuccess?.items ?: listOf()
val cardsData = itemList.map {
TypeaheadCardData(
savedItemId = it.id,
slug = it.slug,
title = it.title,
isArchived = false,
)
}
return SearchQueryResponse(null, cardsData)
} catch (e: java.lang.Exception) {
return SearchQueryResponse(null, listOf())
}
}
suspend fun Networker.search(
cursor: String? = null,
limit: Int = 15,
@ -97,6 +67,21 @@ suspend fun Networker.search(
createdAt = null,
labelDescription = null
)
},
highlights = (it.node.highlights ?: listOf()).map { highlight ->
Highlight(
highlightId = highlight.highlightFields.id,
annotation = highlight.highlightFields.annotation,
createdByMe = highlight.highlightFields.createdByMe,
patch = highlight.highlightFields.patch,
prefix = highlight.highlightFields.prefix,
quote = highlight.highlightFields.quote,
serverSyncStatus = ServerSyncStatus.IS_SYNCED.rawValue,
shortId = highlight.highlightFields.shortId,
suffix = highlight.highlightFields.suffix,
updatedAt = highlight.highlightFields.updatedAt as String?,
createdAt = null,
)
}
)
}

View file

@ -0,0 +1,39 @@
package app.omnivore.omnivore.networking
import app.omnivore.omnivore.graphql.generated.SearchQuery
import app.omnivore.omnivore.graphql.generated.TypeaheadSearchQuery
import app.omnivore.omnivore.persistence.entities.SavedItem
import app.omnivore.omnivore.persistence.entities.SavedItemCardData
import app.omnivore.omnivore.persistence.entities.SavedItemLabel
import app.omnivore.omnivore.persistence.entities.TypeaheadCardData
import com.apollographql.apollo3.api.Optional
data class SearchQueryResponse(
val cursor: String?,
val cardsData: List<TypeaheadCardData>
)
suspend fun Networker.typeaheadSearch(
query: String
): SearchQueryResponse {
try {
val result = authenticatedApolloClient().query(
TypeaheadSearchQuery(query)
).execute()
val itemList = result.data?.typeaheadSearch?.onTypeaheadSearchSuccess?.items ?: listOf()
val cardsData = itemList.map {
TypeaheadCardData(
savedItemId = it.id,
slug = it.slug,
title = it.title,
isArchived = false,
)
}
return SearchQueryResponse(null, cardsData)
} catch (e: java.lang.Exception) {
return SearchQueryResponse(null, listOf())
}
}

View file

@ -4,6 +4,7 @@ import androidx.core.net.toUri
import androidx.lifecycle.LiveData
import androidx.room.*
import app.omnivore.omnivore.BuildConfig
import app.omnivore.omnivore.graphql.generated.SearchQuery
import app.omnivore.omnivore.models.ServerSyncStatus
import app.omnivore.omnivore.ui.library.SavedItemSortFilter
import java.util.*

View file

@ -141,39 +141,13 @@ class LibraryViewModel @Inject constructor(
}
result.savedItems.map {
val isSavedInDB = dataService.isSavedItemContentStoredInDB(it.slug)
val isSavedInDB = dataService.isSavedItemContentStoredInDB(it.savedItem.slug)
if (!isSavedInDB) {
delay(2000)
contentRequestChannel.send(it.slug)
contentRequestChannel.send(it.savedItem.slug)
}
}
val newItems = result.savedItems.map {
SavedItemCardDataWithLabels(
cardData = SavedItemCardData(
savedItemId = it.savedItemId,
slug = it.slug,
publisherURLString = it.publisherURLString,
title = it.title,
author = it.author,
imageURLString = it.imageURLString,
isArchived = it.isArchived,
pageURLString = it.pageURLString,
contentReader = it.contentReader,
savedAt = it.savedAt,
readingProgress = it.readingProgress,
wordsCount = it.wordsCount
),
labels = listOf()
)
}
itemsLiveData.value?.let{
itemsLiveData.postValue(newItems + it)
} ?: run {
itemsLiveData.postValue(newItems)
}
}
}
}
@ -203,6 +177,7 @@ class LibraryViewModel @Inject constructor(
suspend fun handleFilterChanges() {
if (appliedSortFilterLiveData.value != null && appliedFilterLiveData.value != null) {
println("PERFORMING A FILTER CHANGE")
itemsLiveDataInternal = dataService.libraryLiveData(appliedFilterLiveData.value!!, appliedSortFilterLiveData.value!!, activeLabelsLiveData.value ?: listOf())
itemsLiveData.removeSource(itemsLiveDataInternal)
itemsLiveData.addSource(itemsLiveDataInternal, itemsLiveData::setValue)

View file

@ -80,29 +80,29 @@ class SearchViewModel @Inject constructor(
}
result.savedItems.map {
val isSavedInDB = dataService.isSavedItemContentStoredInDB(it.slug)
val isSavedInDB = dataService.isSavedItemContentStoredInDB(it.savedItem.slug)
if (!isSavedInDB) {
delay(2000)
contentRequestChannel.send(it.slug)
contentRequestChannel.send(it.savedItem.slug)
}
}
val newItems = result.savedItems.map {
SavedItemCardDataWithLabels(
cardData = SavedItemCardData(
savedItemId = it.savedItemId,
slug = it.slug,
publisherURLString = it.publisherURLString,
title = it.title,
author = it.author,
imageURLString = it.imageURLString,
isArchived = it.isArchived,
pageURLString = it.pageURLString,
contentReader = it.contentReader,
savedAt = it.savedAt,
readingProgress = it.readingProgress,
wordsCount = it.wordsCount
savedItemId = it.savedItem.savedItemId,
slug = it.savedItem.slug,
publisherURLString = it.savedItem.publisherURLString,
title = it.savedItem.title,
author = it.savedItem.author,
imageURLString = it.savedItem.imageURLString,
isArchived = it.savedItem.isArchived,
pageURLString = it.savedItem.pageURLString,
contentReader = it.savedItem.contentReader,
savedAt = it.savedItem.savedAt,
readingProgress = it.savedItem.readingProgress,
wordsCount = it.savedItem.wordsCount
),
labels = listOf()
)