Skip to content

Commit

Permalink
wip Duration
Browse files Browse the repository at this point in the history
  • Loading branch information
sergiocampama committed Nov 27, 2024
1 parent b652fe1 commit 3e218e6
Show file tree
Hide file tree
Showing 86 changed files with 9,285 additions and 8,338 deletions.
78 changes: 44 additions & 34 deletions Examples/Examples/Calculator/CalcCalendarView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,41 +9,51 @@ import SwiftUI
import Time

struct CalcCalendarView: View {
@Binding var selectedSecond : Fixed<Second>

var selectedDay: Binding<Fixed<Day>> {
return Binding(get: {
selectedSecond.fixedDay
}, set: { newValue in
do {
selectedSecond = try newValue.setting(hour: selectedSecond.hour,
minute: selectedSecond.minute,
second: selectedSecond.second)
} catch {
print("Error: \(selectedSecond.format(time: .long)) does not exist on \(newValue.format(date: .long))")
}
@Binding var selectedSecond: Fixed<Second>

var selectedDay: Binding<Fixed<Day>> {
return Binding(
get: {
selectedSecond.fixedDay
},
set: { newValue in
do {
selectedSecond = try newValue.setting(
hour: selectedSecond.hour,
minute: selectedSecond.minute,
second: selectedSecond.second)
} catch {
print(
"Error: \(selectedSecond.format(time: .long)) does not exist on \(newValue.format(date: .long))"
)
}
})
}

var body: some View {
VStack {
DayPicker(
selection: selectedDay, consistentNumberOfWeeks: true,
label: {
YearMonthPicker(month: $0)
})
}

var body: some View {
VStack {
DayPicker(selection: selectedDay, consistentNumberOfWeeks: true, label: {
YearMonthPicker(month: $0)
})

HStack {
TimePicker(selection: $selectedSecond)
Button("Now") {
let now = Clocks.system.currentSecond
do {
selectedSecond = try selectedSecond.setting(hour: now.hour,
minute: now.minute,
second: now.second)
} catch {
print("The time \(now.format(time: .long)) does not exist on \(selectedSecond.format(date: .long))")
}
}
}

HStack {
TimePicker(selection: $selectedSecond)
Button("Now") {
let now = Clocks.system.currentSecond
do {
selectedSecond = try selectedSecond.setting(
hour: now.hour,
minute: now.minute,
second: now.second)
} catch {
print(
"The time \(now.format(time: .long)) does not exist on \(selectedSecond.format(date: .long))"
)
}
}
}
}
}
}
216 changes: 108 additions & 108 deletions Examples/Examples/Calculator/CalcDifferencesView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,115 +9,115 @@ import SwiftUI
import Time

struct CalcDifferencesView: View {
@Binding var startSecond : Fixed<Second>
@Binding var endSecond : Fixed<Second>

@State var wholeDifference = true

@State var yearDifference = 0
@State var monthDifference = 0
@State var dayDifference = 0
@State var hourDifference = 0
@State var minuteDifference = 0
@State var secondDifference = 0

private var conjunction: String { wholeDifference ? "or" : "and" }

var body: some View {
GroupBox("Differences") {
VStack {
Text("\(startSecond.description)")
.font(.headline)

Text("to")
.font(.caption)

Text("\(endSecond.description)")
.font(.headline)

HStack {
Stepper("^[\(yearDifference) year](inflect: true)") {
endSecond = endSecond.nextYear
} onDecrement: {
endSecond = endSecond.previousYear
}

Picker("Diffence kind", selection: $wholeDifference) {
Text("or").tag(true)
Text("and").tag(false)
}
.pickerStyle(.segmented)
.fixedSize()
.labelsHidden()

Stepper("^[\(monthDifference) month](inflect: true)") {
endSecond = endSecond.nextMonth
} onDecrement: {
endSecond = endSecond.previousMonth
}

Text(conjunction)

Stepper("^[\(dayDifference) day](inflect: true)") {
endSecond = endSecond.nextDay
} onDecrement: {
endSecond = endSecond.previousDay
}
}
.padding(.top, 12)

HStack {
Text(conjunction)

Stepper("^[\(hourDifference) hour](inflect: true)") {
endSecond = endSecond.nextHour
} onDecrement: {
endSecond = endSecond.previousHour
}

Text(conjunction)

Stepper("^[\(minuteDifference) minute](inflect: true)") {
endSecond = endSecond.nextMinute
} onDecrement: {
endSecond = endSecond.previousMinute
}

Text(conjunction)

Stepper("^[\(secondDifference) second](inflect: true)") {
endSecond = endSecond.nextSecond
} onDecrement: {
endSecond = endSecond.previousSecond
}
}
.padding(.top, 12)
}
.frame(maxWidth: .infinity, alignment: .center)
@Binding var startSecond: Fixed<Second>
@Binding var endSecond: Fixed<Second>

@State var wholeDifference = true

@State var yearDifference = 0
@State var monthDifference = 0
@State var dayDifference = 0
@State var hourDifference = 0
@State var minuteDifference = 0
@State var secondDifference = 0

private var conjunction: String { wholeDifference ? "or" : "and" }

var body: some View {
GroupBox("Differences") {
VStack {
Text("\(startSecond.description)")
.font(.headline)

Text("to")
.font(.caption)

Text("\(endSecond.description)")
.font(.headline)

HStack {
Stepper("^[\(yearDifference) year](inflect: true)") {
endSecond = endSecond.nextYear
} onDecrement: {
endSecond = endSecond.previousYear
}

Picker("Diffence kind", selection: $wholeDifference) {
Text("or").tag(true)
Text("and").tag(false)
}
.pickerStyle(.segmented)
.fixedSize()
.labelsHidden()

Stepper("^[\(monthDifference) month](inflect: true)") {
endSecond = endSecond.nextMonth
} onDecrement: {
endSecond = endSecond.previousMonth
}

Text(conjunction)

Stepper("^[\(dayDifference) day](inflect: true)") {
endSecond = endSecond.nextDay
} onDecrement: {
endSecond = endSecond.previousDay
}
}
.onAppear { recompute() }
.onChange(of: startSecond) { _ in recompute() }
.onChange(of: endSecond) { _ in recompute() }
.onChange(of: wholeDifference) { _ in recompute() }
}

private func recompute() {
if wholeDifference {
yearDifference = startSecond.differenceInWholeYears(to: endSecond).years
monthDifference = startSecond.differenceInWholeMonths(to: endSecond).months
dayDifference = startSecond.differenceInWholeDays(to: endSecond).days
hourDifference = startSecond.differenceInWholeHours(to: endSecond).hours
minuteDifference = startSecond.differenceInWholeMinutes(to: endSecond).minutes
secondDifference = startSecond.differenceInWholeSeconds(to: endSecond).seconds
} else {
let difference = startSecond.difference(to: endSecond)

yearDifference = difference.years
monthDifference = difference.months
dayDifference = difference.days
hourDifference = difference.hours
minuteDifference = difference.minutes
secondDifference = difference.seconds
.padding(.top, 12)

HStack {
Text(conjunction)

Stepper("^[\(hourDifference) hour](inflect: true)") {
endSecond = endSecond.nextHour
} onDecrement: {
endSecond = endSecond.previousHour
}

Text(conjunction)

Stepper("^[\(minuteDifference) minute](inflect: true)") {
endSecond = endSecond.nextMinute
} onDecrement: {
endSecond = endSecond.previousMinute
}

Text(conjunction)

Stepper("^[\(secondDifference) second](inflect: true)") {
endSecond = endSecond.nextSecond
} onDecrement: {
endSecond = endSecond.previousSecond
}
}
.padding(.top, 12)
}
.frame(maxWidth: .infinity, alignment: .center)
}
.onAppear { recompute() }
.onChange(of: startSecond) { _ in recompute() }
.onChange(of: endSecond) { _ in recompute() }
.onChange(of: wholeDifference) { _ in recompute() }
}

private func recompute() {
if wholeDifference {
yearDifference = startSecond.differenceInWholeYears(to: endSecond).years
monthDifference = startSecond.differenceInWholeMonths(to: endSecond).months
dayDifference = startSecond.differenceInWholeDays(to: endSecond).days
hourDifference = startSecond.differenceInWholeHours(to: endSecond).hours
minuteDifference = startSecond.differenceInWholeMinutes(to: endSecond).minutes
secondDifference = startSecond.differenceInWholeSeconds(to: endSecond).seconds
} else {
let difference = startSecond.difference(to: endSecond)

yearDifference = difference.years
monthDifference = difference.months
dayDifference = difference.days
hourDifference = difference.hours
minuteDifference = difference.minutes
secondDifference = difference.seconds
}
}
}
52 changes: 26 additions & 26 deletions Examples/Examples/Calculator/CalculatorView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,35 @@
//

import SwiftUI

import Time

struct CalculatorView: View {
@State var startSecond = Clocks.system.currentSecond
@State var endSecond = Clocks.system.currentSecond + .days(1)

var body: some View {
VStack(alignment: .center, spacing: 12) {

HStack {
GroupBox("From") {
CalcCalendarView(selectedSecond: $startSecond)
.frame(maxWidth: .infinity)
}

Image(systemName: "arrow.forward")

GroupBox("To") {
CalcCalendarView(selectedSecond: $endSecond)
.frame(maxWidth: .infinity)
}
}

CalcDifferencesView(startSecond: $startSecond,
endSecond: $endSecond)

Spacer()
@State var startSecond = Clocks.system.currentSecond
@State var endSecond = Clocks.system.currentSecond + .days(1)

var body: some View {
VStack(alignment: .center, spacing: 12) {

HStack {
GroupBox("From") {
CalcCalendarView(selectedSecond: $startSecond)
.frame(maxWidth: .infinity)
}

Image(systemName: "arrow.forward")

GroupBox("To") {
CalcCalendarView(selectedSecond: $endSecond)
.frame(maxWidth: .infinity)
}
.padding()
}

CalcDifferencesView(
startSecond: $startSecond,
endSecond: $endSecond)

Spacer()
}
.padding()
}
}
Loading

0 comments on commit 3e218e6

Please sign in to comment.