Dark Mode

Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 703f1ab

Browse files
committed
fix requests
improve QRCode generation
1 parent 29ae9fd commit 703f1ab

File tree

3 files changed

+63
-43
lines changed
  • Allow2Framework
    • Allow2.swift
    • Allow2PairingViewController.swift
    • Allow2Storyboard.storyboard

3 files changed

+63
-43
lines changed

Allow2Framework/Allow2.swift

Lines changed: 49 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,10 @@ public enum Allow2Response {
5959
case CheckResult(Allow2CheckResult)
6060
case Request(Bool)
6161

62-
static func parseFromJSON(response : JSON) -> Allow2Response {
63-
guard response["error"] != "invalid pairId" else {
62+
static func parseFromJSON(_ json : JSON, response: HTTPURLResponse? = nil) -> Allow2Response {
63+
guard json["error"] != "invalid pairId",
64+
json["error"] != "invalid pairToken",
65+
response?.statusCode ?? 200 != 401 else {
6466
// special case, no longer controlled
6567
Allow2.shared.userId = nil
6668
Allow2.shared.pairId = nil
@@ -76,17 +78,17 @@ public enum Allow2Response {
7678
))
7779
}
7880

79-
if let requestSent = response["requestSent"].bool {
81+
if let requestSent = json["requestSent"].bool {
8082
return .Request(requestSent)
8183
}
8284

83-
guard let allowed = response["allowed"].bool else {
85+
guard let allowed = json["allowed"].bool else {
8486
return .Error(Allow2Error.InvalidResponse)
8587
}
86-
let activities = response["activities"]
87-
let dayTypes = response["dayTypes"]
88-
let children = response["children"]
89-
let allDayTypes = response["allDayTypes"]
88+
let activities = json["activities"]
89+
let dayTypes = json["dayTypes"]
90+
let children = json["children"]
91+
let allDayTypes = json["allDayTypes"]
9092
Allow2.shared._children = children.arrayValue.map { (child) -> Allow2Child in
9193
return Allow2Child(id: child["id"].uInt64Value,
9294
name: child["name"].stringValue,
@@ -417,9 +419,7 @@ public class Allow2 {
417419
public func check(childId: String!, activities: [Allow2Activity]!, log: Bool = true, completion: ((Allow2Response) -> Void)? = nil) {
418420

419421
guard self.isPaired else {
420-
if completion != nil {
421-
completion!(Allow2Response.Error( Allow2Error.NotPaired ))
422-
}
422+
completion?(Allow2Response.Error( Allow2Error.NotPaired ))
423423
return
424424
}
425425

@@ -441,9 +441,7 @@ public class Allow2 {
441441

442442
if checkResult.expires.timeIntervalSinceNow.sign != .minus {
443443
// not expired yet, use cached value
444-
if completion != nil {
445-
completion!( Allow2Response.CheckResult(checkResult) )
446-
}
444+
completion?( Allow2Response.CheckResult(checkResult) )
447445
return
448446
}
449447

@@ -475,6 +473,31 @@ public class Allow2 {
475473

476474
// interpret the result
477475
// todo: 403 is disconnected, clear everything out
476+
// this doesn't work here, it's handled by parseFromJSON below
477+
// if let httpResponse = response as? HTTPURLResponse,
478+
// httpResponse.statusCode == 401 {
479+
// // device released
480+
// self.userId = nil
481+
// self.pairId = nil
482+
// self.childId = nil
483+
// self._children = []
484+
// self._dayTypes = []
485+
//
486+
// // notify everyone
487+
// NotificationCenter.default.post(
488+
// name: .allow2CheckResultNotification,
489+
// object: nil,
490+
// userInfo: [ "result" : [
491+
// "allowed": true,
492+
// "activities": [],
493+
// "dayTypes": [],
494+
// "allDayTypes": [],
495+
// "children": []
496+
// ]]
497+
// )
498+
// completion?(Allow2Response.Error( Allow2Error.NotPaired ))
499+
// return
500+
// }
478501

479502
// handle other errors
480503

@@ -485,7 +508,7 @@ public class Allow2 {
485508
return;
486509
}
487510

488-
let result = Allow2Response.parseFromJSON(response: json)
511+
let result = Allow2Response.parseFromJSON(json, response: response as? HTTPURLResponse)
489512

490513
switch result {
491514
case let .CheckResult(checkResult):
@@ -549,9 +572,7 @@ extension Allow2 {
549572
public func request(dayTypeId: UInt64?, lift: [UInt64]?, message: String?, completion: ((Allow2Response) -> Void)? = nil) {
550573

551574
guard self.isPaired else {
552-
if completion != nil {
553-
completion!(Allow2Response.Error( Allow2Error.NotPaired ))
554-
}
575+
completion?(Allow2Response.Error( Allow2Error.NotPaired ))
555576
return
556577
}
557578

@@ -598,34 +619,20 @@ extension Allow2 {
598619

599620
// attempt to handle valid response
600621
// todo: better error handling on data -> JSON
601-
guard let json = try? JSON(data: data!) else {
602-
completion?(Allow2Response.Error( Allow2Error.InvalidResponse ))
622+
// guard let json = try? JSON(data: data!) else {
623+
// completion?(Allow2Response.Error( Allow2Error.InvalidResponse ))
624+
// return;
625+
// }
626+
627+
guard (response as? HTTPURLResponse)?.statusCode ?? 500 == 200 else {
628+
completion?(Allow2Response.Error( Allow2Error.Other(message: "Something went wrong") ))
603629
return;
604630
}
605631

606-
let result = Allow2Response.parseFromJSON(response: json)
632+
//let result = Allow2Response.parseFromJSON(json)
607633

608-
switch result {
609-
case let .CheckResult(checkResult):
610-
611-
// good response, cache the result first
612-
self.resultCache[key] = checkResult
613-
614-
// notify everyone
615-
NotificationCenter.default.post(
616-
name: .allow2CheckResultNotification,
617-
object: nil,
618-
userInfo: [ "result" : checkResult ]
619-
)
620-
621-
break
622-
default:
623-
completion?(result)
624-
return
625-
}
626-
627-
// now return the result
628-
completion?(result)
634+
completion?(Allow2Response.Request(true))
635+
629636
}
630637
task.resume()
631638

Allow2Framework/Allow2PairingViewController.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public class Allow2PairingViewController: UITableViewController {
2222
@IBOutlet var usernameField : UITextField?
2323
@IBOutlet var passwordField : UITextField?
2424
@IBOutlet var connectButton : UIButton?
25+
@IBOutlet var activityIndicator : UIActivityIndicatorView?
2526

2627
var pollingTimer: Timer!
2728
var bluetooth = Allow2Bluetooth()
@@ -93,6 +94,7 @@ public class Allow2PairingViewController: UITableViewController {
9394

9495
func updateBarcode() {
9596
//if let name = deviceNameField?.text?.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet())
97+
activityIndicator?.startAnimating()
9698
let hasName = (deviceName.count > 0)
9799
if hasName {
98100
barcodeImageView?.isHidden = false
@@ -101,6 +103,7 @@ public class Allow2PairingViewController: UITableViewController {
101103
let newQR = Allow2.shared.generateQRImage(name: self.deviceName, withSize: size)
102104
DispatchQueue.main.async() {
103105
self.barcodeImageView?.image = newQR
106+
self.activityIndicator?.stopAnimating()
104107
}
105108
}
106109
} else {

Allow2Framework/Allow2Storyboard.storyboard

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
device>
66
<dependencies>
77
<deployment identifier="iOS"/>
8-
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="14088"/>
8+
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="14088"/>
99
<capability name="Constraints to layout margins" minToolsVersion="6.0"/>
1010
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
1111
dependencies>
@@ -269,6 +269,13 @@
269269
<constraint firstAttribute="height" constant="249" id="vkg-oZ-3ar"/>
270270
constraints>
271271
imageView>
272+
<activityIndicatorView opaque="NO" contentMode="scaleToFill" horizontalHuggingPriority="750" verticalHuggingPriority="750" ambiguous="YES" hidesWhenStopped="YES" animating="YES" style="gray" translatesAutoresizingMaskIntoConstraints="NO" id="8DL-3A-6Ap">
273+
<rect key="frame" x="23" y="133" width="20" height="20"/>
274+
<constraints>
275+
<constraint firstAttribute="height" constant="20" id="lRy-X6-n0k"/>
276+
<constraint firstAttribute="width" constant="20" id="rQQ-sc-DJ5"/>
277+
constraints>
278+
activityIndicatorView>
272279
subviews>
273280
<constraints>
274281
<constraint firstItem="dCY-ED-SdA" firstAttribute="top" secondItem="NXO-Rm-H92" secondAttribute="topMargin" id="19b-uB-R4e"/>
@@ -277,6 +284,8 @@
277284
<constraint firstItem="dCY-ED-SdA" firstAttribute="leading" relation="greaterThanOrEqual" secondItem="NXO-Rm-H92" secondAttribute="leadingMargin" constant="8" id="SgJ-Jm-8m5"/>
278285
<constraint firstAttribute="trailingMargin" relation="greaterThanOrEqual" secondItem="dCY-ED-SdA" secondAttribute="trailing" constant="8" id="iVy-nV-cMY"/>
279286
<constraint firstItem="dCY-ED-SdA" firstAttribute="centerX" secondItem="NXO-Rm-H92" secondAttribute="centerX" id="pBy-Wr-RE3"/>
287+
<constraint firstItem="8DL-3A-6Ap" firstAttribute="centerY" secondItem="dCY-ED-SdA" secondAttribute="centerY" id="s8v-pJ-vEF"/>
288+
<constraint firstItem="8DL-3A-6Ap" firstAttribute="leading" secondItem="dCY-ED-SdA" secondAttribute="leading" constant="-40" id="yMi-gS-r6u"/>
280289
constraints>
281290
tableViewCellContentView>
282291
tableViewCell>
@@ -396,6 +405,7 @@
396405
<navigationItem key="navigationItem" title="Allow2" id="zPi-zQ-Rgh"/>
397406
<simulatedNavigationBarMetrics key="simulatedTopBarMetrics" prompted="NO"/>
398407
<connections>
408+
<outlet property="activityIndicator" destination="8DL-3A-6Ap" id="SZn-74-mXG"/>
399409
<outlet property="barcodeImageView" destination="dCY-ED-SdA" id="RmR-ez-EMN"/>
400410
<outlet property="connectButton" destination="P3q-Uq-bSo" id="XTa-MQ-KwS"/>
401411
<outlet property="deviceNameField" destination="mDb-uR-oYc" id="bTL-8i-4ap"/>

0 commit comments

Comments
(0)