// // Document.swift // Graphic Analysis 2 // // Created by Kilian Hofmann on 15.06.17. // Copyright © 2017 Kilian Hofmann. All rights reserved. // import Cocoa class Document: NSDocument { var year: FileWrapper? var months: [FileWrapper?] = [nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil] @IBOutlet var collectionView: NSCollectionView! override init() { super.init() // Add your subclass-specific initialization here. } override class func autosavesInPlace() -> Bool { return false } override var windowNibName: String? { return "Document" } override func read(from fileWrapper: FileWrapper, ofType typeName: String) throws { year = fileWrapper for entry in fileWrapper.fileWrappers! { guard let month = Int(entry.key) else { continue } months[month-1] = entry.value } //throw NSError(domain: NSOSStatusErrorDomain, code: unimpErr, userInfo: nil) } override func windowControllerDidLoadNib(_ windowController: NSWindowController) { // Setup collection layout let flowLayout = NSCollectionViewFlowLayout() flowLayout.itemSize = NSSize(width: 228, height: 211) flowLayout.sectionInset = EdgeInsets(top: 10, left: 10, bottom: 10, right: 10) flowLayout.minimumInteritemSpacing = 10 flowLayout.minimumLineSpacing = 10 collectionView.collectionViewLayout = flowLayout // Performance collectionView.superview?.wantsLayer = true } override func shouldCloseWindowController(_ windowController: NSWindowController, delegate: Any?, shouldClose shouldCloseSelector: Selector?, contextInfo: UnsafeMutableRawPointer?) { super.shouldCloseWindowController(windowController, delegate: delegate, shouldClose: shouldCloseSelector, contextInfo: contextInfo) } } extension Document : NSCollectionViewDataSource { func collectionView(_ collectionView: NSCollectionView, numberOfItemsInSection section: Int) -> Int { return 12 } func collectionView(_ itemForRepresentedObjectAtcollectionView: NSCollectionView, itemForRepresentedObjectAt indexPath: IndexPath) -> NSCollectionViewItem { let item = collectionView.makeItem(withIdentifier: "CollectionViewItem", for: indexPath) guard let item2 = item as? CollectionViewItem else {return item } item2.month.stringValue = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"][indexPath.item] return item2 } }