Skip to content

Commit f6023b9

Browse files
leandroBorgesFerreiraLeandro Ferreira
andauthored
Fixing sync for deleted documents (#461)
* Logging the problem * Making last_sync_at work * Code clean * Update DocumentsSync.kt --------- Co-authored-by: Leandro Ferreira <[email protected]>
1 parent 17692ee commit f6023b9

File tree

12 files changed

+73
-30
lines changed

12 files changed

+73
-30
lines changed

application/core/connection/src/commonMain/kotlin/io/writeopia/di/AppConnectionInjection.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ object ApiInjectorDefaults {
5555
}
5656

5757
install(DefaultRequest) {
58-
println("Bearer $tokenJwt")
5958
header(HttpHeaders.Authorization, "Bearer $tokenJwt")
6059
}
6160

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ import kotlinx.datetime.Instant
1919
class DocumentsApi(private val client: HttpClient, private val baseUrl: String) {
2020

2121
suspend fun getNewDocuments(folderId: String, lastSync: Instant): ResultData<List<Document>> {
22-
println("get new documents. $lastSync")
23-
2422
val response = client.post("$baseUrl/api/document/folder/diff") {
2523
contentType(ContentType.Application.Json)
2624
setBody(FolderDiffRequest(folderId, lastSync.toEpochMilliseconds()))
@@ -29,7 +27,6 @@ class DocumentsApi(private val client: HttpClient, private val baseUrl: String)
2927
return if (response.status.isSuccess()) {
3028
ResultData.Complete(response.body<List<DocumentApi>>().map { it.toModel() })
3129
} else {
32-
println("getNewDocuments status: ${response.status.value}")
3330
ResultData.Error()
3431
}
3532
}
@@ -43,7 +40,6 @@ class DocumentsApi(private val client: HttpClient, private val baseUrl: String)
4340
return if (response.status.isSuccess()) {
4441
ResultData.Complete(Unit)
4542
} else {
46-
println("sendDocuments status: ${response.status.value}")
4743
ResultData.Error()
4844
}
4945
}

application/core/documents/src/commonMain/kotlin/io/writeopia/core/folders/extensions/FolderExtensions.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,6 @@ fun Folder.toEntity() = FolderEntity(
1313
last_updated_at = lastUpdatedAt.toEpochMilliseconds(),
1414
favorite = favorite.toLong(),
1515
icon = icon?.label,
16-
icon_tint = icon?.tint?.toLong()
16+
icon_tint = icon?.tint?.toLong(),
17+
last_synced_at = lastSyncedAt?.toEpochMilliseconds()
1718
)

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

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,52 @@ package io.writeopia.core.folders.sync
22

33
import io.writeopia.common.utils.ResultData
44
import io.writeopia.core.folders.api.DocumentsApi
5+
import io.writeopia.core.folders.repository.FolderRepository
6+
import io.writeopia.sdk.models.document.Folder
57
import io.writeopia.sdk.repository.DocumentRepository
68
import kotlinx.datetime.Clock
79
import kotlinx.datetime.Instant
810

911
class DocumentsSync(
1012
private val documentRepository: DocumentRepository,
1113
private val documentsApi: DocumentsApi,
12-
private val documentConflictHandler: DocumentConflictHandler
14+
private val documentConflictHandler: DocumentConflictHandler,
15+
private val folderRepository: FolderRepository
1316
) {
14-
1517
/**
1618
* Sync the folder with the backend end. The lastSync should be data fetched from the backend.
1719
*
1820
* This logic is atomic. If it fails, the whole process must be tried again in a future time.
1921
* The sync time of the folder will only be updated with everything works correctly.
2022
*/
2123
suspend fun syncFolder(folderId: String, userId: String) {
22-
val lastSync = documentRepository.loadDocumentById(folderId)?.lastSyncedAt
24+
// println("folderId: $folderId")
25+
val folder: Folder = folderRepository.getFolderById(folderId) ?: run {
26+
val folder = Folder(
27+
id = "root",
28+
parentId = "null",
29+
title = "root",
30+
createdAt = Instant.DISTANT_PAST,
31+
lastUpdatedAt = Instant.DISTANT_PAST,
32+
itemCount = 0,
33+
userId = userId,
34+
)
35+
36+
folderRepository.createFolder(folder)
37+
folder
38+
}
39+
40+
val lastSync = folder.lastSyncedAt
41+
// println("Sync. lastSync: $lastSync")
2342

2443
// First, receive the documents for the backend.
2544
val response = documentsApi.getNewDocuments(
2645
folderId,
2746
lastSync ?: Instant.DISTANT_PAST
2847
)
2948
val newDocuments = if (response is ResultData.Complete) response.data else return
49+
// println("Sync. received ${newDocuments.size} new documents")
50+
// println("Documents: ${newDocuments.joinToString(separator = "\n\n")}")
3051

3152
// Then, load the outdated documents.
3253
// These documents were updated locally, but were not sent to the backend yet
@@ -38,6 +59,9 @@ class DocumentsSync(
3859
documentConflictHandler.handleConflict(localOutdatedDocs, newDocuments)
3960
documentRepository.refreshDocuments()
4061

62+
// println("Sync. sending ${documentsNotSent.size} documents")
63+
// println("Documents sent: ${documentsNotSent.joinToString(separator = "\n\n")}")
64+
4165
// Send documents to backend
4266
val resultSend = documentsApi.sendDocuments(documentsNotSent)
4367

@@ -50,6 +74,7 @@ class DocumentsSync(
5074
}
5175

5276
documentRepository.refreshDocuments()
77+
folderRepository.updateFolder(folder.copy(lastSyncedAt = now))
5378
} else {
5479
return
5580
}

application/core/persistence_sqldelight/src/commonMain/kotlin/io/writeopia/sqldelight/dao/FolderSqlDelightDao.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ class FolderSqlDelightDao(database: WriteopiaDb?) : FolderSearch {
3131
last_updated_at = folder.last_updated_at,
3232
favorite = folder.favorite,
3333
icon = folder.icon,
34-
icon_tint = folder.icon_tint
34+
icon_tint = folder.icon_tint,
35+
last_synced_at = folder.last_synced_at
3536
)
3637
refreshFolders()
3738
}
@@ -56,7 +57,8 @@ class FolderSqlDelightDao(database: WriteopiaDb?) : FolderSearch {
5657
last_updated_at = folder.last_updated_at,
5758
favorite = folder.favorite,
5859
icon = folder.icon,
59-
icon_tint = folder.icon_tint
60+
icon_tint = folder.icon_tint,
61+
last_synced_at = folder.last_synced_at
6062
)
6163
refreshFolders()
6264
}

application/core/persistence_sqldelight/src/commonMain/kotlin/io/writeopia/sqldelight/extensions/FoldersExtensions.kt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,10 @@ fun FolderEntity.toModel(count: Long) =
1919
icon = if (this.icon != null && this.icon_tint != null) MenuItem.Icon(
2020
this.icon,
2121
this.icon_tint.toInt()
22-
) else null
22+
) else null,
23+
lastSyncedAt = if (last_synced_at != null) {
24+
Instant.fromEpochMilliseconds(last_synced_at)
25+
} else {
26+
null
27+
}
2328
)

