I have a checkbox that, when checked, triggers an alert asking if the user is sure. Upon agreement, all exercises are marked as done.
The problem: I use local storage to save an item called didAllExercises
. When reopening the app, if this item is true, the checkbox is set to true. However, there is an (onChange) event
on the checkbox that displays an alert when checked. This causes the alert to appear every time the app is reopened, even if the didAllExercises
item is true.
Here is the checkbox:
<ion-checkbox checked="false" [(ngModel)]="cheatCheckbox" (ionChange)="cheatButton(cheatCheckbox)"></ion-checkbox>
This is my cheatButton()
function:
cheatButton(cheatCheckbox: boolean){
if(cheatCheckbox){
localForage.setItem("showAlert", true);
console.log('ccTrue', cheatCheckbox);
//DO NOT SHOW ALERT WHEN ALL EXERCISES ARE DONE AND YOU REOPEN THE APP TO NAVIGATE
setTimeout(() => {
localForage.getItem("showAlert", (err, value) => {
if(value){
this.showAlert = value;
console.log('!notRun', value);
}
})
},400)
setTimeout(() => {
let alert = this.alertCtrl.create({
title: 'Complete all exercises',
message: 'Are you sure you have completed all exercises?',
buttons: [
{
text: 'No',
role: 'cancel',
handler: () => {
console.log('Cancel clicked');
this.cheatCheckbox = false;
}
},
{
text: 'Yes, I am sure!',
handler: () => {
this.allExercisesDone = true;
localForage.setItem('didAllExercises', [true, this.data]);
}
}
]
});
alert.present();
},400)
}
}
And here I call the getItem method
in the ionViewWillEnter
:
localForage.getItem('didAllExercises', (err, value) => {
if(value){
this.cheatCheckbox = true;
}
})
How can I resolve this issue so that the alert only appears once upon clicking it, and then subsequently opening the app with the checkbox set to true without triggering the same alert?