move linked item query into networker object

This commit is contained in:
Satindar Dhillon 2022-09-26 13:55:07 -07:00
parent 2be59bdf89
commit 944b3e2d91
2 changed files with 92 additions and 80 deletions

View file

@ -0,0 +1,80 @@
package app.omnivore.omnivore.networking
import app.omnivore.omnivore.graphql.generated.GetArticleQuery
import app.omnivore.omnivore.models.Highlight
import app.omnivore.omnivore.models.LinkedItem
import app.omnivore.omnivore.models.LinkedItemLabel
data class LinkedItemQueryResponse(
val item: LinkedItem?,
val highlights: List<Highlight>,
val labels: List<LinkedItemLabel>
) {
companion object {
fun emptyResponse(): LinkedItemQueryResponse {
return LinkedItemQueryResponse(null, listOf(), listOf())
}
}
}
suspend fun Networker.linkedItem(slug: String): LinkedItemQueryResponse {
val result = authenticatedApolloClient().query(
GetArticleQuery(slug = slug)
).execute()
val article = result.data?.article?.onArticleSuccess?.article
?: return LinkedItemQueryResponse.emptyResponse()
val labels = article.labels ?: listOf()
val linkedItemLabels = labels.map {
LinkedItemLabel(
id = it.labelFields.id,
name = it.labelFields.name,
color = it.labelFields.color,
createdAt = it.labelFields.createdAt,
labelDescription = it.labelFields.description
)
}
val highlights = article.highlights.map {
Highlight(
id = it.highlightFields.id,
shortId = it.highlightFields.shortId,
quote = it.highlightFields.quote,
prefix = it.highlightFields.prefix,
suffix = it.highlightFields.suffix,
patch = it.highlightFields.patch,
annotation = it.highlightFields.annotation,
createdAt = null, // TODO: update gql query to get this
updatedAt = it.highlightFields.updatedAt,
createdByMe = it.highlightFields.createdByMe,
)
}
// TODO: handle errors
val linkedItem = LinkedItem(
id = article.articleFields.id,
title = article.articleFields.title,
createdAt = article.articleFields.createdAt,
savedAt = article.articleFields.savedAt,
readAt = article.articleFields.readAt,
updatedAt = article.articleFields.updatedAt,
readingProgress = article.articleFields.readingProgressPercent,
readingProgressAnchor = article.articleFields.readingProgressAnchorIndex,
imageURLString = article.articleFields.image,
pageURLString = article.articleFields.url,
descriptionText = article.articleFields.description,
publisherURLString = article.articleFields.originalArticleUrl,
siteName = article.articleFields.siteName,
author = article.articleFields.author,
publishDate = article.articleFields.publishedAt,
slug = article.articleFields.slug,
isArchived = article.articleFields.isArchived,
contentReader = article.articleFields.contentReader.rawValue,
content = article.articleFields.content
)
return LinkedItemQueryResponse(item = linkedItem, highlights, labels = linkedItemLabels)
}

View file

@ -4,18 +4,13 @@ import android.util.Log
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import app.omnivore.omnivore.Constants
import app.omnivore.omnivore.DatastoreKeys
import app.omnivore.omnivore.DatastoreRepository
import app.omnivore.omnivore.graphql.generated.GetArticleQuery
import app.omnivore.omnivore.models.Highlight
import app.omnivore.omnivore.models.LinkedItem
import app.omnivore.omnivore.models.LinkedItemLabel
import com.apollographql.apollo3.ApolloClient
import app.omnivore.omnivore.networking.Networker
import app.omnivore.omnivore.networking.linkedItem
import com.google.gson.Gson
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import org.json.JSONObject
import javax.inject.Inject
@ -26,90 +21,27 @@ data class WebReaderParams(
@HiltViewModel
class WebReaderViewModel @Inject constructor(
private val datastoreRepo: DatastoreRepository
private val datastoreRepo: DatastoreRepository,
private val networker: Networker
): ViewModel() {
val webReaderParamsLiveData = MutableLiveData<WebReaderParams?>(null)
private fun getAuthToken(): String? = runBlocking {
datastoreRepo.getString(DatastoreKeys.omnivoreAuthToken)
}
fun loadItem(slug: String) {
viewModelScope.launch {
val authToken = getAuthToken()
val articleQueryResult = networker.linkedItem(slug)
val apolloClient = ApolloClient.Builder()
.serverUrl("${Constants.apiURL}/api/graphql")
.addHttpHeader("Authorization", value = authToken ?: "")
.build()
val response = apolloClient.query(
GetArticleQuery(slug = slug)
).execute()
val article = response.data?.article?.onArticleSuccess?.article ?: return@launch
val labels = article.labels ?: listOf()
val linkedItemLabels = labels.map {
LinkedItemLabel(
id = it.labelFields.id,
name = it.labelFields.name,
color = it.labelFields.color,
createdAt = it.labelFields.createdAt,
labelDescription = it.labelFields.description
)
}
val highlights = article.highlights.map {
Highlight(
id = it.highlightFields.id,
shortId = it.highlightFields.shortId,
quote = it.highlightFields.quote,
prefix = it.highlightFields.prefix,
suffix = it.highlightFields.suffix,
patch = it.highlightFields.patch,
annotation = it.highlightFields.annotation,
createdAt = null, // TODO: update gql query to get this
updatedAt = it.highlightFields.updatedAt,
createdByMe = it.highlightFields.createdByMe,
)
}
// TODO: handle errors
val linkedItem = LinkedItem(
id = article.articleFields.id,
title = article.articleFields.title,
createdAt = article.articleFields.createdAt,
savedAt = article.articleFields.savedAt,
readAt = article.articleFields.readAt,
updatedAt = article.articleFields.updatedAt,
readingProgress = article.articleFields.readingProgressPercent,
readingProgressAnchor = article.articleFields.readingProgressAnchorIndex,
imageURLString = article.articleFields.image,
pageURLString = article.articleFields.url,
descriptionText = article.articleFields.description,
publisherURLString = article.articleFields.originalArticleUrl,
siteName = article.articleFields.siteName,
author = article.articleFields.author,
publishDate = article.articleFields.publishedAt,
slug = article.articleFields.slug,
isArchived = article.articleFields.isArchived,
contentReader = article.articleFields.contentReader.rawValue,
content = article.articleFields.content
)
val article = articleQueryResult.item ?: return@launch
val articleContent = ArticleContent(
title = article.articleFields.title,
htmlContent = article.articleFields.content ?: "",
highlightsJSONString = Gson().toJson(highlights),
title = article.title,
htmlContent = article.content ?: "",
highlightsJSONString = Gson().toJson(articleQueryResult.highlights),
contentStatus = "SUCCEEDED",
objectID = "",
labelsJSONString = Gson().toJson(linkedItemLabels)
labelsJSONString = Gson().toJson(articleQueryResult.labels)
)
webReaderParamsLiveData.value = WebReaderParams(linkedItem, articleContent)
webReaderParamsLiveData.value = WebReaderParams(article, articleContent)
}
}
@ -132,7 +64,7 @@ class WebReaderViewModel @Inject constructor(
Log.d("Loggo", "received annotate action: $json")
}
"existingHighlightTap" -> {
Log.d("Loggo", "receive create highlight action: $json")
Log.d("Loggo", "receive existing highlight tap action: $json")
}
"shareHighlight" -> {
// unimplemented