Finish PoC calculator
This commit is contained in:
parent
5901b5f4c9
commit
17020fcab4
|
@ -0,0 +1,8 @@
|
||||||
|
enum Operand {
|
||||||
|
case add
|
||||||
|
case sub
|
||||||
|
case mul
|
||||||
|
case div
|
||||||
|
case pow
|
||||||
|
case sqrt
|
||||||
|
}
|
|
@ -7,85 +7,125 @@ import CMath
|
||||||
@main
|
@main
|
||||||
struct PMCalc: App {
|
struct PMCalc: App {
|
||||||
let app = AdwaitaApp(id: "me.thealgorithm476.PMCalc.PMCalc")
|
let app = AdwaitaApp(id: "me.thealgorithm476.PMCalc.PMCalc")
|
||||||
|
@State
|
||||||
|
var vm = ViewModel()
|
||||||
|
|
||||||
var scene: Scene {
|
var scene: Scene {
|
||||||
Window(id: "main") { window in
|
Window(id: "main") { window in
|
||||||
VStack(spacing: 8) {
|
VStack(spacing: 8) {
|
||||||
Text("Calculations here...")
|
Text(vm.display)
|
||||||
.selectable(false)
|
.selectable(false)
|
||||||
|
.title2()
|
||||||
|
.halign(.end)
|
||||||
VStack(spacing: 8) {
|
VStack(spacing: 8) {
|
||||||
HStack(spacing: 8) {
|
HStack(spacing: 8) {
|
||||||
Button("AC") {}
|
Button("AC") {
|
||||||
|
vm.clear()
|
||||||
|
}
|
||||||
.keyboardShortcut("c", app: app)
|
.keyboardShortcut("c", app: app)
|
||||||
.hexpand()
|
.hexpand()
|
||||||
.destructive()
|
.destructive()
|
||||||
Button("") {}
|
Button("^") {
|
||||||
|
vm.operation(.pow)
|
||||||
|
}
|
||||||
.hexpand()
|
.hexpand()
|
||||||
.flat()
|
|
||||||
Button("%") {}
|
Button("%") {}
|
||||||
.keyboardShortcut("percent", app: app)
|
.keyboardShortcut("percent", app: app)
|
||||||
.hexpand()
|
.hexpand()
|
||||||
Button("/") {}
|
Button("/") {
|
||||||
|
vm.operation(.div)
|
||||||
|
}
|
||||||
.keyboardShortcut("slash", app: app)
|
.keyboardShortcut("slash", app: app)
|
||||||
.hexpand()
|
.hexpand()
|
||||||
}
|
}
|
||||||
.vexpand()
|
.vexpand()
|
||||||
HStack(spacing: 8) {
|
HStack(spacing: 8) {
|
||||||
Button("7") {}
|
Button("7") {
|
||||||
|
vm.addNumber(7)
|
||||||
|
}
|
||||||
.keyboardShortcut("7", app: app)
|
.keyboardShortcut("7", app: app)
|
||||||
.hexpand()
|
.hexpand()
|
||||||
Button("8") {}
|
Button("8") {
|
||||||
|
vm.addNumber(8)
|
||||||
|
}
|
||||||
.keyboardShortcut("8", app: app)
|
.keyboardShortcut("8", app: app)
|
||||||
.hexpand()
|
.hexpand()
|
||||||
Button("9") {}
|
Button("9") {
|
||||||
|
vm.addNumber(9)
|
||||||
|
}
|
||||||
.keyboardShortcut("9", app: app)
|
.keyboardShortcut("9", app: app)
|
||||||
.hexpand()
|
.hexpand()
|
||||||
Button("*") {}
|
Button("*") {
|
||||||
|
vm.operation(.mul)
|
||||||
|
}
|
||||||
.keyboardShortcut("asterisk", app: app)
|
.keyboardShortcut("asterisk", app: app)
|
||||||
.hexpand()
|
.hexpand()
|
||||||
}
|
}
|
||||||
.vexpand()
|
.vexpand()
|
||||||
HStack(spacing: 8) {
|
HStack(spacing: 8) {
|
||||||
Button("4") {}
|
Button("4") {
|
||||||
|
vm.addNumber(4)
|
||||||
|
}
|
||||||
.keyboardShortcut("4", app: app)
|
.keyboardShortcut("4", app: app)
|
||||||
.hexpand()
|
.hexpand()
|
||||||
Button("5") {}
|
Button("5") {
|
||||||
|
vm.addNumber(5)
|
||||||
|
}
|
||||||
.keyboardShortcut("5", app: app)
|
.keyboardShortcut("5", app: app)
|
||||||
.hexpand()
|
.hexpand()
|
||||||
Button("6") {}
|
Button("6") {
|
||||||
|
vm.addNumber(6)
|
||||||
|
}
|
||||||
.keyboardShortcut("6", app: app)
|
.keyboardShortcut("6", app: app)
|
||||||
.hexpand()
|
.hexpand()
|
||||||
Button("-") {}
|
Button("-") {
|
||||||
|
vm.operation(.sub)
|
||||||
|
}
|
||||||
.keyboardShortcut("minus", app: app)
|
.keyboardShortcut("minus", app: app)
|
||||||
.hexpand()
|
.hexpand()
|
||||||
}
|
}
|
||||||
.vexpand()
|
.vexpand()
|
||||||
HStack(spacing: 8) {
|
HStack(spacing: 8) {
|
||||||
Button("1") {}
|
Button("1") {
|
||||||
|
vm.addNumber(1)
|
||||||
|
}
|
||||||
.keyboardShortcut("1", app: app)
|
.keyboardShortcut("1", app: app)
|
||||||
.hexpand()
|
.hexpand()
|
||||||
Button("2") {}
|
Button("2") {
|
||||||
|
vm.addNumber(2)
|
||||||
|
}
|
||||||
.keyboardShortcut("2", app: app)
|
.keyboardShortcut("2", app: app)
|
||||||
.hexpand()
|
.hexpand()
|
||||||
Button("3") {}
|
Button("3") {
|
||||||
|
vm.addNumber(3)
|
||||||
|
}
|
||||||
.keyboardShortcut("3", app: app)
|
.keyboardShortcut("3", app: app)
|
||||||
.hexpand()
|
.hexpand()
|
||||||
Button("+") {}
|
Button("+") {
|
||||||
|
vm.operation(.add)
|
||||||
|
}
|
||||||
.keyboardShortcut("plus", app: app)
|
.keyboardShortcut("plus", app: app)
|
||||||
.hexpand()
|
.hexpand()
|
||||||
}
|
}
|
||||||
.vexpand()
|
.vexpand()
|
||||||
HStack(spacing: 8) {
|
HStack(spacing: 8) {
|
||||||
Button("0") {}
|
Button("0") {
|
||||||
|
vm.addNumber(0)
|
||||||
|
}
|
||||||
.keyboardShortcut("0", app: app)
|
.keyboardShortcut("0", app: app)
|
||||||
.hexpand()
|
.hexpand()
|
||||||
Button("") {}
|
Button("sq") {
|
||||||
|
vm.operation(.sqrt)
|
||||||
|
}
|
||||||
.hexpand()
|
.hexpand()
|
||||||
.flat()
|
Button(",") {
|
||||||
Button(",") {}
|
vm.addComma()
|
||||||
|
}
|
||||||
.keyboardShortcut("comma", app: app)
|
.keyboardShortcut("comma", app: app)
|
||||||
.hexpand()
|
.hexpand()
|
||||||
Button("=") {}
|
Button("=") {
|
||||||
|
vm.calculate()
|
||||||
|
}
|
||||||
.keyboardShortcut("equal", app: app)
|
.keyboardShortcut("equal", app: app)
|
||||||
.hexpand()
|
.hexpand()
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,73 @@
|
||||||
|
import Adwaita
|
||||||
|
import CMath
|
||||||
|
|
||||||
|
struct ViewModel {
|
||||||
|
private var numberBuffer: String = ""
|
||||||
|
private var number1: Double? = nil
|
||||||
|
private var number2: Double? = nil
|
||||||
|
private var operand: Operand? = nil
|
||||||
|
@State
|
||||||
|
private(set) var display: String = ""
|
||||||
|
|
||||||
|
mutating func clear() {
|
||||||
|
numberBuffer = ""
|
||||||
|
number1 = nil
|
||||||
|
number2 = nil
|
||||||
|
operand = nil
|
||||||
|
display = ""
|
||||||
|
}
|
||||||
|
|
||||||
|
mutating func addComma() {
|
||||||
|
guard !numberBuffer.isEmpty else { return }
|
||||||
|
|
||||||
|
numberBuffer.append(",")
|
||||||
|
display = numberBuffer
|
||||||
|
}
|
||||||
|
|
||||||
|
mutating func addNumber(_ number: UInt8) {
|
||||||
|
if numberBuffer.isEmpty { display = "" } // Reset display after new number is added to cleared buffer
|
||||||
|
|
||||||
|
numberBuffer.append(String(number))
|
||||||
|
display = numberBuffer
|
||||||
|
}
|
||||||
|
|
||||||
|
mutating func operation(_ operand: Operand) {
|
||||||
|
guard !numberBuffer.isEmpty && number1 == nil else { return }
|
||||||
|
|
||||||
|
self.operand = operand
|
||||||
|
if number1 == nil { number1 = Double(numberBuffer) }
|
||||||
|
else { number2 = Double(numberBuffer) }
|
||||||
|
numberBuffer = ""
|
||||||
|
|
||||||
|
if operand == .sqrt {
|
||||||
|
display = String(sqrt(number1!))
|
||||||
|
number1 = nil
|
||||||
|
self.operand = nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
mutating func calculate() {
|
||||||
|
number2 = Double(numberBuffer)
|
||||||
|
numberBuffer = ""
|
||||||
|
|
||||||
|
var result: Double = 0.0
|
||||||
|
|
||||||
|
switch operand {
|
||||||
|
case .add:
|
||||||
|
result = add_nums(number1!, number2!)
|
||||||
|
case .sub:
|
||||||
|
result = sub_nums(number1!, number2!)
|
||||||
|
case .mul:
|
||||||
|
result = mul_nums(number1!, number2!)
|
||||||
|
case .div:
|
||||||
|
result = div_nums(number1!, number2!)
|
||||||
|
case .pow:
|
||||||
|
result = pow(number1!, number2!)
|
||||||
|
case .sqrt, .none:
|
||||||
|
// do nothing
|
||||||
|
print("Attempting to apply Square Root to nothing!")
|
||||||
|
}
|
||||||
|
|
||||||
|
display = String(result)
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue