120 lines
4.5 KiB
Swift
120 lines
4.5 KiB
Swift
//
|
|
// CollectionViewMonth.swift
|
|
// Graphic Analysis 2
|
|
//
|
|
// Created by Kilian Hofmann on 16.06.17.
|
|
// Copyright © 2017 Kilian Hofmann. All rights reserved.
|
|
//
|
|
|
|
import Cocoa
|
|
|
|
class CollectionViewMonth: NSObject {
|
|
var days: [[FileWrapper?]] = []
|
|
var month: Int? = nil
|
|
let monthNames: [String] = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
|
|
let operationQueue: OperationQueue = OperationQueue()
|
|
var doc: Document!
|
|
|
|
override init() {
|
|
super.init()
|
|
operationQueue.maxConcurrentOperationCount = 10
|
|
}
|
|
|
|
init(days: [[FileWrapper?]], document: Document) {
|
|
super.init()
|
|
self.days = days
|
|
doc = document
|
|
}
|
|
}
|
|
|
|
extension CollectionViewMonth: NSCollectionViewDataSource {
|
|
func collectionView(_ collectionView: NSCollectionView, numberOfItemsInSection section: Int) -> Int {
|
|
return 31
|
|
}
|
|
|
|
func collectionView(_ itemForRepresentedObjectAtcollectionView: NSCollectionView, itemForRepresentedObjectAt indexPath: IndexPath) -> NSCollectionViewItem {
|
|
let item = itemForRepresentedObjectAtcollectionView.makeItem(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "CollectionViewItemMonth"), for: indexPath)
|
|
guard let item2 = item as? CollectionViewItemMonth else {
|
|
self.doc.progress.increment(by: 100/31)
|
|
if self.doc.progress.doubleValue > 98 {
|
|
self.doc.progress.isHidden = true
|
|
}
|
|
return item
|
|
}
|
|
if item2.isSelected {
|
|
item2.view.layer?.borderColor = NSColor.gray.cgColor
|
|
} else {
|
|
item2.view.layer?.borderColor = NSColor.clear.cgColor
|
|
item2.graph?.close()
|
|
}
|
|
item2.distribution = []
|
|
let op: OperationQueue = OperationQueue()
|
|
op.maxConcurrentOperationCount = 1
|
|
item2.clearDistribution()
|
|
item2.loss.stringValue = ""
|
|
item2.day.textColor = NSColor.controlShadowColor
|
|
item2.day.stringValue = "\(indexPath.item + 1)"
|
|
guard month != nil else {
|
|
self.doc.progress.increment(by: 100/31)
|
|
if self.doc.progress.doubleValue > 98 {
|
|
self.doc.progress.isHidden = true
|
|
}
|
|
return item2
|
|
}
|
|
if indexPath.item == 0 {
|
|
item2.month.stringValue = monthNames[month!]
|
|
} else {
|
|
item2.month.stringValue = ""
|
|
}
|
|
guard let day = days[month!][indexPath.item] else {
|
|
self.doc.progress.increment(by: 100/31)
|
|
if self.doc.progress.doubleValue > 98 {
|
|
self.doc.progress.isHidden = true
|
|
}
|
|
return item2
|
|
}
|
|
item2.day.textColor = NSColor.black
|
|
operationQueue.addOperation {
|
|
guard let log = day.fileWrappers?["KDLog.hex"] else {
|
|
OperationQueue.main.addOperation {
|
|
self.doc.progress.increment(by: 100/31)
|
|
if self.doc.progress.doubleValue > 98 {
|
|
self.doc.progress.isHidden = true
|
|
}
|
|
}
|
|
return
|
|
}
|
|
/*
|
|
guard var logA = String(data: log.regularFileContents!, encoding: .utf8)?.components(separatedBy: "\n") else { return }
|
|
*/
|
|
guard let logA = FileOperations.load(log: log.regularFileContents! as NSData) else { return }
|
|
item2.distribution = logA
|
|
OperationQueue.main.addOperation {
|
|
item2.loss.stringValue = "\(logA.count)"
|
|
item2.redrawDistribution()
|
|
self.doc.progress.increment(by: 100/31)
|
|
if self.doc.progress.doubleValue > 98 {
|
|
self.doc.progress.isHidden = true
|
|
}
|
|
}
|
|
}
|
|
item2.dayFW = day
|
|
return item2
|
|
}
|
|
}
|
|
|
|
extension CollectionViewMonth : NSCollectionViewDelegate {
|
|
func collectionView(_ collectionView: NSCollectionView, didSelectItemsAt indexPaths: Set<IndexPath>) {
|
|
guard let item = (collectionView.item(at: indexPaths.first!) as? CollectionViewItemMonth) else { return }
|
|
item.view.layer?.borderColor = NSColor.gray.cgColor
|
|
item.graph?.showWindow(self)
|
|
}
|
|
|
|
func collectionView(_ collectionView: NSCollectionView, didDeselectItemsAt indexPaths: Set<IndexPath>) {
|
|
guard let item = (collectionView.item(at: indexPaths.first!) as? CollectionViewItemMonth) else { return }
|
|
item.view.layer?.borderColor = NSColor.clear.cgColor
|
|
item.graph?.close()
|
|
}
|
|
}
|
|
|