Created
June 8, 2017 10:50
-
-
Save RomanovX/e4a1a0c3d76fd7ead473e1f453263ece to your computer and use it in GitHub Desktop.
TodayViewController of widget
This file contains 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
// | |
// TodayViewController.swift | |
// NowWidget | |
// | |
// Created by appName on 23-01-17. | |
// | |
// | |
import UIKit | |
import NotificationCenter | |
class TodayViewController: UIViewController, NCWidgetProviding { | |
private struct sharedData { | |
static var baseUrl: String? | |
static var token: String? | |
static var phoneDeviceId: String? | |
static var buttons: Array<Button>? | |
} | |
private struct Button { | |
var color: String | |
var id: String | |
var name: String | |
} | |
@IBOutlet weak var spinner: UIActivityIndicatorView! | |
@IBOutlet weak var appButton: UIButton! | |
@IBAction func openApp(sender: AnyObject) { | |
print("Should open appName...") | |
extensionContext?.openURL(NSURL(string: "appName://buttons")!, completionHandler: nil) | |
} | |
func loadData() -> Bool { | |
let groupIdentifier = "group." + NSBundle.mainBundle().bundleIdentifier! | |
var groupIdArray = groupIdentifier.componentsSeparatedByString(".") | |
groupIdArray.removeAtIndex(groupIdArray.count - 1) | |
let appGroupIdentifier = groupIdArray.joinWithSeparator("."); | |
let userDefaults = NSUserDefaults.init(suiteName: appGroupIdentifier) | |
if (userDefaults == nil) { | |
print("Error in user defaults") | |
setButtonTitle("Not logged in. Open appName to continue.") | |
return false | |
} | |
sharedData.baseUrl = userDefaults?.valueForKey("baseUrl") as? String | |
sharedData.token = userDefaults?.valueForKey("token") as? String | |
sharedData.phoneDeviceId = userDefaults?.valueForKey("phoneDeviceId") as? String | |
let buttons = userDefaults?.valueForKey("buttons") as? NSArray | |
if (sharedData.baseUrl == nil || sharedData.token == nil || sharedData.phoneDeviceId == nil || buttons == nil) { | |
print("Missing data") | |
setButtonTitle("Not logged in. Open appName to continue.") | |
return false | |
} | |
if (buttons?.count == 0) { | |
print("No buttons configured") | |
setButtonTitle("No buttons configured. Open appName to continue.") | |
return false; | |
} | |
var list: Array<Button> = [] | |
for (_, element) in buttons!.enumerate() { | |
let button = Button(color: element["color"]! as! String, id: element["id"]! as! String, name: element["name"]! as! String) | |
list.append(button) | |
} | |
sharedData.buttons = list | |
NSLog("Data initialized. \nServer baseURL: \n\(sharedData.baseUrl!), \ntoken: \n\(sharedData.token!),\nuuid: \n\(sharedData.phoneDeviceId!)"); | |
return true; | |
} | |
func setButtonTitle(message: String) { | |
spinner.stopAnimating() | |
appButton.setTitle(message, forState: .Normal) | |
} | |
func buttonAction(sender: UIButton!) { | |
// Not important, actually works | |
} | |
func touchUp(sender: UIButton!) { | |
sender.backgroundColor = colorFromName(sharedData.buttons![sender.tag].color) | |
} | |
func touchDown(sender: UIButton!) { | |
sender.backgroundColor = colorFromName(sharedData.buttons![sender.tag].color).darkerColor(0.5) | |
} | |
override func didReceiveMemoryWarning() { | |
super.didReceiveMemoryWarning() | |
// Dispose of any resources that can be recreated. | |
} | |
func widgetPerformUpdateWithCompletionHandler(completionHandler: ((NCUpdateResult) -> Void)) { | |
// Perform any setup necessary in order to update the view. | |
if (loadData()) { | |
print(sharedData.buttons) | |
appButton.hidden = true | |
spinner.stopAnimating() | |
for (index, element) in sharedData.buttons!.enumerate() { | |
if index > 5 { | |
return | |
} | |
print("Item \(index): \(element)") | |
let button = drawButton(view, index, colorFromName(element.color), element.name) | |
button.addTarget(self, action: #selector(buttonAction), forControlEvents: .TouchUpInside) | |
button.addTarget(self, action: #selector(touchUp), forControlEvents: .TouchUpInside) | |
button.addTarget(self, action: #selector(touchUp), forControlEvents: .TouchDragExit) | |
button.addTarget(self, action: #selector(touchUp), forControlEvents: .TouchUpOutside) | |
button.addTarget(self, action: #selector(touchUp), forControlEvents: .TouchCancel) | |
button.addTarget(self, action: #selector(touchDown), forControlEvents: .TouchDown) | |
view.addSubview(button) | |
} | |
} | |
completionHandler(NCUpdateResult.NewData) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment