article

Wednesday, August 11, 2021

SwiftUI Simple Calculator

SwiftUI Simple Calculator
ContentView.swift
 
//
//  ContentView.swift
//  swiftuidev
//
//  Created by Cairocoders on 7/30/21.
//

import SwiftUI

struct ContentView: View {
     
    @State private var firstNumber = 0
    @State private var secondNumber = 0
    @State private var operand = ""
    @State private var calculatorText = "0"
    @State private var isTypingNumber = false
     
    var body: some View {
        VStack(spacing: 30) {
            TextField("0", text: $calculatorText)
                .border(Color.white, width: 2)
                .padding()
                .multilineTextAlignment(.trailing)
                .foregroundColor(Color.white)
            HStack {
                createCalcDigit("1")
                    .modifier(CustomButton())
                Spacer()
                createCalcDigit("2")
                    .modifier(CustomButton())
                Spacer()
                createCalcDigit("3")
                    .modifier(CustomButton())
            }
            .padding()
            .foregroundColor(Color.white)
 
            HStack {
                createCalcDigit("4")
                    .modifier(CustomButton())
                Spacer()
                createCalcDigit("5")
                    .modifier(CustomButton())
                Spacer()
                createCalcDigit("6")
                    .modifier(CustomButton())
            }
            .padding()
            .foregroundColor(Color.white)
             
            HStack {
                createCalcDigit("7")
                    .modifier(CustomButton())
                Spacer()
                createCalcDigit("8")
                    .modifier(CustomButton())
                Spacer()
                createCalcDigit("9")
                    .modifier(CustomButton())
            }
            .padding()
            .foregroundColor(Color.white)
             
            HStack {
                Button(action: {
                    self.operandTapped("-")
                }) {
                    (Text("-"))
                }
                .modifier(CustomButton(buttonbackgroundColor: Color.gray))
                 
                Spacer()
                createCalcDigit("0")
                    .frame(width: 70, height: 70)
                    .foregroundColor(Color.white)
                    .background(Color.orange)
                    .clipShape(Circle())
                Spacer()
                Button(action: {
                    self.operandTapped("+")
                }) {
                    (Text("+"))
                }
                .modifier(CustomButton(buttonbackgroundColor: Color.gray))
            }
            .padding()
            .foregroundColor(Color.white)
             
            HStack {
                Spacer()
 
                Button(action: {
                    self.calculate()
                }) {
                    (Text("="))
                }
                .modifier(CustomButton(buttonbackgroundColor: Color.gray))
                Spacer()
            }
            .foregroundColor(Color.white)
        }
        .font(.largeTitle)
        .frame(maxWidth: .infinity, maxHeight: .infinity)
        .background(Color.black)
        .ignoresSafeArea()
    }
     
    private func createCalcDigit(_ number: String) -> Button<Text> {
        return Button(action: {
            self.digitTapped(number)
        }) {
            (Text(number))
        }
    }
 
    private func digitTapped(_ number: String) -> Void {
        if isTypingNumber {
            calculatorText += number
        } else {
            calculatorText = number
            isTypingNumber = true
        }
    }
 
    private func operandTapped(_ operand: String) {
        isTypingNumber = false
        firstNumber = Int(calculatorText)!
        self.operand = operand
        calculatorText = operand
    }
 
    private func calculate() {
        isTypingNumber = false
        var result  = 0
        secondNumber = Int(calculatorText)!
 
        if operand == "+" {
            result = firstNumber + secondNumber
        } else if operand == "-" {
            result = firstNumber - secondNumber
        }
 
        calculatorText = "\(result)"
    }
}

struct CustomButton: ViewModifier {
     
    var buttonbackgroundColor = Color.orange
    
    func body(content: Content) -> some View {
        return content
            .frame(width: 70, height: 70)
            .foregroundColor(Color.white)
            .background(buttonbackgroundColor)
            .clipShape(Circle())
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

Related Post