Skip to content

Commit ae209fb

Browse files
committed
Refactor(api): Convert biometricAuthAvailable and deleteKeys to asynchronous calls
Updates the `BiometricSignatureApi` to use callback-based (Android) and completion-based (iOS/macOS) asynchronous patterns for `biometricAuthAvailable` and `deleteKeys`. This ensures consistency across the plugin's native implementations and aligns with Pigeon's asynchronous messaging requirements. - **Android**: Updated `biometricAuthAvailable` and `deleteKeys` to accept a `Result` callback. - **iOS/macOS**: Refactored `biometricAuthAvailable` and `deleteKeys` to use `@escaping` completion handlers instead of synchronous return values. - **Pigeon**: Updated the generated API glue code to handle the asynchronous message flow for these methods.
1 parent 4202f47 commit ae209fb

File tree

4 files changed

+34
-29
lines changed

4 files changed

+34
-29
lines changed

android/src/main/kotlin/com/visionflutter/biometric_signature/BiometricSignaturePlugin.kt

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -95,15 +95,16 @@ class BiometricSignaturePlugin : FlutterPlugin, BiometricSignatureApi, ActivityA
9595

9696
// ==================== BiometricSignatureApi Implementation ====================
9797

98-
override fun biometricAuthAvailable(): BiometricAvailability {
98+
override fun biometricAuthAvailable(callback: (Result<BiometricAvailability>) -> Unit) {
9999
val act = activity
100100
if (act == null) {
101-
return BiometricAvailability(
101+
callback(Result.success(BiometricAvailability(
102102
canAuthenticate = false,
103103
hasEnrolledBiometrics = false,
104104
availableBiometrics = emptyList(),
105105
reason = "NO_ACTIVITY"
106-
)
106+
)))
107+
return
107108
}
108109

109110
val manager = BiometricManager.from(act)
@@ -118,12 +119,12 @@ class BiometricSignaturePlugin : FlutterPlugin, BiometricSignatureApi, ActivityA
118119

119120
val reason = if (!canAuthenticate) biometricErrorName(canAuth) else null
120121

121-
return BiometricAvailability(
122+
callback(Result.success(BiometricAvailability(
122123
canAuthenticate = canAuthenticate,
123124
hasEnrolledBiometrics = hasEnrolledBiometrics,
124125
availableBiometrics = types,
125126
reason = reason
126-
)
127+
)))
127128
}
128129

129130
override fun createKeys(
@@ -440,9 +441,9 @@ class BiometricSignaturePlugin : FlutterPlugin, BiometricSignatureApi, ActivityA
440441
}
441442
}
442443

