Skip to content
This repository has been archived by the owner on May 10, 2024. It is now read-only.

Commit

Permalink
Fix #593: Bookmark order migration (#602)
Browse files Browse the repository at this point in the history
  • Loading branch information
iccub authored and Joel Reis committed Dec 7, 2018
1 parent fff6688 commit bce40f3
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
5 changes: 5 additions & 0 deletions Client/Application/Migration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,14 @@ extension Preferences {
// This needs to be translated to our new preference.
Preferences.General.isFirstLaunch.value = Preferences.DAU.lastLaunchInfo.value == nil

// Core Data

// Migrate the shield overrides
migrateShieldOverrides()

Bookmark.migrateOrder(forFavorites: true)
Bookmark.migrateOrder(forFavorites: false)

Preferences.Migration.completed.value = true
}

Expand Down
34 changes: 34 additions & 0 deletions Data/models/Bookmark.swift
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,40 @@ public final class Bookmark: NSManagedObject, WebsitePresentable, Syncable, CRUD
DataController.save(context: frc.managedObjectContext)
}

/// Takes all Bookmarks and Favorites from 1.6 and sets correct order for them.
/// 1.6 had few bugs with reordering which we want to avoid, in particular non-reordered bookmarks on 1.6
/// all have order set to 0 which makes sorting confusing.
/// In migration we take all bookmarks using the same sorting method as on 1.6 and add a proper `order`
/// attribute to them. The goal is to have all bookmarks with a proper unique order number set.
public class func migrateOrder(parentFolder: Bookmark? = nil,
forFavorites: Bool,
context: NSManagedObjectContext = DataController.newBackgroundContext()) {

let predicate = forFavorites ?
NSPredicate(format: "isFavorite == true") : allBookmarksOfAGivenLevelPredicate(parent: parentFolder)

let orderSort = NSSortDescriptor(key: #keyPath(Bookmark.order), ascending: true)
let folderSort = NSSortDescriptor(key: #keyPath(Bookmark.isFolder), ascending: false)
let createdSort = NSSortDescriptor(key: #keyPath(Bookmark.created), ascending: true)

let sort = [orderSort, folderSort, createdSort]

guard let allBookmarks = all(where: predicate, sortDescriptors: sort, context: context),
!allBookmarks.isEmpty else {
return
}

for (i, bookmark) in allBookmarks.enumerated() {
bookmark.order = Int16(i)
// Calling this method recursively to get ordering for nested bookmarks
if !forFavorites && bookmark.isFolder {
migrateOrder(parentFolder: bookmark, forFavorites: forFavorites, context: context)
}
}

DataController.save(context: context)
}

// TODO: Migration syncUUIDS still needs to be solved
// Should only ever be used for migration from old db
// Always uses worker context
Expand Down

0 comments on commit bce40f3

Please sign in to comment.