-
Notifications
You must be signed in to change notification settings - Fork 4
Edit Calculator && Use NaverOpenAPI #11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: hyeongseok
Are you sure you want to change the base?
Changes from 6 commits
9e04d9d
1b8972c
6114815
1b03827
b272cba
7ec500f
8ed199b
22ecceb
09d412b
78f819b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,18 +9,14 @@ import Foundation | |
|
|
||
| struct Calculator { | ||
|
|
||
| // MARK: Alias(es) | ||
|
|
||
| typealias BinaryOperator = CalculatorManager.BinaryOperator | ||
| typealias Digit = CalculatorManager.Digit | ||
|
|
||
| // MARK: Propery(ies) | ||
|
|
||
| private(set) var displayValue = "0" | ||
| private(set) var isAllClear = true | ||
| private var calculationResult: Decimal? = 0 | ||
| private var newValue: Decimal? = nil | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 네이밍 개선하면 가독성을 더 올릴 수 있을 거 같네요. |
||
| private var operation: BinaryOperator? = .add | ||
| private var `operator`: BinaryOperator? = .add | ||
| private var isPreOperatorEqual = false | ||
| private var isCalculationResultIsNil: Bool { | ||
| calculationResult == nil | ||
| } | ||
|
|
@@ -33,44 +29,47 @@ struct Calculator { | |
| self.newValue = Decimal(string: displayValue) | ||
| isAllClear = false | ||
| } else { | ||
| if String(describing: self.newValue ?? 0).count >= 9 { | ||
| let maxLengthOfInputDigit = displayValue.contains(".") ? 10 : 9 | ||
| if displayValue.count >= maxLengthOfInputDigit { | ||
| return | ||
| } | ||
| displayValue = displayValue.appending(String(describing: newValue.rawValue)) | ||
| self.newValue = Decimal(string: displayValue) | ||
| } | ||
| } | ||
|
|
||
| mutating func setOperation(_ newOperation: BinaryOperator) { | ||
| guard let operation = operation, let newValue = newValue else { | ||
| self.operation = newOperation | ||
| return | ||
| mutating func setOperator(_ newOperator: BinaryOperator) { | ||
| if isPreOperatorEqual { | ||
| `operator` = nil | ||
| newValue = nil | ||
| isPreOperatorEqual = false | ||
| } | ||
| calculationResult = calculate(operation, newValue) | ||
| guard let calculationResult = calculationResult else { | ||
| displayValue = "오류" | ||
| return | ||
| } | ||
| displayValue = String(describing: calculationResult) | ||
| proveAndCalculate(newOperator: newOperator) | ||
| self.newValue = nil | ||
| self.operation = newOperation | ||
| self.`operator` = newOperator | ||
| } | ||
|
|
||
| mutating func equal() { | ||
| guard let operation = operation, let newValue = newValue else { | ||
| proveAndCalculate(newOperator: nil) | ||
| } | ||
|
|
||
| private mutating func proveAndCalculate(newOperator: BinaryOperator?) { | ||
| let isEqual = newOperator == nil | ||
| guard let `operator` = `operator`, let newValue = newValue else { | ||
| return | ||
| } | ||
| calculationResult = calculate(operation, newValue) | ||
| if isEqual { | ||
| isPreOperatorEqual = true | ||
| } | ||
| calculationResult = calculate(`operator`, newValue) | ||
| guard let calculationResult = calculationResult else { | ||
| displayValue = "오류" | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "오류"라는 string 값이 많은데, displayValue = errorMessage 이렇게? |
||
| self.newValue = nil | ||
| return | ||
| } | ||
| displayValue = String(describing: calculationResult) | ||
| self.newValue = nil | ||
| self.operation = nil | ||
| } | ||
|
|
||
| mutating func dot() { | ||
| if displayValue.contains(".") || isCalculationResultIsNil { | ||
| return | ||
|
|
@@ -81,36 +80,32 @@ struct Calculator { | |
| } | ||
|
|
||
| mutating func percent() { | ||
| guard let calculationResult = calculationResult else { | ||
| return | ||
| } | ||
| if let newValue = newValue { | ||
| displayValue = String(describing: newValue * 0.01) | ||
| self.newValue = Decimal(string: displayValue) | ||
| } else { | ||
| displayValue = String(describing: calculationResult * 0.01) | ||
| self.calculationResult = Decimal(string: displayValue) | ||
| } | ||
| percentOrToggleSignOfDisplayNumber(isToggle: false) | ||
| } | ||
|
|
||
| mutating func toggleSignOfDisplayNumber() { | ||
| percentOrToggleSignOfDisplayNumber(isToggle: true) | ||
| } | ||
|
|
||
| mutating func toggle() { | ||
| private mutating func percentOrToggleSignOfDisplayNumber(isToggle: Bool) { | ||
| guard let calculationResult = calculationResult else { | ||
| return | ||
| } | ||
| if let newValue = newValue { | ||
| displayValue = String(describing: -newValue) | ||
| let operand = isToggle ? -1 : 0.01 | ||
| if let newValue = newValue, !isPreOperatorEqual { | ||
| displayValue = String(describing: newValue * Decimal(operand)) | ||
| self.newValue = Decimal(string: displayValue) | ||
| } else { | ||
| displayValue = String(describing: -calculationResult) | ||
| displayValue = String(describing: calculationResult * Decimal(operand)) | ||
| self.calculationResult = Decimal(string: displayValue) | ||
| } | ||
| } | ||
|
|
||
| mutating func allClear() { | ||
| displayValue = "0" | ||
| newValue = nil | ||
| clear() | ||
| isPreOperatorEqual = false | ||
| calculationResult = 0 | ||
| operation = .add | ||
| `operator` = .add | ||
| } | ||
|
|
||
| mutating func clear() { | ||
|
|
@@ -120,22 +115,21 @@ struct Calculator { | |
| } | ||
|
|
||
| mutating func undoWhenDragged() { | ||
| if newValue == nil { | ||
| if displayValue.count > 1 { | ||
| displayValue.removeLast() | ||
| calculationResult = Decimal(string: displayValue) | ||
| } else if displayValue.count == 1 { | ||
| displayValue = "0" | ||
| calculationResult = Decimal(string: displayValue) | ||
| } | ||
| let isNewValueNil = newValue == nil | ||
| if displayValue.count == 1 { | ||
| displayValue = "0" | ||
| assignValueWhenUndo(isNewValueNil) | ||
| } else { | ||
| if displayValue.count > 1 { | ||
| displayValue.removeLast() | ||
| newValue = Decimal(string: displayValue) | ||
| } else if displayValue.count == 1 { | ||
| displayValue = "0" | ||
| newValue = Decimal(string: displayValue) | ||
| } | ||
| displayValue.removeLast() | ||
| assignValueWhenUndo(isNewValueNil) | ||
| } | ||
| } | ||
|
|
||
| private mutating func assignValueWhenUndo(_ newValueIsNil: Bool) { | ||
| if newValueIsNil { | ||
| calculationResult = Decimal(string: displayValue) | ||
| } else { | ||
| newValue = Decimal(string: displayValue) | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -150,11 +144,82 @@ struct Calculator { | |
| return calculationResult - newValue | ||
| case .divide: | ||
| if newValue == 0 { | ||
| return nil | ||
| return nil | ||
| } | ||
| return calculationResult / newValue | ||
| case .multiply: | ||
| return calculationResult * newValue | ||
| } | ||
| } | ||
| } | ||
|
|
||
| extension Calculator { | ||
| enum Button: Hashable { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Button ? |
||
| case digit(_ digit: Digit) | ||
| case binaryOperator(_ binaryOperator: BinaryOperator) | ||
| case equal | ||
| case dot | ||
| case percent | ||
| case toggle | ||
| case allClear | ||
| case clear | ||
|
|
||
| var appearance: String { | ||
| switch self { | ||
| case .digit(let digit): | ||
| return digit.appearance | ||
| case .binaryOperator(let binaryOperator): | ||
| return binaryOperator.appearance | ||
| case .equal: | ||
| return "=" | ||
| case .dot: | ||
| return "." | ||
| case .percent: | ||
| return "%" | ||
| case .toggle: | ||
| return "+/-" | ||
| case .allClear: | ||
| return "AC" | ||
| case .clear: | ||
| return "C" | ||
| } | ||
| } | ||
| } | ||
|
|
||
| enum BinaryOperator { | ||
| case add | ||
| case substarct | ||
| case divide | ||
| case multiply | ||
|
|
||
| var appearance: String { | ||
| switch self { | ||
| case .add: | ||
| return "+" | ||
| case .substarct: | ||
| return "-" | ||
| case .divide: | ||
| return "/" | ||
| case .multiply: | ||
| return "*" | ||
| } | ||
| } | ||
| } | ||
|
|
||
| enum Digit: Int { | ||
| case zero | ||
| case one | ||
| case two | ||
| case three | ||
| case four | ||
| case five | ||
| case six | ||
| case seven | ||
| case eight | ||
| case nine | ||
|
|
||
| var appearance: String { | ||
| return String(describing: self.rawValue) | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,36 +7,24 @@ | |
| import SwiftUI | ||
|
|
||
| struct CalculateButtonStyle: ButtonStyle { | ||
| let buttonColor: (Color, Color) | ||
| let buttonColor: (background: Color, foreground: Color) | ||
| let isZero: Bool | ||
|
|
||
| func makeBody(configuration: Self.Configuration) -> some View { | ||
| if isZero { | ||
| configuration.label | ||
| .frame(maxWidth:UIScreen.main.bounds.size.width / 2, maxHeight: UIScreen.main.bounds.size.height / 12, alignment: .leading) | ||
| .font(.title) | ||
| .padding() | ||
| .background(buttonColor.0) | ||
| .foregroundColor(buttonColor.1) | ||
| .overlay { | ||
| if configuration.isPressed { | ||
| Color(white: 1.0, opacity: 0.3) | ||
| } | ||
| } | ||
| .clipShape(Capsule()) | ||
| } else { | ||
| configuration.label | ||
| .frame(maxWidth:UIScreen.main.bounds.size.width / 6.5, maxHeight: UIScreen.main.bounds.size.height / 12, alignment: .center) | ||
| .font(.title) | ||
| .padding() | ||
| .background(buttonColor.0) | ||
| .foregroundColor(buttonColor.1) | ||
| .overlay { | ||
| if configuration.isPressed { | ||
| Color(white: 1.0, opacity: 0.3) | ||
| } | ||
| configuration.label | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 상수는 따로 모아서 처리해주면 좋겠네요 |
||
| .frame( | ||
| maxWidth:UIScreen.main.bounds.size.width / (isZero ? 2.0 : 6.5), | ||
| maxHeight: UIScreen.main.bounds.size.height / 12, | ||
| alignment: isZero ? .leading : .center) | ||
| .font(.title) | ||
| .padding() | ||
| .background(buttonColor.background) | ||
| .foregroundColor(buttonColor.foreground) | ||
| .overlay { | ||
| if configuration.isPressed { | ||
| Color(white: 1.0, opacity: 0.3) | ||
| } | ||
| .clipShape(Circle()) | ||
| } | ||
| } | ||
| .clipShape(Capsule()) | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
코드를 그대로 사용한 것이지만, 문법은 통일해 주세요.
vs