BLACK FRIDAY SALE: Save 50% on all my Swift books and bundles! >>

@metropol: Day 19 Challenge - Unit Conversion with enum and Measurement

Forums > 100 Days of SwiftUI

Here's my solution for the challenge. I used volume units and created an enum that integrates Measurement to handle the conversion automatically.

import SwiftUI

struct ContentView: View {
    @State private var conversionValue = 0.0
    @FocusState private var valueIsFocused: Bool
    @State private var inputUnit = ConversionUnits.L
    @State private var outputUnit = ConversionUnits.gal

    private enum ConversionUnits: String, CaseIterable {
        case mL
        case L
        case cup
        case pt
        case gal

        var unit: UnitVolume {
            switch self {
            case .mL: return UnitVolume.milliliters
            case .L: return UnitVolume.liters
            case .cup: return UnitVolume.cups
            case .pt: return UnitVolume.pints
            case .gal: return UnitVolume.gallons
            }
        }
    }

    private var convertedValue: Double {
        let inValue = Measurement(value: conversionValue, unit: inputUnit.unit)
        let value = inValue.converted(to: outputUnit.unit)

        return value.value
    }

    var body: some View {
        NavigationStack {
            Form {
                Section {
                    TextField("Value to convert", value: $conversionValue, format: .number)
                        .keyboardType(.decimalPad)
                        .focused($valueIsFocused)
                } header: {
                    Text("Value to convert")
                }

                Section {
                    Picker("Source Unit", selection: $inputUnit) {
                        ForEach(ConversionUnits.allCases, id: \.self) {
                            Text($0.rawValue)
                        }
                    }
                    .pickerStyle(.segmented)
                } header: {
                    Text("Convert from")
                }

                Section {
                    Picker("Source Unit", selection: $outputUnit) {
                        ForEach(ConversionUnits.allCases, id: \.self) {
                            Text($0.rawValue)
                        }
                    }
                    .pickerStyle(.segmented)
                } header: {
                    Text("Convert to")
                }

                Section {
                    Text(convertedValue.formatted())
                } header: {
                    Text("Converted value")
                }
            }
            .navigationTitle("Unit Conversion")
            .toolbar {
                ToolbarItemGroup(placement: .keyboard) {
                    Spacer()

                    Button("Done") {
                        valueIsFocused = false
                    }
                }
            }
        }
    }
}

   

Hacking with Swift is sponsored by Guardsquare

SPONSORED AppSweep by Guardsquare helps developers automate the mobile app security testing process with fast, free scans. By using AppSweep’s actionable recommendations, developers can improve the security posture of their apps in accordance with security standards like OWASP.

Learn more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.