WWDC24 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
                    }
                }
            }
        }
    }
}

2      

Save 50% in my WWDC sale.

SAVE 50% To celebrate WWDC24, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

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.