My approach is to include everything in the viewModel including the callbacks. I then call a function in the viewModel and pass in an activity parameter. see below:
fun verifyPhoneNumber(phoneNumber: String, activity: Activity) {
_validFullPhoneNumber.value = phoneNumber
val options = PhoneAuthOptions.newBuilder(mAuth)
.setPhoneNumber(phoneNumber) // Phone number to verify
.setTimeout(60L, TimeUnit.SECONDS) // Timeout and unit
.setActivity(activity)
.setCallbacks(callbacks) // OnVerificationStateChangedCallbacks
.build()
PhoneAuthProvider.verifyPhoneNumber(options)
}
and in the UI controller, in my case a fragment I call it as:
viewModel.verifyPhoneNumber(validatedPhoneNumber, requireActivity())
same with resend button function.
viewModel:
fun resendVerificationCode(activity: Activity) {
val options =
PhoneAuthOptions.newBuilder(mAuth)
.setPhoneNumber(_validFullPhoneNumber.value!!) // Phone number to verify
.setTimeout(60L, TimeUnit.SECONDS) // Timeout and unit
.setActivity(activity)
.setCallbacks(callbacks) // OnVerificationStateChangedCallbacks
.setForceResendingToken(_resendToken) // ForceResendingToken from callbacks
.build()
PhoneAuthProvider.verifyPhoneNumber(options)
_isVerificationCodeExpired.value = false
}
UI controller(fragment):
viewModel.resendVerificationCode(requireActivity())