application/core/persistence_sqldelight/src/commonMain/sqldelight/io/writeopia/app/sql/FolderEntity.sq

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ CREATE TABLE folderEntity (
55
title TEXT NOT NULL,
66
created_at INTEGER NOT NULL,
77
last_updated_at INTEGER NOT NULL,
8+
last_synced_at INTEGER,
89
favorite INTEGER NOT NULL,
910
icon TEXT,
1011
icon_tint INTEGER
@@ -57,10 +58,12 @@ FROM documentEntity
5758
GROUP BY parent_document_id;
5859

5960
insert:
60-
INSERT INTO folderEntity(id, parent_id, user_id, title, created_at, last_updated_at, favorite, icon, icon_tint)
61-
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
61+
INSERT INTO folderEntity(id, parent_id, user_id, title, created_at, last_updated_at, favorite, icon, icon_tint, last_synced_at)
62+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
6263
ON CONFLICT(id) DO
63-
UPDATE SET id=excluded.id, parent_id=excluded.parent_id, user_id=excluded.user_id, title=excluded.title, created_at=excluded.created_at, last_updated_at=excluded.last_updated_at, favorite=excluded.favorite, icon=excluded.icon, icon_tint=excluded.icon_tint;
64+
UPDATE SET id=excluded.id, parent_id=excluded.parent_id, user_id=excluded.user_id, title=excluded.title,
65+
created_at=excluded.created_at, last_updated_at=excluded.last_updated_at, favorite=excluded.favorite,
66+
icon=excluded.icon, icon_tint=excluded.icon_tint, last_synced_at=excluded.last_synced_at;
6467

6568
-- update:
6669
-- UPDATE(title, parent_id, update_at): SET title=title, parent_id=parent_id, update_at=update_at;

application/features/note_menu/src/commonMain/kotlin/io/writeopia/notemenu/di/NotesMenuKmpInjection.kt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,13 @@ class NotesMenuKmpInjection private constructor(
3939
private fun provideDocumentRepository(): DocumentRepository =
4040
repositoryInjection.provideDocumentRepository()
4141

42+
private fun provideFolderRepository() = FoldersInjector.singleton().provideFoldersRepository()
43+
4244
private fun provideNotesUseCase(
4345
documentRepository: DocumentRepository = provideDocumentRepository(),
4446
configurationRepository: ConfigurationRepository =
4547
appConfigurationInjector.provideNotesConfigurationRepository(),
46-
folderRepository: FolderRepository = FoldersInjector.singleton().provideFoldersRepository()
48+
folderRepository: FolderRepository = provideFolderRepository()
4749
): NotesUseCase {
4850
return NotesUseCase.singleton(
4951
documentRepository,
@@ -71,7 +73,8 @@ class NotesMenuKmpInjection private constructor(
7173
documentConflictHandler = DocumentConflictHandler(
7274
documentRepository,
7375
authCoreInjection.provideAuthRepository()
74-
)
76+
),
77+
folderRepository = provideFolderRepository()
7578
)
7679
}
7780

backend/documents/documents/src/main/java/io/writeopia/api/documents/documents/repository/DocumentSqlBeDao.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ class DocumentSqlBeDao(
8585
icon_tint = document.icon?.tint,
8686
is_locked = document.isLocked,
8787
company_id = "",
88-
deleted = false
88+
deleted = document.deleted
8989
)
9090
}
9191

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

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ class DocumentSqlDao(
8383
is_locked = document.isLocked.toLong(),
8484
user_id_ = userId,
8585
company_id = companyId,
86-
deleted = 0
86+
deleted = document.deleted.toLong()
8787
)
8888
}
8989

