Below is my code to get the result from the barcode scanner, then verify if it is a legit referral code that exist within the Firebase database. If so, the scanner should then display a message to tell the user that they have successfully connected and output the name of the owner of the referral code. However, the listener is somehow not executing, therefore the result is just a blank alert dialog with the "Result" title.
var barcodeLauncher = registerForActivityResult(
ScanContract()
) { result: ScanIntentResult ->
if (result.contents != null) {
val builder =
AlertDialog.Builder(this@ReferralActivity)
val ref = FirebaseDatabase.getInstance().getReference("Users")
builder.setTitle("Result")
var name = ""
ref.addListenerForSingleValueEvent(object:ValueEventListener{
override fun onDataChange(snapshot: DataSnapshot) {
var exists = false
for (postSnapshot in snapshot.children) {
var refcode = "${postSnapshot.child("referralCode").value}"
if (refcode == result.contents) {
name = "${snapshot.child("name").value}"
exists = true
break
}
}
if (exists) {
builder.setMessage("You've successfully connected with $name, enjoy your rewards!")
} else {
builder.setMessage("No user with such referral code is found...")
}
}
override fun onCancelled(error: DatabaseError) {
builder.setMessage("No user with such referral code is found...")
}
})
builder.setPositiveButton(
"OK"
) { dialogInterface, i -> dialogInterface.dismiss() }.show()
}
}