Skip to content

Instantly share code, notes, and snippets.

@anupamchugh
Last active July 1, 2020 12:53
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 anupamchugh/bc07154c3dd42805176359723eaa7398 to your computer and use it in GitHub Desktop.
Save anupamchugh/bc07154c3dd42805176359723eaa7398 to your computer and use it in GitHub Desktop.
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