Created
August 12, 2020 20:34
-
-
Save ishabazz/4893c6f6193b5ab938bd371dd6f6210f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ContentView.swift | |
// Units | |
// | |
// Created by Ishmael Shabazz on 5/16/20. | |
// Copyright © 2020 Illuminated Bits LLC. All rights reserved. | |
// | |
import SwiftUI | |
struct ContentView: View { | |
@State private var fromAmount = "" | |
@State private var toAmount = "2" | |
@State private var inputUnit = 0 | |
@State private var outputUnit = 1 | |
@State private var selection1 = 0 //Index of first picker | |
@State private var selction2 = 1 //Index of second picer | |
var units:[UnitTemperature] = [.fahrenheit, | |
.celsius, | |
.kelvin] | |
var convertedAmount:Double{ | |
let from = Double( fromAmount) ?? 0 | |
let fromMeasurement = Measurement(value: from, unit: units[inputUnit]) | |
let toMeasurement = fromMeasurement.converted(to: units[outputUnit]) | |
return toMeasurement.value | |
} | |
var body: some View { | |
NavigationView{ | |
VStack{ | |
Form{ | |
TextField("Original Amount", text: $fromAmount) | |
Picker("Tip Percentage", selection: $inputUnit) { | |
ForEach (0 ..< units.count) { | |
Text("\(self.units[$0].symbol)") | |
} | |
}.pickerStyle(SegmentedPickerStyle()) | |
if Double(fromAmount) != nil{ | |
Picker("", selection: $outputUnit) { | |
ForEach (0 ..< units.count) { | |
Text("\(self.units[$0].symbol)") | |
} | |
}.pickerStyle(SegmentedPickerStyle()) | |
Text( "\(convertedAmount,specifier: "%.2f")\(units[outputUnit].symbol)") | |
} | |
} | |
Spacer() | |
} | |
.navigationBarTitle(Text("Units")) | |
} | |
} | |
} | |
struct ContentView_Previews: PreviewProvider { | |
static var previews: some View { | |
ContentView().environment(\.colorScheme, .dark) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment