Skip to content

Instantly share code, notes, and snippets.

@ribeiroevandro
Created January 6, 2023 18:15
Show Gist options
  • Save ribeiroevandro/012b95996ff60398186795eb2363425b to your computer and use it in GitHub Desktop.
Save ribeiroevandro/012b95996ff60398186795eb2363425b to your computer and use it in GitHub Desktop.
//
// PerfectWidget.swift
// PerfectWidget
//
// Created by Evandro Ribeiro on 04/01/23.
//
import WidgetKit
import SwiftUI
private let widgetGroupId = "SET_WIDGET_GROUD_ID"
struct Provider: TimelineProvider {
func placeholder(in context: Context) -> DashboardEntry {
DashboardEntry(date: Date(), today: "Hoje", yesterday: "Ontem", thisMonth: "Esse mês", lastMonth: "Mês passado")
}
func getSnapshot(in context: Context, completion: @escaping (DashboardEntry) -> ()) {
let data = UserDefaults.init(suiteName: widgetGroupId)
let today = data?.string(forKey: "_dashboardDataToday") ?? "R$ 0,00"
let yesterday = data?.string(forKey: "_dashboardDataYesterday") ?? "R$ 0,00"
let thisMonth = data?.string(forKey: "_dashboardDataThisMonth") ?? "R$ 0,00"
let lastMonth = data?.string(forKey: "_dashboardDataLastMonth") ?? "R$ 0,00"
let entry = DashboardEntry(date: Date(), today: today, yesterday: yesterday, thisMonth: thisMonth, lastMonth: lastMonth)
completion(entry)
}
func getTimeline(in context: Context, completion: @escaping (Timeline<Entry>) -> ()) {
getSnapshot(in: context) { (entry) in
let currentDate = Date()
let refreshDate = Calendar.current.date(byAdding: .minute, value: 15, to: currentDate)!
let timeline = Timeline(entries: [entry], policy: .after(refreshDate))
completion(timeline)
}
}
}
struct DashboardEntry: TimelineEntry {
let date: Date
let today: String
let yesterday: String
let thisMonth: String
let lastMonth: String
}
struct PerfectWidgetEntryView : View {
var entry: Provider.Entry
let data = UserDefaults.init(suiteName: widgetGroupId)
var body: some View {
VStack.init(spacing: 6, content: {
HStack.init( spacing: 10, content: {
Image("logo").resizable() .aspectRatio(contentMode: .fit)
.frame(width: 25, height: 25, alignment: .center)
Text("Perfect Pay").font(/*@START_MENU_TOKEN@*/.body/*@END_MENU_TOKEN@*/).bold()
}
)
HStack.init( spacing: 10, content: {
Text("Hoje").font(/*@START_MENU_TOKEN@*/.subheadline/*@END_MENU_TOKEN@*/)
Spacer()
Text(entry.today).font(/*@START_MENU_TOKEN@*/.body/*@END_MENU_TOKEN@*/).bold()
}
)
HStack.init(spacing: 10, content: {
Text("Ontem").font(/*@START_MENU_TOKEN@*/.subheadline/*@END_MENU_TOKEN@*/)
Spacer()
Text(entry.yesterday).font(/*@START_MENU_TOKEN@*/.body/*@END_MENU_TOKEN@*/).bold()
}
)
HStack.init(spacing: 10, content: {
Text("Esse mês").font(/*@START_MENU_TOKEN@*/.subheadline/*@END_MENU_TOKEN@*/)
Spacer()
Text(entry.thisMonth).font(/*@START_MENU_TOKEN@*/.body/*@END_MENU_TOKEN@*/).bold()
}
)
HStack.init(spacing: 10, content: {
Text("Mês passado").font(/*@START_MENU_TOKEN@*/.subheadline/*@END_MENU_TOKEN@*/)
Spacer()
Text(entry.lastMonth).font(/*@START_MENU_TOKEN@*/.body/*@END_MENU_TOKEN@*/).bold()
}
)
}
).padding(20)
}
}
struct PerfectWidget: Widget {
let kind: String = "PerfectWidget"
var body: some WidgetConfiguration {
StaticConfiguration(kind: kind, provider: Provider()) { entry in
PerfectWidgetEntryView(entry: entry)
}
.configurationDisplayName("Perfect Pay")
.description("Tenha todos os relatórios na palma da sua mão!")
.supportedFamilies([.systemMedium])
}
}
struct PerfectWidget_Previews: PreviewProvider {
static var previews: some View {
PerfectWidgetEntryView(entry: DashboardEntry(date: Date(), today: "R$ 0,00", yesterday: "R$ 0,00", thisMonth: "R$ 0,00", lastMonth: "R$ 0,00"))
.previewContext(WidgetPreviewContext(family: .systemMedium))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment