From 7df108e288b94376c9d056ee85ba5c80b14ed531 Mon Sep 17 00:00:00 2001 From: Adam Wushensky Date: Mon, 28 Mar 2022 10:42:05 -0700 Subject: [PATCH 1/2] Update the interface so reference to the callback is not lost on activity killed --- .../getbouncer/cardscan/ui/CardScanSheet.kt | 37 ++++++++++++------- 1 file changed, 23 insertions(+), 14 deletions(-) diff --git a/cardscan-ui/src/main/java/com/getbouncer/cardscan/ui/CardScanSheet.kt b/cardscan-ui/src/main/java/com/getbouncer/cardscan/ui/CardScanSheet.kt index 7c3cbb87..f02517ae 100644 --- a/cardscan-ui/src/main/java/com/getbouncer/cardscan/ui/CardScanSheet.kt +++ b/cardscan-ui/src/main/java/com/getbouncer/cardscan/ui/CardScanSheet.kt @@ -40,10 +40,15 @@ sealed interface CardScanSheetResult : Parcelable { class CardScanSheet private constructor(private val apiKey: String) { - private var onFinished: - ((cardScanSheetResult: CardScanSheetResult) -> Unit)? = null private lateinit var launcher: ActivityResultLauncher + /** + * Callback to notify when scanning finishes and a result is available. + */ + fun interface CardScanResultCallback { + fun onCardScanSheetResult(cardScanSheetResult: CardScanSheetResult) + } + companion object { /** * Create a [CardScanSheet] instance with [ComponentActivity]. @@ -57,9 +62,14 @@ class CardScanSheet private constructor(private val apiKey: String) { fun create( from: ComponentActivity, apiKey: String, + cardScanResultCallback: CardScanResultCallback, registry: ActivityResultRegistry = from.activityResultRegistry, ) = CardScanSheet(apiKey).apply { - launcher = from.registerForActivityResult(activityResultContract, registry, ::onResult) + launcher = from.registerForActivityResult( + activityResultContract, + registry, + cardScanResultCallback::onCardScanSheetResult, + ) } @JvmStatic @@ -67,12 +77,20 @@ class CardScanSheet private constructor(private val apiKey: String) { fun create( from: Fragment, apiKey: String, + cardScanResultCallback: CardScanResultCallback, registry: ActivityResultRegistry? = null, ) = CardScanSheet(apiKey).apply { launcher = if (registry != null) { - from.registerForActivityResult(activityResultContract, registry, ::onResult) + from.registerForActivityResult( + activityResultContract, + registry, + cardScanResultCallback::onCardScanSheetResult, + ) } else { - from.registerForActivityResult(activityResultContract, ::onResult) + from.registerForActivityResult( + activityResultContract, + cardScanResultCallback::onCardScanSheetResult, + ) } } @@ -138,9 +156,7 @@ class CardScanSheet private constructor(private val apiKey: String) { enableEnterManually: Boolean, enableNameExtraction: Boolean, enableExpiryExtraction: Boolean, - onFinished: (cardScanSheetResult: CardScanSheetResult) -> Unit ) { - this.onFinished = onFinished launcher.launch( CardScanSheetParams( apiKey = apiKey, @@ -150,11 +166,4 @@ class CardScanSheet private constructor(private val apiKey: String) { ) ) } - - /** - * When a result is available from the activity, call [onFinished] if it's available. - */ - private fun onResult(cardScanSheetResult: CardScanSheetResult) { - onFinished?.let { it(cardScanSheetResult) } - } } From 7f66760d8d9af5ebf5108bcb4ef8de7e4a1e3466 Mon Sep 17 00:00:00 2001 From: Adam Wushensky Date: Mon, 28 Mar 2022 10:46:03 -0700 Subject: [PATCH 2/2] Fix the demo --- .../java/com/getbouncer/cardscan/demo/LaunchActivity.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/cardscan-demo/src/main/java/com/getbouncer/cardscan/demo/LaunchActivity.java b/cardscan-demo/src/main/java/com/getbouncer/cardscan/demo/LaunchActivity.java index f43a6bea..79381cce 100644 --- a/cardscan-demo/src/main/java/com/getbouncer/cardscan/demo/LaunchActivity.java +++ b/cardscan-demo/src/main/java/com/getbouncer/cardscan/demo/LaunchActivity.java @@ -30,7 +30,7 @@ protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_launch); - final CardScanSheet sheet = CardScanSheet.create(this, API_KEY); + final CardScanSheet sheet = CardScanSheet.create(this, API_KEY, this::handleScanResult); // Because this activity displays card numbers, disallow screenshots. getWindow().setFlags( @@ -52,8 +52,7 @@ protected void onCreate(@Nullable Bundle savedInstanceState) { sheet.present( /* enableEnterCardManually */ enableEnterCardManually, /* enableExpiryExtraction */ enableExpiryExtraction, - /* enableNameExtraction */ enableNameExtraction, - this::handleScanResult + /* enableNameExtraction */ enableNameExtraction ); });