Skip to content

Commit

Permalink
TableView reorder rows (Drag & Drop)
Browse files Browse the repository at this point in the history
Now sessions can be rearranged by dragging the sessions in the table
  • Loading branch information
AlexPerathoner committed Jan 2, 2021
1 parent 1522306 commit 232efcd
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ class SafariExtensionViewController: SFSafariExtensionViewController, NSControlT
var filteredSessions = [Session]()
// MARK: table
@IBOutlet weak var tableView: NSTableView!
let dragDropType = NSPasteboard.PasteboardType.string

// MARK: searchfield
@IBOutlet weak var searchField: NSSearchField!
var isSearching = false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ extension SafariExtensionViewController: NSTableViewDelegate, NSTableViewDataSou
tableView.delegate = self
tableView.dataSource = self
tableView.menu?.autoenablesItems = true
tableView.registerForDraggedTypes([dragDropType])
}


Expand Down Expand Up @@ -53,4 +54,73 @@ extension SafariExtensionViewController: NSTableViewDelegate, NSTableViewDataSou
@IBAction func tableDoubleClick(_ sender: Any?) {
restoreMenuItem(sender: sender)
}

// MARK: reorder rows

func tableView(_ tableView: NSTableView, pasteboardWriterForRow row: Int) -> NSPasteboardWriting? {
let item = NSPasteboardItem()
item.setString(String(row), forType: dragDropType)
return item
}

func tableView(_ tableView: NSTableView, validateDrop info: NSDraggingInfo, proposedRow row: Int, proposedDropOperation dropOperation: NSTableView.DropOperation) -> NSDragOperation {
if dropOperation == .above {
return .move
}
return []
}

func tableView(_ tableView: NSTableView, acceptDrop info: NSDraggingInfo, row: Int, dropOperation: NSTableView.DropOperation) -> Bool {
let originalRow = Int((info.draggingPasteboard.pasteboardItems?.first!.string(forType: .string))!) ?? -5
if originalRow == -5 { return false }

var newRow = row
// When you drag an item downwards, the "new row" index is actually --1. Remember dragging operation is `.above`.
if originalRow < newRow {
newRow = row - 1
}

// Animate the rows
tableView.beginUpdates()
tableView.moveRow(at: originalRow, to: newRow)
tableView.endUpdates()

// Persist the ordering by saving your data model
let trackItem = sessions[originalRow] //saving item
sessions.remove(at: originalRow) //removing from old position
sessions.insert(trackItem, at: newRow) //inserting at new pos

return true


/*
var oldIndexes = [Int]()
info.enumerateDraggingItems(options: [], for: tableView, classes: [NSPasteboardItem.self], searchOptions: [:]) { (item, i, pointer) in
if let str = (item.item as! NSPasteboardItem).string(forType: self.dragDropType), let index = Int(str) {
oldIndexes.append(index)
}
}

var oldIndexOffset = 0
var newIndexOffset = 0

// For simplicity, the code below uses `tableView.moveRowAtIndex` to move rows around directly.
// You may want to move rows in your content array and then call `tableView.reloadData()` instead.
tableView.beginUpdates()
for oldIndex in oldIndexes {
if oldIndex < row {
tableView.moveRow(at: oldIndex + oldIndexOffset, to: row - 1)
sessions.swapAt(oldIndex + oldIndexOffset, row - 1)
oldIndexOffset -= 1
} else {
tableView.moveRow(at: oldIndex, to: row + newIndexOffset)
sessions.swapAt(oldIndex, row + newIndexOffset)
newIndexOffset += 1
}
}
tableView.endUpdates()
*/
return true
}

}
4 changes: 2 additions & 2 deletions Sessions.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,7 @@
CODE_SIGN_IDENTITY = "Apple Development";
CODE_SIGN_STYLE = Automatic;
DEVELOPMENT_TEAM = XX9N244QQT;
ENABLE_HARDENED_RUNTIME = YES;
ENABLE_HARDENED_RUNTIME = NO;
INFOPLIST_FILE = "Sessions Extension/Others/Info.plist";
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
Expand All @@ -522,7 +522,7 @@
CODE_SIGN_IDENTITY = "Apple Development";
CODE_SIGN_STYLE = Automatic;
DEVELOPMENT_TEAM = XX9N244QQT;
ENABLE_HARDENED_RUNTIME = YES;
ENABLE_HARDENED_RUNTIME = NO;
INFOPLIST_FILE = "Sessions Extension/Others/Info.plist";
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,8 +224,8 @@
endingColumnNumber = "9223372036854775807"
startingLineNumber = "106"
endingLineNumber = "106"
landmarkName = "restoreMenuItem(sender:)"
landmarkType = "7">
landmarkName = "SafariExtensionViewController"
landmarkType = "3">
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
Expand Down Expand Up @@ -256,8 +256,8 @@
endingColumnNumber = "9223372036854775807"
startingLineNumber = "107"
endingLineNumber = "107"
landmarkName = "restoreMenuItem(sender:)"
landmarkType = "7">
landmarkName = "SafariExtensionViewController"
landmarkType = "3">
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
Expand Down

0 comments on commit 232efcd

Please sign in to comment.