Skip to content

Instantly share code, notes, and snippets.

@EthanRDoesMC
Created September 29, 2020 03:43
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 EthanRDoesMC/64cad62ec5e681fe9c76646eb67f1095 to your computer and use it in GitHub Desktop.
Save EthanRDoesMC/64cad62ec5e681fe9c76646eb67f1095 to your computer and use it in GitHub Desktop.
SwiftUI gradients are strange things
//
// widget.swift
// widget
//
// Created by EthanRDoesMC on 9/27/20.
//
// i left in a lot of example code
// it doesn't make me a bad programmer
// what makes me a bad programmer is my ✨🌟p e r s o n a l i t y🌟✨
import WidgetKit
import SwiftUI
import Intents
struct Provider: IntentTimelineProvider {
func placeholder(in context: Context) -> SimpleEntry {
SimpleEntry(date: Date(), configuration: ConfigurationIntent())
}
func getSnapshot(for configuration: ConfigurationIntent, in context: Context, completion: @escaping (SimpleEntry) -> ()) {
let entry = SimpleEntry(date: Date(), configuration: configuration)
completion(entry)
}
func getTimeline(for configuration: ConfigurationIntent, in context: Context, completion: @escaping (Timeline<Entry>) -> ()) {
var entries: [SimpleEntry] = []
// Generate a timeline consisting of five entries an hour apart, starting from the current date.
let currentDate = Date()
for hourOffset in 0 ..< 5 {
let entryDate = Calendar.current.date(byAdding: .hour, value: hourOffset, to: currentDate)!
let entry = SimpleEntry(date: entryDate, configuration: configuration)
entries.append(entry)
}
let timeline = Timeline(entries: entries, policy: .atEnd)
completion(timeline)
}
}
struct SimpleEntry: TimelineEntry {
let date: Date
let configuration: ConfigurationIntent
}
struct widgetEntryView : View {
@Environment(\.widgetFamily) var family: WidgetFamily
var entry: Provider.Entry
var gradient = LinearGradient(gradient: Gradient(colors: [Color(.systemBackground), Color(.quaternaryLabel), Color(.secondaryLabel)]), startPoint: .top, endPoint: .bottom)
//var gradientTest = LinearGradient(gradient: Gradient(colors: [.pink, .red, Color(.cyan)]), startPoint: .top, endPoint: .bottom)
@ViewBuilder
var body: some View {
switch family {
//Text(entry.date, style: .time)
case .systemSmall: HStack(spacing: 0.0) {
VStack(alignment: .trailing) {
Text("💧: don't forget to take some time to drink a bit of water please")
.font(.system(.footnote, design: .rounded))
.fontWeight(.semibold)
.multilineTextAlignment(.leading)
.foregroundColor(Color(.label))
.padding(.top)
Text("@tinycarebot")
.font(.system(.headline, design: .rounded))
.fontWeight(.light)
.foregroundColor(Color(.secondaryLabel))
.multilineTextAlignment(.trailing)
.padding(.top)
Spacer(minLength: 0)
}
Spacer(minLength: 0)
}
.padding(.all, 8.0)
//.padding(.all)
.background(ContainerRelativeShape().fill(gradient).scaledToFill())
case .systemMedium: HStack(spacing: 0.0) {
VStack(alignment: .trailing) {
Spacer(minLength: 0)
Text("💧: don't forget to take some time to drink a bit of water please")
.font(.system(.headline, design: .rounded))
.fontWeight(.semibold)
.multilineTextAlignment(.leading)
.padding(.horizontal)
.foregroundColor(Color(.label))
Text("@tinycarebot")
.font(.system(.title3, design: .rounded))
.fontWeight(.light)
.foregroundColor(Color(.secondaryLabel))
.multilineTextAlignment(.trailing)
.padding([.top, .trailing])
Spacer(minLength: 0)
}
Spacer(minLength: 0)
}
//.frame(maxWidth: .infinity, maxHeight: .infinity)
//.padding(.all)
.frame(maxWidth: .infinity, maxHeight: .infinity)
.edgesIgnoringSafeArea(.all)
.background(ContainerRelativeShape().fill(gradient).scaledToFill())
case .systemLarge: HStack {
VStack(alignment: .trailing) {
Spacer(minLength: 0)
Text("💧: don't forget to take some time to drink a bit of water please")
.font(.system(.title, design: .rounded))
.fontWeight(.regular)
.multilineTextAlignment(.leading)
.foregroundColor(Color(.label))
//Spacer(minLength: 0)
Text("@tinycarebot")
.font(.system(.title3, design: .rounded))
.fontWeight(.light)
.foregroundColor(Color(.secondaryLabel))
.multilineTextAlignment(.trailing)
Text("")
Spacer(minLength: 0)
}
Spacer(minLength: 0)
}
.padding(.all)
.background(ContainerRelativeShape().fill(gradient).scaledToFill())
default: HStack {
VStack(alignment: .trailing) {
Text("💧: don't forget to take some time to drink a bit of water please")
.font(.footnote)
.fontWeight(.semibold)
.multilineTextAlignment(.leading)
.foregroundColor(Color(.label))
Text("@tinycarebot")
.font(.headline)
.fontWeight(.light)
.foregroundColor(Color(.secondaryLabel))
.multilineTextAlignment(.trailing)
.padding(.top)
}
Spacer(minLength: 0)
}
.padding(.all)
.background(ContainerRelativeShape().fill(gradient).scaledToFill())
}
}
@main
struct widget: Widget {
let kind: String = "widget"
var body: some WidgetConfiguration {
IntentConfiguration(kind: kind, intent: ConfigurationIntent.self, provider: Provider()) { entry in
widgetEntryView(entry: entry)
}
.configurationDisplayName("My Widget")
.description("This is an example widget.")
}
}
struct widget_Previews: PreviewProvider {
static var previews: some View {
widgetEntryView(entry: SimpleEntry(date: Date(), configuration: ConfigurationIntent()))
.previewContext(WidgetPreviewContext(family: .systemSmall))
widgetEntryView(entry: SimpleEntry(date: Date(), configuration: ConfigurationIntent()))
.previewContext(WidgetPreviewContext(family: .systemMedium))
widgetEntryView(entry: SimpleEntry(date: Date(), configuration: ConfigurationIntent()))
.previewContext(WidgetPreviewContext(family: .systemLarge))
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment