Skip to content
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

[iOS] Fix reordering playlist items with multiple folders (uplift to 1.74.x) #26658

Merged
merged 1 commit into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 38 additions & 6 deletions ios/brave-ios/Sources/Data/models/PlaylistItem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import CoreData
import Foundation
import Shared
import SwiftUI
import os.log

@objc(PlaylistItem)
Expand Down Expand Up @@ -164,7 +165,7 @@ final public class PlaylistItem: NSManagedObject, CRUD, Identifiable {
context: context
)

PlaylistItem.reorderItems(context: context)
PlaylistItem.reorderItems(context: context, folderUUID: folderUUID)
PlaylistItem.saveContext(context)

DispatchQueue.main.async {
Expand Down Expand Up @@ -200,7 +201,7 @@ final public class PlaylistItem: NSManagedObject, CRUD, Identifiable {
playlistItem.playlistFolder = folder
}

PlaylistItem.reorderItems(context: context)
PlaylistItem.reorderItems(context: context, folderUUID: folderUUID)
PlaylistItem.saveContext(context)

DispatchQueue.main.async {
Expand All @@ -209,6 +210,31 @@ final public class PlaylistItem: NSManagedObject, CRUD, Identifiable {
}
}

public static func reorderItems(
in folder: PlaylistFolder,
fromOffsets indexSet: IndexSet,
toOffset offset: Int
) {
let frc = PlaylistItem.frc(parentFolder: folder)
try? frc.performFetch()
guard var objects = frc.fetchedObjects else {
return
}
frc.managedObjectContext.perform {
objects.move(fromOffsets: indexSet, toOffset: offset)

for (order, item) in objects.enumerated().reversed() {
item.order = Int32(order)
}

do {
try frc.managedObjectContext.save()
} catch {
Logger.module.error("\(error.localizedDescription)")
}
}
}

public static func addInMemoryItems(
_ items: [PlaylistInfo],
folderUUID: String,
Expand All @@ -235,7 +261,7 @@ final public class PlaylistItem: NSManagedObject, CRUD, Identifiable {
playlistItem.playlistFolder = folder
})

PlaylistItem.reorderItems(context: context)
PlaylistItem.reorderItems(context: context, folderUUID: folderUUID)
PlaylistItem.saveContext(context)

DispatchQueue.main.async {
Expand Down Expand Up @@ -269,7 +295,7 @@ final public class PlaylistItem: NSManagedObject, CRUD, Identifiable {
playlistItem.playlistFolder = folder
})

PlaylistItem.reorderItems(context: context)
PlaylistItem.reorderItems(context: context, folderUUID: folderUUID)

// Issue #6243 The policy change is added to prevent merge conflicts
// Occasionally saving context will give error
Expand Down Expand Up @@ -451,7 +477,7 @@ final public class PlaylistItem: NSManagedObject, CRUD, Identifiable {
playlistItem.playlistFolder = folder
}

PlaylistItem.reorderItems(context: context)
PlaylistItem.reorderItems(context: context, folderUUID: folderUUID)
PlaylistItem.saveContext(context)

DispatchQueue.main.async {
Expand Down Expand Up @@ -532,12 +558,18 @@ final public class PlaylistItem: NSManagedObject, CRUD, Identifiable {
}

// MARK: - Internal
private static func reorderItems(context: NSManagedObjectContext) {
private static func reorderItems(context: NSManagedObjectContext, folderUUID: String?) {
DataController.perform(context: .existing(context), save: true) { context in
let request = NSFetchRequest<PlaylistItem>()
request.entity = PlaylistItem.entity(context)
request.fetchBatchSize = 20

if let folderUUID = folderUUID {
request.predicate = NSPredicate(format: "playlistFolder.uuid == %@", folderUUID)
} else {
request.predicate = NSPredicate(format: "playlistFolder == nil")
}

let orderSort = NSSortDescriptor(key: "order", ascending: true)
let items = PlaylistItem.all(sortDescriptors: [orderSort], context: context) ?? []

Expand Down
24 changes: 0 additions & 24 deletions ios/brave-ios/Sources/Playlist/PlaylistManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -187,30 +187,6 @@ public class PlaylistManager: NSObject {
frc.fetchedObjects?.firstIndex(where: { $0.uuid == itemId })
}

public func reorderItems(
fromOffsets indexSet: IndexSet,
toOffset offset: Int
) {
guard var objects = frc.fetchedObjects else {
return
}
frc.managedObjectContext.perform { [weak self] in
guard let self = self else { return }

objects.move(fromOffsets: indexSet, toOffset: offset)

for (order, item) in objects.enumerated().reversed() {
item.order = Int32(order)
}

do {
try self.frc.managedObjectContext.save()
} catch {
Logger.module.error("\(error.localizedDescription)")
}
}
}

public func reorderItems(
from sourceIndexPath: IndexPath,
to destinationIndexPath: IndexPath,
Expand Down
2 changes: 1 addition & 1 deletion ios/brave-ios/Sources/PlaylistUI/EditFolderView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ struct EditFolderView: View {
)
}
.onMove { indexSet, offset in
PlaylistManager.shared.reorderItems(fromOffsets: indexSet, toOffset: offset)
PlaylistItem.reorderItems(in: folder, fromOffsets: indexSet, toOffset: offset)
}
.listRowBackground(Color.clear)
.listRowSeparator(.hidden)
Expand Down
Loading