mirror of
https://github.com/JohnEstropia/CoreStore.git
synced 2026-01-16 22:16:53 +01:00
small refactoring
This commit is contained in:
@@ -74,8 +74,11 @@ class ObjectListObserverDemoViewController: UITableViewController, ManagedObject
|
||||
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
|
||||
|
||||
let cell = tableView.dequeueReusableCellWithIdentifier("PaletteTableViewCell") as! PaletteTableViewCell
|
||||
|
||||
let palette = paletteList[indexPath]
|
||||
cell.setHue(palette.hue, saturation: palette.saturation, brightness: palette.brightness)
|
||||
cell.colorView?.backgroundColor = palette.color
|
||||
cell.label?.text = palette.colorText
|
||||
|
||||
return cell
|
||||
}
|
||||
|
||||
@@ -132,11 +135,7 @@ class ObjectListObserverDemoViewController: UITableViewController, ManagedObject
|
||||
|
||||
func managedObjectList(listController: ManagedObjectListController<Palette>, didUpdateObject object: Palette, atIndexPath indexPath: NSIndexPath) {
|
||||
|
||||
if let cell = self.tableView.cellForRowAtIndexPath(indexPath) as? PaletteTableViewCell {
|
||||
|
||||
let palette = paletteList[indexPath]
|
||||
cell.setHue(palette.hue, saturation: palette.saturation, brightness: palette.brightness)
|
||||
}
|
||||
self.tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic)
|
||||
}
|
||||
|
||||
func managedObjectList(listController: ManagedObjectListController<Palette>, didMoveObject object: Palette, fromIndexPath: NSIndexPath, toIndexPath: NSIndexPath) {
|
||||
@@ -176,10 +175,7 @@ class ObjectListObserverDemoViewController: UITableViewController, ManagedObject
|
||||
for _ in 0 ... 2 {
|
||||
|
||||
let palette = transaction.create(Palette)
|
||||
palette.hue = Int32(arc4random_uniform(360))
|
||||
palette.saturation = 1.0
|
||||
palette.brightness = 0.5
|
||||
palette.dateAdded = NSDate()
|
||||
palette.setInitialValues()
|
||||
}
|
||||
|
||||
transaction.commit { (result) -> Void in }
|
||||
|
||||
@@ -35,10 +35,7 @@ class ObjectObserverDemoViewController: UIViewController, ManagedObjectObserver
|
||||
HardcoreData.beginSynchronous { (transaction) -> Void in
|
||||
|
||||
let palette = transaction.create(Palette)
|
||||
palette.hue = Int32(arc4random_uniform(360))
|
||||
palette.saturation = 1.0
|
||||
palette.brightness = 0.5
|
||||
palette.dateAdded = NSDate()
|
||||
palette.setInitialValues()
|
||||
|
||||
transaction.commitAndWait()
|
||||
}
|
||||
@@ -156,15 +153,12 @@ class ObjectObserverDemoViewController: UIViewController, ManagedObjectObserver
|
||||
|
||||
func reloadPaletteInfo(palette: Palette) {
|
||||
|
||||
let color = UIColor(
|
||||
hue: CGFloat(palette.hue) / 360.0,
|
||||
saturation: CGFloat(palette.saturation),
|
||||
brightness: CGFloat(palette.brightness),
|
||||
alpha: 1.0
|
||||
)
|
||||
self.colorNameLabel?.textColor = color
|
||||
self.colorNameLabel?.text = palette.colorName
|
||||
|
||||
let color = palette.color
|
||||
self.colorNameLabel?.textColor = color
|
||||
self.colorView?.backgroundColor = color
|
||||
|
||||
self.hsbLabel?.text = "H: \(palette.hue)˚, S: \(round(palette.saturation * 100.0))%, B: \(round(palette.brightness * 100.0))%"
|
||||
|
||||
let dateString = NSDateFormatter.localizedStringFromDate(
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import UIKit
|
||||
import CoreData
|
||||
|
||||
class Palette: NSManagedObject {
|
||||
@@ -55,4 +56,27 @@ class Palette: NSManagedObject {
|
||||
self.didChangeValueForKey(key)
|
||||
}
|
||||
}
|
||||
|
||||
var color: UIColor {
|
||||
|
||||
return UIColor(
|
||||
hue: CGFloat(self.hue) / 360.0,
|
||||
saturation: CGFloat(self.saturation),
|
||||
brightness: CGFloat(self.brightness),
|
||||
alpha: 1.0
|
||||
)
|
||||
}
|
||||
|
||||
var colorText: String {
|
||||
|
||||
return "H: \(self.hue)˚, S: \(round(self.saturation * 100.0))%, B: \(round(self.brightness * 100.0))%"
|
||||
}
|
||||
|
||||
func setInitialValues() {
|
||||
|
||||
self.hue = Int32(arc4random_uniform(360))
|
||||
self.saturation = 1.0
|
||||
self.brightness = 0.5
|
||||
self.dateAdded = NSDate()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,15 +12,4 @@ class PaletteTableViewCell: UITableViewCell {
|
||||
|
||||
@IBOutlet weak var colorView: UIView?
|
||||
@IBOutlet weak var label: UILabel?
|
||||
|
||||
func setHue(hue: Int32, saturation: Float, brightness: Float) {
|
||||
|
||||
let color = UIColor(
|
||||
hue: CGFloat(hue) / 360.0,
|
||||
saturation: CGFloat(saturation),
|
||||
brightness: CGFloat(brightness),
|
||||
alpha: 1.0)
|
||||
self.colorView?.backgroundColor = color
|
||||
self.label?.text = "H: \(hue)˚, S: \(round(saturation * 100.0))%, B: \(round(brightness * 100.0))%"
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user