@@ -195,7 +195,8 @@ class DocumentSqlDao(
195195
document.icon_tint?.toInt()
196196
)
197197
},
198-
isLocked = document.is_locked == 1L
198+
isLocked = document.is_locked == 1L,
199+
deleted = document.deleted == 1L
199200
)
200201
}
201202
} ?: emptyList()
@@ -261,7 +262,8 @@ class DocumentSqlDao(
261262
document.icon_tint?.toInt()
262263
)
263264
},
264-
isLocked = document.is_locked == 1L
265+
isLocked = document.is_locked == 1L,
266+
deleted = document.deleted == 1L
265267
)
266268
}
267269
}
@@ -333,7 +335,8 @@ class DocumentSqlDao(
333335
document.icon_tint?.toInt()
334336
)
335337
},
336-
isLocked = document.is_locked == 1L
338+
isLocked = document.is_locked == 1L,
339+
deleted = document.deleted == 1L
337340
)
338341
}
339342
}
@@ -405,7 +408,8 @@ class DocumentSqlDao(
405408
document.icon_tint?.toInt()
406409
)
407410
},
408-
isLocked = document.is_locked == 1L
411+
isLocked = document.is_locked == 1L,
412+
deleted = document.deleted == 1L
409413
)
410414
}
411415
} ?: emptyList()
@@ -476,7 +480,8 @@ class DocumentSqlDao(
476480
document.icon_tint?.toInt()
477481
)
478482
},
479-
isLocked = document.is_locked == 1L
483+
isLocked = document.is_locked == 1L,
484+
deleted = document.deleted == 1L
480485
)
481486
}
482487
} ?: emptyList()
@@ -553,7 +558,8 @@ class DocumentSqlDao(
553558
document.icon_tint?.toInt()
554559
)
555560
},
556-
isLocked = document.is_locked == 1L
561+
isLocked = document.is_locked == 1L,
562+
deleted = document.deleted == 1L
557563
)
558564
}
559565
}
@@ -620,7 +626,8 @@ class DocumentSqlDao(
620626
document.icon_tint?.toInt()
621627
)
622628
},
623-
isLocked = document.is_locked == 1L
629+
isLocked = document.is_locked == 1L,
630+
deleted = document.deleted == 1L
624631
)
625632
}
626633
} ?: emptyList()
@@ -687,7 +694,8 @@ class DocumentSqlDao(
687694
document.icon_tint?.toInt()
688695
)
689696
},
690-
isLocked = document.is_locked == 1L
697+
isLocked = document.is_locked == 1L,
698+
deleted = document.deleted == 1L,
691699
)
692700
}
693701
} ?: emptyList()

0 commit comments

Comments
 (0)