diff --git a/README.md b/README.md index 2db2192..d1ddb28 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,2 @@ -# calculator-swift - +# mrCalculator +Calculator for iOS written in Swift diff --git a/src/ContentView.swift b/src/ContentView.swift new file mode 100644 index 0000000..6ac27e1 --- /dev/null +++ b/src/ContentView.swift @@ -0,0 +1,162 @@ +// +// ContentView.swift +// mrCalculator +// +// Created by mrtuxa on 25.08.22. +// + +import SwiftUI + +enum CalcButton: String { + case one = "1" + case two = "2" + case three = "3" + case four = "4" + case five = "5" + case six = "6" + case seven = "7" + case eight = "8" + case nine = "9" + case zero = "0" + case add = "+" + case subtract = "-" + case divide = "/" + case multiply = "x" + case equal = "=" + case clear = "AC" + case decimal = "." + case percent = "%" + case negative = "-/+" + + var buttonColor: Color { + switch self { + case .add, .subtract, .multiply, .divide, .equal: + return .orange + case .clear, .negative, .percent: + return Color(.lightGray) + default: + return Color(UIColor(red: 55/255.0, green: 55/255.0, blue: 55/255.0, alpha: 1)) + } + } +} + +enum operation { + case add, subtract, multiply, divide, none +} + +struct ContentView: View { + + @State var value = "0" + @State var currentOperation: operation = .none + @State var runningNumber = 0 + + let buttons: [[CalcButton]] = [ + [.clear, .negative, .percent, .divide], + [.seven, .eight, .nine, .multiply], + [.four, .five, .six, .subtract], + [.one, .two, .three, .add], + [.zero, .decimal, .equal] + ] + + var body: some View { + ZStack { + Color.black.edgesIgnoringSafeArea(.all) + + VStack { + Spacer() + + // Text display + HStack { + Spacer() + Text("0") + .bold() + .font(.system(size: 100)) + .foregroundColor(.white) + }.padding() + + // Buttons + ForEach(buttons, id: \.self) { row in + HStack(spacing: 12) { + ForEach(row, id: \.self) { item in + Button(action: { + self.onTapListener(button: item) + }, label: { + Text(item.rawValue) + .font(.system(size: 32)) + .frame(width: self.buttonWidth(item: item), + height: self.buttonHeight()) + .background(item.buttonColor) + .foregroundColor(.white) + .cornerRadius(self.buttonWidth(item: item)/2) + }) + } + }.padding(.bottom, 3) + } + } + } + } + + func onTapListener(button: CalcButton) { + switch button { + case .add, .subtract, .multiply, .divide, .equal: + if button == .add { + self.currentOperation = .add + self.runningNumber += Int(self.value) ?? 0 + } else if button == .subtract { + self.currentOperation = .subtract + self.runningNumber += Int(self.value) ?? 0 + } else if button == .multiply { + self.currentOperation = .multiply + self.runningNumber += Int(self.value) ?? 0 + } else if button == .divide { + self.currentOperation = .divide + self.runningNumber += Int(self.value) ?? 0 + } else if button == .equal { + let runningValue = self.runningNumber + let currentValue = Int(self.value) ?? 0 + switch self.currentOperation { + case .add: self.value = "\(runningValue + currentValue)" + case .subtract: self.value = "\(runningValue - currentValue)" + case .multiply: self.value = "\(runningValue * currentValue)" + case .divide: self.value = "\(runningValue / currentValue)" + case .none: + break + } + } + + if button != .equal { + self.value = "0" + } + + case .clear: + break + case .decimal, .negative, .percent: + break + default: + let number = button.rawValue + if self.value == "0" { + value = number + } else { + self.value = "\(self.value)\(number)" + } + } + } + + func buttonWidth(item: CalcButton) -> CGFloat { + if item == .zero { + return ((UIScreen.main.bounds.width - (4*12)) / 4) * 2 + } + return (UIScreen.main.bounds.width - (5*12)) / 4 + } + + func buttonHeight() -> CGFloat { + return (UIScreen.main.bounds.width - (5*12)) / 4 + } +} + + +struct ContentView_Previews: PreviewProvider { + static var previews: some View { + ContentView() + } +} diff --git a/src/mrCalculatorApp.swift b/src/mrCalculatorApp.swift new file mode 100644 index 0000000..f7d0b63 --- /dev/null +++ b/src/mrCalculatorApp.swift @@ -0,0 +1,17 @@ +// +// mrCalculatorApp.swift +// mrCalculator +// +// Created by mrtuxa on 25.08.22. +// + +import SwiftUI + +@main +struct mrCalculatorApp: App { + var body: some Scene { + WindowGroup { + ContentView() + } + } +}