Last active
July 30, 2020 04:48
-
-
Save apatronl/957501ba0a304635ff0fcd6fff18e73c to your computer and use it in GitHub Desktop.
This file contains hidden or 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 CustomEmojiWidgetProvider: IntentTimelineProvider { | |
func snapshot( | |
for configuration: SelectEmojiIntent, | |
with context: Context, | |
completion: @escaping (CustomEmojiEntry) -> () | |
) { | |
// Use a random emoji in the widget gallery. | |
if context.isPreview { | |
completion(CustomEmojiEntry(date: Date(), emojiDetails: EmojiProvider.random())) | |
return | |
} | |
let emojiDetails = lookupEmojiDetails(for: configuration) | |
let entry = CustomEmojiEntry(date: Date(), emojiDetails: emojiDetails) | |
completion(entry) | |
} | |
public func timeline( | |
for configuration: SelectEmojiIntent, | |
with context: Context, | |
completion: @escaping (Timeline<CustomEmojiEntry>) -> () | |
) { | |
var entries = [CustomEmojiEntry]() | |
let emojiDetails = lookupEmojiDetails(for: configuration) | |
let entry = CustomEmojiEntry(date: Date(), emojiDetails: emojiDetails) | |
entries.append(entry) | |
let timeline = Timeline(entries: entries, policy: .never) | |
completion(timeline) | |
} | |
private func lookupEmojiDetails(for configuration: SelectEmojiIntent) -> EmojiDetails { | |
guard let emojiId = configuration.emoji?.identifier, | |
let emojiForConfig = EmojiProvider.all().first(where: { emoji in | |
emoji.id == emojiId | |
}) | |
else { | |
return EmojiProvider.random() | |
} | |
return emojiForConfig | |
} | |
} | |
struct CustomEmojiEntry: TimelineEntry { | |
public let date: Date | |
public let emojiDetails: EmojiDetails | |
} | |
struct CustomEmojiWidgetEntryView : View { | |
var entry: CustomEmojiWidgetProvider.Entry | |
var body: some View { | |
EmojiWidgetView(emojiDetails: entry.emojiDetails) | |
} | |
} | |
struct CustomEmojiWidget: Widget { | |
private let kind: String = "CustomEmojiWidget" | |
public var body: some WidgetConfiguration { | |
IntentConfiguration( | |
kind: kind, | |
intent: SelectEmojiIntent.self, | |
provider: CustomEmojiWidgetProvider(), | |
placeholder: EmojiWidgetPlaceholderView() | |
) { entry in | |
CustomEmojiWidgetEntryView(entry: entry) | |
} | |
.configurationDisplayName("Custom Emoji Widget") | |
.description("Display a widget with an emoji of your choice.") | |
.supportedFamilies([.systemSmall, .systemMedium, .systemLarge]) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment