Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,20 @@ class DemoActivity : AppCompatActivity() {
launch.setOnClickListener {
startActivityForResult(Intent(this, ScanQrActivity::class.java).apply {
putExtra(ScanQrActivity.OPTION_SHOW_BARCODE_BOX, BuildConfig.DEBUG)
putExtra(ScanQrActivity.AUTOACCEPT_RESULT, false)
}, REQUEST_SCAN)
}

// Auto-accept scanner
launch_autoaccept.setOnClickListener {
startActivityForResult(Intent(this, ScanQrActivity::class.java).apply {
putExtra(ScanQrActivity.OPTION_SHOW_BARCODE_BOX, BuildConfig.DEBUG)
putExtra(ScanQrActivity.AUTOACCEPT_RESULT, true)
Copy link
Member

@westonal westonal Jun 2, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Idea for future syntax:

startActivityForResult(Intent(this, ScanQrActivity::class.java)
    .withScannerOptions {
        if (BuildConfig.DEBUG) showBox()
        autoAccept()
    }, REQUEST_SCAN)

Then these ugly CAPITAL options and the room for error in the values passed to them disappears.

fun Intent.withScannerOptions(function: (IntentOptions) -> Unit): Intent {
    function(IntentOptions(this))
    return this
}

class IntentOptions(private intent: Intent) {
    fun autoAccept(){
        intent.putExtra(ScanQrActivity.AUTOACCEPT_RESULT, true)
    }
    //etc
}

You get the idea? Would be separate issue and PR.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, This is a good use of extension functions.

}, REQUEST_SCAN)
}

// Custom scanner
launch_xpub.setOnClickListener {
launch_xpub.setOnClickListener {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Be nice to revert this extra space.

startActivityForResult(Intent(this, XPubScannerActivity::class.java).apply {
putExtra(ScanQrActivity.OPTION_SHOW_BARCODE_BOX, BuildConfig.DEBUG)
}, REQUEST_SCAN)
Expand Down
13 changes: 12 additions & 1 deletion app/src/main/res/layout/content_demo.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,23 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/launch_scanner"
app:layout_constraintBottom_toTopOf="@+id/launch_xpub"
app:layout_constraintBottom_toTopOf="@+id/launch_autoaccept"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>

<Button
android:id="@+id/launch_autoaccept"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/launch_scanner_auto_accept"
app:layout_constraintBottom_toTopOf="@+id/launch_xpub"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/launch"/>

<Button
android:id="@+id/launch_xpub"
android:layout_width="wrap_content"
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
<string name="title_activity_scan_xpub">Scan XPub</string>

<string name="launch_scanner">Launch scanner</string>
<string name="launch_scanner_auto_accept">Launch scanner auto-accept</string>
<string name="launch_xpub_scanner">Launch xpub scanner</string>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,7 @@

package io.github.novacrypto.qrscanner

internal class Options(val showBarcodeBounds: Boolean)
internal class Options(
val showBarcodeBounds: Boolean,
val autoAcceptResult: Boolean
)
17 changes: 11 additions & 6 deletions qrScanner/src/main/java/io/github/novacrypto/qrscanner/QrCamera.kt
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,17 @@ internal class QrCamera(
if (value == field) return
field = value
value?.let {
Snackbar.make(cameraSurfaceView, it, Snackbar.LENGTH_INDEFINITE)
.setAction(android.R.string.ok, { _ ->
Timber.d("Accepted barcode")
onAccept(it)
})
.show()
if (options.autoAcceptResult) {
Timber.d("Accepted barcode automatically")
onAccept(it)
} else {
Snackbar.make(cameraSurfaceView, it, Snackbar.LENGTH_INDEFINITE)
.setAction(android.R.string.ok, { _ ->
Timber.d("Accepted barcode")
onAccept(it)
})
.show()
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ open class ScanQrActivity : AppCompatActivity() {
companion object {
const val OPTION_SHOW_BARCODE_BOX = "SHOW_BARCODE_BOX"
const val BARCODE_DATA = "BARCODE_DATA"
const val AUTOACCEPT_RESULT = "OPTION_AUTOACCEPT_RESULT"
internal const val CAMERA_PERMISSION_RESPONSE = 23
}

Expand Down Expand Up @@ -75,8 +76,10 @@ open class ScanQrActivity : AppCompatActivity() {
super.onDestroy()
}

private fun Intent?.toOptions() =
Options(this?.extras?.getBoolean(OPTION_SHOW_BARCODE_BOX) ?: false)
private fun Intent?.toOptions() = Options(
this?.extras?.getBoolean(OPTION_SHOW_BARCODE_BOX) ?: false,
this?.extras?.getBoolean(AUTOACCEPT_RESULT) ?: false
)

override fun onRequestPermissionsResult(
requestCode: Int,
Expand Down