master
mrtuxa 2022-11-24 01:27:29 +01:00
parent dad4631a4d
commit 5d47085f93
3 changed files with 181 additions and 2 deletions

View File

@ -1,2 +1,2 @@
# calculator-swift
# mrCalculator
Calculator for iOS written in Swift

162
src/ContentView.swift Normal file
View File

@ -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()
}
}

17
src/mrCalculatorApp.swift Normal file
View File

@ -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()
}
}
}