150 lines
4.5 KiB
Swift
150 lines
4.5 KiB
Swift
//
|
|
// ContentView.swift
|
|
// Splits
|
|
//
|
|
// Created by Isaac Greene on 4/3/22.
|
|
//
|
|
|
|
import SwiftUI
|
|
import Foundation
|
|
|
|
struct ContentView: View {
|
|
var SISystem = ["km","mi"]
|
|
var minutes = Array(0...300)
|
|
var times = Array(0...300).map { String($0) }
|
|
@State var timeHours: String = ""
|
|
@State var timeMinutes: String = ""
|
|
@State var timeSeconds: String = ""
|
|
@State var selectedSystem: String = "km"
|
|
@State var distance: String = ""
|
|
|
|
var body: some View {
|
|
VStack {
|
|
VStack {
|
|
TextField("Enter distance here", text: $distance)
|
|
.padding()
|
|
.keyboardType(.decimalPad)
|
|
.textFieldStyle(.roundedBorder)
|
|
}
|
|
VStack {
|
|
Text("Unit of measurement:")
|
|
Picker("System of measurement", selection: $selectedSystem, content: {
|
|
ForEach(SISystem, id: \.self, content: { unit in
|
|
Text(unit)
|
|
})
|
|
})
|
|
.pickerStyle(.segmented)
|
|
.frame(minWidth: 60, maxWidth: 300)
|
|
HStack {
|
|
VStack {
|
|
Text("Hours")
|
|
TextField("Enter hours here", text: $timeHours)
|
|
.keyboardType(.numberPad)
|
|
.textFieldStyle(.roundedBorder)
|
|
}
|
|
.frame(minWidth: 100)
|
|
.padding(.trailing, -15)
|
|
.padding()
|
|
VStack {
|
|
Text("Minutes")
|
|
TextField("Enter minutes here", text: $timeMinutes)
|
|
.keyboardType(.numberPad)
|
|
.textFieldStyle(.roundedBorder)
|
|
}
|
|
.frame(minWidth: 100)
|
|
.padding(.trailing, -15)
|
|
.padding(.leading, -15)
|
|
.padding()
|
|
VStack {
|
|
Text("Seconds")
|
|
TextField("Enter seconds here", text: $timeSeconds)
|
|
.keyboardType(.numberPad)
|
|
.textFieldStyle(.roundedBorder)
|
|
}
|
|
.frame(minWidth: 100)
|
|
.padding(.leading, -15)
|
|
.padding()
|
|
}
|
|
PaceResults(timeHours: $timeHours, timeMinutes: $timeMinutes, timeSeconds: $timeSeconds, selectedSystem: $selectedSystem, distance: $distance)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
struct PaceResults: View {
|
|
@Binding var timeHours: String
|
|
@Binding var timeMinutes: String
|
|
@Binding var timeSeconds: String
|
|
@Binding var selectedSystem: String
|
|
@Binding var distance: String
|
|
|
|
var body: some View {
|
|
let distanceDub = Double(distance) ?? 1.0
|
|
|
|
let convertedSeconds:Double = (Double(timeSeconds) ?? 0) * (1.666666666666666666666666)
|
|
let timeSecondsInt:Int = Int(timeSeconds) ?? 0
|
|
let timeSecondsUnderSixty:Int = (timeSecondsInt % 60)
|
|
|
|
let timeSecondsToMinutes:Int = (timeSecondsInt - timeSecondsUnderSixty) / 60
|
|
|
|
let timeMinutesDouble:Double = Double(timeMinutes) ?? 0.0
|
|
|
|
let timeMinutesInt:Int = (Int(timeMinutes) ?? 0) + (timeSecondsToMinutes)
|
|
let timeMinutesUnderSixty:Int = timeMinutesInt % 60
|
|
let timeMinutesToHours:Int = (timeMinutesInt - timeMinutesUnderSixty) / 60
|
|
|
|
let totalHours:Double = Double(timeMinutesToHours) + (Double(timeHours) ?? 0)
|
|
|
|
let leadingZeros:String = String(format: "%02d:%02d", timeMinutesUnderSixty, timeSecondsUnderSixty)
|
|
|
|
let actualTime:Double = timeMinutesDouble + (convertedSeconds / 100) + ((Double(timeHours) ?? 0) * 60)
|
|
let pace:Double = actualTime / distanceDub
|
|
let paceString:String = String(format: "%.2f", pace)
|
|
let hoursFormatted:String = String(format: "%.0f", totalHours)
|
|
|
|
VStack {
|
|
Text("Distance: \(distance)\(selectedSystem)")
|
|
Text("Total time: \(hoursFormatted):\(leadingZeros)")
|
|
Text("\(paceString) minutes per \(selectedSystem)")
|
|
}
|
|
}
|
|
}
|
|
|
|
struct ContentView_Previews: PreviewProvider {
|
|
static var previews: some View {
|
|
ContentView()
|
|
}
|
|
}
|
|
|
|
/*
|
|
struct AllTheMath {
|
|
@Binding var timeHours: String
|
|
@Binding var timeMinutes: String
|
|
@Binding var timeSeconds: String
|
|
@Binding var selectedSystem: String
|
|
@Binding var distance: String
|
|
|
|
let distanceDub = Double(distance) ?? 1.0
|
|
|
|
let convertedSeconds:Double = (Double(timeSeconds) ?? 0) * (1.666666666666666666666666)
|
|
let timeSecondsInt:Int = Int(timeSeconds) ?? 0
|
|
let timeSecondsUnderSixty:Int = (timeSecondsInt % 60)
|
|
|
|
let timeSecondsToMinutes:Int = (timeSecondsInt - timeSecondsUnderSixty) / 60
|
|
|
|
let timeMinutesDouble:Double = Double(timeMinutes) ?? 0.0
|
|
|
|
let timeMinutesInt:Int = (Int(timeMinutes) ?? 0) + (timeSecondsToMinutes)
|
|
let timeMinutesUnderSixty:Int = timeMinutesInt % 60
|
|
let timeMinutesToHours:Int = (timeMinutesInt - timeMinutesUnderSixty) / 60
|
|
|
|
let totalHours:Double = Double(timeMinutesToHours) + (Double(timeHours) ?? 0)
|
|
|
|
let leadingZeros:String = String(format: "%02d:%02d", timeMinutesUnderSixty, timeSecondsUnderSixty)
|
|
|
|
let actualTime:Double = timeMinutesDouble + (convertedSeconds / 100) + ((Double(timeHours) ?? 0) * 60)
|
|
let pace:Double = actualTime / distanceDub
|
|
let paceString:String = String(format: "%.2f", pace)
|
|
let hoursFormatted:String = String(format: "%.0f", totalHours)
|
|
}
|
|
*/
|