small refactoring

This commit is contained in:
John Rommel Estropia
2015-05-07 01:06:17 +09:00
parent eade08d0cd
commit b16c9deeb9
4 changed files with 35 additions and 32 deletions

View File

@@ -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 }

View File

@@ -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(

View File

@@ -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()
}
}

View File

@@ -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))%"
}
}