import SwiftUI
struct ContentView: View {
var body: some View {
VStack(spacing : 20){
Text("0")
.frame(maxWidth: .infinity, maxHeight: 200, alignment: .trailing)
.padding(.trailing, 50)
.font(.system(size: 60))
.foregroundColor(Color("resule_fg"))
ForEach(0..<calcu_text.count){ i in
HStack{
ForEach(0..<calcu_text[i].count){ j in
CustomButton(calcu_text[i][j])
}
}
}
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
.background(Color("calc_bg"))
.edgesIgnoringSafeArea(.all)
}
}
struct CustomButton : View {
let type : CalcButtonType
init(_ type : CalcButtonType) {
self.type = type
}
var body: some View{
Text(self.type.text)
.modifier(CalcModifier(type:self.type))
}
}
struct CalcModifier : ViewModifier {
let type : CalcButtonType
func body(content: Content) -> some View {
content
.frame(width: 80,height: 80)
.font(.title)
.background(self.type.bg_color)
.foregroundColor(self.type.fg_color)
.cornerRadius(40.0)
}
}
enum CalcButtonType{
case number(_ text:String)
case calc_opterator(_ text:String)
case calc_s_opterator(_ text:String)
var text : String {
switch self {
case let.number(text) :
return text
case let.calc_opterator(text) :
return text
case let.calc_s_opterator(text) :
return text
}
}
var fg_color : Color{
switch self {
case .number(_) :
return Color("darkgrey_operator_fg")
case .calc_opterator(_) :
return Color("yellow_operator_fg")
case .calc_s_opterator(_) :
return Color("s_operator_fg")
}
}
var bg_color : Color{
switch self {
case .number(_) :
return Color("darkgrey_operator_bg")
case .calc_opterator(_) :
return Color("yellow_operator_bg")
case .calc_s_opterator(_) :
return Color("s_operator_bg")
}
}
}
prefix operator %
prefix operator -
prefix operator +
prefix func % (right : String) -> CalcButtonType{
return .calc_s_opterator(right)
}
prefix func - (right : String) -> CalcButtonType{
return .calc_opterator(right)
}
prefix func + (right : String) -> CalcButtonType{
return .number(right)
}
let calcu_text : [[CalcButtonType]] = [[%"AC",%"+/-",%"%",-"+"],[+"7",+"8",+"9",-"x"],[+"4",+"5",+"6",-"2"],[+"AC",+"+/-",+"%",-"2"],[+"0",+"+/-",-"="]]
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}