Skip to content

Commit

Permalink
#12 (widget)
Browse files Browse the repository at this point in the history
- WIP.
  • Loading branch information
Dejal committed Apr 29, 2020
1 parent b1f9534 commit 5e33d57
Show file tree
Hide file tree
Showing 20 changed files with 164 additions and 116 deletions.
10 changes: 10 additions & 0 deletions Turn Touch iOS.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
172590542459235600055830 /* TTModeIfttt.swift in Sources */ = {isa = PBXBuildFile; fileRef = FF534C2E1EB0351A002EE584 /* TTModeIfttt.swift */; };
172590552459276D00055830 /* TTModeTab.swift in Sources */ = {isa = PBXBuildFile; fileRef = FFB41B781CEEA8E500CD5F09 /* TTModeTab.swift */; };
172590562459277A00055830 /* TTDiamondView.swift in Sources */ = {isa = PBXBuildFile; fileRef = FF0F10D61CF6697A00CC1F29 /* TTDiamondView.swift */; };
174A15FC24592A2D00F4AA2E /* TTActionDiamondView.swift in Sources */ = {isa = PBXBuildFile; fileRef = FFC9FE941D021A0400038817 /* TTActionDiamondView.swift */; };
174A15FD24592A3800F4AA2E /* TTDiamondLabel.swift in Sources */ = {isa = PBXBuildFile; fileRef = FF0F10DB1CF669BE00CC1F29 /* TTDiamondLabel.swift */; };
174A15FF24592CD700F4AA2E /* Preferences.plist in Resources */ = {isa = PBXBuildFile; fileRef = FF0F10BF1CF5638D00CC1F29 /* Preferences.plist */; };
17AE44062458FF9C00D06762 /* NotificationCenter.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 17AE44052458FF9C00D06762 /* NotificationCenter.framework */; };
17AE44092458FF9C00D06762 /* WidgetExtensionViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 17AE44082458FF9C00D06762 /* WidgetExtensionViewController.swift */; };
17AE440C2458FF9C00D06762 /* MainInterface.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 17AE440A2458FF9C00D06762 /* MainInterface.storyboard */; };
Expand Down Expand Up @@ -927,6 +930,7 @@

