refactor: improve sync merging categories (#1559)
* feat: Add versioning to categories * feat: use random UID for categories. For legacy and migration we should assign uid on insert, and modify existing one as well in the migration. * feat: sync category metadata Add version, uid and lastModifiedAt fields to Category model to allow syncing. * chore: fix category merging logic Improve the category merging logic by matching using UIDs first, with a fallback to matching by name for legacy remote categories. Previously, categories were only matched by name, which could lead to incorrect merges if names were changed. This change ensures more accurate synchronization by prioritizing the unique identifier. Conflict resolution is now based on the `version` field, and logging has been added for better visibility into the merging process. * refactor: prioritize UID when restoring categories If a category with the same UID exists, update it instead of creating a new one. Fallback to matching by name if no UID match is found. * chore: add isSyncing flag like before. This make sure the version is consistent, and it's not accidentally appended by the trigger, if it does then one device will always be ahead, than previous, and they need to make multiple changes to increase the version. * Apply suggestion from @jobobby04 Use SY specific numbers(601, 602 for now) Co-authored-by: jobobby04 <jobobby04@users.noreply.github.com> * chore: commit review, re-order. * chore: surround changes in // SY --> // SY <-- * refactor: fallback to existing category UID if backup UID is 0 during restore. when dealing with old backups (backups created before we added UIDs). In those old backups, backupCategory.uid defaults to 0. If a user restored an old backup, it would match by name, and then overwrite the newly generated local UID with 0. This would break the synchronization. * refactor: change to 6xx * feat: improve sync reliability for categories and settings - Refactor `mergeCategoriesLists` to correctly match categories by name when UID matching fails, ensuring better reconciliation across devices. - Fix a bug in category merging where multiple categories with UID 0 (common for non-synced items) caused data loss. - Update `SyncManager` to detect changes in categories, sources, preferences, saved searches, and extension repos, ensuring they synchronize even when the library favorites haven't changed. - Convert `BackupCategory` and `BackupExtensionRepos` to data classes to support robust content-aware comparison during the sync process. - Fix data loss in `mergeSourcesLists`, `mergePreferencesLists`, and `mergeSavedSearchesLists` by retaining local versions when conflicting with remote data. * fix(sync): properly sync category deletions across devices Previously, the sync system could not distinguish between a category that was deleted locally and a new category created on another device, causing deleted categories to be restored from the remote backup. - Update `SyncService` to use `lastSyncTimestamp` to deduce if a missing local category was deleted (if modified before last sync) or newly created remotely (if modified after). - Update `SyncManager` to explicitly delete local categories that are absent from the merged remote backup, propagating deletions to other devices. - Fix `RestoreOptions` in `SyncManager` to respect the user's sync preferences instead of hardcoding `categories = true`. * chore: change it to 6xx and not 600. * chore: don't need to change this. * chore: use kotlin time duration units --------- Co-authored-by: jobobby04 <jobobby04@users.noreply.github.com>
This commit is contained in:
@@ -7,6 +7,9 @@ data class Category(
|
||||
val name: String,
|
||||
val order: Long,
|
||||
val flags: Long,
|
||||
val version: Long = 0,
|
||||
val uid: Long = 0,
|
||||
val lastModifiedAt: Long = 0,
|
||||
) : Serializable {
|
||||
|
||||
val isSystemCategory: Boolean = id == UNCATEGORIZED_ID
|
||||
|
||||
@@ -5,4 +5,7 @@ data class CategoryUpdate(
|
||||
val name: String? = null,
|
||||
val order: Long? = null,
|
||||
val flags: Long? = null,
|
||||
val version: Long? = null,
|
||||
val uid: Long? = null,
|
||||
val lastModifiedAt: Long? = null,
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user