@@ -16,11 +16,7 @@ let kFLTFirebaseInstallationsChannelName = "plugins.flutter.io/firebase_app_inst
16
16
public class FirebaseInstallationsPluginSwift : FLTFirebasePlugin , FlutterPlugin {
17
17
private var eventSink : FlutterEventSink ?
18
18
private var messenger : FlutterBinaryMessenger
19
-
20
- var result : FLTFirebaseMethodCallResult ?
21
- var streamHandler = [ String: IdChangedStreamHandler? ] ( )
22
-
23
- var args = NSDictionary ( )
19
+ private var streamHandler = [ String: IdChangedStreamHandler? ] ( )
24
20
25
21
init ( messenger: FlutterBinaryMessenger ) {
26
22
self . messenger = messenger
@@ -45,60 +41,71 @@ public class FirebaseInstallationsPluginSwift: FLTFirebasePlugin, FlutterPlugin
45
41
46
42
/// Gets Installations instance for a Firebase App.
47
43
/// - Returns: a Firebase Installations instance for the passed app from Dart
48
- func getInstallations( ) -> Installations {
49
- let app : FirebaseApp = FLTFirebasePlugin . firebaseAppNamed ( args [ " appName " ] as! String ) !
44
+ private func getInstallations( appName : String ) -> Installations {
45
+ let app : FirebaseApp = FLTFirebasePlugin . firebaseAppNamed ( appName) !
50
46
return Installations . installations ( app: app)
51
47
}
52
48
53
49
/// Gets Installations Id for an instance.
54
50
/// - Parameter arguments: the arguments passed by the Dart calling method
55
51
/// - Parameter result: the result instance used to send the result to Dart.
56
- func getId( ) {
57
- let instance : Installations = getInstallations ( )
52
+ /// - Parameter errorBlock: the error block used to send the error to Dart.
53
+ private func getId( arguments: NSDictionary , result: @escaping FlutterResult ,
54
+ errorBlock: @escaping FLTFirebaseMethodCallErrorBlock ) {
55
+ let instance = getInstallations ( appName: arguments [ " appName " ] as! String )
58
56
instance. installationID { ( id: String ? , error: Error ? ) in
59
- if error != nil {
60
- self . result! . error ( nil , nil , nil , error)
57
+ if let error {
58
+ errorBlock ( nil , nil , nil , error)
61
59
} else {
62
- self . result! . success ( id)
60
+ result ( id)
63
61
}
64
62
}
65
63
}
66
64
67
- /// Deletes Installations Id for an instance.
68
- func deleteId( ) {
69
- let instance : Installations = getInstallations ( )
65
+ /// Deletes the Installations Id for an instance.
66
+ /// - Parameter arguments: the arguments passed by the Dart calling method
67
+ /// - Parameter result: the result instance used to send the result to Dart.
68
+ /// - Parameter errorBlock: the error block used to send the error to Dart.
69
+ private func deleteId( arguments: NSDictionary , result: @escaping FlutterResult ,
70
+ errorBlock: @escaping FLTFirebaseMethodCallErrorBlock ) {
71
+ let instance = getInstallations ( appName: arguments [ " appName " ] as! String )
70
72
instance. delete { ( error: Error ? ) in
71
- if error != nil {
72
- self . result! . error ( nil , nil , nil , error)
73
+ if let error {
74
+ errorBlock ( nil , nil , nil , error)
73
75
} else {
74
- self . result! . success ( nil )
76
+ result ( nil )
75
77
}
76
78
}
77
79
}
78
80
79
- /// Gets the token Id for an instance.
80
- func getToken( ) {
81
- let instance : Installations = getInstallations ( )
82
- let forceRefresh : Bool = ( args [ " forceRefresh " ] as? Bool ) ?? false
83
-
84
- instance. authTokenForcingRefresh (
85
- forceRefresh,
86
- completion: { ( tokenResult: InstallationsAuthTokenResult ? , error: Error ? ) in
87
- if error != nil {
88
- self . result!. error ( nil , nil , nil , error)
89
- } else {
90
- self . result!. success ( tokenResult? . authToken)
91
- }
81
+ /// Gets the Auth Token for an instance.
82
+ /// - Parameter arguments: the arguments passed by the Dart calling method
83
+ /// - Parameter result: the result instance used to send the result to Dart.
84
+ /// - Parameter errorBlock: the error block used to send the error to Dart.
85
+ private func getToken( arguments: NSDictionary , result: @escaping FlutterResult ,
86
+ errorBlock: @escaping FLTFirebaseMethodCallErrorBlock ) {
87
+ let instance = getInstallations ( appName: arguments [ " appName " ] as! String )
88
+ let forceRefresh = arguments [ " forceRefresh " ] as? Bool ?? false
89
+ instance
90
+ . authTokenForcingRefresh ( forceRefresh) { ( tokenResult: InstallationsAuthTokenResult ? ,
91
+ error: Error ? ) in
92
+ if let error {
93
+ errorBlock ( nil , nil , nil , error)
94
+ } else {
95
+ result ( tokenResult? . authToken)
96
+ }
92
97
}
93
- )
94
98
}
95
99
96
- /// Starts listening to Installation ID events for an instance.
97
- func registerIdChangeListener( ) {
98
- let instance : Installations = getInstallations ( )
99
-
100
- let appName = ( args [ " appName " ] as! String )
101
- let eventChannelName : String = kFLTFirebaseInstallationsChannelName + " /token/ " + appName
100
+ /// Registers a listener for changes in the Installations Id.
101
+ /// - Parameter arguments: the arguments passed by the Dart calling method
102
+ /// - Parameter result: the result instance used to send the result to Dart.
103
+ /// - Parameter errorBlock: the error block used to send the error to Dart.
104
+ private func registerIdChangeListener( arguments: NSDictionary , result: @escaping FlutterResult ,
105
+ errorBlock: @escaping FLTFirebaseMethodCallErrorBlock ) {
106
+ let instance = getInstallations ( appName: arguments [ " appName " ] as! String )
107
+ let appName = arguments [ " appName " ] as! String
108
+ let eventChannelName = kFLTFirebaseInstallationsChannelName + " /token/ " + appName
102
109
103
110
let eventChannel = FlutterEventChannel ( name: eventChannelName, binaryMessenger: messenger)
104
111
@@ -108,10 +115,10 @@ public class FirebaseInstallationsPluginSwift: FLTFirebasePlugin, FlutterPlugin
108
115
109
116
eventChannel. setStreamHandler ( streamHandler [ eventChannelName] !)
110
117
111
- result? . success ( eventChannelName)
118
+ result ( eventChannelName)
112
119
}
113
120
114
- func mapInstallationsErrorCodes( code: UInt ) -> NSString {
121
+ private func mapInstallationsErrorCodes( code: UInt ) -> NSString {
115
122
let error = InstallationsErrorCode ( InstallationsErrorCode
116
123
. Code ( rawValue: Int ( code) ) ?? InstallationsErrorCode . unknown)
117
124
@@ -130,44 +137,49 @@ public class FirebaseInstallationsPluginSwift: FLTFirebasePlugin, FlutterPlugin
130
137
}
131
138
132
139
public func handle( _ call: FlutterMethodCall , result: @escaping FlutterResult ) {
133
- let args = call. arguments as! NSDictionary
134
-
135
- let errorBlock : FLTFirebaseMethodCallErrorBlock = { ( code, message, details, error: Error ? ) in
136
- var errorDetails = [ String: Any? ] ( )
137
-
138
- errorDetails [ " code " ] = code ?? self
139
- . mapInstallationsErrorCodes ( code: UInt ( ( error! as NSError ) . code) )
140
- errorDetails [ " message " ] = message ?? error?
141
- . localizedDescription ?? " An unknown error has occurred. "
142
- errorDetails [ " additionalData " ] = details
143
-
144
- if code == " unknown " {
145
- NSLog (
146
- " FLTFirebaseInstallations: An error occurred while calling method %@ " ,
147
- call. method
148
- )
149
- }
150
-
151
- result ( FLTFirebasePlugin . createFlutterError ( fromCode: errorDetails [ " code " ] as! String ,
152
- message: errorDetails [ " message " ] as! String ,
153
- optionalDetails: errorDetails [
154
- " additionalData "
155
- ] as? [ AnyHashable : Any ] ,
156
- andOptionalNSError: error) )
140
+ guard let args = call. arguments as? NSDictionary else {
141
+ result ( FlutterError (
142
+ code: " invalid-arguments " ,
143
+ message: " Arguments are not a dictionary " ,
144
+ details: nil
145
+ ) )
146
+ return
157
147
}
158
148
159
- self . result = . create( success: result, andErrorBlock: errorBlock)
160
- self . args = args
149
+ let errorBlock : FLTFirebaseMethodCallErrorBlock = { ( code, message, details,
150
+ error: Error ? ) in
151
+ var errorDetails = [ String: Any? ] ( )
152
+
153
+ errorDetails [ " code " ] = code ?? self
154
+ . mapInstallationsErrorCodes ( code: UInt ( ( error! as NSError ) . code) )
155
+ errorDetails [ " message " ] = message ?? error?
156
+ . localizedDescription ?? " An unknown error has occurred. "
157
+ errorDetails [ " additionalData " ] = details
158
+
159
+ if code == " unknown " {
160
+ NSLog (
161
+ " FLTFirebaseInstallations: An error occurred while calling method %@ " ,
162
+ call. method
163
+ )
164
+ }
165
+
166
+ result ( FLTFirebasePlugin . createFlutterError ( fromCode: errorDetails [ " code " ] as! String ,
167
+ message: errorDetails [ " message " ] as! String ,
168
+ optionalDetails: errorDetails [
169
+ " additionalData "
170
+ ] as? [ AnyHashable : Any ] ,
171
+ andOptionalNSError: error) )
172
+ }
161
173
162
174
switch call. method {
163
175
case " FirebaseInstallations#getId " :
164
- getId ( )
176
+ getId ( arguments : args , result : result , errorBlock : errorBlock )
165
177
case " FirebaseInstallations#delete " :
166
- deleteId ( )
178
+ deleteId ( arguments : args , result : result , errorBlock : errorBlock )
167
179
case " FirebaseInstallations#getToken " :
168
- getToken ( )
180
+ getToken ( arguments : args , result : result , errorBlock : errorBlock )
169
181
case " FirebaseInstallations#registerIdChangeListener " :
170
- registerIdChangeListener ( )
182
+ registerIdChangeListener ( arguments : args , result : result , errorBlock : errorBlock )
171
183
default :
172
184
result ( FlutterMethodNotImplemented)
173
185
}
0 commit comments