/* Begin PBXFileReference section */
170C8DC5245906150089B2C0 /* WidgetDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WidgetDelegate.swift; sourceTree = "<group>"; };
174A15FE24592C6700F4AA2E /* Turn Touch Widget.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = "Turn Touch Widget.entitlements"; sourceTree = "<group>"; };
17AE44042458FF9C00D06762 /* Turn Touch Widget.appex */ = {isa = PBXFileReference; explicitFileType = "wrapper.app-extension"; includeInIndex = 0; path = "Turn Touch Widget.appex"; sourceTree = BUILT_PRODUCTS_DIR; };
17AE44052458FF9C00D06762 /* NotificationCenter.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = NotificationCenter.framework; path = System/Library/Frameworks/NotificationCenter.framework; sourceTree = SDKROOT; };
17AE44082458FF9C00D06762 /* WidgetExtensionViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WidgetExtensionViewController.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1437,6 +1441,7 @@
17AE44072458FF9C00D06762 /* Widget Extension */ = {
isa = PBXGroup;
children = (
174A15FE24592C6700F4AA2E /* Turn Touch Widget.entitlements */,
17AE44082458FF9C00D06762 /* WidgetExtensionViewController.swift */,
170C8DC5245906150089B2C0 /* WidgetDelegate.swift */,
17AE440A2458FF9C00D06762 /* MainInterface.storyboard */,
Expand Down Expand Up @@ -2409,6 +2414,7 @@
buildActionMask = 2147483647;
files = (
17AE440C2458FF9C00D06762 /* MainInterface.storyboard in Resources */,
174A15FF24592CD700F4AA2E /* Preferences.plist in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -3107,6 +3113,7 @@
170C8DC6245906150089B2C0 /* WidgetDelegate.swift in Sources */,
1725905024591EFC00055830 /* TTBatchActions.swift in Sources */,
1725904824591E5400055830 /* TTMode.swift in Sources */,
174A15FC24592A2D00F4AA2E /* TTActionDiamondView.swift in Sources */,
172590542459235600055830 /* TTModeIfttt.swift in Sources */,
172590532459231C00055830 /* TTModeMenuContainer.swift in Sources */,
1725904A24591E6000055830 /* TTActionMap.swift in Sources */,
Expand All @@ -3116,6 +3123,7 @@
172590552459276D00055830 /* TTModeTab.swift in Sources */,
1725904924591E6000055830 /* TTAction.swift in Sources */,
1725904B24591E8200055830 /* TTModeDouble.swift in Sources */,
174A15FD24592A3800F4AA2E /* TTDiamondLabel.swift in Sources */,
1725904C24591ED900055830 /* UIColor+hex.swift in Sources */,
170C8DC7245907A70089B2C0 /* TTModeDirection.swift in Sources */,
17AE44092458FF9C00D06762 /* WidgetExtensionViewController.swift in Sources */,
Expand Down Expand Up @@ -3479,6 +3487,7 @@
CLANG_ENABLE_OBJC_WEAK = YES;
CLANG_WARN_DOCUMENTATION_COMMENTS = YES;
CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE;
CODE_SIGN_ENTITLEMENTS = "Widget Extension/Turn Touch Widget.entitlements";
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 37;
DEVELOPMENT_TEAM = 6Z7QW53WB6;
Expand Down Expand Up @@ -3507,6 +3516,7 @@
CLANG_ENABLE_OBJC_WEAK = YES;
CLANG_WARN_DOCUMENTATION_COMMENTS = YES;
CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE;
CODE_SIGN_ENTITLEMENTS = "Widget Extension/Turn Touch Widget.entitlements";
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 37;
DEVELOPMENT_TEAM = 6Z7QW53WB6;
Expand Down
39 changes: 29 additions & 10 deletions Turn Touch iOS/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate, CLLocationManagerDelegate
print(" ---> centralManagerIdentifiers: \(String(describing: centralManagerIdentifiers))")
}

// print(UserDefaults.standardUserDefaults().dictionaryRepresentation())
// print(preferences()UserDefaults().dictionaryRepresentation())
UIApplication.shared.beginReceivingRemoteControlEvents()

bluetoothMonitor = TTBluetoothMonitor()
Expand Down Expand Up @@ -76,8 +76,6 @@ class AppDelegate: UIResponder, UIApplicationDelegate, CLLocationManagerDelegate
print(" ---> applicationDidEnterBackground")

self.recordState()
// let prefs = UserDefaults.standardUserDefaults()
// prefs.synchronize()
}

func applicationWillEnterForeground(_ application: UIApplication) {
Expand All @@ -99,8 +97,6 @@ class AppDelegate: UIResponder, UIApplicationDelegate, CLLocationManagerDelegate
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
print(" ---> applicationWillTerminate");
bluetoothMonitor.terminate()
let prefs = UserDefaults.standard
prefs.synchronize()
}

func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any]) -> Bool {
Expand Down Expand Up @@ -137,11 +133,13 @@ class AppDelegate: UIResponder, UIApplicationDelegate, CLLocationManagerDelegate
}

func erasePreferences() {
UserDefaults.standard.removePersistentDomain(forName: Bundle.main.bundleIdentifier!)
preferences().removePersistentDomain(forName: Bundle.main.bundleIdentifier!)
}

func loadPreferences() {
let prefs = UserDefaults.standard
migratePreferencesToGroup()

let prefs = preferences()
let defaultPrefsFile = Bundle.main.path(forResource: "Preferences", ofType: "plist")
let defaultPrefs = NSDictionary(contentsOfFile: defaultPrefsFile!) as! [String: AnyObject]
prefs.register(defaults: defaultPrefs)
Expand All @@ -154,8 +152,25 @@ class AppDelegate: UIResponder, UIApplicationDelegate, CLLocationManagerDelegate
prefs.synchronize()
}

func migratePreferencesToGroup() {
let localDefaults = UserDefaults.standard
let localDictionary = localDefaults.dictionaryRepresentation()
let groupDefaults = preferences()
let didMigrateToAppGroups = "TT:didMigrateToAppGroups"

if !groupDefaults.bool(forKey: didMigrateToAppGroups) {
for key in localDictionary.keys {
if key.hasPrefix("TT:") {
groupDefaults.set(localDictionary[key], forKey: key)
}
}

groupDefaults.set(true, forKey: didMigrateToAppGroups)
}
}

