Skip to content

Commit

Permalink
Merge pull request #499 from getbouncer/awush/update_interface
Browse files Browse the repository at this point in the history
Update the interface so reference to the callback is not lost on activity killed
  • Loading branch information
awush-stripe authored Mar 28, 2022
2 parents 454b180 + 7f66760 commit f422a32
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -52,8 +52,7 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
sheet.present(
/* enableEnterCardManually */ enableEnterCardManually,
/* enableExpiryExtraction */ enableExpiryExtraction,
/* enableNameExtraction */ enableNameExtraction,
this::handleScanResult
/* enableNameExtraction */ enableNameExtraction
);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<CardScanSheetParams>

/**
* 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].
Expand All @@ -57,22 +62,35 @@ 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
@JvmOverloads
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,
)
}
}

Expand Down Expand Up @@ -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,
Expand All @@ -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) }
}
}

0 comments on commit f422a32

Please sign in to comment.