Skip to content

Commit 263d5b5

Browse files
Sending android outdated documents (#546)
* Sending android outdated documents * Fixing deletion in android * removing unused import
1 parent fdea6c6 commit 263d5b5

File tree

12 files changed

+50
-38
lines changed

12 files changed

+50
-38
lines changed

application/core/documents/src/commonMain/kotlin/io/writeopia/core/folders/api/DocumentsApi.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class DocumentsApi(private val client: HttpClient, private val baseUrl: String)
3030
lastSync: Instant,
3131
token: String
3232
): ResultData<List<Document>> {
33-
val response = client.post("$baseUrl/api/document/folder/diff") {
33+
val response = client.post("$baseUrl/api/workspace/document/folder/diff") {
3434
contentType(ContentType.Application.Json)
3535
setBody(FolderDiffRequest(folderId, workspaceId, lastSync.toEpochMilliseconds()))
3636
header(HttpHeaders.Authorization, "Bearer $token")
@@ -39,6 +39,7 @@ class DocumentsApi(private val client: HttpClient, private val baseUrl: String)
3939
return if (response.status.isSuccess()) {
4040
ResultData.Complete(response.body<List<DocumentApi>>().map { it.toModel() })
4141
} else {
42+
println("getFolderNewDocuments failed. response: $response")
4243
ResultData.Error()
4344
}
4445
}

application/core/documents/src/commonMain/kotlin/io/writeopia/core/folders/sync/FolderSync.kt

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class FolderSync(
1717
private val documentConflictHandler: DocumentConflictHandler,
1818
private val folderRepository: FolderRepository,
1919
private val authRepository: AuthRepository,
20-
private val minSyncInternal: Duration = 3.seconds
20+
private val minSyncInternal: Duration = 2.seconds
2121
) {
2222

2323
private var lastSuccessfulSync: Instant = Instant.DISTANT_PAST
@@ -64,13 +64,19 @@ class FolderSync(
6464
authToken
6565
)
6666

67-
val newDocuments = if (response is ResultData.Complete) response.data else return
67+
val newDocuments = if (response is ResultData.Complete) {
68+
response.data
69+
} else {
70+
println("newDocuments failed.")
71+
return
72+
}
6873
println("Sync. received ${newDocuments.size} new documents")
6974
// println("Documents: ${newDocuments.joinToString(separator = "\n\n")}")
7075

7176
// Then, load the outdated documents.
7277
// These documents were updated locally, but were not sent to the backend yet
73-
val localOutdatedDocs = documentRepository.loadOutdatedDocuments(folderId, workspaceId)
78+
val localOutdatedDocs = documentRepository.loadOutdatedDocumentsByFolder(folderId, workspaceId)
79+
println("Sync. loaded ${localOutdatedDocs.size} outdated documents")
7480

7581
// Resolve conflicts of documents that were updated both locally and in the backend.
7682
// Documents will be saved locally by documentConflictHandler.handleConflict

application/core/ollama/config/ktlint/baseline.xml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@
33
<file name="src/commonMain/kotlin/io/writeopia/OllamaRepository.kt">
44
<error line="56" column="83" source="standard:function-expression-body" />
55
</file>
6-
<file name="src/commonMain/kotlin/io/writeopia/api/OllamaApi.kt">
7-
<error line="193" column="72" source="standard:function-expression-body" />
8-
</file>
96
<file name="src/commonMain/kotlin/io/writeopia/persistence/OllamaSqlDao.kt">
107
<error line="10" column="17" source="standard:backing-property-naming" />
118
<error line="34" column="79" source="standard:function-expression-body" />

application/features/note_menu/src/commonMain/kotlin/io/writeopia/notemenu/ui/screen/menu/MobileChooseNoteScreen.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ internal fun MobileChooseNoteScreen(
7070

7171
val hasSelectedNotes by chooseNoteViewModel.hasSelectedNotes.collectAsState()
7272
val editState by chooseNoteViewModel.editState.collectAsState()
73-
7473
val folderEdit = chooseNoteViewModel.editFolderState.collectAsState().value
7574

7675
val showFab by derivedStateOf { !editState && !hasSelectedNotes }

plugins/writeopia_persistence_core/src/commonMain/kotlin/io/writeopia/sdk/persistence/core/repository/InMemoryDocumentRepository.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ class InMemoryDocumentRepository : DocumentRepository {
139139
override suspend fun stopListeningForFoldersByParentId(parentId: String, workspaceId: String) {
140140
}
141141

142-
override suspend fun loadOutdatedDocuments(folderId: String, workspaceId: String): List<Document> = emptyList()
142+
override suspend fun loadOutdatedDocumentsByFolder(folderId: String, workspaceId: String): List<Document> = emptyList()
143143

144144
override suspend fun loadOutdatedDocumentsForWorkspace(workspaceId: String): List<Document> =
145145
emptyList()
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<baseline version="1.0">
33
<file name="src/commonMain/kotlin/io/writeopia/sdk/persistence/dao/DocumentEntityDao.kt">
4-
<error line="48" column="12" source="standard:argument-list-wrapping" />
5-
<error line="48" column="140" source="standard:max-line-length" />
6-
<error line="48" column="161" source="standard:argument-list-wrapping" />
4+
<error line="52" column="141" source="standard:max-line-length" />
75
</file>
86
</baseline>

plugins/writeopia_persistence_room/src/commonMain/kotlin/io/writeopia/sdk/persistence/dao/DocumentEntityDao.kt

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ interface DocumentEntityDao {
2828
suspend fun deleteDocuments(vararg documents: DocumentEntity)
2929

3030
@Query("DELETE FROM $DOCUMENT_ENTITY WHERE workspace_id = :userId")
31-
suspend fun deleteDocumentsByUserId(userId: String)
31+
suspend fun purgeDocumentsByUserId(userId: String)
3232

3333
@Query("SELECT * FROM $DOCUMENT_ENTITY ORDER BY $DOCUMENT_ENTITY.last_updated_at LIMIT 10")
3434
suspend fun selectByLastUpdated(): List<DocumentEntity>
@@ -45,8 +45,14 @@ interface DocumentEntityDao {
4545
@Query("SELECT * FROM $DOCUMENT_ENTITY WHERE $DOCUMENT_ENTITY.parent_id = :id")
4646
suspend fun loadDocumentsByParentId(id: String): List<DocumentEntity>
4747

48-
@Query("SELECT * FROM $DOCUMENT_ENTITY WHERE $DOCUMENT_ENTITY.parent_id = :folderId AND $DOCUMENT_ENTITY.last_updated_at > $DOCUMENT_ENTITY.last_synced_at")
49-
suspend fun loadOutdatedDocumentsByFolderId(folderId: String): List<DocumentEntity>
48+
@Query(
49+
"SELECT * " +
50+
"FROM $DOCUMENT_ENTITY " +
51+
"LEFT OUTER JOIN $STORY_UNIT_ENTITY ON $DOCUMENT_ENTITY.id = $STORY_UNIT_ENTITY.document_id " +
52+
"WHERE $DOCUMENT_ENTITY.parent_id = :folderId AND ($DOCUMENT_ENTITY.last_updated_at > $DOCUMENT_ENTITY.last_synced_at OR $DOCUMENT_ENTITY.last_synced_at IS NULL) " +
53+
"ORDER BY $DOCUMENT_ENTITY.created_at, $STORY_UNIT_ENTITY.position"
54+
)
55+
suspend fun loadOutdatedDocumentsByFolderId(folderId: String): Map<DocumentEntity, List<StoryStepEntity>>
5056

5157
@Query("SELECT * FROM $DOCUMENT_ENTITY")
5258
suspend fun loadAllDocuments(): List<DocumentEntity>

plugins/writeopia_persistence_room/src/commonMain/kotlin/io/writeopia/sdk/persistence/dao/room/RoomDocumentRepository.kt

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ import io.writeopia.sdk.search.DocumentSearch
99
import io.writeopia.sdk.repository.DocumentRepository
1010
import io.writeopia.sdk.persistence.dao.DocumentEntityDao
1111
import io.writeopia.sdk.persistence.dao.StoryUnitEntityDao
12-
import io.writeopia.sdk.persistence.entity.document.DocumentEntity
1312
import io.writeopia.sdk.persistence.entity.story.StoryStepEntity
1413
import io.writeopia.sdk.persistence.parse.toEntity
1514
import io.writeopia.sdk.persistence.parse.toModel
1615
import kotlinx.coroutines.flow.Flow
1716
import kotlinx.coroutines.flow.MutableStateFlow
1817
import kotlinx.coroutines.flow.map
18+
import kotlinx.datetime.Clock
1919
import kotlinx.datetime.Instant
2020
import kotlin.collections.component1
2121
import kotlin.collections.component2
@@ -136,18 +136,24 @@ class RoomDocumentRepository(
136136
}
137137

138138
override suspend fun deleteDocument(document: Document) {
139-
documentEntityDao.deleteDocuments(document.toEntity())
139+
documentEntityDao.updateDocument(
140+
document.toEntity()
141+
.copy(
142+
lastUpdatedAt = Clock.System.now().toEpochMilliseconds(),
143+
isDeleted = true
144+
)
145+
)
140146
}
141147

142148
override suspend fun deleteDocumentByIds(ids: Set<String>) {
143149
// The user ID is not relevant in the way to delete documents
144-
documentEntityDao.deleteDocuments(
145-
*ids.map {
146-
DocumentEntity.createById(
147-
it,
148-
"",
149-
parentId = ""
150-
)
150+
documentEntityDao.updateDocument(
151+
*ids.mapNotNull {
152+
documentEntityDao.loadDocumentById(it)
153+
?.copy(
154+
lastUpdatedAt = Clock.System.now().toEpochMilliseconds(),
155+
isDeleted = true
156+
)
151157
}.toTypedArray()
152158
)
153159
}
@@ -162,7 +168,7 @@ class RoomDocumentRepository(
162168
}
163169

164170
override suspend fun deleteByWorkspace(userId: String) {
165-
documentEntityDao.deleteDocumentsByUserId(userId)
171+
documentEntityDao.purgeDocumentsByUserId(userId)
166172
}
167173

168174
override suspend fun moveDocumentsToWorkspace(oldUserId: String, newUserId: String) {
@@ -225,16 +231,15 @@ class RoomDocumentRepository(
225231
) {
226232
}
227233

228-
override suspend fun loadOutdatedDocuments(
234+
override suspend fun loadOutdatedDocumentsByFolder(
229235
folderId: String,
230236
workspaceId: String
231237
): List<Document> =
232-
documentEntityDao.loadOutdatedDocumentsByFolderId(folderId).map { entity ->
233-
val content = loadInnerSteps(
234-
storyUnitEntityDao?.loadDocumentContent(entity.id) ?: emptyList()
235-
)
236-
entity.toModel(content = content)
237-
}
238+
documentEntityDao.loadOutdatedDocumentsByFolderId(folderId)
239+
.map { (documentEntity, storyEntity) ->
240+
val content = loadInnerSteps(storyEntity)
241+
documentEntity.toModel(content)
242+
}
238243

239244
override suspend fun loadOutdatedDocumentsForWorkspace(workspaceId: String): List<Document> =
240245
documentEntityDao.loadOutdatedDocumentsWithContentForWorkspace(workspaceId)

plugins/writeopia_persistence_room/src/commonMain/kotlin/io/writeopia/sdk/persistence/parse/DocumentParse.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ fun DocumentEntity.toModel(content: Map<Int, StoryStep> = emptyMap()) = Document
1515
favorite = favorite,
1616
parentId = parentId,
1717
isLocked = isLocked,
18-
lastSyncedAt = lastSyncedAt?.let(Instant::fromEpochMilliseconds)
18+
lastSyncedAt = lastSyncedAt?.let(Instant::fromEpochMilliseconds),
19+
deleted = isDeleted
1920
)
2021

2122
fun Document.toEntity() = DocumentEntity(

plugins/writeopia_persistence_sqldelight/src/commonMain/kotlin/io/writeopia/sdk/persistence/sqldelight/dao/sql/SqlDelightDocumentRepository.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ class SqlDelightDocumentRepository(
8181
)
8282
}
8383

84-
override suspend fun loadOutdatedDocuments(
84+
override suspend fun loadOutdatedDocumentsByFolder(
8585
folderId: String,
8686
workspaceId: String
8787
): List<Document> =

0 commit comments

Comments
 (0)