Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Coledunsby committed Jul 19, 2018
1 parent 0e3e4b7 commit ba8df4b
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 94 deletions.
39 changes: 20 additions & 19 deletions CDCodabarEncoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,14 @@ import Foundation
*********************************************************************/

public struct CDCodabarEncoder {
struct CDCodabarEncoder {

private struct Constants {
private enum Constants {

static let minLength = 3
static let maxLength = 16
enum Length {
static let min = 3
static let max = 16
}

static let encodings: [Character: [Int]] = [
"0": [1, 0, 1, 0, 1, 0, 0, 1, 1],
Expand All @@ -75,32 +77,31 @@ public struct CDCodabarEncoder {
}

enum Error: Swift.Error {

case invalidLength
case invalidStartCharacter
case invalidEndCharacter
case invalidIntermediateCharacter
case length
case startCharacter
case endCharacter
case intermediateCharacter
}

private let code: String

public init(code: String) throws {
let code = code.uppercased()

guard (Constants.minLength ... Constants.maxLength) ~= code.characters.count else {
throw Error.invalidLength
guard (Constants.Length.min ... Constants.Length.max) ~= code.count else {
throw Error.length
}

guard let startChar = code.characters.first, ("A" ... "D") ~= startChar else {
throw Error.invalidStartCharacter
guard let startChar = code.first, ("A" ... "D") ~= startChar else {
throw Error.startCharacter
}

guard let stopChar = code.characters.last, ("A" ... "D") ~= stopChar else {
throw Error.invalidEndCharacter
guard let stopChar = code.last, ("A" ... "D") ~= stopChar else {
throw Error.endCharacter
}

guard code.characters.filter({ !Constants.encodings.keys.contains($0) }).isEmpty else {
throw Error.invalidIntermediateCharacter
guard code.filter({ !Constants.encodings.keys.contains($0) }).isEmpty else {
throw Error.intermediateCharacter
}

self.code = code
Expand All @@ -111,9 +112,9 @@ public struct CDCodabarEncoder {
///
/// - Returns: Returns array of integer representing bits
public func sequence() -> [Int] {
return code.characters
return code
.map { Constants.encodings[$0]! }
.joined(separator: [0])
.flatMap { $0 }
.compactMap { $0 }
}
}
2 changes: 1 addition & 1 deletion CDCodabarView.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = "CDCodabarView"
s.version = "1.0.0"
s.version = "1.1.0"
s.summary = "Codabar Barcode Generator for iOS."
s.homepage = "https://github.com/Coledunsby/CDCodabarView"
s.authors = { "Cole Dunsby" => "[email protected]" }
Expand Down
27 changes: 17 additions & 10 deletions CDCodabarView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,15 @@
import UIKit

@IBDesignable
public final class CDCodabarView: UIView {
open class CDCodabarView: UIView {

// MARK: - Inspectable Properties

@IBInspectable public var barColor: UIColor = #colorLiteral(red: 0, green: 0, blue: 0, alpha: 1) { didSet { setNeedsDisplay() }}
@IBInspectable public var textColor: UIColor = #colorLiteral(red: 0, green: 0, blue: 0, alpha: 1) { didSet { setNeedsDisplay() }}
@IBInspectable public var padding: CGFloat = 2.0 { didSet { setNeedsDisplay() }}
@IBInspectable public var hideCode: Bool = false { didSet { setNeedsDisplay() }}
@IBInspectable public var invalidText: String = "Invalid Code" { didSet { setNeedsDisplay() }}

@IBInspectable public var code: String = "A123456789B" {
didSet {
Expand All @@ -23,10 +26,16 @@ public final class CDCodabarView: UIView {
}
}

// MARK: - Public Properties

public var font = UIFont.systemFont(ofSize: 15.0) { didSet { setNeedsDisplay() }}

// MARK: - Private Properties

private var encoder: CDCodabarEncoder?

// MARK: - Lifecycle

override init(frame: CGRect) {
super.init(frame: frame)
}
Expand All @@ -35,27 +44,25 @@ public final class CDCodabarView: UIView {
super.init(coder: aDecoder)
}

override public func draw(_ rect: CGRect) {
override open func draw(_ rect: CGRect) {
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = .center

let attributes: [NSAttributedStringKey: Any] = [
NSAttributedStringKey.font: font,
NSAttributedStringKey.foregroundColor: textColor,
NSAttributedStringKey.paragraphStyle: paragraphStyle,
.font: font,
.foregroundColor: textColor,
.paragraphStyle: paragraphStyle
]

guard let encoder = encoder else {
let text = "Invalid Code"

let textSize = text.boundingRect(
let textSize = invalidText.boundingRect(
with: CGSize(width: bounds.size.width, height: CGFloat.greatestFiniteMagnitude),
options: [.truncatesLastVisibleLine, .usesLineFragmentOrigin],
attributes: [NSAttributedStringKey.font: font],
attributes: [.font: font],
context: nil
)

text.draw(
invalidText.draw(
at: CGPoint(x: bounds.size.width / 2 - textSize.width / 2, y: bounds.size.height / 2 - textSize.height / 2),
withAttributes: attributes
)
Expand Down
62 changes: 40 additions & 22 deletions CDCodabarViewExample.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

/* Begin PBXBuildFile section */
2092DBCB1E9BC13000B735A5 /* CDCodabarEncoder.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2092DBCA1E9BC13000B735A5 /* CDCodabarEncoder.swift */; };
2092DBD31E9BC1B300B735A5 /* EncoderSpecs.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2092DBD21E9BC1B300B735A5 /* EncoderSpecs.swift */; };
2092DBD31E9BC1B300B735A5 /* CDCodabarViewTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2092DBD21E9BC1B300B735A5 /* CDCodabarViewTests.swift */; };
252518E51DD3BB9B00F770A3 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 252518DF1DD3BB9B00F770A3 /* AppDelegate.swift */; };
252518E61DD3BB9B00F770A3 /* Images.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 252518E01DD3BB9B00F770A3 /* Images.xcassets */; };
252518E81DD3BB9B00F770A3 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 252518E21DD3BB9B00F770A3 /* LaunchScreen.storyboard */; };
Expand All @@ -30,7 +30,7 @@
/* Begin PBXFileReference section */
2092DBCA1E9BC13000B735A5 /* CDCodabarEncoder.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CDCodabarEncoder.swift; sourceTree = "<group>"; };
2092DBD01E9BC1B300B735A5 /* CDCodabarViewTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = CDCodabarViewTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
2092DBD21E9BC1B300B735A5 /* EncoderSpecs.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EncoderSpecs.swift; sourceTree = "<group>"; };
2092DBD21E9BC1B300B735A5 /* CDCodabarViewTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CDCodabarViewTests.swift; sourceTree = "<group>"; };
2092DBD41E9BC1B300B735A5 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
252518DF1DD3BB9B00F770A3 /* AppDelegate.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
252518E01DD3BB9B00F770A3 /* Images.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Images.xcassets; sourceTree = "<group>"; };
Expand All @@ -39,7 +39,8 @@
252518E31DD3BB9B00F770A3 /* Main.storyboard */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.storyboard; path = Main.storyboard; sourceTree = "<group>"; };
252518E41DD3BB9B00F770A3 /* ViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = "<group>"; };
252518ED1DD3BDDB00F770A3 /* CDCodabarView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CDCodabarView.swift; sourceTree = "<group>"; };
607FACD01AFB9204008FA782 /* CDCodabarView_Example.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = CDCodabarView_Example.app; sourceTree = BUILT_PRODUCTS_DIR; };
607FACD01AFB9204008FA782 /* CDCodabarViewExample.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = CDCodabarViewExample.app; sourceTree = BUILT_PRODUCTS_DIR; };
EDEF7BA721014883002D4DCB /* CDCodabarView.podspec */ = {isa = PBXFileReference; explicitFileType = text.script.ruby; path = CDCodabarView.podspec; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand All @@ -63,7 +64,7 @@
2092DBD11E9BC1B300B735A5 /* CDCodabarViewTests */ = {
isa = PBXGroup;
children = (
2092DBD21E9BC1B300B735A5 /* EncoderSpecs.swift */,
2092DBD21E9BC1B300B735A5 /* CDCodabarViewTests.swift */,
2092DBD41E9BC1B300B735A5 /* Info.plist */,
);
path = CDCodabarViewTests;
Expand All @@ -85,6 +86,7 @@
607FACC71AFB9204008FA782 = {
isa = PBXGroup;
children = (
EDEF7BA721014883002D4DCB /* CDCodabarView.podspec */,
252518ED1DD3BDDB00F770A3 /* CDCodabarView.swift */,
2092DBCA1E9BC13000B735A5 /* CDCodabarEncoder.swift */,
252518DE1DD3BB9B00F770A3 /* CDCodabarViewExample */,
Expand All @@ -96,7 +98,7 @@
607FACD11AFB9204008FA782 /* Products */ = {
isa = PBXGroup;
children = (
607FACD01AFB9204008FA782 /* CDCodabarView_Example.app */,
607FACD01AFB9204008FA782 /* CDCodabarViewExample.app */,
2092DBD01E9BC1B300B735A5 /* CDCodabarViewTests.xctest */,
);
name = Products;
Expand All @@ -123,9 +125,9 @@
productReference = 2092DBD01E9BC1B300B735A5 /* CDCodabarViewTests.xctest */;
productType = "com.apple.product-type.bundle.unit-test";
};
607FACCF1AFB9204008FA782 /* CDCodabarView_Example */ = {
607FACCF1AFB9204008FA782 /* CDCodabarViewExample */ = {
isa = PBXNativeTarget;
buildConfigurationList = 607FACEF1AFB9204008FA782 /* Build configuration list for PBXNativeTarget "CDCodabarView_Example" */;
buildConfigurationList = 607FACEF1AFB9204008FA782 /* Build configuration list for PBXNativeTarget "CDCodabarViewExample" */;
buildPhases = (
607FACCC1AFB9204008FA782 /* Sources */,
607FACCD1AFB9204008FA782 /* Frameworks */,
Expand All @@ -135,9 +137,9 @@
);
dependencies = (
);
name = CDCodabarView_Example;
name = CDCodabarViewExample;
productName = CDCodabarView;
productReference = 607FACD01AFB9204008FA782 /* CDCodabarView_Example.app */;
productReference = 607FACD01AFB9204008FA782 /* CDCodabarViewExample.app */;
productType = "com.apple.product-type.application";
};
/* End PBXNativeTarget section */
Expand All @@ -147,19 +149,19 @@
isa = PBXProject;
attributes = {
LastSwiftUpdateCheck = 0830;
LastUpgradeCheck = 0810;
LastUpgradeCheck = 1000;
ORGANIZATIONNAME = CocoaPods;
TargetAttributes = {
2092DBCF1E9BC1B300B735A5 = {
CreatedOnToolsVersion = 8.3;
DevelopmentTeam = B3YTJ78KXJ;
DevelopmentTeam = W8W9HUZP5Z;
LastSwiftMigration = 0940;
ProvisioningStyle = Automatic;
TestTargetID = 607FACCF1AFB9204008FA782;
};
607FACCF1AFB9204008FA782 = {
CreatedOnToolsVersion = 6.3.1;
DevelopmentTeam = JU6JS2KXPW;
DevelopmentTeam = W8W9HUZP5Z;
LastSwiftMigration = 0940;
};
};
Expand All @@ -177,7 +179,7 @@
projectDirPath = "";
projectRoot = "";
targets = (
607FACCF1AFB9204008FA782 /* CDCodabarView_Example */,
607FACCF1AFB9204008FA782 /* CDCodabarViewExample */,
2092DBCF1E9BC1B300B735A5 /* CDCodabarViewTests */,
);
};
Expand Down Expand Up @@ -208,7 +210,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
2092DBD31E9BC1B300B735A5 /* EncoderSpecs.swift in Sources */,
2092DBD31E9BC1B300B735A5 /* CDCodabarViewTests.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand All @@ -228,7 +230,7 @@
/* Begin PBXTargetDependency section */
2092DBD61E9BC1B300B735A5 /* PBXTargetDependency */ = {
isa = PBXTargetDependency;
target = 607FACCF1AFB9204008FA782 /* CDCodabarView_Example */;
target = 607FACCF1AFB9204008FA782 /* CDCodabarViewExample */;
targetProxy = 2092DBD51E9BC1B300B735A5 /* PBXContainerItemProxy */;
};
/* End PBXTargetDependency section */
Expand All @@ -242,7 +244,7 @@
CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE;
CLANG_WARN_DOCUMENTATION_COMMENTS = YES;
DEBUG_INFORMATION_FORMAT = dwarf;
DEVELOPMENT_TEAM = B3YTJ78KXJ;
DEVELOPMENT_TEAM = W8W9HUZP5Z;
INFOPLIST_FILE = CDCodabarViewTests/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 10.3;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
Expand All @@ -251,7 +253,7 @@
SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG;
SWIFT_SWIFT3_OBJC_INFERENCE = Default;
SWIFT_VERSION = 4.0;
TEST_HOST = "$(BUILT_PRODUCTS_DIR)/CDCodabarView_Example.app/CDCodabarView_Example";
TEST_HOST = "$(BUILT_PRODUCTS_DIR)/CDCodabarViewExample.app/CDCodabarViewExample";
};
name = Debug;
};
Expand All @@ -262,15 +264,15 @@
CLANG_ANALYZER_NONNULL = YES;
CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE;
CLANG_WARN_DOCUMENTATION_COMMENTS = YES;
DEVELOPMENT_TEAM = B3YTJ78KXJ;
DEVELOPMENT_TEAM = W8W9HUZP5Z;
INFOPLIST_FILE = CDCodabarViewTests/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 10.3;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = com.appunite.CDCodabarViewTests;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_SWIFT3_OBJC_INFERENCE = Default;
SWIFT_VERSION = 4.0;
TEST_HOST = "$(BUILT_PRODUCTS_DIR)/CDCodabarView_Example.app/CDCodabarView_Example";
TEST_HOST = "$(BUILT_PRODUCTS_DIR)/CDCodabarViewExample.app/CDCodabarViewExample";
};
name = Release;
};
Expand All @@ -282,14 +284,22 @@
CLANG_CXX_LIBRARY = "libc++";
CLANG_ENABLE_MODULES = YES;
CLANG_ENABLE_OBJC_ARC = YES;
CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES;
CLANG_WARN_BOOL_CONVERSION = YES;
CLANG_WARN_COMMA = YES;
CLANG_WARN_CONSTANT_CONVERSION = YES;
CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES;
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES;
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
CLANG_WARN_STRICT_PROTOTYPES = YES;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
Expand Down Expand Up @@ -329,14 +339,22 @@
CLANG_CXX_LIBRARY = "libc++";
CLANG_ENABLE_MODULES = YES;
CLANG_ENABLE_OBJC_ARC = YES;
CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES;
CLANG_WARN_BOOL_CONVERSION = YES;
CLANG_WARN_COMMA = YES;
CLANG_WARN_CONSTANT_CONVERSION = YES;
CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES;
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES;
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
CLANG_WARN_STRICT_PROTOTYPES = YES;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
Expand Down Expand Up @@ -365,7 +383,7 @@
isa = XCBuildConfiguration;
buildSettings = {
CLANG_ENABLE_MODULES = YES;
DEVELOPMENT_TEAM = JU6JS2KXPW;
DEVELOPMENT_TEAM = W8W9HUZP5Z;
INFOPLIST_FILE = "$(SRCROOT)/CDCodabarViewExample/Info.plist";
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
Expand All @@ -382,7 +400,7 @@
isa = XCBuildConfiguration;
buildSettings = {
CLANG_ENABLE_MODULES = YES;
DEVELOPMENT_TEAM = JU6JS2KXPW;
DEVELOPMENT_TEAM = W8W9HUZP5Z;
INFOPLIST_FILE = "$(SRCROOT)/CDCodabarViewExample/Info.plist";
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
Expand Down Expand Up @@ -415,7 +433,7 @@
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
607FACEF1AFB9204008FA782 /* Build configuration list for PBXNativeTarget "CDCodabarView_Example" */ = {
607FACEF1AFB9204008FA782 /* Build configuration list for PBXNativeTarget "CDCodabarViewExample" */ = {
isa = XCConfigurationList;
buildConfigurations = (
607FACF01AFB9204008FA782 /* Debug */,
Expand Down
Loading

0 comments on commit ba8df4b

Please sign in to comment.