77//
88
99import UIKit
10+ import StoreKit
11+ import SVProgressHUD
1012
1113public protocol Allow2PairingViewControllerDelegate {
1214 func Allow2PairingCompleted( result: Allow2Response )
1315}
1416
15- public class Allow2PairingViewController : UITableViewController {
17+ public class Allow2PairingViewController : UIViewController {
1618
1719 public var delegate : Allow2PairingViewControllerDelegate ?
1820
@@ -23,6 +25,13 @@ public class Allow2PairingViewController: UITableViewController {
2325 @IBOutlet var passwordField : UITextField ?
2426 @IBOutlet var connectButton : UIButton ?
2527 @IBOutlet var activityIndicator : UIActivityIndicatorView ?
28+ @IBOutlet var generatingLabel : UILabel ?
29+ @IBOutlet var generateButton : UIButton ?
30+
31+ @IBOutlet var scanView : UIView ?
32+ @IBOutlet var manualView : UIView ?
33+ @IBOutlet var orConnectManuallyButton : UIButton ?
34+ @IBOutlet var orScanButton : UIButton ?
2635
2736 var pollingTimer : Timer !
2837 var bluetooth = Allow2Bluetooth ( )
@@ -72,6 +81,7 @@ public class Allow2PairingViewController: UITableViewController {
7281 bluetooth. startAdvertising ( )
7382
7483 self . pollingTimer = Timer . scheduledTimer ( timeInterval: 3.0 , target: self , selector: #selector( Allow2PairingViewController . pollPairing) , userInfo: nil , repeats: true )
84+ self . navigationItem. title = " Parental Freedom "
7585 }
7686
7787 override public func viewDidDisappear( _ animated: Bool ) {
@@ -93,22 +103,17 @@ public class Allow2PairingViewController: UITableViewController {
93103 }
94104
95105 func updateBarcode( ) {
96- //if let name = deviceNameField?.text?.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet())
97106 activityIndicator? . startAnimating ( )
98- let hasName = ( deviceName. count > 0 )
99- if hasName {
100- barcodeImageView? . isHidden = false
101- let size = CGSize ( width: self . barcodeImageView!. frame. width, height: self . barcodeImageView!. frame. height)
102- DispatchQueue . global ( qos: . background) . async ( ) {
103- let newQR = Allow2 . shared. generateQRImage ( name: self . deviceName, withSize: size)
104- DispatchQueue . main. async ( ) {
105- self . barcodeImageView? . image = newQR
106- self . activityIndicator? . stopAnimating ( )
107- }
107+ generatingLabel? . isHidden = false
108+ generateButton? . isHidden = true
109+ let size = CGSize ( width: self . barcodeImageView!. frame. width, height: self . barcodeImageView!. frame. height)
110+ DispatchQueue . global ( qos: . background) . async ( ) {
111+ let newQR = Allow2 . shared. generateQRImage ( name: self . deviceName, withSize: size)
112+ DispatchQueue . main. async ( ) {
113+ self . barcodeImageView? . image = newQR
114+ self . activityIndicator? . stopAnimating ( )
115+ self . generatingLabel? . isHidden = true
108116 }
109- } else {
110- barcodeImageView? . isHidden = true
111- barcodeImageView? . image = nil
112117 }
113118 enableLogin ( )
114119 }
@@ -124,6 +129,7 @@ public class Allow2PairingViewController: UITableViewController {
124129
125130 UIApplication . shared. beginIgnoringInteractionEvents ( )
126131 UIApplication . shared. isNetworkActivityIndicatorVisible = true
132+ SVProgressHUD . show ( withStatus: " Contacting Allow2 " )
127133 Allow2 . shared. pair ( user: user, password: password, deviceName: deviceName) { ( result) in
128134 DispatchQueue . main. async {
129135 UIApplication . shared. endIgnoringInteractionEvents ( )
@@ -133,31 +139,83 @@ public class Allow2PairingViewController: UITableViewController {
133139 case . PairResult( let pairResult) :
134140 print ( " paired \( pairResult) " )
135141 //self.selectChild(result.children)
142+ DispatchQueue . main. async {
143+ SVProgressHUD . showSuccess ( withStatus: " Connected " )
144+ }
136145 self . delegate? . Allow2PairingCompleted ( result: result)
137146 break
138147 case . Error( let error) :
139148 print ( " pair error \( error) " )
149+ DispatchQueue . main. async {
150+ SVProgressHUD . showError ( withStatus: error. localizedDescription)
151+ }
140152 self . delegate? . Allow2PairingCompleted ( result: result)
141153 return
142154 default :
155+ DispatchQueue . main. async {
156+ SVProgressHUD . showError ( withStatus: " Unknown Error " )
157+ }
143158 break // cannot happen
144159 }
145160 }
146161 }
162+
163+ @IBAction func scanMode( ) {
164+ setScanMode ( scan: true )
165+ }
166+
167+ @IBAction func manualMode( ) {
168+ setScanMode ( scan: false )
169+ }
170+
171+ func setScanMode( scan : Bool ) {
172+ orScanButton? . isHidden = scan
173+ orConnectManuallyButton? . isHidden = !scan
174+ scanView? . isHidden = !scan
175+ manualView? . isHidden = scan
176+ }
177+
178+ @IBAction func getAllow2( ) {
179+ //if let URL = https://geo.itunes.apple.com/us/app/allow2/id569486440?mt=12
180+ let storeProductVC = SKStoreProductViewController ( )
181+ storeProductVC. delegate = self
182+ storeProductVC. loadProduct ( withParameters: [
183+ SKStoreProductParameterITunesItemIdentifier : " 569486440 " ,
184+ SKStoreProductParameterCampaignToken: " Allow2iOSApp "
185+ ] ) { ( success, err) in
186+ // if let err = err {
187+ // SVProgressHUD.showError(withStatus: err.localizedDescription)
188+ // }
189+
190+ storeProductVC. presentingViewController? . dismiss ( animated: true )
191+ }
192+
193+ // present right away to avoid pause
194+ self . present ( storeProductVC, animated: true , completion: nil )
195+ //UIApplication.shared.delegate?.window??.rootViewController ?.present(storeProductVC, animated:true, completion:nil)
196+ }
147197}
148198
149199extension Allow2PairingViewController : UITextFieldDelegate {
150-
151- @IBAction func textFieldEdited( textField: UITextField ) {
152- if textField == self . deviceNameField {
153- let newDeviceName = textField. text? . trimmingCharacters ( in: CharacterSet . whitespacesAndNewlines)
154- if newDeviceName != self . deviceName {
155- self . deviceName = newDeviceName
200+
201+ public func textField( _ textField: UITextField , shouldChangeCharactersIn range: NSRange , replacementString string: String ) -> Bool {
202+ guard textField != self . deviceNameField else {
203+ if let text = textField. text,
204+ let textRange = Range ( range, in: text) {
205+ let updatedText = text. replacingCharacters ( in: textRange,
206+ with: string) . trimmingCharacters ( in: . whitespacesAndNewlines)
207+ if updatedText != self . deviceName {
208+ self . _deviceName = updatedText
209+ barcodeUpdateDebouncer. call ( )
210+ }
211+ } else {
212+ self . _deviceName = nil
213+ barcodeUpdateDebouncer. call ( )
156214 }
157- barcodeUpdateDebouncer. call ( )
158- return
215+ return true
159216 }
160217 enableLogin ( )
218+ return true
161219 }
162220
163221 public func textFieldShouldReturn( _ textField: UITextField ) -> Bool {
@@ -178,6 +236,21 @@ extension Allow2PairingViewController : UITextFieldDelegate {
178236 }
179237 return true
180238 }
239+
240+ @IBAction func dismissKeyboard( ) {
241+ if ( self . deviceNameField? . isFirstResponder ?? false ) {
242+ self . deviceNameField? . resignFirstResponder ( )
243+ return
244+ }
245+ if ( self . usernameField? . isFirstResponder ?? false ) {
246+ self . usernameField? . resignFirstResponder ( )
247+ return
248+ }
249+ if ( self . passwordField? . isFirstResponder ?? false ) {
250+ self . passwordField? . resignFirstResponder ( )
251+ return
252+ }
253+ }
181254}
182255
183256extension Allow2PairingViewController {
@@ -249,6 +322,9 @@ extension Allow2PairingViewController {
249322 }
250323}
251324
325+ extension Allow2PairingViewController : SKStoreProductViewControllerDelegate {
326+
327+ }
252328
253329class Debouncer : NSObject {
254330 var callback : ( ( ) -> ( ) )
0 commit comments