Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
struct JokeProvider: TimelineProvider {
public func snapshot(with context: Context, completion: @escaping (JokesEntry) -> ()) {
let entry = JokesEntry(date: Date(), joke: "...")
completion(entry)
}
public func timeline(with context: Context,
completion: @escaping (Timeline<Entry>) -> ()) {
DataFetcher.shared.getJokes{
response in
let date = Date()
let calendar = Calendar.current
let entries = response?.enumerated().map { offset, currentJoke in
JokesEntry(date: calendar.date(byAdding: .second, value: offset*2, to: date)!, joke: currentJoke.joke ?? "...")
}
let timeLine = Timeline(entries: entries ?? [], policy: .atEnd)
completion(timeLine)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment