Guest User

iConvert

a guest
Dec 14th, 2021
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 2.54 KB | None | 0 0
  1. //
  2. //  ContentView.swift
  3. //  iConvert
  4. //
  5. //  Created by Justin Trubela on 12/13/21.
  6. //
  7.  
  8. import SwiftUI
  9.  
  10. struct ContentView: View {
  11.     @State private var temperature = 0.0
  12.     @State private var selectedTemp = "Fahrenheit"
  13.     @FocusState private var tempIsFocused: Bool
  14.     private var temperatureUnits = ["Fahrenheit","Celcius","Kelvin"]
  15.    
  16.     var getTemps: [Double] {
  17.         var temps = [0.0,0.0,0.0]
  18.         if(selectedTemp == "Fahrenheit"){
  19.             temps[0] = temperature
  20.             temps[1] = ((temperature - 32.0) * (5.0/9.0))
  21.             temps[2] = (temperature - 32.0) * (9.0/5.0) + 32.0
  22.         }
  23.         else if(selectedTemp == "Celcius"){
  24.             temps[0] = (temperature * (9.0/5.0)) + 32.0
  25.             temps[1] = temperature
  26.             temps[2] = temperature + 273.15
  27.         }
  28.         else {
  29.             temps[0] = (temperature - 273.15) * (9.0/5.0) + 32.0
  30.             temps[1] = temperature - 273.15
  31.             temps[2] = temperature
  32.         }
  33.         return temps
  34.     }
  35.     let decimalFormatter: NumberFormatter = {
  36.         let formatter = NumberFormatter()
  37.         formatter.numberStyle = .decimal
  38.         return formatter
  39.     }()
  40.     var body: some View {
  41.         NavigationView{
  42.             Form{
  43.                 Section{
  44.                     TextField("Enter Temperature", value: $temperature, formatter: decimalFormatter)
  45.                         .keyboardType(.decimalPad)
  46.                         .focused($tempIsFocused)
  47.                     Picker("From \(selectedTemp)", selection: $selectedTemp){
  48.                         ForEach(temperatureUnits, id: \.self){
  49.                             Text($0)
  50.                         }
  51.                     }.pickerStyle(.segmented)
  52.                 }
  53.                 Section{Text("\(getTemps[0].formatted()) °F")} header: {Text("Farenheight")}
  54.                 Section{Text("\(getTemps[1].formatted()) °C")} header: {Text("Celcius")}
  55.                 Section{ Text("\(getTemps[2].formatted()) °K")} header: {Text("Kelvin")}
  56.             }.navigationTitle("iConvert")
  57.                 .toolbar {
  58.                     ToolbarItemGroup(placement: .keyboard) {
  59.                        
  60.                         Spacer()
  61.                        
  62.                         Button("Done"){
  63.                             tempIsFocused = false
  64.                         }
  65.                     }
  66.                 }
  67.         }
  68.     }
  69.    
  70.    
  71.     struct ContentView_Previews: PreviewProvider {
  72.         static var previews: some View {
  73.             ContentView()
  74.         }
  75.     }
  76. }
  77.  
Add Comment
Please, Sign In to add comment