I have Dialog box to enter pin. if user enters the invalid pin it starts the timer. When i dismiss the dialog box on back button and then again opens the dialog box on item click it resets it states and timer is gone.Its a pin dialog box . when user enters the wrong pin a timer starts under pin layout such that he cannot enter pin for that particular time for eg: 30 seconds. While 30 seconds count down is running and i close the dialog box and immediately open it again timer is gone and it has new state.
How to save the state of dialog box after dismiss such that timer runs in background and when i open it again it shows the state where timer is running? need help regarding this
onItemClick it opens dialog box():
lockCardView.setOnClickListener {
showEnterPinDialog(Input.fromNullable(ItemList))
}
fun showEnterPinDialog(ItemList: Input<ArrayList<String>>) {
var dialog = EnterPinDialog(activity.requireActivity(),Repository, ItemList)
dialog.show()
dialog.setCanceledOnTouchOutside(false)
}
DialogBox code:
class EnterPinDialog(
context: Context,
var Repository: ItemRepository,
var ItemList: Input<ArrayList<String>>
): Dialog(context) {
lateinit var dataBind : EnterPinDialogBinding
var itemPinList: ArrayList<String> = ArrayList()
var confirm_pin :String? = null
var sum = 0
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
dataBind = DataBindingUtil.inflate(layoutInflater, R.layout.enter_pin_dialog, null, false)
setContentView(dataBind.root)
dataBind.materialTextView53.visibility = View.GONE
dataBind.pinView.transformationMethod = AsteriskPasswordTransformationMethod()
dataBind.pinView.addTextChangedListener(object : TextWatcher{
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
// TODO("Not yet implemented")
}
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
// TODO("Not yet implemented")
confirm_pin = s.toString()
val epin = md5(confirm_pin!!)
for (ls in itemPinList.indices){
if (s!!.length>5) {
if (epin.equals(itemPinList.get(ls).toString())) {
setItemLockAPi(ItemList, false)
dismiss()
}
}, 1500)
}
else
{
sum++
dataBind.pinView.setLineColor(
ContextCompat.getColor(
dataBind.root.context,
R.color.red
)
)
dataBind.materialTextView53.visibility = View.VISIBLE
dataBind.pinView.text?.clear()
dataBind.attemptsLeft.visibility = View.VISIBLE
dataBind.attemptsLeft.text = "${3 - sum} attempts left"
if(sum == 3){
dataBind.attemptsLeft.visibility = View.GONE
dataBind.materialTextView53.visibility = View.GONE
dataBind.failedAttempts.visibility = View.VISIBLE
dataBind.pinView.clearFocus()
dataBind.pinView.isEnabled = false
dataBind.timerThirty.visibility = View.VISIBLE
object : CountDownTimer(30000, 1000) {
// Callback function, fired on regular interval
override fun onTick(millisUntilFinished: Long) {
dataBind.timerThirty.text = "${millisUntilFinished / 1000} Seconds"
}
// Callback function, fired
// when the time is up
override fun onFinish() {
// textView.setText("done!")
dataBind.pinView.isEnabled = true
dataBind.failedAttempts.visibility = View.GONE
dataBind.pinView.setLineColor(
ContextCompat.getColor(
dataBind.root.context,
R.color.white
)
)
dataBind.timerThirty.visibility = View.GONE
sum = 0
}
}.start()
}
}
}
}
}
override fun afterTextChanged(s: Editable?) {
// TODO("Not yet implemented")
}
})
dataBind.backPin.setOnClickListener {
dismiss()
}
}
The timer is reset because you have
0as the default value for the timer. So when the dialog is being created, you don't give the timer a value instead it has its default value0.When a timer starts due to incorrect PIN entered, save the timer value as a
statethat keeps incrementing by the second in yourViewModel. Then when creating the dialog, check yourViewModelto see if thestatehas a value already. If yes, set the dialog timer's value to it.Don't forget to reset the state when the timer is exhausted (when the timer counts down to
zero). :)