DEV Community

BC
BC

Posted on

2

Learn SwiftUI (Day 19/100)

SwiftUI

  • Challenge day: Unit Converter

https://www.hackingwithswift.com/100/swiftui/19

I used VStack and HStack with the help of GPT-4.

import SwiftUI

struct ContentView: View {
    @State private var f: Double = 68
    @State private var c: Double = 0
    @FocusState private var isFocusedF
    @FocusState private var isFocusedC

    func f2c(_ f: Double) -> Double {
        Double(String(format: "%.2f", (f-32) * 5 / 9))!
    }

    func c2f(_ c: Double) -> Double {
        Double(String(format:"%.2f", c * 9 / 5 + 32))!
    }

    var body: some View {
        NavigationStack {
            Form {
                Section("Temperature") {
                    VStack(alignment: .leading) {
                        HStack {
                            TextField("F", value: $f, format: .number)
                                .keyboardType(.numberPad)
                                .onChange(of: f) { (oldValue, newValue) in
                                    if isFocusedF {
                                        self.c = f2c(newValue)
                                    }
                                }
                                .focused($isFocusedF)
                            Text("°F").foregroundColor(.gray)
                        }
                        Divider().padding(.vertical)
                        HStack {
                            TextField("C", value: $c, format: .number)
                                .keyboardType(.numberPad)
                                .onChange(of: c) { (oldValue, newValue) in
                                    if isFocusedC {
                                        self.f = c2f(newValue)
                                    }
                                }
                                .focused($isFocusedC)
                            Text("°C").foregroundColor(.gray)
                        }
                        .onAppear {
                            self.c = f2c(f)
                        }
                    }
                    .padding()
                }
            }
            .navigationTitle("Simple Metrics")
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Screenshot:

Image description

👋 Kindness is contagious

Please leave your appreciation by commenting on this post!

It takes one minute and is worth it for your career.

Get started

Thank you!

Top comments (0)

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay