Skip to content

Commit

Permalink
Updated Dynamic Intent Widget
Browse files Browse the repository at this point in the history
  • Loading branch information
pawello2222 committed Nov 3, 2020
1 parent 6dd48b3 commit 6f987cb
Show file tree
Hide file tree
Showing 10 changed files with 87 additions and 46 deletions.
File renamed without changes.
4 changes: 0 additions & 4 deletions IntentHandler/IntentHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,4 @@ class IntentHandler: INExtension, DynamicPersonSelectionIntentHandling {
let collection = INObjectCollection(items: persons)
completion(collection, nil)
}

override func handler(for intent: INIntent) -> Any {
return self
}
}
22 changes: 13 additions & 9 deletions Shared/Model/Contact.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@

import Foundation

struct Contact {
let name: String
let birthday: Date
struct Contact: Hashable, Codable {
var name: String
var dateOfBirth: Date
}

extension Contact: Identifiable {
Expand All @@ -20,18 +20,22 @@ extension Contact: Identifiable {
extension Contact {
static func getAll() -> [Contact] {
let key = UserDefaults.Keys.contacts.rawValue
let contacts = UserDefaults.appGroup.object(forKey: key) as? [Contact]
return contacts ?? [.friend1, .friend2]
guard let contacts: [Contact] = UserDefaults.appGroup.getArray(forKey: key) else {
let contacts: [Contact] = [.friend1, .friend2]
UserDefaults.appGroup.setArray(contacts, forKey: key)
return contacts
}
return contacts
}

static let friend1: Contact = {
let birthday = Calendar.current.date(byAdding: .day, value: 1, to: Date())!
return Contact(name: "Friend 1", birthday: birthday)
let date = Calendar.current.date(byAdding: .month, value: -2, to: Date())!
return Contact(name: "Friend 1", dateOfBirth: date)
}()

static let friend2: Contact = {
let birthday = Calendar.current.date(byAdding: .day, value: 3, to: Date())!
return Contact(name: "Friend 2", birthday: birthday)
let date = Calendar.current.date(byAdding: .year, value: -3, to: Date())!
return Contact(name: "Friend 2", dateOfBirth: date)
}()
}

Expand Down
12 changes: 12 additions & 0 deletions Shared/UserDefaults+Ext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,15 @@ extension UserDefaults {
case contacts
}
}

extension UserDefaults {
func setArray<Element>(_ array: [Element], forKey key: String) where Element: Encodable {
let data = try? JSONEncoder().encode(array)
set(data, forKey: key)
}

func getArray<Element>(forKey key: String) -> [Element]? where Element: Decodable {
guard let data = data(forKey: key) else { return nil }
return try? JSONDecoder().decode([Element].self, from: data)
}
}
10 changes: 7 additions & 3 deletions WidgetExamples.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@
E55497D42551C02E0066870B /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
E55497EB2551CA0B0066870B /* DynamicPersonSelection.intentdefinition */ = {isa = PBXFileReference; lastKnownFileType = file.intentdefinition; path = DynamicPersonSelection.intentdefinition; sourceTree = "<group>"; };
E55498372551D2160066870B /* Contact.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Contact.swift; sourceTree = "<group>"; };
E554985A25521B8D0066870B /* IntentHandler.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = IntentHandler.entitlements; sourceTree = "<group>"; };
E591C5712551666A002044C8 /* IntentWidget.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = IntentWidget.swift; sourceTree = "<group>"; };
E591C57825516810002044C8 /* BackgroundColorSelection.intentdefinition */ = {isa = PBXFileReference; lastKnownFileType = file.intentdefinition; path = BackgroundColorSelection.intentdefinition; sourceTree = "<group>"; };
/* End PBXFileReference section */
Expand Down Expand Up @@ -162,7 +163,6 @@
E542134D253879B700CCC9C3 = {
isa = PBXGroup;
children = (
E54213CF253892AB00CCC9C3 /* WidgetExamplesWidgetExtension.entitlements */,
E5421358253879B700CCC9C3 /* WidgetExamples */,
E542137325387A0300CCC9C3 /* WidgetExamplesWidget */,
E55497D12551C02E0066870B /* IntentHandler */,
Expand Down Expand Up @@ -213,6 +213,7 @@
E542137325387A0300CCC9C3 /* WidgetExamplesWidget */ = {
isa = PBXGroup;
children = (
E54213CF253892AB00CCC9C3 /* WidgetExamplesWidgetExtension.entitlements */,
E542137425387A0300CCC9C3 /* WidgetExamplesWidget.swift */,
E54213C7253891CB00CCC9C3 /* AppGroupWidget */,
E542139725387ECE00CCC9C3 /* ClockWidget */,
Expand Down Expand Up @@ -367,6 +368,7 @@
E55497D12551C02E0066870B /* IntentHandler */ = {
isa = PBXGroup;
children = (
E554985A25521B8D0066870B /* IntentHandler.entitlements */,
E55497D22551C02E0066870B /* IntentHandler.swift */,
E55497D42551C02E0066870B /* Info.plist */,
);
Expand Down Expand Up @@ -762,7 +764,7 @@
buildSettings = {
ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor;
ASSETCATALOG_COMPILER_WIDGET_BACKGROUND_COLOR_NAME = WidgetBackground;
CODE_SIGN_ENTITLEMENTS = WidgetExamplesWidgetExtension.entitlements;
CODE_SIGN_ENTITLEMENTS = WidgetExamplesWidget/WidgetExamplesWidgetExtension.entitlements;
CODE_SIGN_STYLE = Automatic;
DEVELOPMENT_TEAM = DMCVYHA293;
INFOPLIST_FILE = WidgetExamplesWidget/Resources/Info.plist;
Expand All @@ -784,7 +786,7 @@
buildSettings = {
ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor;
ASSETCATALOG_COMPILER_WIDGET_BACKGROUND_COLOR_NAME = WidgetBackground;
CODE_SIGN_ENTITLEMENTS = WidgetExamplesWidgetExtension.entitlements;
CODE_SIGN_ENTITLEMENTS = WidgetExamplesWidget/WidgetExamplesWidgetExtension.entitlements;
CODE_SIGN_STYLE = Automatic;
DEVELOPMENT_TEAM = DMCVYHA293;
INFOPLIST_FILE = WidgetExamplesWidget/Resources/Info.plist;
Expand All @@ -804,6 +806,7 @@
E55497D92551C02E0066870B /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
CODE_SIGN_ENTITLEMENTS = IntentHandler/IntentHandler.entitlements;
CODE_SIGN_STYLE = Automatic;
DEVELOPMENT_TEAM = DMCVYHA293;
INFOPLIST_FILE = IntentHandler/Info.plist;
Expand All @@ -824,6 +827,7 @@
E55497DA2551C02E0066870B /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
CODE_SIGN_ENTITLEMENTS = IntentHandler/IntentHandler.entitlements;
CODE_SIGN_STYLE = Automatic;
DEVELOPMENT_TEAM = DMCVYHA293;
INFOPLIST_FILE = IntentHandler/Info.plist;
Expand Down
37 changes: 36 additions & 1 deletion WidgetExamples/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,23 @@ struct ContentView: View {

@AppStorage(UserDefaults.Keys.luckyNumber.rawValue, store: UserDefaults.appGroup) private var luckyNumber = 0

@State private var contacts = Contact.getAll()

@FetchRequest(entity: Item.entity(), sortDescriptors: []) private var items: FetchedResults<Item>

var body: some View {
List {
appGroupWidgetSection
coreDataWidgetSection
deepLinkWidgetSection
dynamicIntentWidgetSection
previewWidgetSection
}
.listStyle(InsetGroupedListStyle())
}
}

private extension ContentView {
var appGroupWidgetSection: some View {
Section(header: Text("AppGroup Widget")) {
Text("Lucky number: \(luckyNumber)")
Expand All @@ -42,9 +47,11 @@ struct ContentView: View {
try? String(luckyNumber).write(to: url, atomically: false, encoding: .utf8)
}
}
}

private extension ContentView {
var coreDataWidgetSection: some View {
return Section(header: Text("CoreData Widget")) {
Section(header: Text("CoreData Widget")) {
Text("Items count: \(items.count)")
Button("Add new item") {
let context = CoreDataStack.shared.workingContext
Expand Down Expand Up @@ -74,7 +81,9 @@ struct ContentView: View {
try? String(luckyNumber).write(to: url, atomically: false, encoding: .utf8)
}
}
}

private extension ContentView {
var deepLinkWidgetSection: some View {
Section(header: Text("DeepLink Widget")) {
Text("")
Expand All @@ -86,7 +95,33 @@ struct ContentView: View {
}
}
}
}

private extension ContentView {
var dynamicIntentWidgetSection: some View {
Section(header: Text("Dynamic Intent Widget")) {
ForEach(contacts.indices, id: \.self) { index in
HStack {
TextField("", text: $contacts[index].name, onCommit: {
saveContacts()
})
DatePicker("", selection: $contacts[index].dateOfBirth, displayedComponents: .date)
.onChange(of: contacts[index].dateOfBirth) { _ in
saveContacts()
}
}
}
}
}

func saveContacts() {
let key = UserDefaults.Keys.contacts.rawValue
UserDefaults.appGroup.setArray(contacts, forKey: key)
WidgetCenter.shared.reloadTimelines(ofKind: "DynamicIntentWidget")
}
}

private extension ContentView {
var previewWidgetSection: some View {
let entry = PreviewWidgetEntry(date: Date(), systemImageName: "star.fill")
return Section(header: Text("Preview Widget")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ import WidgetKit

private struct Provider: IntentTimelineProvider {
func placeholder(in context: Context) -> SimpleEntry {
SimpleEntry(date: Date())
SimpleEntry(date: Date(), contact: .friend1)
}

func getSnapshot(for configuration: DynamicPersonSelectionIntent, in context: Context, completion: @escaping (SimpleEntry) -> Void) {
let entry = SimpleEntry(date: Date())
let entry = SimpleEntry(date: Date(), contact: .friend1)
completion(entry)
}

Expand All @@ -35,19 +35,19 @@ private struct Provider: IntentTimelineProvider {

private struct SimpleEntry: TimelineEntry {
let date: Date
var contact: Contact = .friend1
let contact: Contact
}

private struct DynamicIntentWidgetEntryView: View {
var entry: Provider.Entry

var body: some View {
VStack {
Text(String(describing: entry.contact.name))
Text(entry.contact.name)
.fontWeight(.semibold)
Group {
Text("Born on:")
Text(entry.contact.birthday, style: .date)
Text("Date of birth:")
Text(entry.contact.dateOfBirth, style: .date)
.multilineTextAlignment(.center)
}
.font(.footnote)
Expand All @@ -64,7 +64,7 @@ struct DynamicIntentWidget: Widget {
DynamicIntentWidgetEntryView(entry: entry)
}
.configurationDisplayName("Dynamic Intent Widget")
.description("A dynamically configurable Widget that displays the remaining time.")
.description("A Widget that has dynamically configurable data.")
.supportedFamilies([.systemSmall])
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,26 +62,6 @@
<key>INIntentParameterPromptDialogType</key>
<string>Primary</string>
</dict>
<dict>
<key>INIntentParameterPromptDialogCustom</key>
<true/>
<key>INIntentParameterPromptDialogFormatString</key>
<string>There are ${count} options matching ‘${person}’.</string>
<key>INIntentParameterPromptDialogFormatStringID</key>
<string>MJwysV</string>
<key>INIntentParameterPromptDialogType</key>
<string>DisambiguationIntroduction</string>
</dict>
<dict>
<key>INIntentParameterPromptDialogCustom</key>
<true/>
<key>INIntentParameterPromptDialogFormatString</key>
<string>Just to confirm, you wanted ‘${person}’?</string>
<key>INIntentParameterPromptDialogFormatStringID</key>
<string>zrGV1L</string>
<key>INIntentParameterPromptDialogType</key>
<string>Confirmation</string>
</dict>
</array>
<key>INIntentParameterSupportsDynamicEnumeration</key>
<true/>
Expand Down
4 changes: 2 additions & 2 deletions WidgetExamplesWidget/WidgetExamplesWidget.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import WidgetKit
@main
struct WidgetExamplesWidgetBundle: WidgetBundle {
var body: some Widget {
WidgetBundle1().body
WidgetBundle2().body
// WidgetBundle1().body
// WidgetBundle2().body
WidgetBundle3().body
}
}
Expand Down
10 changes: 10 additions & 0 deletions WidgetExamplesWidget/WidgetExamplesWidgetExtension.entitlements
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>com.apple.security.application-groups</key>
<array>
<string>group.com.pawelwiszenko.WidgetExamples</string>
</array>
</dict>
</plist>

0 comments on commit 6f987cb

Please sign in to comment.