Skip to content

Commit

Permalink
video 9 - calc
Browse files Browse the repository at this point in the history
  • Loading branch information
jacob-i committed Nov 19, 2023
1 parent a4e7c87 commit 1df9097
Show file tree
Hide file tree
Showing 3 changed files with 202 additions and 0 deletions.
141 changes: 141 additions & 0 deletions video/ai-extensions/video-9-calc-swiftui/AiExtensions.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
import SwiftUI

enum CalculatorOperation {
case addition, subtraction, multiplication, division, none
}

struct CalculatorButton: View {
let symbol: String
var action: () -> Void

var body: some View {
Button(action: action) {
Text(symbol)
.font(.title)
.frame(width: 80, height: 80)
.foregroundColor(Color.black)
.background(Color.gray.opacity(0.2))
.cornerRadius(40)
}
}
}

struct CalculatorScreen: View {
@State private var displayValue: String = "0"
@State private var currentOperation: CalculatorOperation = .none
@State private var storedValue: Double? = nil

let buttons: [[String]] = [
["C", "+/-", "%", "/"],
["7", "8", "9", "X"],
["4", "5", "6", "-"],
["1", "2", "3", "+"],
["0", ".", "="]
]

func input(digit: String) {
if displayValue == "0" || displayValue == "0.0" {
displayValue = digit != "." ? digit : "0."
} else {
if digit == "." && displayValue.contains(".") { return }
displayValue += digit
}
}

func performOperation(_ operation: CalculatorOperation) {
if let storedVal = storedValue, let displayVal = Double(displayValue) {
switch currentOperation {
case .addition:
displayValue = "\(storedVal + displayVal)"
case .subtraction:
displayValue = "\(storedVal - displayVal)"
case .multiplication:
displayValue = "\(storedVal * displayVal)"
case .division:
displayValue = storedVal != 0 ? "\(storedVal / displayVal)" : "Error"
case .none:
break
}
self.storedValue = Double(displayValue)
self.currentOperation = operation
} else {
storedValue = Double(displayValue)
currentOperation = operation
displayValue = "0"
}
}

func clear() {
displayValue = "0"
storedValue = nil
currentOperation = .none
}

func toggleSign() {
if let value = Double(displayValue) {
displayValue = "\(value * -1)"
}
}

func calculatePercentage() {
if let value = Double(displayValue) {
displayValue = "\(value / 100)"
}
}

var body: some View {
VStack {
Spacer()
Text(displayValue)
.font(.largeTitle)
.frame(maxWidth: .infinity, alignment: .trailing)
.padding()

ForEach(buttons, id: \.self) { row in
HStack {
ForEach(row, id: \.self) { buttonSymbol in
CalculatorButton(symbol: buttonSymbol) {
switch buttonSymbol {
case "C":
clear()
case "+/-":
toggleSign()
case "%":
calculatePercentage()
case "/":
performOperation(.division)
case "X":
performOperation(.multiplication)
case "-":
performOperation(.subtraction)
case "+":
performOperation(.addition)
case "=":
performOperation(.none)
default:
input(digit: buttonSymbol)
}
}
}
}
}
Spacer()
}
.padding(.bottom)
}
}

struct GlowbyScreen: View {
static let enabled = true
static let title = "Calculator"

var body: some View {
CalculatorScreen()
}
}

struct GlowbyScreen_Previews: PreviewProvider {
static var previews: some View {
GlowbyScreen()
}
}
24 changes: 24 additions & 0 deletions video/ai-extensions/video-9-calc-swiftui/calculator_netlify/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@

let display = document.getElementById('display');

document.querySelectorAll('button').forEach(button => {
button.addEventListener('click', () => {
let buttonText = button.innerText;

if (buttonText === 'C') {
display.innerText = '0';
} else if (buttonText === '=') {
try {
display.innerText = eval(display.innerText);
} catch {
display.innerText = 'Error';
}
} else {
if (display.innerText === '0') {
display.innerText = buttonText;
} else {
display.innerText += buttonText;
}
}
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Calculator</title>
<script src="https://cdn.tailwindcss.com"></script>
</head>
<body class="bg-gray-100 flex items-center justify-center h-screen">
<div class="bg-white p-10 rounded-lg shadow-md">
<div id="display" class="bg-gray-300 p-2 text-right rounded mb-5">0</div>
<div class="grid grid-cols-4 gap-2">
<button class="col-span-2 bg-gray-400 p-4 rounded text-xl">C</button>
<button class="bg-gray-400 p-4 rounded text-xl">+/-</button>
<button class="bg-gray-400 p-4 rounded text-xl">%</button>
<button class="bg-gray-400 p-4 rounded text-xl">/</button>
<button class="bg-gray-400 p-4 rounded text-xl">7</button>
<button class="bg-gray-400 p-4 rounded text-xl">8</button>
<button class="bg-gray-400 p-4 rounded text-xl">9</button>
<button class="bg-gray-400 p-4 rounded text-xl">*</button>
<button class="bg-gray-400 p-4 rounded text-xl">4</button>
<button class="bg-gray-400 p-4 rounded text-xl">5</button>
<button class="bg-gray-400 p-4 rounded text-xl">6</button>
<button class="bg-gray-400 p-4 rounded text-xl">-</button>
<button class="bg-gray-400 p-4 rounded text-xl">1</button>
<button class="bg-gray-400 p-4 rounded text-xl">2</button>
<button class="bg-gray-400 p-4 rounded text-xl">3</button>
<button class="bg-gray-400 p-4 rounded text-xl">+</button>
<button class="col-span-2 bg-gray-400 p-4 rounded text-xl">0</button>
<button class="bg-gray-400 p-4 rounded text-xl">.</button>
<button class="bg-gray-400 p-4 rounded text-xl">=</button>
</div>
</div>
<script src="app.js"></script>
</body>
</html>

0 comments on commit 1df9097

Please sign in to comment.