func processDefaultSettings() {
let defaults = UserDefaults.standard
let defaults = preferences()
defaults.synchronize()

guard let settingsBundle = Bundle.main.path(forResource: "Settings", ofType: "bundle") as NSString? else {
Expand Down Expand Up @@ -251,7 +266,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate, CLLocationManagerDelegate
}

func startLocationMonitoring() {
let prefs = UserDefaults.standard
let prefs = preferences()
if let coords = prefs.dictionary(forKey: "TT:geofence:1") as? [String: NSNumber] {
let center = CLLocationCoordinate2D(latitude: coords["lat"] as! CLLocationDegrees,
longitude: coords["long"] as! CLLocationDegrees)
Expand Down Expand Up @@ -305,6 +320,10 @@ class AppDelegate: UIResponder, UIApplicationDelegate, CLLocationManagerDelegate

}

func appDelegate () -> AppDelegate {
func appDelegate() -> AppDelegate {
return UIApplication.shared.delegate as! AppDelegate
}

func preferences() -> UserDefaults {
return UserDefaults(suiteName: "group.com.turntouch.ios-remote") ?? UserDefaults.standard
}
18 changes: 9 additions & 9 deletions Turn Touch iOS/Bluetooth/TTBluetoothMonitor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,8 @@ class TTBluetoothMonitor: NSObject, CBCentralManagerDelegate, CBPeripheralDelega

func knownPeripheralIdentifiers() -> [UUID] {
var identifiers: [UUID] = []
let preferences = UserDefaults.standard
let pairedDevices = preferences.array(forKey: "TT:devices:paired") as! [String]?
let prefs = preferences()
let pairedDevices = prefs.array(forKey: "TT:devices:paired") as! [String]?
if pairedDevices != nil {
for identifier: String in pairedDevices! {
identifiers.append(NSUUID(uuidString: identifier)! as UUID)
Expand Down Expand Up @@ -376,7 +376,7 @@ class TTBluetoothMonitor: NSObject, CBCentralManagerDelegate, CBPeripheralDelega

func forgetDevice(_ device: TTDevice) {
var pairedDevicesArray: [String] = []
let prefs = UserDefaults.standard
let prefs = preferences()
if let pairedDevices = prefs.array(forKey: "TT:devices:paired") as! [String]? {
for identifier: String in pairedDevices {
if identifier != device.uuid {
Expand Down Expand Up @@ -749,7 +749,7 @@ class TTBluetoothMonitor: NSObject, CBCentralManagerDelegate, CBPeripheralDelega
let emptyNickname = NSMutableData(length: 32)
let deviceNickname = device.nickname
let deviceNicknameData = device.nickname?.data(using: String.Encoding.utf8)
let prefs = UserDefaults.standard
let prefs = preferences()
let nicknameKey = "TT:device:\(device.uuid ?? "?"):nickname"
let existingNickname = prefs.string(forKey: nicknameKey)

Expand Down Expand Up @@ -785,7 +785,7 @@ class TTBluetoothMonitor: NSObject, CBCentralManagerDelegate, CBPeripheralDelega

func writeNicknameToDevice(_ device: TTDevice, nickname: String) {
var data = NSMutableData(data: nickname.data(using: String.Encoding.utf8)!)
let prefs = UserDefaults.standard
let prefs = preferences()

if data.length > 32 {
var dataString = String(data: data as Data, encoding: String.Encoding.utf8)
Expand Down Expand Up @@ -849,14 +849,14 @@ class TTBluetoothMonitor: NSObject, CBCentralManagerDelegate, CBPeripheralDelega
// MARK: Pairing

func pairDeviceSuccess(_ peripheral: CBPeripheral) {
let preferences = UserDefaults.standard
var pairedDevices = preferences.array(forKey: "TT:devices:paired") as! [String]?
let prefs = preferences()
var pairedDevices = prefs.array(forKey: "TT:devices:paired") as! [String]?
if pairedDevices == nil {
pairedDevices = []
}
pairedDevices?.append(peripheral.identifier.uuidString)
preferences.set(pairedDevices, forKey: "TT:devices:paired")
preferences.synchronize()
prefs.set(pairedDevices, forKey: "TT:devices:paired")
prefs.synchronize()

if let device = foundDevices.deviceForPeripheral(peripheral) {
device.isPaired = foundDevices.isDevicePaired(device)
Expand Down
2 changes: 1 addition & 1 deletion Turn Touch iOS/Models/TTBatchActions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class TTBatchActions: NSObject {
}

func assembleBatchAction(in direction: TTModeDirection) -> [TTAction] {
let prefs = UserDefaults.standard
let prefs = preferences()
var batchActions: [TTAction] = []
let key = self.batchActionKey(in: direction)
let batchActionKeys: [String]? = prefs.object(forKey: key) as? [String]
Expand Down
4 changes: 2 additions & 2 deletions Turn Touch iOS/Models/TTDevice.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class TTDevice: NSObject {
peripheral = newPeripheral
uuid = peripheral.identifier.uuidString

let prefs = UserDefaults.standard
let prefs = preferences()
let nicknameKey = "TT:device:\(uuid ?? "nil"):nickname"
nickname = prefs.string(forKey: nicknameKey)
}
Expand Down Expand Up @@ -79,7 +79,7 @@ class TTDevice: NSObject {
func setFirmwareVersion(firmwareVersion version: Int) {
firmwareVersion = version

let prefs = UserDefaults.standard
let prefs = preferences()
let latestVersion = prefs.integer(forKey: "TT:firmware:version")

isFirmwareOld = firmwareVersion! < latestVersion
Expand Down
4 changes: 2 additions & 2 deletions Turn Touch iOS/Models/TTDeviceList.swift
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ class TTDeviceList: NSObject {
// MARK: Pairing

func isPeripheralPaired(_ peripheral: CBPeripheral) -> Bool {
let prefs = UserDefaults.standard
let prefs = preferences()
let pairedDevices = prefs.array(forKey: "TT:devices:paired") as! [String]?

if pairedDevices == nil {
Expand Down Expand Up @@ -176,7 +176,7 @@ class TTDeviceList: NSObject {
}

func totalPairedCount() -> Int {
let prefs = UserDefaults.standard
let prefs = preferences()
let pairedDevices = prefs.array(forKey: "TT:devices:paired") as! [String]?

if pairedDevices == nil {
Expand Down
24 changes: 12 additions & 12 deletions Turn Touch iOS/Models/TTMode.swift
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ class TTMode : NSObject, TTModeProtocol {
}

func setCustomTitle(_ title: String?, direction: TTModeDirection) {
let prefs = UserDefaults.standard
let prefs = preferences()
let modeDirectionName = appDelegate().modeMap.directionName(modeDirection)
let actionDirectionName = appDelegate().modeMap.directionName(direction)
let actionName = self.actionNameInDirection(direction)
Expand Down Expand Up @@ -216,7 +216,7 @@ class TTMode : NSObject, TTModeProtocol {
}

// First check if given custom title
let prefs = UserDefaults.standard
let prefs = preferences()
let modeDirectionName = appDelegate().modeMap.directionName(modeDirection)
let actionDirectionName = appDelegate().modeMap.directionName(direction)
let prefKey = "TT:\(mode.nameOfClass)-\(modeDirectionName):action:\(actionName)-\(actionDirectionName):customTitle"
Expand Down Expand Up @@ -279,7 +279,7 @@ class TTMode : NSObject, TTModeProtocol {
return action.actionName
}

let prefs = UserDefaults.standard
let prefs = preferences()

let mode = self.modeInDirection(direction)
let modeDirectionName = appDelegate().modeMap.directionName(modeDirection)
Expand Down Expand Up @@ -348,7 +348,7 @@ class TTMode : NSObject, TTModeProtocol {
// MARK: Mode options

func modeOptionValue(_ optionName: String) -> Any? {
let prefs = UserDefaults.standard
let prefs = preferences()
let modeDirectionName = appDelegate().modeMap.directionName(modeDirection)

if modeDirection == .no_DIRECTION {
Expand Down Expand Up @@ -391,7 +391,7 @@ class TTMode : NSObject, TTModeProtocol {
// MARK: Changing mode settings

func changeDirection(_ direction: TTModeDirection, toAction actionClassName: String) {
let prefs = UserDefaults.standard
let prefs = preferences()

let modeDirectionName = appDelegate().modeMap.directionName(modeDirection)
let actionDirectionName = appDelegate().modeMap.directionName(direction)
Expand All @@ -409,7 +409,7 @@ class TTMode : NSObject, TTModeProtocol {
print(" ---> BUSTED: \(optionValue)")
return
}
let prefs = UserDefaults.standard
let prefs = preferences()
let modeDirectionName = appDelegate().modeMap.directionName(modeDirection)
var optionKey = "TT:mode:\(self.nameOfClass)-\(modeDirectionName):option:\(optionName)"

Expand All @@ -430,7 +430,7 @@ class TTMode : NSObject, TTModeProtocol {
// MARK: Action options

func actionOptionValue(_ optionName: String, actionName: String, direction: TTModeDirection) -> Any? {
let prefs = UserDefaults.standard
let prefs = preferences()
let modeDirectionName = appDelegate().modeMap.directionName(modeDirection)
let actionDirectionName = appDelegate().modeMap.directionName(direction)

Expand Down Expand Up @@ -486,7 +486,7 @@ class TTMode : NSObject, TTModeProtocol {

func batchActionOptionValue(batchActionKey: String, optionName: String, actionName: String,
actionDirection: TTModeDirection) -> Any? {
let prefs = UserDefaults.standard
let prefs = preferences()
let modeDirectionName = appDelegate().modeMap.directionName(modeDirection)
let actionDirectionName = appDelegate().modeMap.directionName(actionDirection)
let optionKey = "TT:mode:\(modeDirectionName):action:\(actionDirectionName):batchactions:\(batchActionKey):actionoption:\(optionName)"
Expand Down Expand Up @@ -553,7 +553,7 @@ class TTMode : NSObject, TTModeProtocol {
print(" ---> BUSTED: \(optionValue)")
return
}
let prefs = UserDefaults.standard
let prefs = preferences()
let inspectingModeDirection = direction ?? appDelegate().modeMap.inspectingModeDirection
let modeDirectionName = appDelegate().modeMap.directionName(modeDirection)
let actionDirectionName = appDelegate().modeMap.directionName(inspectingModeDirection)
Expand All @@ -568,7 +568,7 @@ class TTMode : NSObject, TTModeProtocol {

func changeBatchActionOption(_ batchActionKey: String, optionName: String, to optionValue: Any,
direction: TTModeDirection? = nil, actionDirection: TTModeDirection? = nil) {
let prefs = UserDefaults.standard
let prefs = preferences()
let modeDirectionName = appDelegate().modeMap.directionName(direction ?? modeDirection)
let actionDirectionName = appDelegate().modeMap.directionName(actionDirection ?? appDelegate().modeMap.inspectingModeDirection)
let optionKey = "TT:mode:\(modeDirectionName):action:\(actionDirectionName):batchactions:\(batchActionKey):actionoption:\(optionName)"
Expand All @@ -580,7 +580,7 @@ class TTMode : NSObject, TTModeProtocol {
}

func removeActionOption(_ optionName: String, direction: TTModeDirection?=nil) {
let prefs = UserDefaults.standard
let prefs = preferences()
let inspectingModeDirection = direction ?? appDelegate().modeMap.inspectingModeDirection
let modeDirectionName = appDelegate().modeMap.directionName(modeDirection)
let actionDirectionName = appDelegate().modeMap.directionName(inspectingModeDirection)
Expand All @@ -595,7 +595,7 @@ class TTMode : NSObject, TTModeProtocol {

func removeBatchActionOption(_ batchActionKey: String, optionName: String,
direction: TTModeDirection? = nil, actionDirection: TTModeDirection? = nil) {
let prefs = UserDefaults.standard
let prefs = preferences()
let modeDirectionName = appDelegate().modeMap.directionName(direction ?? modeDirection)
let actionDirectionName = appDelegate().modeMap.directionName(actionDirection ?? appDelegate().modeMap.inspectingModeDirection)
let optionKey = "TT:mode:\(modeDirectionName):action:\(actionDirectionName):batchactions:\(batchActionKey):actionoption:\(optionName)"
Expand Down
Loading

0 comments on commit 5e33d57

Please sign in to comment.