Skip to content
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
29 changes: 26 additions & 3 deletions CalculatorKeyboard.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@
isa = PBXProject;
attributes = {
LastSwiftUpdateCheck = 0710;
LastUpgradeCheck = 0710;
LastUpgradeCheck = 0900;
ORGANIZATIONNAME = "Reefactor, Inc.";
TargetAttributes = {
45C5244D1C120F4E00510CF7 = {
Expand Down Expand Up @@ -240,13 +240,21 @@
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_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_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;
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
Expand All @@ -269,11 +277,12 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 9.1;
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
MTL_ENABLE_DEBUG_INFO = YES;
ONLY_ACTIVE_ARCH = YES;
SDKROOT = iphoneos;
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 4.0;
TARGETED_DEVICE_FAMILY = "1,2";
VERSIONING_SYSTEM = "apple-generic";
VERSION_INFO_PREFIX = "";
Expand All @@ -288,13 +297,21 @@
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_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_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;
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
Expand All @@ -311,9 +328,11 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 9.1;
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
MTL_ENABLE_DEBUG_INFO = NO;
SDKROOT = iphoneos;
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
SWIFT_VERSION = 4.0;
TARGETED_DEVICE_FAMILY = "1,2";
VALIDATE_PRODUCT = YES;
VERSIONING_SYSTEM = "apple-generic";
Expand All @@ -325,6 +344,7 @@
isa = XCBuildConfiguration;
buildSettings = {
CLANG_ENABLE_MODULES = YES;
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "";
DEFINES_MODULE = YES;
DYLIB_COMPATIBILITY_VERSION = 1;
DYLIB_CURRENT_VERSION = 1;
Expand All @@ -336,13 +356,15 @@
PRODUCT_NAME = "$(TARGET_NAME)";
SKIP_INSTALL = YES;
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 4.0;
};
name = Debug;
};
45C524641C120F4E00510CF7 /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
CLANG_ENABLE_MODULES = YES;
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "";
DEFINES_MODULE = YES;
DYLIB_COMPATIBILITY_VERSION = 1;
DYLIB_CURRENT_VERSION = 1;
Expand All @@ -353,6 +375,7 @@
PRODUCT_BUNDLE_IDENTIFIER = com.reefactor.CalculatorKeyboard;
PRODUCT_NAME = "$(TARGET_NAME)";
SKIP_INSTALL = YES;
SWIFT_VERSION = 4.0;
};
name = Release;
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0710"
LastUpgradeVersion = "0900"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand All @@ -26,6 +26,7 @@
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
language = ""
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
<TestableReference
Expand Down Expand Up @@ -55,6 +56,7 @@
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
language = ""
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
Expand Down
51 changes: 24 additions & 27 deletions CalculatorKeyboard/CalculatorKeyboard.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@
import UIKit

public protocol CalculatorDelegate: class {
func calculator(calculator: CalculatorKeyboard, didChangeValue value: String)
func calculator(_: CalculatorKeyboard, didChangeValue value: String)
}

enum CalculatorKey: Int {
case Zero = 1
case Sign
case Zero
case One
case Two
case Three
Expand All @@ -40,7 +41,7 @@ public class CalculatorKeyboard: UIView {
adjustLayout()
}
}
public var numbersTextColor = UIColor.blackColor() {
public var numbersTextColor = UIColor.black {
didSet {
adjustLayout()
}
Expand All @@ -50,7 +51,7 @@ public class CalculatorKeyboard: UIView {
adjustLayout()
}
}
public var operationsTextColor = UIColor.whiteColor() {
public var operationsTextColor = UIColor.white {
didSet {
adjustLayout()
}
Expand All @@ -60,7 +61,7 @@ public class CalculatorKeyboard: UIView {
adjustLayout()
}
}
public var equalTextColor = UIColor.whiteColor() {
public var equalTextColor = UIColor.white {
didSet {
adjustLayout()
}
Expand Down Expand Up @@ -88,6 +89,10 @@ public class CalculatorKeyboard: UIView {
loadXib()
}

convenience init() {
self.init(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 300))
}

public override func awakeFromNib() {
super.awakeFromNib()
adjustLayout()
Expand All @@ -96,55 +101,47 @@ public class CalculatorKeyboard: UIView {
private func loadXib() {
view = loadViewFromNib()
view.frame = bounds
view.autoresizingMask = [UIViewAutoresizing.FlexibleWidth, UIViewAutoresizing.FlexibleHeight]
view.autoresizingMask = [UIViewAutoresizing.flexibleWidth, UIViewAutoresizing.flexibleHeight]
adjustLayout()
addSubview(view)
}

private func loadViewFromNib() -> UIView {
let bundle = NSBundle(forClass: self.dynamicType)
let bundle = Bundle(for: type(of: self))
let nib = UINib(nibName: "CalculatorKeyboard", bundle: bundle)
let view = nib.instantiateWithOwner(self, options: nil)[0] as! UIView
adjustButtonConstraint()
let view = nib.instantiate(withOwner: self, options: nil)[0] as! UIView
return view
}

private func adjustLayout() {
if viewWithTag(CalculatorKey.Decimal.rawValue) != nil {
adjustButtonConstraint()
}

for var i = 1; i <= CalculatorKey.Decimal.rawValue; i++ {
for i in 1...CalculatorKey.Decimal.rawValue {
if let button = self.view.viewWithTag(i) as? UIButton {
button.tintColor = numbersBackgroundColor
button.setTitleColor(numbersTextColor, forState: .Normal)
button.setTitleColor(numbersTextColor, for: .normal)
}
}

for var i = CalculatorKey.Clear.rawValue; i <= CalculatorKey.Add.rawValue; i++ {
for i in CalculatorKey.Clear.rawValue...CalculatorKey.Add.rawValue {
if let button = self.view.viewWithTag(i) as? UIButton {
button.tintColor = operationsBackgroundColor
button.setTitleColor(operationsTextColor, forState: .Normal)
button.setTitleColor(operationsTextColor, for: .normal)
button.tintColor = operationsTextColor
}
}

if let button = self.view.viewWithTag(CalculatorKey.Equal.rawValue) as? UIButton {
button.tintColor = equalBackgroundColor
button.setTitleColor(equalTextColor, forState: .Normal)
button.setTitleColor(equalTextColor, for: .normal)
}
}

private func adjustButtonConstraint() {
let width = UIScreen.mainScreen().bounds.width / 4.0
zeroDistanceConstraint.constant = showDecimal ? width + 2.0 : 1.0
layoutIfNeeded()
}

@IBAction func buttonPressed(sender: UIButton) {
@IBAction func buttonPressed(_ sender: UIButton) {
switch (sender.tag) {
case CalculatorKey.Sign.rawValue:
let output = processor.toggleSign()
delegate?.calculator(self, didChangeValue: output)
case (CalculatorKey.Zero.rawValue)...(CalculatorKey.Nine.rawValue):
let output = processor.storeOperand(sender.tag-1)
let output = processor.storeOperand(value: sender.tag - 1)
delegate?.calculator(self, didChangeValue: output)
case CalculatorKey.Decimal.rawValue:
let output = processor.addDecimal()
Expand All @@ -156,7 +153,7 @@ public class CalculatorKeyboard: UIView {
let output = processor.deleteLastDigit()
delegate?.calculator(self, didChangeValue: output)
case (CalculatorKey.Multiply.rawValue)...(CalculatorKey.Add.rawValue):
let output = processor.storeOperator(sender.tag)
let output = processor.storeOperator(rawValue: sender.tag)
delegate?.calculator(self, didChangeValue: output)
case CalculatorKey.Equal.rawValue:
let output = processor.computeFinalValue()
Expand Down
Loading