Skip to content

Instantly share code, notes, and snippets.

@IhwanID
Created January 13, 2021 09:26
Show Gist options
  • Save IhwanID/d163d903220227346598d546612086af to your computer and use it in GitHub Desktop.
Save IhwanID/d163d903220227346598d546612086af to your computer and use it in GitHub Desktop.
Daily Motivation WidgetKit SwiftUI
//
// MotivasiHarianWidget.swift
// MotivasiHarianWidget
//
// Created by Ihwan ID on 13/01/21.
//
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>) -> ()) {
let entries: [SimpleEntry] = [SimpleEntry(date: Date(), configuration: configuration)]
let currentDate = Date()
let entryDate = Calendar.current.date(byAdding: .minute, value: 1, to: currentDate)!
let timeline = Timeline(entries: entries, policy: .after(entryDate))
completion(timeline)
}
}
struct SimpleEntry: TimelineEntry {
let date: Date
let motivations = ["Kalau uang bisa membuatku melupakan sahabat terbaikku, maka aku lebih memilih untuk tidak punya uang sama sekali. -Spongebob", "Kalau kamu memberitahukan rahasia kepada seseorang, maka itu namanya bukan rahasia lagi. -Patrick", "Pemujaan yang berlebihan itu tidak sehat.. -Patrick" ]
let configuration: ConfigurationIntent
}
struct MotivasiHarianWidgetEntryView : View {
var entry: Provider.Entry
var body: some View {
Text(entry.motivations.randomElement()!).padding()
}
}
@main
struct MotivasiHarianWidget: Widget {
let kind: String = "widget"
var body: some WidgetConfiguration {
IntentConfiguration(kind: kind, intent: ConfigurationIntent.self, provider: Provider()) { entry in
MotivasiHarianWidgetEntryView(entry: entry)
}
.configurationDisplayName("Motivasi Harian")
.description("Widget Motivasi Harian untuk menambah Semangatmu")
.supportedFamilies([.systemMedium]) }
}
struct MotivasiHarianWidget_Previews: PreviewProvider {
static var previews: some View {
MotivasiHarianWidgetEntryView(entry: SimpleEntry(date: Date(), configuration: ConfigurationIntent()))
.previewContext(WidgetPreviewContext(family: .systemSmall))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment