Skip to content

Commit

Permalink
Merge pull request #8 from cemolcay/fix/history
Browse files Browse the repository at this point in the history
Add more customisation ability for history
  • Loading branch information
cemolcay authored Feb 14, 2018
2 parents 277210f + 3eee143 commit 96c23ef
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 32 deletions.
8 changes: 4 additions & 4 deletions MIDITimeTableView/Source/MIDITimeTableCellView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,13 @@ open class MIDITimeTableCellView: UIView {
/// View that holds the pan gesture on right most side in the view to use in resizing cell.
private let resizeView = UIView()
/// Inset from the rightmost side on the cell to capture resize gesture.
public var resizePanThreshold: CGFloat = 10
open var resizePanThreshold: CGFloat = 10
/// Delegate that informs about editing cell.
public weak var delegate: MIDITimeTableCellViewDelegate?
open weak var delegate: MIDITimeTableCellViewDelegate?
/// Custom items other than delete, when you long press cell.
public var customMenuItems = [MIDITimeTableCellViewCustomMenuItem]()
open var customMenuItems = [MIDITimeTableCellViewCustomMenuItem]()
/// When cell's position or duration editing, is selected.
public var isSelected: Bool = false
open var isSelected: Bool = false

open override var canBecomeFirstResponder: Bool {
return true
Expand Down
17 changes: 6 additions & 11 deletions MIDITimeTableView/Source/MIDITimeTableHistory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public protocol MIDITimeTableHistoryDelegate: class {
public class MIDITimeTableHistory {
/// Items holding in the history queue.
public private(set) var items = [MIDITimeTableHistoryItem]()
/// Current index of history. Defaults first item, 0.
/// Current index of history. Defaults nothing, -1.
public private(set) var currentIndex = -1
/// Limit of the history items. Defaults 10.
public var limit: Int { didSet{ limitDidChange() }}
Expand Down Expand Up @@ -84,15 +84,10 @@ public class MIDITimeTableHistory {
///
/// - Parameter item: Item to add to history.
public func append(item: MIDITimeTableHistoryItem) {
// If we hit the limit, remove first item.
if items.count >= limit {
items.remove(at: 0)
}
// Append item to history.
currentIndex += 1
if currentIndex >= limit {
currentIndex = limit - 1
}
items.insert(item, at: currentIndex)
var newHistory = items.enumerated().filter({ $0.offset <= currentIndex }).map({ $0.element })
newHistory.append(item)
newHistory = Array(newHistory.suffix(limit))
currentIndex = newHistory.count - 1
items = newHistory
}
}
6 changes: 5 additions & 1 deletion MIDITimeTableView/Source/MIDITimeTableRowData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ public struct MIDITimeTableRowData {
public var headerCellView: MIDITimeTableHeaderCellView
/// View of each cell in the row.
public var cellView: (MIDITimeTableCellData) -> MIDITimeTableCellView
/// Other data for your custom objects. It is useful when moving history related custom data back and forth.
public var customData: Any?

/// Calculates the duration of cells in the row.
public var duration: Double {
Expand All @@ -98,9 +100,11 @@ public struct MIDITimeTableRowData {
/// - cells: Data of the cells.
/// - headerCellView: Row header cell view reference.
/// - cellView: Each view of cell data in row.
public init(cells: [MIDITimeTableCellData], headerCellView: MIDITimeTableHeaderCellView, cellView: @escaping (MIDITimeTableCellData) -> MIDITimeTableCellView) {
/// - customData: Other data for your custom objects. It is useful when moving history related custom data back and forth.
public init(cells: [MIDITimeTableCellData], headerCellView: MIDITimeTableHeaderCellView, cellView: @escaping (MIDITimeTableCellData) -> MIDITimeTableCellView, customData: Any? = nil) {
self.cells = cells
self.headerCellView = headerCellView
self.cellView = cellView
self.customData = customData
}
}
5 changes: 3 additions & 2 deletions MIDITimeTableView/Source/MIDITimeTableView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -292,8 +292,9 @@ open class MIDITimeTableView: UIScrollView, MIDITimeTableCellViewDelegate, MIDIT

/// Populates row and cell datas from its data source and redraws time table. Could be invoked with an history item.
///
/// - Parameter keepHistory: If you specify the history writing even if it is enabled, you can control it from here either.
/// - Parameter historyItem: Optional history item. Defaults nil.
public func reloadData(historyItem: MIDITimeTableHistoryItem? = nil) {
public func reloadData(keepHistory: Bool = true, historyItem: MIDITimeTableHistoryItem? = nil) {
// Reset data source
rowHeaderCellViews.forEach({ $0.removeFromSuperview() })
rowHeaderCellViews = []
Expand Down Expand Up @@ -333,7 +334,7 @@ open class MIDITimeTableView: UIScrollView, MIDITimeTableCellViewDelegate, MIDIT
gridLayer.setNeedsLayout()

// Keep history
if holdsHistory, historyItem == nil {
if holdsHistory, keepHistory, historyItem == nil {
history.append(item: rowData)
}
}
Expand Down
29 changes: 15 additions & 14 deletions MIDITimeTableView/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -179,12 +179,12 @@ class ViewController: UIViewController, MIDITimeTableViewDataSource, MIDITimeTab

override func viewDidLoad() {
super.viewDidLoad()
updateHistoryButtons()

timeTableView?.dataSource = self
timeTableView?.timeTableDelegate = self
timeTableView?.gridLayer.showsSubbeatLines = false
timeTableView?.reloadData()
updateHistoryButtons()

timeTableView?.backgroundColor = UIColor(red: 18.0/255.0, green: 20.0/255.0, blue: 19.0/255.0, alpha: 1)
timeTableView?.measureView.backgroundColor = UIColor(red: 26.0/255.0, green: 28.0/255.0, blue: 27.0/255.0, alpha: 1)
Expand Down Expand Up @@ -240,20 +240,21 @@ class ViewController: UIViewController, MIDITimeTableViewDataSource, MIDITimeTab
}

func midiTimeTableView(_ midiTimeTableView: MIDITimeTableView, didDelete cells: [MIDITimeTableCellIndex]) {
var deletingIndices = [Int: [Int]]() // [rowIndex: [colIndex]]
for cell in cells {
if deletingIndices[cell.row] == nil {
deletingIndices[cell.row] = [cell.index]
} else {
deletingIndices[cell.row]?.append(cell.index)
deletingIndices[cell.row]?.sort()
}
}

for (row, col) in deletingIndices {
rowData[row].cells = rowData[row].cells.enumerated().filter({ !col.contains($0.offset) }).map({ $0.element })
}
// var deletingIndices = [Int: [Int]]() // [rowIndex: [colIndex]]
// for cell in cells {
// if deletingIndices[cell.row] == nil {
// deletingIndices[cell.row] = [cell.index]
// } else {
// deletingIndices[cell.row]?.append(cell.index)
// deletingIndices[cell.row]?.sort()
// }
// }
//
// for (row, col) in deletingIndices {
// rowData[row].cells = rowData[row].cells.enumerated().filter({ !col.contains($0.offset) }).map({ $0.element })
// }

rowData.removeCells(at: cells)
timeTableView?.reloadData()
updateHistoryButtons()
}
Expand Down

0 comments on commit 96c23ef

Please sign in to comment.