Skip to content

Instantly share code, notes, and snippets.

View arthurpalves's full-sized avatar
👨‍💻

Arthur Alves arthurpalves

👨‍💻
View GitHub Profile
ZStack(alignment: .topLeading) {
RoundedRectangle(cornerRadius: 0, style: .continuous)
.fill(Color("widgetBackgroundColor"))
HStack(alignment: .center, spacing: 8) {
RoundedRectangle(cornerRadius: 3, style: .continuous)
.fill(LinearGradient(
gradient: .init(colors: [.blue, .green]),
startPoint: .init(x: 0, y: 0),
endPoint: .init(x: 1, y: 1)
struct Previews: PreviewProvider {
static var previews: some View {
Group {
MainAccountEntryView(entry: previewEntry)
.previewContext(WidgetPreviewContext(family: .systemSmall))
PlaceholderView()
.previewContext(WidgetPreviewContext(family: .systemSmall))
}
}
struct MainAccountEntryView : View {
var entry: Provider.Entry
var body: some View {
VStack(alignment: .leading, spacing: 2) {
Text(entry.product.name)
Text(entry.product.iban)
Text(entry.product.formattedAmount)
})
}
struct PlaceholderView : View {
var body: some View {
Text("Placeholder View")
}
}
struct MainAccountEntryView : View {
var entry: Provider.Entry
var body: some View {
@main
struct MainAccount: Widget {
private let kind: String = "MainAccount"
public var body: some WidgetConfiguration {
StaticConfiguration(kind: kind, provider: Provider(), placeholder: PlaceholderView()) { entry in
MainAccountEntryView(entry: entry)
}
.configurationDisplayName("Main Account")
.description("See information about your main account.")
public func timeline(with context: Context, completion: @escaping (Timeline<Entry>) -> ()) {
let currentDate = Date()
let futureDate = Calendar.current.date(byAdding: .minute, value: 10, 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)
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)
}
let currentDate = Date()
let futureDate = Calendar.current.date(byAdding: .minute, value: 10, to: currentDate)!
public func timeline(with context: Context, completion: @escaping (Timeline<Entry>) -> ()) {
var entries: [AccountEntry] = []
/* ... */
let timeline = Timeline(entries: entries, policy: .atEnd)
completion(timeline)
}
public func snapshot(with context: Context, completion: @escaping (AccountEntry) -> ()) {
let previewProduct = Product(
id: "123a67cf",
title: "Main Account",
amount: 280.25,
iban: "NL27AAA0726252510",
type: .main
)
let entry = AccountEntry(date: Date(), product: previewProduct)
completion(entry)