-
-
Save vladimir-bebeshko/4283c5fd23a252b183f45b036d8f6ca2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SwiftUI | |
import WidgetKit | |
struct WaterAppTimelineProvider: TimelineProvider { | |
func placeholder(in context: Self.Context) -> WaterAppWidgetEntry { | |
return .previewData | |
} | |
func getSnapshot(in context: Self.Context, completion: @escaping (WaterAppWidgetEntry) -> Void) { | |
completion(WaterAppWidgetEntry.previewData) | |
} | |
func getTimeline(in context: Context, completion: @escaping (Timeline<WaterAppWidgetEntry>) -> Void) { | |
let entry: WaterAppWidgetEntry = .init( | |
date: Date(), | |
text: "Hello, widget again") | |
let timeline: Timeline<WaterAppWidgetEntry> = .init( | |
entries: [entry], | |
policy: .never) | |
completion(timeline) | |
} | |
} | |
struct WaterAppWidgetEntry: TimelineEntry { | |
let date: Date | |
let text: String | |
static let previewData = WaterAppWidgetEntry( | |
date: Date(), | |
text: "Widget, hello world!") | |
} | |
struct WaterAppWidgetEntryView: View { | |
let entry: WaterAppWidgetEntry | |
var body: some View { | |
ZStack { | |
Color("BackgroundColor") | |
Text(entry.text) | |
.padding() | |
} | |
} | |
} | |
@main | |
struct WaterAppWidget: Widget { | |
let kind = "WaterAppWidget" | |
var body: some WidgetConfiguration { | |
StaticConfiguration( | |
kind: kind, | |
provider: WaterAppTimelineProvider()) { entry in | |
WaterAppWidgetEntryView(entry: entry) | |
} | |
.configurationDisplayName("your_water_results") | |
.description("your_water_results_description") | |
.supportedFamilies([.systemSmall, .systemMedium]) | |
} | |
} | |
struct WaterAppWidget_Previews: PreviewProvider { | |
static var previews: some View { | |
WaterAppWidgetEntryView(entry: .previewData) | |
.previewContext(WidgetPreviewContext(family: .systemSmall)) | |
WaterAppWidgetEntryView(entry: .previewData) | |
.previewContext(WidgetPreviewContext(family: .systemMedium)) | |
WaterAppWidgetEntryView(entry: .previewData) | |
.previewContext(WidgetPreviewContext(family: .systemSmall)) | |
.redacted(reason: .placeholder) | |
WaterAppWidgetEntryView(entry: .previewData) | |
.previewContext(WidgetPreviewContext(family: .systemMedium)) | |
.environment(\.colorScheme, .dark) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment