-
Notifications
You must be signed in to change notification settings - Fork 11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Alessandro/address bookmarks feedback folder logic #2221
Merged
alessandroboron
merged 7 commits into
alessandro/address-bookmarks-feedback
from
alessandro/address-bookmarks-feedback-folder-logic
Feb 23, 2024
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
3a313a1
Initial logic for AddEditBookmarkFolder
alessandroboron aebe74f
Update logic and remove addFolderAction
alessandroboron 09f051d
Move AddEditBookmarkFolderDialogViewModel in its own file
alessandroboron 92e40f2
Refactor AddEditBookmarkFolderDialogView and AddBookmarkFolderPopover…
alessandroboron 4d07f5d
Add unit tests for AddEditBookmarkFolderViewModel
alessandroboron 58e69c2
Improve view model to update and move bookmark folders in one transac…
alessandroboron afeebfb
Update unit tests
alessandroboron File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -391,18 +391,21 @@ final class LocalBookmarkStore: BookmarkStore { | |
} | ||
|
||
func update(folder: BookmarkFolder) { | ||
|
||
do { | ||
_ = try applyChangesAndSave(changes: { context in | ||
let folderFetchRequest = BaseBookmarkEntity.singleEntity(with: folder.id) | ||
let folderFetchRequestResults = try? context.fetch(folderFetchRequest) | ||
|
||
guard let bookmarkFolderMO = folderFetchRequestResults?.first else { | ||
assertionFailure("LocalBookmarkStore: Failed to get BookmarkEntity from the context") | ||
throw BookmarkStoreError.missingEntity | ||
} | ||
try update(folder: folder, in: context) | ||
}) | ||
} catch { | ||
let error = error as NSError | ||
commonOnSaveErrorHandler(error) | ||
} | ||
} | ||
|
||
bookmarkFolderMO.update(with: folder) | ||
func update(folder: BookmarkFolder, andMoveToParent parent: ParentFolderType) { | ||
do { | ||
_ = try applyChangesAndSave(changes: { context in | ||
let folderEntity = try update(folder: folder, in: context) | ||
try move(entities: [folderEntity], toIndex: nil, withinParentFolderType: parent, in: context) | ||
}) | ||
} catch { | ||
let error = error as NSError | ||
|
@@ -566,39 +569,15 @@ final class LocalBookmarkStore: BookmarkStore { | |
throw BookmarkStoreError.storeDeallocated | ||
} | ||
|
||
guard let rootFolder = self.bookmarksRoot(in: context) else { | ||
throw BookmarkStoreError.missingRoot | ||
} | ||
|
||
// Guarantee that bookmarks are fetched in the same order as the UUIDs. In the future, this should fetch all objects at once with a | ||
// batch fetch request and have them sorted in the correct order. | ||
let bookmarkManagedObjects: [BookmarkEntity] = objectUUIDs.compactMap { uuid in | ||
let entityFetchRequest = BaseBookmarkEntity.singleEntity(with: uuid) | ||
return (try? context.fetch(entityFetchRequest))?.first | ||
} | ||
|
||
let newParentFolder: BookmarkEntity | ||
|
||
switch type { | ||
case .root: newParentFolder = rootFolder | ||
case .parent(let newParentUUID): | ||
let bookmarksFetchRequest = BaseBookmarkEntity.singleEntity(with: newParentUUID) | ||
|
||
if let fetchedParent = try context.fetch(bookmarksFetchRequest).first, fetchedParent.isFolder { | ||
newParentFolder = fetchedParent | ||
} else { | ||
throw BookmarkStoreError.missingEntity | ||
} | ||
} | ||
try move(entities: bookmarkManagedObjects, toIndex: index, withinParentFolderType: type, in: context) | ||
|
||
if let index = index, index < newParentFolder.childrenArray.count { | ||
self.move(entities: bookmarkManagedObjects, to: index, within: newParentFolder) | ||
} else { | ||
for bookmarkManagedObject in bookmarkManagedObjects { | ||
bookmarkManagedObject.parent = nil | ||
newParentFolder.addToChildren(bookmarkManagedObject) | ||
} | ||
} | ||
}, onError: { [weak self] error in | ||
self?.commonOnSaveErrorHandler(error) | ||
DispatchQueue.main.async { completion(error) } | ||
|
@@ -996,6 +975,53 @@ final class LocalBookmarkStore: BookmarkStore { | |
|
||
} | ||
|
||
private extension LocalBookmarkStore { | ||
|
||
@discardableResult | ||
func update(folder: BookmarkFolder, in context: NSManagedObjectContext) throws -> BookmarkEntity { | ||
let folderFetchRequest = BaseBookmarkEntity.singleEntity(with: folder.id) | ||
let folderFetchRequestResults = try? context.fetch(folderFetchRequest) | ||
|
||
guard let bookmarkFolderMO = folderFetchRequestResults?.first else { | ||
assertionFailure("LocalBookmarkStore: Failed to get BookmarkEntity from the context") | ||
throw BookmarkStoreError.missingEntity | ||
} | ||
|
||
bookmarkFolderMO.update(with: folder) | ||
return bookmarkFolderMO | ||
} | ||
|
||
func move(entities: [BookmarkEntity], toIndex index: Int?, withinParentFolderType type: ParentFolderType, in context: NSManagedObjectContext) throws { | ||
guard let rootFolder = bookmarksRoot(in: context) else { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above, I just moved this existing logic in its own function so I can use it when doing an update and move in one go. |
||
throw BookmarkStoreError.missingRoot | ||
} | ||
|
||
let newParentFolder: BookmarkEntity | ||
|
||
switch type { | ||
case .root: newParentFolder = rootFolder | ||
case .parent(let newParentUUID): | ||
let bookmarksFetchRequest = BaseBookmarkEntity.singleEntity(with: newParentUUID) | ||
|
||
if let fetchedParent = try context.fetch(bookmarksFetchRequest).first, fetchedParent.isFolder { | ||
newParentFolder = fetchedParent | ||
} else { | ||
throw BookmarkStoreError.missingEntity | ||
} | ||
} | ||
|
||
if let index = index, index < newParentFolder.childrenArray.count { | ||
self.move(entities: entities, to: index, within: newParentFolder) | ||
} else { | ||
for bookmarkManagedObject in entities { | ||
bookmarkManagedObject.parent = nil | ||
newParentFolder.addToChildren(bookmarkManagedObject) | ||
} | ||
} | ||
} | ||
|
||
} | ||
|
||
extension LocalBookmarkStore.BookmarkStoreError: CustomNSError { | ||
|
||
var errorCode: Int { | ||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This logic was already existing, I just moved into its own function so that I can use either when the folder is updated (title changes) and when the folder moves within another parent folder