Skip to content

Instantly share code, notes, and snippets.

@arthurpalves
Created June 30, 2020 15:32
Show Gist options
  • Save arthurpalves/b1c16bcb3a7667ee243a926120390eb0 to your computer and use it in GitHub Desktop.
Save arthurpalves/b1c16bcb3a7667ee243a926120390eb0 to your computer and use it in GitHub Desktop.
struct Provider: TimelineProvider {
public typealias Entry = AccountEntry
let viewModel = ViewModel()
public func snapshot(with context: Context, completion: @escaping (AccountEntry) -> ()) {
let entry = SimpleEntry(date: Date(), product: previewProduct, maskSensitiveData: false)
completion(entry)
}
public func timeline(with context: Context, completion: @escaping (Timeline<Entry>) -> ()) {
let currentDate = Date()
let refreshDate = Calendar.current.date(byAdding: .minute, value: 5, to: currentDate)!
viewModel.fetchProducts() { products in
guard let mainProduct = products.first(where: { $0.type == .main }) else { return }
let entry = AccountEntry(date: currentDate, product: mainProduct)
let timeline = Timeline(entries: [entry], policy: .after(refreshDate))
completion(timeline)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment