UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

@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      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

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

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.