Skip to content
This repository has been archived by the owner on Sep 17, 2022. It is now read-only.

Converted to Swift 3.0 #3

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cartfile.private
Original file line number Diff line number Diff line change
@@ -1 +1 @@
github "Quick/Nimble" ~> 4.1.0
github "Quick/Nimble"
2 changes: 1 addition & 1 deletion Cartfile.resolved
Original file line number Diff line number Diff line change
@@ -1 +1 @@
github "Quick/Nimble" "v4.1.0"
github "Quick/Nimble" "220152be528dcc0537764c179c95b8174028c80c"
2 changes: 1 addition & 1 deletion Carthage/Checkouts/Nimble
Submodule Nimble updated 129 files
2 changes: 1 addition & 1 deletion Mixer.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = "Mixer"
s.version = "0.1.2"
s.version = "0.2"
s.summary = "A tiny library helping to centralize color definitions"
s.homepage = "https://github.com/ios-studio/Mixer"
s.license = { :type => "MIT", :file => "LICENSE" }
Expand Down
10 changes: 6 additions & 4 deletions Mixer.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,7 @@
ONLY_ACTIVE_ARCH = YES;
SDKROOT = iphoneos;
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 3.0;
TARGETED_DEVICE_FAMILY = "1,2";
VERSIONING_SYSTEM = "apple-generic";
VERSION_INFO_PREFIX = "";
Expand Down Expand Up @@ -400,6 +401,7 @@
MTL_ENABLE_DEBUG_INFO = NO;
SDKROOT = iphoneos;
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
SWIFT_VERSION = 3.0;
TARGETED_DEVICE_FAMILY = "1,2";
VALIDATE_PRODUCT = YES;
VERSIONING_SYSTEM = "apple-generic";
Expand All @@ -424,7 +426,7 @@
PRODUCT_NAME = "$(TARGET_NAME)";
SKIP_INSTALL = YES;
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 2.3;
SWIFT_VERSION = 3.0;
};
name = Debug;
};
Expand All @@ -444,7 +446,7 @@
PRODUCT_BUNDLE_IDENTIFIER = studio.Mixer;
PRODUCT_NAME = "$(TARGET_NAME)";
SKIP_INSTALL = YES;
SWIFT_VERSION = 2.3;
SWIFT_VERSION = 3.0;
};
name = Release;
};
Expand All @@ -459,7 +461,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = studio.MixerTests;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 2.3;
SWIFT_VERSION = 3.0;
};
name = Debug;
};
Expand All @@ -474,7 +476,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = studio.MixerTests;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 2.3;
SWIFT_VERSION = 3.0;
};
name = Release;
};
Expand Down
14 changes: 7 additions & 7 deletions Mixer/CSV.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import Foundation
internal class CSV {
let rows: [[String: String]]

private let headers: [String]
private static let delimiter = NSCharacterSet(charactersInString: ",")
fileprivate let headers: [String]
fileprivate static let delimiter = CharacterSet(charactersIn: ",")

init?(contentsOfFile file: String) {
let contents: String
Expand All @@ -16,9 +16,9 @@ internal class CSV {
return nil
}

let newline = NSCharacterSet.newlineCharacterSet()
let newline = CharacterSet.newlines
var lines: [String] = []
contents.stringByTrimmingCharactersInSet(newline).enumerateLines { line, stop in lines.append(line)
contents.trimmingCharacters(in: newline).enumerateLines { line, stop in lines.append(line)
}

guard lines.count > 1 else {
Expand All @@ -27,17 +27,17 @@ internal class CSV {
return nil
}

let headers = lines[0].componentsSeparatedByCharactersInSet(CSV.delimiter)
let headers = lines[0].components(separatedBy: CSV.delimiter)
self.headers = headers
self.rows = Array(lines)[1..<lines.count].reduce([[String: String]]()) { allRows, line in
var newRows = allRows
var row = [String: String]()
for (key, value) in zip(headers, line.componentsSeparatedByCharactersInSet(CSV.delimiter)) {
for (key, value) in zip(headers, line.components(separatedBy: CSV.delimiter)) {
row[key] = value
}

newRows.append(row)
return newRows
}
}
}
}
2 changes: 1 addition & 1 deletion Mixer/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<key>CFBundlePackageType</key>
<string>FMWK</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<string>2.0</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
Expand Down
24 changes: 12 additions & 12 deletions Mixer/Mixer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import UIKit

Where the columns headers are RGBA channel values and the row headers are the names of the colors.
*/
public class Mixer {
open class Mixer {
internal struct Color {
let red: Int
let green: Int
Expand All @@ -26,35 +26,35 @@ public class Mixer {
)
}

private func cgFloatValueOf(value: Int) -> CGFloat {
fileprivate func cgFloatValueOf(_ value: Int) -> CGFloat {
return CGFloat(value) / CGFloat(255.0)
}
}

internal typealias Colors = [String: Color]

private let colors: Colors
fileprivate let colors: Colors

/**
Check if this instance of `Mixer` has colors loaded. Returns `true` if this instance has successfully loaded colors from a file, `false` if not.
*/
public var hasColors: Bool {
open var hasColors: Bool {
return self.colors.count > 0
}

private static var resourcePaths: [NSBundle: String] = [:]
private static func defaultResourcePathForBundle(bundle: NSBundle) -> String? {
fileprivate static var resourcePaths: [Bundle: String] = [:]
fileprivate static func defaultResourcePathForBundle(_ bundle: Bundle) -> String? {
if let path = resourcePaths[bundle] {
return path
} else if let path = bundle.pathForResource("Colors", ofType: "csv") {
} else if let path = bundle.path(forResource: "Colors", ofType: "csv") {
resourcePaths[bundle] = path
return path
}

return nil
}

private let configuration: MixerConfiguration
fileprivate let configuration: MixerConfiguration

/**
Initialize an instance of `Mixer` with a given custom `MixerConfiguration`. Files loaded are cached for the run time of the application so there is no performance penalty in initializing multiple `Mixer` instances.
Expand All @@ -74,7 +74,7 @@ public class Mixer {

- Parameter bundle: The bundle in which to look for the file named `Colors.csv`
*/
public convenience init(bundle: NSBundle) {
public convenience init(bundle: Bundle) {
guard let path = Mixer.defaultResourcePathForBundle(bundle) else {
self.init(configuration: MixerConfiguration(colorDefinitionsPath: "NoPath"))
return
Expand All @@ -88,7 +88,7 @@ public class Mixer {

- Parameter name: The color to return
*/
public func colorFor(name: String) -> UIColor? {
open func colorFor(_ name: String) -> UIColor? {
return colors[name]?.uiColor
}

Expand All @@ -97,8 +97,8 @@ public class Mixer {

- Parameter name: The color to return
*/
public func colorFor(color: MixerColor) -> UIColor? {
open func colorFor(_ color: MixerColor) -> UIColor? {
return colors[color.name]?.uiColor
}

}
}
30 changes: 15 additions & 15 deletions Mixer/MixerColorsLoader.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import UIKit

internal class MixerColorsLoader {
private static var cachedPaths: [String: Mixer.Colors] = [:]
fileprivate static var cachedPaths: [String: Mixer.Colors] = [:]

let path: String

Expand All @@ -10,14 +10,14 @@ internal class MixerColorsLoader {
}

func clear() {
MixerColorsLoader.cachedPaths.removeValueForKey(path)
MixerColorsLoader.cachedPaths.removeValue(forKey: path)
}


func load() -> Mixer.Colors? {
if let cached = MixerColorsLoader.cachedPaths[path] { return cached }

guard let csv = CSV(contentsOfFile: path) where !csv.rows.isEmpty else {
guard let csv = CSV(contentsOfFile: path) , !csv.rows.isEmpty else {
logReadFailure("Could not find or read colors csv")
return nil
}
Expand All @@ -30,14 +30,14 @@ internal class MixerColorsLoader {
return nil
}

private func loadColors(csv: CSV) -> Mixer.Colors? {
fileprivate func loadColors(_ csv: CSV) -> Mixer.Colors? {
var colors = Mixer.Colors()
for row in csv.rows {
guard let name = row["Name"],
red = extractColorValue(row, channel: "Red"),
green = extractColorValue(row, channel: "Green"),
blue = extractColorValue(row, channel: "Blue"),
alpha = extractAlphaValue(row) else {
let red = extractColorValue(row, channel: "Red"),
let green = extractColorValue(row, channel: "Green"),
let blue = extractColorValue(row, channel: "Blue"),
let alpha = extractAlphaValue(row) else {
return nil
}

Expand All @@ -47,7 +47,7 @@ internal class MixerColorsLoader {
return colors
}

private func extractColorValue(row: [String: String], channel: String) -> Int? {
fileprivate func extractColorValue(_ row: [String: String], channel: String) -> Int? {
guard let string = row[channel] else {
logReadFailureForRow(row, message: "Value for \(channel) channel not found")
return nil
Expand All @@ -63,7 +63,7 @@ internal class MixerColorsLoader {
return value
}

private func extractAlphaValue(row: [String: String]) -> Float? {
fileprivate func extractAlphaValue(_ row: [String: String]) -> Float? {
guard let string = row["Alpha"] else {
logReadFailureForRow(row, message: "Value for alpha channel not found")
return nil
Expand All @@ -79,19 +79,19 @@ internal class MixerColorsLoader {
return value
}

private func isValidColorValue(value: Int) -> Bool {
fileprivate func isValidColorValue(_ value: Int) -> Bool {
return (0..<256).contains(value)
}

private func isValidAlphaValue(value: Float) -> Bool {
fileprivate func isValidAlphaValue(_ value: Float) -> Bool {
return (0.0..<1.0).contains(value) || value == 1.0
}

private func logReadFailureForRow(row: [String: String], message: String) {
fileprivate func logReadFailureForRow(_ row: [String: String], message: String) {
NSLog("Mixer: Failure parsing color \"\(row["Name"] ?? "")\" - \(message) - colors will not be loaded")
}

private func logReadFailure(message: String) {
fileprivate func logReadFailure(_ message: String) {
NSLog("Mixer: \(message) - colors will not be loaded")
}
}
}
4 changes: 2 additions & 2 deletions MixerTests/CSVTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class CSVTests: XCTestCase {
expect(parsed).to(beNil())
}

private func csvPath(fileName: String) -> String {
return NSBundle(forClass: self.dynamicType).pathForResource(fileName, ofType: "csv") ?? ""
fileprivate func csvPath(_ fileName: String) -> String {
return Bundle(for: type(of: self)).path(forResource: fileName, ofType: "csv") ?? ""
}
}
20 changes: 10 additions & 10 deletions MixerTests/MixerColorsLoaderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import Nimble

class MixerColorsLoaderTests: XCTestCase {

var fileManager: NSFileManager!
var fileManager: FileManager!

override func setUp() {
fileManager = NSFileManager.defaultManager()
fileManager = FileManager.default
}

override func tearDown() {
Expand All @@ -17,7 +17,7 @@ class MixerColorsLoaderTests: XCTestCase {

func testCache() {
let testPath = copyCSVFixtureToTestPath(csvPath("Colors"))
MixerColorsLoader(path: testPath).load()
_ = MixerColorsLoader(path: testPath).load()
removeCSVFixtureAtPath(testPath)

let cacheLoader = MixerColorsLoader(path: testPath)
Expand All @@ -32,20 +32,20 @@ class MixerColorsLoaderTests: XCTestCase {
expect(deletedResult).to(beNil())
}

private func copyCSVFixtureToTestPath(path: String) -> String {
let newPath = path.stringByReplacingOccurrencesOfString("Colors", withString: "CorrectFontSizes-CacheTest")
fileprivate func copyCSVFixtureToTestPath(_ path: String) -> String {
let newPath = path.replacingOccurrences(of: "Colors", with: "CorrectFontSizes-CacheTest")
do {
try fileManager.copyItemAtPath(path, toPath: newPath)
try fileManager.copyItem(atPath: path, toPath: newPath)
} catch _ {
fail("Could not copy file for cache test")
}

return newPath
}

private func removeCSVFixtureAtPath(path: String) {
fileprivate func removeCSVFixtureAtPath(_ path: String) {
do {
try fileManager.removeItemAtPath(path)
try fileManager.removeItem(atPath: path)
} catch _ {
fail("Could not remove file for cache test")
}
Expand Down Expand Up @@ -111,7 +111,7 @@ class MixerColorsLoaderTests: XCTestCase {
expect(colors).to(beNil())
}

private func csvPath(fileName: String) -> String {
return NSBundle(forClass: self.classForCoder).pathForResource(fileName, ofType: "csv") ?? ""
fileprivate func csvPath(_ fileName: String) -> String {
return Bundle(for: self.classForCoder).path(forResource: fileName, ofType: "csv") ?? ""
}
}
Loading