Last active
December 15, 2015 22:27
-
-
Save edwardIshaq/715b0e134fb47d2e28cc to your computer and use it in GitHub Desktop.
Generic protocols to map out the properties of DataSource
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
public protocol Generative { | |
typealias GeneratedType | |
var elements: [GeneratedType] { get } | |
} | |
public extension Generative { | |
func count() -> Int { | |
return elements.count | |
} | |
subscript(index:Int) -> GeneratedType? { | |
if index >= count() { | |
return nil | |
} | |
return elements[index] | |
} | |
} | |
public protocol Selectable { | |
typealias SelectableType: Hashable | |
var selectedElements: Set<SelectableType> { get set } | |
} | |
extension Selectable { | |
public func isSelected(elem: SelectableType) -> Bool { | |
return selectedElements.contains(elem) | |
} | |
public mutating func addSelection(elem: SelectableType) { | |
selectedElements.insert(elem) | |
} | |
public mutating func removeSelection(elem: SelectableType) { | |
selectedElements.remove(elem) | |
} | |
public mutating func clearSelections() { | |
selectedElements.removeAll() | |
} | |
} | |
func selectedIndices<S: Selectable where S: Generative, S.GeneratedType == S.SelectableType>(ds: S) -> [NSIndexPath] { | |
return ds.selectedElements | |
.flatMap { (p: S.GeneratedType) -> NSIndexPath? in | |
if let idx = ds.elements.indexOf(p) { idx | |
return NSIndexPath(forRow: idx, inSection: 0) | |
} | |
return nil | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment