Skip to content

chanheki/mobileModule00

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Calculator App

This app use the math_operation library.

Apply AutoLayout via Geometry Reader


What I learned

image image

A quick look at SWIFT code

var buttonsView: some View {
        let buttons: [[(String, () -> Void)]] = [
            [("7", { self.buttonClickEvent("7") }), ("8", { self.buttonClickEvent("8") }), ("9", { self.buttonClickEvent("9") }), ("C", { self.buttonClickEvent("C") }), ("AC", { self.buttonClickEvent("AC") })],
            [("4", { self.buttonClickEvent("4") }), ("5", { self.buttonClickEvent("5") }), ("6", { self.buttonClickEvent("6") }), ("+", { self.buttonClickEvent("+") }), ("-", { self.buttonClickEvent("-") })],
            [("1", { self.buttonClickEvent("1") }), ("2", { self.buttonClickEvent("2") }), ("3", { self.buttonClickEvent("3") }), ("*", { self.buttonClickEvent("*") }), ("/", { self.buttonClickEvent("/") })],
            [("0", { self.buttonClickEvent("0") }), (".", { self.buttonClickEvent(".") }), ("00", { self.buttonClickEvent("00") }), ("=", { self.buttonCalculate() }), ("",{self.buttonCalculate()})]
        ]
        
        return GeometryReader { geometry in
            VStack(spacing: 0) {
                ForEach(0..<buttons.count, id: \.self) { rowIndex in
                    HStack(spacing: 0) {
                        ForEach(0..<buttons[rowIndex].count, id: \.self) { columnIndex in
                            let button = buttons[rowIndex][columnIndex]
                            Button(action: button.1) {
                                Text(button.0)
                                    .font(.largeTitle)
                                    .foregroundColor(.white)
                                    .frame(width: self.calculateButtonWidth(containerWidth: geometry.size.width, buttonCount: buttons[rowIndex].count), height: calculateButtonHeight(containerHeight: geometry.size.height, buttonCount: buttons.count))
                                    .background(self.buttonColor(button.0))
                            }
                        }
                    }
                }
            }
            .frame(width: geometry.size.width)
        }
    }

Releases

No releases published

Packages

No packages published

Languages