Skip to content

Instantly share code, notes, and snippets.

@k-mao
Last active September 15, 2020 00:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save k-mao/e7a863937b9d06cfec4ed0ba156be743 to your computer and use it in GitHub Desktop.
Save k-mao/e7a863937b9d06cfec4ed0ba156be743 to your computer and use it in GitHub Desktop.
Time Turner 2
import SwiftUI
struct ResultRow: View {
var convertedTime: Text
var unit: String
var body: some View {
Section {
HStack {
Spacer()
VStack(alignment: .trailing) {
convertedTime
.font(.system(size: 54, weight: .thin, design: .rounded))
Text("\(unit)")
.font(.system(size: 16, weight: .regular, design: .default))
.opacity(0.66)
}
}
}
.padding(EdgeInsets(top: 4, leading: 8, bottom: 12, trailing: 8))
.animation(.easeInOut(duration: 0.1))
}
}
struct ContentView: View {
enum timeUnits: String, CaseIterable, Identifiable {
var id: String { self.rawValue }
case seconds = "seconds"
case minutes = "minutes"
case hours = "hours"
case days = "days"
}
@State private var input = ""
@State private var inputUnit: timeUnits = .seconds
@State private var outputUnit: timeUnits = .seconds
var inputInSeconds: Double {
let totalSeconds = Double(input) ?? 0
switch inputUnit {
case .seconds: return totalSeconds
case .minutes: return totalSeconds * 60
case .hours: return totalSeconds * 3600
case .days: return totalSeconds * 86_400
}
}
var secondsConversion: Double { self.inputInSeconds }
var minutesConversion: Double { self.inputInSeconds / 60 }
var hoursConversion: Double { self.inputInSeconds / 3600 }
var daysConversion: Double { self.inputInSeconds / 86_400 }
var body: some View {
Form {
Section {
VStack {
TextField("0", text: $input)
.font(.system(size: 36, weight: .light, design: .default))
.multilineTextAlignment(.trailing)
.keyboardType(.decimalPad)
Picker("Input Unit", selection: $inputUnit) {
ForEach(timeUnits.allCases) {
Text($0.rawValue).tag($0)
}
}
.pickerStyle(SegmentedPickerStyle())
}
.padding(EdgeInsets(top: 16, leading: 8, bottom: 16, trailing: 8))
}
Section {
HStack {
Spacer()
Image(systemName: "arrow.up.left").opacity(0.4)
Image(systemName: "hourglass").opacity(0.8)
Image(systemName: "arrow.down.right").opacity(0.4)
Spacer()
}
.frame(height: 44)
}
.background(Color(UIColor.systemBackground))
.listRowInsets(EdgeInsets())
if inputUnit != .seconds {
ResultRow (
convertedTime: Text("\(secondsConversion, specifier: "%.f")"),
unit: "seconds"
)
}
if inputUnit != .minutes {
ResultRow (
convertedTime: Text("\(minutesConversion, specifier: "%.6g")"),
unit: "minutes"
)
}
if inputUnit != .hours {
ResultRow (
convertedTime: Text("\(hoursConversion, specifier: "%.4g")"),
unit: "hours"
)
}
if inputUnit != .days {
ResultRow (
convertedTime: Text("\(daysConversion, specifier: 0.000001 < daysConversion && daysConversion < 0.00002 ? "%.5f" : "%.g")"),
unit: "days"
)
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
.previewDevice("iPhone 11 Pro")
.environment(\.colorScheme, .dark)
}
}
@k-mao
Copy link
Author

k-mao commented Sep 15, 2020

An iteration of my first full SwiftUI project, a time unit conversion app.
Check it out in action here: https://dribbble.com/shots/14201800-SwiftUI-Time-Conversion-App

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment