Skip to content

Commit

Permalink
Keep it working for now
Browse files Browse the repository at this point in the history
  • Loading branch information
Supereg committed Sep 4, 2024
1 parent 5c0dc60 commit 6d65471
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 8 deletions.
1 change: 1 addition & 0 deletions Sources/SpeziScheduler/InfinityLoop/EventQuery.swift
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ extension EventQuery: DynamicProperty {

do {
// TODO: should this run on the main thread?
// TODO: does this refresh when there is a new outcome?
fetchedEvents = try scheduler.queryEvents(for: configuration.range, predicate: configuration.taskPredicate)
} catch {
configuration.fetchError = error
Expand Down
2 changes: 1 addition & 1 deletion Sources/SpeziScheduler/InfinityLoop/ILScheduler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ public final class ILScheduler {
/// - Returns: The list of `ILTask` that are effective in the specified date range and match the specified `predicate`. The result is ordered by the specified `sortDescriptors`.
public func queryTasks(
for range: Range<Date>,
predicate: Predicate<ILTask> = #Predicate { _ in true },
predicate: Predicate<ILTask> = #Predicate { _ in true }, // TODO: maybe remove?
sortBy sortDescriptors: [SortDescriptor<ILTask>] = [],
prefetchOutcomes: Bool = false
) throws -> [ILTask] {
Expand Down
2 changes: 1 addition & 1 deletion Sources/SpeziScheduler/InfinityLoop/Playground.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ struct PlaygroundList: View {


#if DEBUG
#Preview {
#Preview(traits: .schedulerSampleData) {
PlaygroundList()
.previewWith {
ILScheduler()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,65 @@ extension ILSchedule {
}


extension ILSchedule.RecurrenceRule: Equatable, Sendable, Codable {}
extension ILSchedule.RecurrenceRule: Equatable, Sendable {}

extension ILSchedule.RecurrenceRule: Codable {
private enum CodingKeys: String, CodingKey {
case calendar
case matchingPolicy
case repeatedTimePolicy
case frequency
case interval
case end
case seconds
case minutes
case hours
case weekdays
case daysOfTheMonth
case daysOfTheYear
case months
case weeks
case setPositions
}

init(from decoder: any Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
calendar = try container.decode(Data.self, forKey: .calendar)
matchingPolicy = try container.decode(Calendar.MatchingPolicy.self, forKey: .matchingPolicy)
repeatedTimePolicy = try container.decode(Calendar.RepeatedTimePolicy.self, forKey: .repeatedTimePolicy)
frequency = try container.decode(Calendar.RecurrenceRule.Frequency.self, forKey: .frequency)
interval = try container.decode(Int.self, forKey: .interval)
end = try container.decode(Calendar.RecurrenceRule.End.self, forKey: .end)
seconds = try container.decode([Int].self, forKey: .seconds)
minutes = try container.decode([Int].self, forKey: .minutes)
hours = try container.decode([Int].self, forKey: .hours)
weekdays = try container.decode([Calendar.RecurrenceRule.Weekday].self, forKey: .weekdays)
daysOfTheMonth = try container.decode([Int].self, forKey: .daysOfTheMonth)
daysOfTheYear = try container.decode([Int].self, forKey: .daysOfTheYear)
months = try container.decode([Calendar.RecurrenceRule.Month].self, forKey: .months)
weeks = try container.decode([Int].self, forKey: .weeks)
setPositions = try container.decode([Int].self, forKey: .setPositions)
}

func encode(to encoder: any Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(calendar, forKey: .calendar)
try container.encode(matchingPolicy, forKey: .matchingPolicy)
try container.encode(repeatedTimePolicy, forKey: .repeatedTimePolicy)
try container.encode(frequency, forKey: .frequency)
try container.encode(interval, forKey: .interval)
try container.encode(end, forKey: .end)
try container.encode(seconds, forKey: .seconds)
try container.encode(minutes, forKey: .minutes)
try container.encode(hours, forKey: .hours)
try container.encode(weekdays, forKey: .weekdays)
try container.encode(daysOfTheMonth, forKey: .daysOfTheMonth)
try container.encode(daysOfTheYear, forKey: .daysOfTheYear)
try container.encode(months, forKey: .months)
try container.encode(weeks, forKey: .weeks)
try container.encode(setPositions, forKey: .setPositions)
}
}


extension ILSchedule.RecurrenceRule {
Expand Down
42 changes: 37 additions & 5 deletions Sources/SpeziScheduler/InfinityLoop/Schedule/ILSchedule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public struct ILSchedule {
/// We need a separate storage container as SwiftData cannot store values of type `Swift.Duration`.
private var scheduleDuration: Duration.SwiftDataDuration

private var recurrenceRule: RecurrenceRule?
private var recurrenceRule: Data?

/// The duration of a single occurrence.
public var duration: Duration {
Expand All @@ -74,13 +74,25 @@ public struct ILSchedule {
public var recurrence: Calendar.RecurrenceRule? {
@storageRestrictions(initializes: recurrenceRule)
init(initialValue) {
recurrenceRule = initialValue.map { RecurrenceRule(from: $0) }
do {
recurrenceRule = try initialValue.map { try PropertyListEncoder().encode($0) }
} catch {
preconditionFailure("Failed to encode initial value \(String(describing: initialValue)): \(error)")
}
}
get {
recurrenceRule.map { Calendar.RecurrenceRule(from: $0) }
do {
return try recurrenceRule.map { try PropertyListDecoder().decode(Calendar.RecurrenceRule.self, from: $0) }
} catch {
preconditionFailure("Failed to decode calendar from \(String(describing: recurrenceRule)): \(error)")
}
}
set {
recurrenceRule = newValue.map { RecurrenceRule(from: $0) }
do {
recurrenceRule = try newValue.map { try PropertyListEncoder().encode($0) }
} catch {
preconditionFailure("Failed to encode new value \(String(describing: newValue)): \(error)")
}
}
}

Expand Down Expand Up @@ -169,7 +181,27 @@ public struct ILSchedule {
}


extension ILSchedule: Equatable, Sendable, Codable {}
extension ILSchedule: Equatable, Sendable, Codable {
private enum CodingKeys: String, CodingKey {
case startDate
case scheduleDuration
case recurrenceRule
}

public init(from decoder: any Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
self.startDate = try container.decode(Date.self, forKey: .startDate)
self.scheduleDuration = try container.decode(ILSchedule.Duration.SwiftDataDuration.self, forKey: .scheduleDuration)
self.recurrenceRule = try container.decodeIfPresent(Data.self, forKey: .recurrenceRule)
}

public func encode(to encoder: any Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(startDate, forKey: .startDate)
try container.encode(scheduleDuration, forKey: .scheduleDuration)
try container.encode(recurrenceRule, forKey: .recurrenceRule)
}
}


extension ILSchedule { // TODO: examples for each? and the init
Expand Down

0 comments on commit 6d65471

Please sign in to comment.