diff --git a/Switch WatchKit App/Assets.xcassets/AppIcon.appiconset/Contents.json b/Switch WatchKit App/Assets.xcassets/AppIcon.appiconset/Contents.json index 6c0f2b4..1b164f3 100644 --- a/Switch WatchKit App/Assets.xcassets/AppIcon.appiconset/Contents.json +++ b/Switch WatchKit App/Assets.xcassets/AppIcon.appiconset/Contents.json @@ -36,6 +36,7 @@ { "size" : "44x44", "idiom" : "watch", + "filename" : "Switch.png", "scale" : "2x", "role" : "appLauncher", "subtype" : "40mm" diff --git a/Switch WatchKit App/Assets.xcassets/AppIcon.appiconset/Switch.png b/Switch WatchKit App/Assets.xcassets/AppIcon.appiconset/Switch.png new file mode 100644 index 0000000..ef267be Binary files /dev/null and b/Switch WatchKit App/Assets.xcassets/AppIcon.appiconset/Switch.png differ diff --git a/Switch WatchKit Extension/ComplicationController.swift b/Switch WatchKit Extension/ComplicationController.swift index 4d5dff9..cdd1360 100644 --- a/Switch WatchKit Extension/ComplicationController.swift +++ b/Switch WatchKit Extension/ComplicationController.swift @@ -28,12 +28,14 @@ class ComplicationController: NSObject, CLKComplicationDataSource { // MARK: - Timeline Population func getCurrentTimelineEntry(for complication: CLKComplication, withHandler handler: @escaping (CLKComplicationTimelineEntry?) -> Void) { + NSLog("get current timeline entry") + // Call the handler with the current timeline entry if (complication.family == .graphicCorner) { - handler(getEntryForGraphicCorner()) - } - - handler(nil) + getFront() { front in + handler(self.getEntryForGraphicCorner(front)) + } + } else { handler(nil) } } func getTimelineEntries(for complication: CLKComplication, before date: Date, limit: Int, withHandler handler: @escaping ([CLKComplicationTimelineEntry]?) -> Void) { @@ -47,17 +49,16 @@ class ComplicationController: NSObject, CLKComplicationDataSource { } func getLocalizableSampleTemplate(for complication: CLKComplication, withHandler handler: @escaping (CLKComplicationTemplate?) -> Void) { + NSLog("get sample template") // This method will be called once per supported complication, and the results will be cached if (complication.family == .graphicCorner) { - handler(getTemplateForGraphicCorner("Example")) - } - - handler(nil) + handler(self.getTemplateForGraphicCorner("Example")) + } else { handler(nil) } } - func getEntryForGraphicCorner() -> CLKComplicationTimelineEntry { - let gc = getTemplateForGraphicCorner(getFront()) + func getEntryForGraphicCorner(_ front: String) -> CLKComplicationTimelineEntry { + let gc = getTemplateForGraphicCorner(front) return CLKComplicationTimelineEntry(date: Date(), complicationTemplate: gc) } diff --git a/Switch WatchKit Extension/ContentView.swift b/Switch WatchKit Extension/ContentView.swift index f2296e9..a43a84f 100644 --- a/Switch WatchKit Extension/ContentView.swift +++ b/Switch WatchKit Extension/ContentView.swift @@ -34,8 +34,9 @@ struct SwitchButton: View { var parent: ContentView var body: some View { Button(action: { - setFront(self.who) - self.parent.front = self.who + setFront(self.who) {front in + self.parent.front = front + } }) { Text(self.who) } @@ -43,15 +44,30 @@ struct SwitchButton: View { } struct ContentView: View { - @State var front = getFront() + @State var front: String = "" + func refresh() { - self.front = getFront() + getFront() { front in + self.front = front + } reloadComplications() + NSLog("reloaded...") } var body: some View { ScrollView{ - Text(self.front + " is front") + HStack{ + if self.front != "" { + Text(self.front + " is front") + } + Spacer() + Button(action: { + self.refresh() + }) { + Text("🔄") + }.fixedSize() + + } if self.front != "Cadey" { SwitchButton(who: "Cadey", parent: self) } @@ -70,12 +86,9 @@ struct ContentView: View { if self.front != "Mai" { SwitchButton(who: "Mai", parent: self) } - Button(action: { - self.refresh() - }) { - Text("🔄") - } - } + }.onAppear() { + self.refresh() + }.navigationBarTitle("Switch") } } diff --git a/Switch WatchKit Extension/Switch.swift b/Switch WatchKit Extension/Switch.swift index d96d525..c40d5c9 100644 --- a/Switch WatchKit Extension/Switch.swift +++ b/Switch WatchKit Extension/Switch.swift @@ -8,17 +8,30 @@ import Foundation +func getFront(_ callback: @escaping (String) -> Void) { + let task = URLSession.shared.dataTask(with: url) { data, resp, error in + guard data != nil else { return } + + let frontString = String(decoding: data!, as: UTF8.self) + callback(frontString) + } + task.resume() +} + func getFront() -> String { let data = try! Data(contentsOf: url) return String(decoding: data, as: UTF8.self) } -func setFront(_ to: String) { +func setFront(_ to: String, _ callback: @escaping (String) -> Void) { var request = URLRequest(url: url) request.httpMethod = "POST" request.httpBody = to.data(using: .utf8) let task = URLSession.shared.dataTask(with: request) { data, resp, error in guard data != nil else { return } + + let frontString = String(decoding: data!, as: UTF8.self) + callback(frontString) } task.resume() } diff --git a/Switch.xcodeproj/project.xcworkspace/xcshareddata/WorkspaceSettings.xcsettings b/Switch.xcodeproj/project.xcworkspace/xcshareddata/WorkspaceSettings.xcsettings new file mode 100644 index 0000000..f9b0d7c --- /dev/null +++ b/Switch.xcodeproj/project.xcworkspace/xcshareddata/WorkspaceSettings.xcsettings @@ -0,0 +1,8 @@ + + + + + PreviewsEnabled + + + diff --git a/Switch.xcodeproj/project.xcworkspace/xcuserdata/within.xcuserdatad/WorkspaceSettings.xcsettings b/Switch.xcodeproj/project.xcworkspace/xcuserdata/within.xcuserdatad/WorkspaceSettings.xcsettings new file mode 100644 index 0000000..dd7403b --- /dev/null +++ b/Switch.xcodeproj/project.xcworkspace/xcuserdata/within.xcuserdatad/WorkspaceSettings.xcsettings @@ -0,0 +1,16 @@ + + + + + BuildLocationStyle + UseAppPreferences + CustomBuildLocationType + RelativeToDerivedData + DerivedDataLocationStyle + Default + IssueFilterStyle + ShowActiveSchemeOnly + LiveSourceIssuesEnabled + + + diff --git a/Switch.xcodeproj/xcuserdata/within.xcuserdatad/xcschemes/xcschememanagement.plist b/Switch.xcodeproj/xcuserdata/within.xcuserdatad/xcschemes/xcschememanagement.plist index c076e62..684f67f 100644 --- a/Switch.xcodeproj/xcuserdata/within.xcuserdatad/xcschemes/xcschememanagement.plist +++ b/Switch.xcodeproj/xcuserdata/within.xcuserdatad/xcschemes/xcschememanagement.plist @@ -4,6 +4,11 @@ SchemeUserState + Switch WatchKit App (Complication).xcscheme_^#shared#^_ + + orderHint + 1 + Switch WatchKit App.xcscheme_^#shared#^_ orderHint