66 TNSParticleDeviceVariable ,
77 TNSParticleLoginOptions
88} from "./particle.common" ;
9- import * as frameModule from 'tns-core-modules/ui/frame' ;
109
1110const toJsArray = ( nativeArray : NSArray < any > ) : Array < any > => {
1211 const result : Array < any > = [ ] ;
@@ -18,7 +17,6 @@ const toJsArray = (nativeArray: NSArray<any>): Array<any> => {
1817 return result ;
1918} ;
2019
21-
2220const toJsonVariables = ( nativeDictionary : NSDictionary < string , string > ) : Array < TNSParticleDeviceVariable > => {
2321 const result : Array < TNSParticleDeviceVariable > = [ ] ;
2422 if ( nativeDictionary ) {
@@ -54,7 +52,7 @@ class MyTNSParticleDevice implements TNSParticleDevice {
5452 functions : Array < string > ;
5553 variables : Array < TNSParticleDeviceVariable > ;
5654 eventIds : string [ ] = [ ] ;
57-
55+
5856 constructor ( public nativeDevice : ParticleDevice ) {
5957 this . id = nativeDevice . id ;
6058 this . name = nativeDevice . name ;
@@ -85,15 +83,15 @@ class MyTNSParticleDevice implements TNSParticleDevice {
8583 } ) ;
8684 }
8785
88- subscribe ( name : string , eventHandler :any ) : void {
89- const id = this . nativeDevice . subscribeToEventsWithPrefixHandler ( name , ( event : ParticleEvent , error : NSError ) => {
90- if ( ! error ) {
91- if ( event . data ) eventHandler ( event . data ) ;
92- } else {
93- console . log ( `Error subscribing to event: ${ error } ` ) ;
94- }
95- } ) ;
96- this . eventIds . push ( id ) ;
86+ subscribe ( name : string , eventHandler : any ) : void {
87+ const id = this . nativeDevice . subscribeToEventsWithPrefixHandler ( name , ( event : ParticleEvent , error : NSError ) => {
88+ if ( ! error ) {
89+ if ( event . data ) eventHandler ( event . data ) ;
90+ } else {
91+ console . log ( `Error subscribing to event: ${ error } ` ) ;
92+ }
93+ } ) ;
94+ this . eventIds . push ( id ) ;
9795 }
9896
9997 unsubscribe ( ) : void {
@@ -105,7 +103,7 @@ class MyTNSParticleDevice implements TNSParticleDevice {
105103
106104export class Particle implements TNSParticleAPI {
107105
108- private wizardDelegate : ParticleSetupControllerDelegateImpl ;
106+ private wizardDelegate : ParticleSetupControllerDelegateImpl ;
109107
110108 public login ( options : TNSParticleLoginOptions ) : Promise < void > {
111109 return new Promise < void > ( ( resolve , reject ) => {
@@ -124,7 +122,7 @@ export class Particle implements TNSParticleAPI {
124122 ParticleCloud . sharedInstance ( ) . injectSessionAccessToken ( token ) ;
125123 }
126124
127- public setOAuthConfig ( id :string , secret :string ) : void {
125+ public setOAuthConfig ( id : string , secret : string ) : void {
128126 ParticleCloud . sharedInstance ( ) . oAuthClientId = id ;
129127 ParticleCloud . sharedInstance ( ) . oAuthClientSecret = secret ;
130128 }
@@ -135,10 +133,10 @@ export class Particle implements TNSParticleAPI {
135133
136134 public isAuthenticated ( ) : Boolean {
137135 return ParticleCloud . sharedInstance ( ) . isAuthenticated ;
138- }
139-
136+ }
137+
140138 public accessToken ( ) : string {
141- return ParticleCloud . sharedInstance ( ) . accessToken ;
139+ return ParticleCloud . sharedInstance ( ) . accessToken ;
142140 }
143141
144142 public listDevices ( ) : Promise < Array < TNSParticleDevice > > {
@@ -158,45 +156,43 @@ export class Particle implements TNSParticleAPI {
158156 } ) ;
159157 }
160158
161- public startDeviceSetupWizard ( finishHandler : any ) : void {
162- console . log ( 'Particle.startDeviceSetup' ) ;
163- const setupController = new ParticleSetupMainController ( ) ;
164- this . wizardDelegate = new ParticleSetupControllerDelegateImpl ( ) ;
165- this . wizardDelegate . cb = finishHandler ;
166- setupController . delegate = this . wizardDelegate ;
167- var ctrl = frameModule . topmost ( ) . ios . controller ;
168- ctrl . navigationBarHidden = true ;
169- ctrl . pushViewControllerAnimated ( setupController , true ) ;
159+ public startDeviceSetupWizard ( finishHandler : any ) : void {
160+ const setupController = ParticleSetupMainController . new ( ) ;
161+ console . log ( ">> bundle: " + ParticleSetupMainController . getResourcesBundle ( ) ) ;
162+ console . log ( ">> bundle img: " + ParticleSetupMainController . loadImageFromResourceBundle ( "spinner" ) ) ;
163+ this . wizardDelegate = ParticleSetupControllerDelegateImpl . createWithOwnerAndCallback ( new WeakRef ( this ) , finishHandler ) ;
164+ setupController . delegate = < any > this . wizardDelegate ;
165+ UIApplication . sharedApplication . keyWindow . rootViewController . presentViewControllerAnimatedCompletion ( setupController , true , null ) ;
170166 }
171167
172- public getDeviceSetupCustomizer ( ) : any {
168+ public getDeviceSetupCustomizer ( ) : any {
173169 return ParticleSetupCustomization . sharedInstance ( ) ;
174170 }
175171}
176172
177-
178- declare var ParticleSetupMainController : any ;
179- declare var ParticleSetupCustomization : any ;
180173class ParticleSetupControllerDelegateImpl extends NSObject implements ParticleSetupMainControllerDelegate {
181- static ObjCProtocols = [ ParticleSetupMainControllerDelegate ] // define our native protocalls
174+ static ObjCProtocols = [ ParticleSetupMainControllerDelegate ] ; // define our native protocols
175+
176+ private owner : WeakRef < Particle > ;
177+ private cb : ( result : ParticleSetupMainControllerResult ) => void ;
182178
183- static new ( ) : ParticleSetupControllerDelegateImpl {
184- return < ParticleSetupControllerDelegateImpl > super . new ( ) // calls new() on the NSObject
179+ public static new ( ) : ParticleSetupControllerDelegateImpl {
180+ return < ParticleSetupControllerDelegateImpl > super . new ( ) ; // calls new() on the NSObject
185181 }
186- cb :any ;
187182
188- public particleSetupViewControllerDidFinishWithResultDevice ( controller , result , device ) {
189- console . log ( 'particleSetupViewControllerDidFinishWithResultDevice' ) ;
190-
191- if ( this . cb ) {
192- this . cb ( result ) ;
193- }
183+ public static createWithOwnerAndCallback ( owner : WeakRef < Particle > , callback : ( result : ParticleSetupMainControllerResult ) => void ) : ParticleSetupControllerDelegateImpl {
184+ const delegate = < ParticleSetupControllerDelegateImpl > ParticleSetupControllerDelegateImpl . new ( ) ;
185+ delegate . owner = owner ;
186+ delegate . cb = callback ;
187+ return delegate ;
188+ }
189+
190+ public particleSetupViewControllerDidFinishWithResultDevice ( controller : ParticleSetupMainController , result : ParticleSetupMainControllerResult , device : ParticleDevice ) : void {
191+ console . log ( "particleSetupViewControllerDidFinishWithResultDevice, result: " + result ) ;
192+ this . cb && this . cb ( result ) ;
193+ }
194+
195+ particleSetupViewControllerDidNotSucceeedWithDeviceID ( controller : ParticleSetupMainController , deviceID : string ) : void {
196+ console . log ( "particleSetupViewControllerDidFinishWithResultDevice" ) ;
194197 }
195198}
196- interface ParticleSetupMainControllerDelegate {
197- particleSetupViewControllerDidFinishWithResultDevice ( controller : any , result : any , device : any ) : void ;
198- particleSetupViewControllerDidNotSucceeedWithDeviceID ?( controller : any , deviceID : string ) : void ;
199- }
200- declare var ParticleSetupMainControllerDelegate : {
201- prototype : ParticleSetupMainControllerDelegate ;
202- } ;
0 commit comments