mirror of
https://github.com/omnivore-app/omnivore.git
synced 2026-03-11 08:54:26 +00:00
sync offline highlight changes with serevr before syncing library
This commit is contained in:
parent
a0d00028a7
commit
8cd14c7540
2 changed files with 70 additions and 3 deletions
|
|
@ -3,10 +3,13 @@ package app.omnivore.omnivore
|
|||
import android.content.Context
|
||||
import android.util.Log
|
||||
import androidx.room.Room
|
||||
import app.omnivore.omnivore.graphql.generated.type.CreateHighlightInput
|
||||
import app.omnivore.omnivore.graphql.generated.type.UpdateHighlightInput
|
||||
import app.omnivore.omnivore.models.ServerSyncStatus
|
||||
import app.omnivore.omnivore.networking.*
|
||||
import app.omnivore.omnivore.persistence.AppDatabase
|
||||
import app.omnivore.omnivore.persistence.entities.*
|
||||
import com.apollographql.apollo3.api.Optional
|
||||
import javax.inject.Inject
|
||||
|
||||
class DataService @Inject constructor(
|
||||
|
|
@ -171,8 +174,66 @@ private suspend fun DataService.syncSavedItem(item: SavedItem) {
|
|||
}
|
||||
}
|
||||
|
||||
private suspend fun DataService.syncHighlight(item: Highlight) {
|
||||
// TODO: update
|
||||
private suspend fun DataService.syncHighlight(highlight: Highlight) {
|
||||
fun updateSyncStatus(status: ServerSyncStatus) {
|
||||
highlight.serverSyncStatus = status.rawValue
|
||||
db.highlightDao().update(highlight)
|
||||
}
|
||||
|
||||
when (highlight.serverSyncStatus) {
|
||||
ServerSyncStatus.NEEDS_DELETION.rawValue -> {
|
||||
updateSyncStatus(ServerSyncStatus.IS_SYNCING)
|
||||
|
||||
val isDeletedOnServer = networker.deleteHighlights(listOf(highlight.highlightId))
|
||||
|
||||
if (isDeletedOnServer) {
|
||||
db.highlightDao().deleteById(highlight.highlightId)
|
||||
} else {
|
||||
updateSyncStatus(ServerSyncStatus.NEEDS_DELETION)
|
||||
}
|
||||
}
|
||||
ServerSyncStatus.NEEDS_UPDATE.rawValue -> {
|
||||
updateSyncStatus(ServerSyncStatus.IS_SYNCING)
|
||||
|
||||
val isUpdatedOnServer = networker.updateHighlight(
|
||||
UpdateHighlightInput(
|
||||
annotation = Optional.presentIfNotNull(highlight.annotation),
|
||||
highlightId = highlight.highlightId ?: "",
|
||||
sharedAt = Optional.absent()
|
||||
)
|
||||
)
|
||||
|
||||
if (isUpdatedOnServer) {
|
||||
updateSyncStatus(ServerSyncStatus.IS_SYNCED)
|
||||
} else {
|
||||
updateSyncStatus(ServerSyncStatus.NEEDS_UPDATE)
|
||||
}
|
||||
}
|
||||
ServerSyncStatus.NEEDS_CREATION.rawValue -> {
|
||||
updateSyncStatus(ServerSyncStatus.IS_SYNCING)
|
||||
|
||||
val savedItemID = db.savedItemAndHighlightCrossRefDao()
|
||||
.associatedSavedItemID(highlightId = highlight.highlightId)
|
||||
|
||||
val isCreatedOnServer = networker.createHighlight(
|
||||
CreateHighlightInput(
|
||||
annotation = Optional.presentIfNotNull(highlight.annotation),
|
||||
articleId = savedItemID ?: "",
|
||||
id = highlight.highlightId,
|
||||
patch = highlight.patch ?: "",
|
||||
quote = highlight.quote ?: "",
|
||||
shortId = highlight.shortId ?: ""
|
||||
)
|
||||
)
|
||||
|
||||
if (isCreatedOnServer != null) {
|
||||
updateSyncStatus(ServerSyncStatus.IS_SYNCED)
|
||||
} else {
|
||||
updateSyncStatus(ServerSyncStatus.NEEDS_UPDATE)
|
||||
}
|
||||
}
|
||||
else -> return
|
||||
}
|
||||
}
|
||||
|
||||
data class SavedItemSyncResult(
|
||||
|
|
|
|||
|
|
@ -14,7 +14,7 @@ data class Highlight(
|
|||
val patch: String,
|
||||
val prefix: String?,
|
||||
val quote: String,
|
||||
val serverSyncStatus: Int = ServerSyncStatus.IS_SYNCED.rawValue,
|
||||
var serverSyncStatus: Int = ServerSyncStatus.IS_SYNCED.rawValue,
|
||||
val shortId: String,
|
||||
val suffix: String?,
|
||||
val updatedAt: String?
|
||||
|
|
@ -50,6 +50,9 @@ data class SavedItemAndHighlightCrossRef(
|
|||
interface SavedItemAndHighlightCrossRefDao {
|
||||
@Insert(onConflict = OnConflictStrategy.REPLACE)
|
||||
fun insertAll(items: List<SavedItemAndHighlightCrossRef>)
|
||||
|
||||
@Query("SELECT savedItemId FROM savedItemAndHighlightCrossRef WHERE highlightId = :highlightId")
|
||||
fun associatedSavedItemID(highlightId: String): String?
|
||||
}
|
||||
|
||||
data class SavedItemWithLabelsAndHighlights(
|
||||
|
|
@ -78,6 +81,9 @@ interface HighlightDao {
|
|||
@Insert(onConflict = OnConflictStrategy.REPLACE)
|
||||
fun insertAll(items: List<Highlight>)
|
||||
|
||||
@Query("DELETE FROM highlight WHERE highlightId = :highlightId")
|
||||
fun deleteById(highlightId: String)
|
||||
|
||||
@Update
|
||||
fun update(highlight: Highlight)
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue