122 lines
3.6 KiB
Swift
122 lines
3.6 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 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("Total Minutes")
|
|
TextField("Enter minutes here", text: $timeMinutes)
|
|
.keyboardType(.numberPad)
|
|
.textFieldStyle(.roundedBorder)
|
|
}
|
|
.frame(minWidth: 100)
|
|
.padding(.trailing, -15)
|
|
.padding()
|
|
VStack {
|
|
Text("Total Seconds")
|
|
TextField("Enter seconds here", text: $timeSeconds)
|
|
.keyboardType(.numberPad)
|
|
.textFieldStyle(.roundedBorder)
|
|
}
|
|
.frame(minWidth: 100)
|
|
.padding(.leading, -15)
|
|
.padding()
|
|
}
|
|
PaceResults(timeMinutes: $timeMinutes, timeSeconds: $timeSeconds, selectedSystem: $selectedSystem, distance: $distance)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
struct PaceResults: View {
|
|
@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
|
|
|
|
//var notSelectedSystem = ""
|
|
//var pacePerUnit = 0.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 leadingZeros:String = String(format: "%02d:%02d", timeMinutesUnderSixty, timeSecondsUnderSixty)
|
|
|
|
let actualTime:Double = timeMinutesDouble + (convertedSeconds / 100)
|
|
let pace:Double = actualTime / distanceDub
|
|
let paceString:String = String(format: "%.2f", pace)
|
|
|
|
/*
|
|
func pacePerOpString(selectedSystem: String, _ pacePerUnit: inout Double, actualTime: Double, distanceDub: Double, _ notSelectedSystem: inout String) -> String {
|
|
if selectedSystem == "km" {
|
|
pacePerUnit = actualTime / (distanceDub * 1.609344)
|
|
notSelectedSystem = "mi"
|
|
}
|
|
else {
|
|
pacePerUnit = actualTime / (distanceDub * 0.6213711922)
|
|
notSelectedSystem = "km"
|
|
}
|
|
|
|
let pacePerUnitString:String = String(format: "%.02f", pacePerUnit)
|
|
return pacePerUnitString
|
|
}
|
|
*/
|
|
|
|
VStack {
|
|
Text("Distance: \(distance)\(selectedSystem)")
|
|
Text("Total time: \(timeMinutesToHours):\(leadingZeros)")
|
|
Text("\(paceString) minutes per \(selectedSystem)")
|
|
//Text("\(pacePerOpString(selectedSystem: selectedSystem, &pacePerUnit, actualTime: actualTime, distanceDub: distanceDub, ¬SelectedSystem)) minutes per \(notSelectedSystem)")
|
|
}
|
|
}
|
|
}
|
|
|
|
struct ContentView_Previews: PreviewProvider {
|
|
static var previews: some View {
|
|
ContentView()
|
|
}
|
|
}
|