443-
override fun deleteKeys(): Boolean {
444+
override fun deleteKeys(callback: (Result<Boolean>) -> Unit) {
444445
deleteAllKeys()
445-
return true
446+
callback(Result.success(true))
446447
}
447448

448449
override fun getKeyInfo(checkValidity: Boolean, keyFormat: KeyFormat, callback: (Result<KeyInfo>) -> Unit) {

ios/Classes/BiometricSignaturePlugin.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ public class BiometricSignaturePlugin: NSObject, FlutterPlugin, BiometricSignatu
130130

131131
// MARK: - BiometricSignatureApi Implementation
132132

133-
func biometricAuthAvailable() throws -> BiometricAvailability {
133+
func biometricAuthAvailable(completion: @escaping (Result<BiometricAvailability, Error>) -> Void) {
134134
let context = LAContext()
135135
var error: NSError?
136136
let canEvaluate = context.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &error)
@@ -149,12 +149,12 @@ public class BiometricSignaturePlugin: NSObject, FlutterPlugin, BiometricSignatu
149149

150150
let hasEnrolled = error?.code != LAError.biometryNotEnrolled.rawValue
151151

152-
return BiometricAvailability(
152+
completion(.success(BiometricAvailability(
153153
canAuthenticate: canEvaluate,
154154
hasEnrolledBiometrics: hasEnrolled,
155155
availableBiometrics: availableBiometrics,
156156
reason: error?.localizedDescription
157-
)
157+
)))
158158
}
159159

160160
func createKeys(
@@ -361,9 +361,9 @@ public class BiometricSignaturePlugin: NSObject, FlutterPlugin, BiometricSignatu
361361
}
362362
}
363363

364-
func deleteKeys() throws -> Bool {
364+
func deleteKeys(completion: @escaping (Result<Bool, Error>) -> Void) {
365365
deleteExistingKeys()
366-
return true
366+
completion(.success(true))
367367
}
368368

369369
func getKeyInfo(checkValidity: Bool, keyFormat: KeyFormat, completion: @escaping (Result<KeyInfo, Error>) -> Void) {

macos/Classes/BiometricSignatureApi.swift

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -761,7 +761,7 @@ class BiometricSignatureApiPigeonCodec: FlutterStandardMessageCodec, @unchecked
761761
/// Generated protocol from Pigeon that represents a handler of messages from Flutter.
762762
protocol BiometricSignatureApi {
763763
/// Checks if biometric authentication is available.
764-
func biometricAuthAvailable() throws -> BiometricAvailability
764+
func biometricAuthAvailable(completion: @escaping (Result<BiometricAvailability, Error>) -> Void)
765765
/// Creates a new key pair.
766766
///
767767
/// [config] contains platform-specific options. See [CreateKeysConfig].
@@ -785,7 +785,7 @@ protocol BiometricSignatureApi {
785785
/// [promptMessage] is the message shown to the user during authentication.
786786
func decrypt(payload: String, payloadFormat: PayloadFormat, config: DecryptConfig?, promptMessage: String?, completion: @escaping (Result<DecryptResult, Error>) -> Void)
787787
/// Deletes keys.
788-
func deleteKeys() throws -> Bool
788+
func deleteKeys(completion: @escaping (Result<Bool, Error>) -> Void)
789789
/// Gets detailed information about existing biometric keys.
790790
///
791791
/// Returns key metadata including algorithm, size, validity, and public keys.
@@ -802,11 +802,13 @@ class BiometricSignatureApiSetup {
802802
let biometricAuthAvailableChannel = FlutterBasicMessageChannel(name: "dev.flutter.pigeon.biometric_signature.BiometricSignatureApi.biometricAuthAvailable\(channelSuffix)", binaryMessenger: binaryMessenger, codec: codec)
803803
if let api = api {
804804
biometricAuthAvailableChannel.setMessageHandler { _, reply in
805-
do {
806-
let result = try api.biometricAuthAvailable()
807-
reply(wrapResult(result))
808-
} catch {
809-
reply(wrapError(error))
805+
api.biometricAuthAvailable { result in
806+
switch result {
807+
case .success(let res):
808+
reply(wrapResult(res))
809+
case .failure(let error):
810+
reply(wrapError(error))
811+
}
810812
}
811813
}
812814
} else {
@@ -895,11 +897,13 @@ class BiometricSignatureApiSetup {
895897
let deleteKeysChannel = FlutterBasicMessageChannel(name: "dev.flutter.pigeon.biometric_signature.BiometricSignatureApi.deleteKeys\(channelSuffix)", binaryMessenger: binaryMessenger, codec: codec)
896898
if let api = api {
897899
deleteKeysChannel.setMessageHandler { _, reply in
898-
do {
899-
let result = try api.deleteKeys()
900-
reply(wrapResult(result))
901-
} catch {
902-
reply(wrapError(error))
900+
api.deleteKeys { result in
901+
switch result {
902+
case .success(let res):
903+
reply(wrapResult(res))
904+
case .failure(let error):
905+
reply(wrapError(error))
906+
}
903907
}
904908
}
905909
} else {

macos/Classes/BiometricSignaturePlugin.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ public class BiometricSignaturePlugin: NSObject, FlutterPlugin, BiometricSignatu
144144

145145
// MARK: - BiometricSignatureApi Implementation
146146

147-
func biometricAuthAvailable() throws -> BiometricAvailability {
147+
func biometricAuthAvailable(completion: @escaping (Result<BiometricAvailability, Error>) -> Void) {
148148
let context = LAContext()
149149
var error: NSError?
150150
let canEvaluate = context.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &error)
@@ -163,12 +163,12 @@ public class BiometricSignaturePlugin: NSObject, FlutterPlugin, BiometricSignatu
163163

164164
let hasEnrolled = error?.code != LAError.biometryNotEnrolled.rawValue
165165

166-
return BiometricAvailability(
166+
completion(.success(BiometricAvailability(
167167
canAuthenticate: canEvaluate,
168168
hasEnrolledBiometrics: hasEnrolled,
169169
availableBiometrics: availableBiometrics,
170170
reason: error?.localizedDescription
171-
)
171+
)))
172172
}
173173

174174
func createKeys(
@@ -255,9 +255,9 @@ public class BiometricSignaturePlugin: NSObject, FlutterPlugin, BiometricSignatu
255255
}
256256
}
257257

258-
func deleteKeys() throws -> Bool {
258+
func deleteKeys(completion: @escaping (Result<Bool, Error>) -> Void) {
259259
deleteExistingKeys()
260-
return true
260+
completion(.success(true))
261261
}
262262

263263
func getKeyInfo(checkValidity: Bool, keyFormat: KeyFormat, completion: @escaping (Result<KeyInfo, Error>) -> Void) {

0 commit comments

Comments
 (0)