Add QR Code to get adress dialog (#3186)

This commit is contained in:
benthecarman 2021-05-28 04:33:37 -07:00 committed by GitHub
parent 4b42b90784
commit 03a0ca5ee9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 55 additions and 6 deletions

View File

@ -72,6 +72,7 @@ object BundleGUI extends WalletGUI with JFXApp {
scene = guiScene
icons.add(GUIUtil.logo)
}
taskRunner
def changeToWalletGUIScene(): Unit = {
Platform.runLater(

View File

@ -95,4 +95,5 @@ object GUI extends WalletGUI with JFXApp {
}
fetchStartingData()
taskRunner
}

View File

@ -9,16 +9,18 @@ import org.bitcoins.core.wallet.fee.FeeUnit
import org.bitcoins.gui.dialog._
import org.bitcoins.gui.util.GUIUtil
import scalafx.application.Platform
import scalafx.beans.property.{ObjectProperty, StringProperty}
import scalafx.beans.property._
import scalafx.scene.control.Alert
import scalafx.scene.control.Alert.AlertType
import scalafx.stage.Window
import scala.concurrent.duration.DurationInt
import scala.concurrent.{Await, Promise}
import scala.util.{Failure, Success, Try}
class WalletGUIModel()(implicit system: ActorSystem) {
var taskRunner: TaskRunner = _
import system.dispatcher
// Sadly, it is a Java "pattern" to pass null into
// constructors to signal that you want some default
@ -35,7 +37,6 @@ class WalletGUIModel()(implicit system: ActorSystem) {
}
def startWalletInfoScheduler(): Cancellable = {
import system.dispatcher
system.scheduler.scheduleAtFixedRate(0.seconds, 10.seconds)(
UpdateWalletInfoRunnable)
}
@ -49,19 +50,22 @@ class WalletGUIModel()(implicit system: ActorSystem) {
}
def onGetNewAddress(): Unit = {
val address = StringProperty("")
val addressP = Promise[String]()
taskRunner.run(
caption = "Get New Address",
op = {
ConsoleCli.exec(GetNewAddress(None),
GlobalData.consoleCliConfig) match {
case Success(commandReturn) => address.value = commandReturn
case Failure(err) => throw err
case Success(commandReturn) => addressP.success(commandReturn)
case Failure(err) =>
addressP.failure(err)
throw err
}
}
)
val address = Await.result(addressP.future, 15.seconds)
GetNewAddressDialog.showAndWait(parentWindow.value, address)
}

View File

@ -1,9 +1,16 @@
package org.bitcoins.gui.dialog
import com.google.zxing.BarcodeFormat
import com.google.zxing.common.BitMatrix
import com.google.zxing.qrcode.QRCodeWriter
import javafx.scene.image.WritableImage
import javafx.scene.paint.Color
import org.bitcoins.core.protocol.BitcoinAddress
import org.bitcoins.gui.GlobalData
import scalafx.Includes._
import scalafx.beans.property.StringProperty
import scalafx.scene.control.{ButtonType, Dialog, TextArea}
import scalafx.scene.image.{Image, ImageView}
import scalafx.stage.Window
object GetNewAddressDialog {
@ -27,7 +34,33 @@ object GetNewAddressDialog {
text <== address
editable = false
}
val image: Image =
generateQRCode(address = BitcoinAddress.fromString(address.getValue))
val qrView = new ImageView(image)
dialog.graphic = qrView
val _ = dialog.showAndWait()
}
private def generateQRCode(address: BitcoinAddress): Image = {
val width = 250
val height = 250
val qrCodeWriter = new QRCodeWriter
val bitMatrix: BitMatrix =
qrCodeWriter.encode(s"bitcoin:$address",
BarcodeFormat.QR_CODE,
width,
height)
val writableImage = new WritableImage(width, height)
val pixelWriter = writableImage.getPixelWriter
0.until(height).map { x =>
0.until(width).map { y =>
val color = if (bitMatrix.get(x, y)) Color.BLACK else Color.WHITE
pixelWriter.setColor(x, y, color)
}
}
writableImage
}
}

View File

@ -62,6 +62,8 @@ object Deps {
val sttpV = "1.7.2"
val codehausV = "3.1.4"
val scalaJsTimeV = "2.3.0"
val zxingV = "3.4.1"
}
object Compile {
@ -216,6 +218,12 @@ object Deps {
val dropwizardMetrics =
"io.dropwizard.metrics" % "metrics-core" % V.dropwizardMetricsV withSources () withJavadoc ()
val zxingCore =
"com.google.zxing" % "core" % V.zxingV withSources () withJavadoc ()
val zxingJ2SE =
"com.google.zxing" % "javase" % V.zxingV withSources () withJavadoc ()
}
object Test {
@ -410,7 +418,9 @@ object Deps {
val gui = List(Compile.akkaActor,
Compile.breezeViz,
Compile.scalaFx) ++ Compile.javaFxDeps
Compile.scalaFx,
Compile.zxingCore,
Compile.zxingJ2SE) ++ Compile.javaFxDeps
val server = Def.setting {
List(