From 45b37919221752a3a92cc5b6831164a1d10cce42 Mon Sep 17 00:00:00 2001 From: Within Date: Sat, 21 Sep 2019 17:00:13 -0400 Subject: [PATCH] code commit --- Switch WatchKit App/Info.plist | 2 +- Switch WatchKit Extension/.gitignore | 1 + .../ComplicationController.swift | 67 ++++++++++++++++++ Switch WatchKit Extension/ContentView.swift | 69 ++++++++++++++++++- Switch WatchKit Extension/Info.plist | 6 ++ Switch WatchKit Extension/Switch.swift | 24 +++++++ Switch.xcodeproj/project.pbxproj | 12 ++++ 7 files changed, 178 insertions(+), 3 deletions(-) create mode 100644 Switch WatchKit Extension/.gitignore create mode 100644 Switch WatchKit Extension/ComplicationController.swift create mode 100644 Switch WatchKit Extension/Switch.swift diff --git a/Switch WatchKit App/Info.plist b/Switch WatchKit App/Info.plist index d8ca6a2..4a0b546 100644 --- a/Switch WatchKit App/Info.plist +++ b/Switch WatchKit App/Info.plist @@ -5,7 +5,7 @@ CFBundleDevelopmentRegion $(DEVELOPMENT_LANGUAGE) CFBundleDisplayName - Switch WatchKit App + Switch CFBundleExecutable $(EXECUTABLE_NAME) CFBundleIdentifier diff --git a/Switch WatchKit Extension/.gitignore b/Switch WatchKit Extension/.gitignore new file mode 100644 index 0000000..9679f01 --- /dev/null +++ b/Switch WatchKit Extension/.gitignore @@ -0,0 +1 @@ +SwitchURL.swift diff --git a/Switch WatchKit Extension/ComplicationController.swift b/Switch WatchKit Extension/ComplicationController.swift new file mode 100644 index 0000000..81dc8bb --- /dev/null +++ b/Switch WatchKit Extension/ComplicationController.swift @@ -0,0 +1,67 @@ +// +// ComplicationController.swift +// Switch WatchKit Extension +// +// Created by Within on 2019-09-21. +// Copyright © 2019 Within. All rights reserved. +// + +import WatchKit + +class ComplicationController: NSObject, CLKComplicationDataSource { + func getSupportedTimeTravelDirections(for complication: CLKComplication, withHandler handler: @escaping (CLKComplicationTimeTravelDirections) -> Void) { + handler([]) + } + + func getTimelineStartDate(for complication: CLKComplication, withHandler handler: @escaping (Date?) -> Void) { + handler(nil) + } + + func getTimelineEndDate(for complication: CLKComplication, withHandler handler: @escaping (Date?) -> Void) { + handler(nil) + } + + func getPrivacyBehavior(for complication: CLKComplication, withHandler handler: @escaping (CLKComplicationPrivacyBehavior) -> Void) { + handler(.showOnLockScreen) + } + + // MARK: - Timeline Population + + func getCurrentTimelineEntry(for complication: CLKComplication, withHandler handler: @escaping (CLKComplicationTimelineEntry?) -> Void) { + // Call the handler with the current timeline entry + handler(nil) + } + + func getTimelineEntries(for complication: CLKComplication, before date: Date, limit: Int, withHandler handler: @escaping ([CLKComplicationTimelineEntry]?) -> Void) { + // Call the handler with the timeline entries prior to the given date + handler(nil) + } + + func getTimelineEntries(for complication: CLKComplication, after date: Date, limit: Int, withHandler handler: @escaping ([CLKComplicationTimelineEntry]?) -> Void) { + // Call the handler with the timeline entries after to the given date + handler(nil) + } + + func getLocalizableSampleTemplate(for complication: CLKComplication, withHandler handler: @escaping (CLKComplicationTemplate?) -> Void) { + // This method will be called once per supported complication, and the results will be cached + + if (complication.family == .graphicCorner) { + handler(getTemplateForGraphicCorner()) + } + + handler(nil) + } + + func getTemplateForGraphicCorner() -> CLKComplicationTemplateGraphicCornerStackText { + let gc = CLKComplicationTemplateGraphicCornerStackText() + let front = "Cadey" + let text = CLKSimpleTextProvider() + text.text = front + " foo" + text.shortText = "hi" + text.tintColor = UIColor.white + + gc.innerTextProvider = text + + return gc; + } +} diff --git a/Switch WatchKit Extension/ContentView.swift b/Switch WatchKit Extension/ContentView.swift index 79e146e..f2296e9 100644 --- a/Switch WatchKit Extension/ContentView.swift +++ b/Switch WatchKit Extension/ContentView.swift @@ -8,9 +8,74 @@ import SwiftUI -struct ContentView: View { +func reloadComplications() { + if let complications: [CLKComplication] = CLKComplicationServer.sharedInstance().activeComplications { + if complications.count > 0 { + for complication in complications { + CLKComplicationServer.sharedInstance().reloadTimeline(for: complication) + NSLog("Reloading complication \(complication.description)...") + } + WKInterfaceDevice.current().play(WKHapticType.click) // haptic only for debugging + } + } +} + +struct SwitchTo: Codable { + struct WebhookCommand: Codable { + var command: String + var member_name: String + } + + var webhook: WebhookCommand +} + +struct SwitchButton: View { + var who: String + var parent: ContentView var body: some View { - Text("Hello World") + Button(action: { + setFront(self.who) + self.parent.front = self.who + }) { + Text(self.who) + } + } +} + +struct ContentView: View { + @State var front = getFront() + func refresh() { + self.front = getFront() + reloadComplications() + } + + var body: some View { + ScrollView{ + Text(self.front + " is front") + if self.front != "Cadey" { + SwitchButton(who: "Cadey", parent: self) + } + if self.front != "Nicole" { + SwitchButton(who: "Nicole", parent: self) + } + if self.front != "Jessie" { + SwitchButton(who: "Jessie", parent: self) + } + if self.front != "Ashe" { + SwitchButton(who: "Ashe", parent: self) + } + if self.front != "Sephie" { + SwitchButton(who: "Sephie", parent: self) + } + if self.front != "Mai" { + SwitchButton(who: "Mai", parent: self) + } + Button(action: { + self.refresh() + }) { + Text("🔄") + } + } } } diff --git a/Switch WatchKit Extension/Info.plist b/Switch WatchKit Extension/Info.plist index 3c409ca..56211ec 100644 --- a/Switch WatchKit Extension/Info.plist +++ b/Switch WatchKit Extension/Info.plist @@ -20,6 +20,12 @@ 1.0 CFBundleVersion 1 + CLKComplicationPrincipalClass + ComplicationController + CLKComplicationSupportedFamilies + + CLKComplicationFamilyGraphicCorner + NSExtension NSExtensionAttributes diff --git a/Switch WatchKit Extension/Switch.swift b/Switch WatchKit Extension/Switch.swift new file mode 100644 index 0000000..d96d525 --- /dev/null +++ b/Switch WatchKit Extension/Switch.swift @@ -0,0 +1,24 @@ +// +// Switch.swift +// Switch WatchKit Extension +// +// Created by Within on 2019-09-21. +// Copyright © 2019 Within. All rights reserved. +// + +import Foundation + +func getFront() -> String { + let data = try! Data(contentsOf: url) + return String(decoding: data, as: UTF8.self) +} + +func setFront(_ to: String) { + 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 } + } + task.resume() +} diff --git a/Switch.xcodeproj/project.pbxproj b/Switch.xcodeproj/project.pbxproj index 4287332..17c5d7f 100644 --- a/Switch.xcodeproj/project.pbxproj +++ b/Switch.xcodeproj/project.pbxproj @@ -16,6 +16,9 @@ 4225D8E32336B5B000B8B625 /* ExtensionDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4225D8E22336B5B000B8B625 /* ExtensionDelegate.swift */; }; 4225D8E52336B5B100B8B625 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 4225D8E42336B5B100B8B625 /* Assets.xcassets */; }; 4225D8E82336B5B100B8B625 /* Preview Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 4225D8E72336B5B100B8B625 /* Preview Assets.xcassets */; }; + 4225D8F82336B8BF00B8B625 /* ComplicationController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4225D8F72336B8BF00B8B625 /* ComplicationController.swift */; }; + 4225D8FA2336B92C00B8B625 /* Switch.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4225D8F92336B92C00B8B625 /* Switch.swift */; }; + 4225D8FC2336C68400B8B625 /* SwitchURL.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4225D8FB2336C68400B8B625 /* SwitchURL.swift */; }; /* End PBXBuildFile section */ /* Begin PBXContainerItemProxy section */ @@ -73,6 +76,9 @@ 4225D8E42336B5B100B8B625 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 4225D8E72336B5B100B8B625 /* Preview Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = "Preview Assets.xcassets"; sourceTree = ""; }; 4225D8E92336B5B100B8B625 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; + 4225D8F72336B8BF00B8B625 /* ComplicationController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ComplicationController.swift; sourceTree = ""; }; + 4225D8F92336B92C00B8B625 /* Switch.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Switch.swift; sourceTree = ""; }; + 4225D8FB2336C68400B8B625 /* SwitchURL.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SwitchURL.swift; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -118,7 +124,10 @@ 4225D8DD2336B5B000B8B625 /* Switch WatchKit Extension */ = { isa = PBXGroup; children = ( + 4225D8F72336B8BF00B8B625 /* ComplicationController.swift */, + 4225D8FB2336C68400B8B625 /* SwitchURL.swift */, 4225D8DE2336B5B000B8B625 /* ContentView.swift */, + 4225D8F92336B92C00B8B625 /* Switch.swift */, 4225D8E02336B5B000B8B625 /* HostingController.swift */, 4225D8E22336B5B000B8B625 /* ExtensionDelegate.swift */, 4225D8E42336B5B100B8B625 /* Assets.xcassets */, @@ -265,8 +274,11 @@ buildActionMask = 2147483647; files = ( 4225D8E12336B5B000B8B625 /* HostingController.swift in Sources */, + 4225D8F82336B8BF00B8B625 /* ComplicationController.swift in Sources */, 4225D8DF2336B5B000B8B625 /* ContentView.swift in Sources */, 4225D8E32336B5B000B8B625 /* ExtensionDelegate.swift in Sources */, + 4225D8FA2336B92C00B8B625 /* Switch.swift in Sources */, + 4225D8FC2336C68400B8B625 /* SwitchURL.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; };