Skip to content

Commit

Permalink
Multiple changes to support Copilot environment:
Browse files Browse the repository at this point in the history
• Mock out a crude version of Duration to avoid depending on the >iOS 16
Duration type.
• Tags Clock related APIs with proper available checks.
• Adds podspec to be integrated with Cocoapods.
• Runs Copilot's formatter.
  • Loading branch information
sergiocampama committed Nov 28, 2024
1 parent b652fe1 commit 8dadb73
Show file tree
Hide file tree
Showing 86 changed files with 4,739 additions and 3,111 deletions.
57 changes: 36 additions & 21 deletions Examples/Examples/Calculator/CalcCalendarView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,38 +9,53 @@ import SwiftUI
import Time

struct CalcCalendarView: View {
@Binding var selectedSecond : Fixed<Second>
@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))")
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)
})

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)
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))")
print(
"The time \(now.format(time: .long)) does not exist on \(selectedSecond.format(date: .long))"
)
}
}
}
Expand Down
44 changes: 22 additions & 22 deletions Examples/Examples/Calculator/CalcDifferencesView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,82 +9,82 @@ import SwiftUI
import Time

struct CalcDifferencesView: View {
@Binding var startSecond : Fixed<Second>
@Binding var endSecond : Fixed<Second>
@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: {
} 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: {
Expand All @@ -100,7 +100,7 @@ struct CalcDifferencesView: View {
.onChange(of: endSecond) { _ in recompute() }
.onChange(of: wholeDifference) { _ in recompute() }
}

private func recompute() {
if wholeDifference {
yearDifference = startSecond.differenceInWholeYears(to: endSecond).years
Expand All @@ -111,7 +111,7 @@ struct CalcDifferencesView: View {
secondDifference = startSecond.differenceInWholeSeconds(to: endSecond).seconds
} else {
let difference = startSecond.difference(to: endSecond)

yearDifference = difference.years
monthDifference = difference.months
dayDifference = difference.days
Expand Down
19 changes: 10 additions & 9 deletions Examples/Examples/Calculator/CalculatorView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,34 @@
//

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)


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

Spacer()
}
.padding()
Expand Down
22 changes: 12 additions & 10 deletions Examples/Examples/CalendarView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,27 @@ import SwiftUI
import Time

struct CalendarView: View {

@State var currentMonth = Clocks.system.currentMonth
@State var selectedDay: Fixed<Day>?

@State var consistentNumberOfWeeks = false

var body: some View {
VStack {
Toggle("Show the same number of weeks each month", isOn: $consistentNumberOfWeeks)

DayPicker(selection: $selectedDay,
consistentNumberOfWeeks: consistentNumberOfWeeks)


DayPicker(
selection: $selectedDay,
consistentNumberOfWeeks: consistentNumberOfWeeks
)

Divider()

Text("The currently selected day is \(selectedDay?.format(date: .long) ?? "none")")

Spacer()
}

}
}
Loading

0 comments on commit 8dadb73

Please sign in to comment.