Ionic Alert: Wait until button is pressed - ionic-framework

I have a class Action with it's method do() wherein the IonicAlert is called.
What I want to do now is, that I call something like
Action.do().then( () => { /* do domething */ } );
but only after OK was clicked on the alert.
do(): Promise<boolean> {
let alert = this.alertCtrl.create({
buttons: [{
text: 'OK',
handler: () => {
alert.dismiss().then( () => { /* do something */ });
return false;
}
}]
});
alert.present();
return null;
}
}
I added return null; only to get no error, but of course it's not working.
Any idea, how to solve this? Thanks
PS: I also posted it to the ionic forum: https://forum.ionicframework.com/t/ionic-alert-wait-until-button-is-pressed/67448

Found the solution with the help of this site: https://basarat.gitbooks.io/typescript/content/docs/promise.html
do(): Promise<boolean> {
return new Promise((resolve, reject) => {
let alert = this.alertCtrl.create({
buttons: [{
text: 'OK',
handler: () => {
alert.dismiss().then(() => { resolve(true); });
return false;
}
}]
});
alert.present();
});
}
}

Here a version who can return true or false :
showConfirm(): Promise<boolean> {
return new Promise((resolve, reject) =>{
const confirm = this.alertCtrl.create({
title : 'Are you sure ?',
buttons: [
{
text: 'Yes',
handler:_=> resolve(true)
},
{
text: 'No',
handler:_=> resolve(false)
}
]
}).present();
})
}
To call the promise :
this.showConfirm().then((result) => {
if(result){
// do something
}
})

This works for me
handler: () => {
console.log(this.viewCtrl.dismiss());
}

That code didn't work for me in Ionic4. This did:
presentAlert():Promise<boolean> {
return new Promise((resolve, reject) => {
const ctl = this.alertController;
let alert:any = this.alertController.create({
buttons: [{
text: 'OK',
handler: () => {
ctl.dismiss().then(() => { resolve(true); });
return false;
}
}]
}).then((dlg) => dlg.present());
});
}

Related

FileTransfer download returns error with all properties null

I'm trying to use Ionic FileTransfer, but it always returns me with this response:
PS: The url is a direct link to the download
I really have no idea of whats wrong in this code
FileTransferError
{
body:null,
code:null,
exception:null,
http_status:null,
source:null,
target:null
}
My code:
const storageDirectory = '';
this.platform.ready().then(() => {
storageDirectory = cordova.file.externalRootDirectory;
let url = this.apiURL + itemId
const fileTransfer: FileTransferObject = this.transfer.create();
fileTransfer.download(url, storageDirectory + "item" + itemId+ ".xml")
.then(response => {
const alertSuccess = this.alertCtrl.create({
title: `Successfully downloaded!`,
subTitle: `The file is in: \n: ${response.toURL()}`,
buttons: ['Ok']
});
alertSuccess.present();
})
.catch(err => {
const alert = this.alertCtrl.create({
title: 'Error!',
subTitle: 'Try again!',
buttons: [{
text: 'OK',
handler: () => {
this.util.getPermission();
}
}]
});
alert.present();
});
})
}

how can i return Boolean value on popup confirmation button click in ionic?

when I call this.AlertProvider.presentConfirm() then an ionic popup confirmation display on click confirm button i want to return true/false
presentConfirm() {
let alert = this.alertCtrl.create({
title: 'Confirm Activate',
message: 'Do you want to Disable',
buttons: [
{
text: 'Cancel',
role: 'cancel',
handler: () => {
console.log('Cancel clicked');
}
},
{
text: 'Confirm',
handler: () => {
console.log('submit clicked');
}
}
]
});
alert.present();
}
var confirm_value = this.AlertProvider.presentConfirm();
alert(confirm_value);

How to test $ionicPopup.show

I have an ionicPopup like so:
function editNotifications() {
var popUp = $ionicPopup.show({
template: '<ion-checkbox ng-repeat="item in vm.notifications" ng-model="item.notification" ng-checked="item.notification">' +
'<span class="notificationsFont">{{item.settingsText}}</span></ion-checkbox>',
title: 'Notification Settings',
scope: $scope,
buttons: [
{
text: 'Cancel',
onTap: function(e) {
vm.notifications = localStorageManager.get('notificationStatus');
}
},
{
text: '<b>Ok</b>',
type: 'button-positive',
onTap: function(e) {
localStorageManager.set('notificationStatus', vm.notifications);
vm.notificationText = "Notifications off";
setNotificationValuesBasedOnUserInput(vm.notifications);
}
}]
});
};
Here is what I have so far;
it('Should edit notifications', function() {
spyOn($ionicPopup, 'show').and.callFake(function() {
return $q.resolve(true);
})
spyOn(localStorageManager, 'set');
controller = createController();
controller.editNotifications();
$scope.$apply();
expect(localStorageManager.set).toHaveBeenCalled();
// expect(localStorageManager.set).toHaveBeenCalledWith('notificationStatus');
});
I basically have no idea how to test this thing. There doesn't seem to be much on the internet. I would think I need to do something in my callFake function but I'm a bit stuck. Anyone ever successfully tested this beast before?
Go through this answer: https://stackoverflow.com/a/52565500/7943457
And modify your code something like this:
function editNotifications() {
var popUp = $ionicPopup.show({
template: '<ion-checkbox ng-repeat="item in vm.notifications" ng-model="item.notification" ng-checked="item.notification">' +
'<span class="notificationsFont">{{item.settingsText}}</span></ion-checkbox>',
title: 'Notification Settings',
scope: $scope,
buttons: [
{
text: 'Cancel',
onTap: function(e) {
return false;
}
},
{
text: '<b>Ok</b>',
type: 'button-positive',
onTap: function(e) {
return true;
}
}]
});
popup.then(function(response){
if(response){ //when response is true
localStorageManager.set('notificationStatus', vm.notifications);
vm.notificationText = "Notifications off";
setNotificationValuesBasedOnUserInput(vm.notifications);
}else{ //when response is false
vm.notifications = localStorageManager.get('notificationStatus');
}
})
};
You can test it like this:
it('Should edit notifications', function() {
spyOn($ionicPopup, 'show').and.callFake(function() {
return $q.resolve(true);
})
spyOn(localStorageManager, 'set');
controller = createController();
controller.editNotifications();
$scope.$apply();
expect(localStorageManager.set).toHaveBeenCalled();
// expect(localStorageManager.set).toHaveBeenCalledWith('notificationStatus');
});
it('Should get notifications', function() {
spyOn($ionicPopup, 'show').and.callFake(function() {
return $q.resolve(false);
})
spyOn(localStorageManager, 'get');
controller = createController();
controller.editNotifications();
$scope.$apply();
expect(localStorageManager.get).toHaveBeenCalled();
// expect(localStorageManager.get).toHaveBeenCalledWith('notificationStatus');
});
Hope it helps.

ionic2 modal error no data sending

When i use alertCtrl like this data is going to db it's working well.
addTodo(){
let prompt = this.alertCtrl.create({
title: 'add',
message: 'add',
inputs: [
{
name: 'title'
},
{
name: 'kan'
},
{
name: 'geos'
},
{
name: 'geod'
},
{
name: 'sahip'
}
],
buttons: [
{
text: 'İptal'
},
{
text: 'Kaydet',
handler: todo => {
if(todo){
this.showLoader();
this.todoService.createTodo(todo).then((result) => {
this.loading.dismiss();
this.todos = result;
console.log("todo created");
}, (err) => {
this.loading.dismiss();
console.log("not allowed");
});
}
}
}
]
});
prompt.present();
}
But when i try to use modal , showloader is running but createtodo is not working , no data is going to db .
addTodo(){
let modal = this.modalCtrl.create(KaneklePage);
modal.onDidDismiss(todo => {
if(todo){
this.showLoader();
this.todoService.createTodo(todo).then((result) => {
this.loading.dismiss();
this.todos = result;
console.log("todo created");
}, (err) => {
this.loading.dismiss();
console.log("not allowed");
});
}
});
modal.present();
}
This is dismiss code in modalpage
save(): void {
let todo = {
title: this.title,
kan: this.kan,
geos: this.geos,
geod: this.geod,
sahip: this.sahip
};
this.viewCtrl.dismiss(todo);
}

How to pass selected Value form Popup to normal controller page in ionic framework

How to pass selected Value form Popup to normal controller page in ionic framework
`$scope.showprofpopup = function()
{
$scope.data = {}
var myPopup = $ionicPopup.show
({
templateUrl: 'templates/popover.html',
title: 'Please Choose Category',
scope: $scope,
buttons: [ { text : 'Cancel' }, { text: 'Select', type: 'button-dark', onTap: function(e) { return $scope.data; } }, ]
});
myPopup.then(function(res)
{
//$scope.create(res.category);
//$state.go('app.userdetails');
//$scope.contactMessage = { text: res };
if(!res.category)
{
$ionicLoading.show({ template: '<ion-spinner icon="android"></ion-spinner>', animation: 'fade-in', showBackdrop: true, maxWidth: 100,showDelay: 50 });
$scope.showprofpopup();
$timeout(function () { $ionicLoading.hide(); }, 3000);
//$ionicPopup.alert({ title: "Please Choose Category" });
}
else
{
$scope.SelectedProfessional = { text: res.category};
//alert(res.category);
$state.go('app.userdetails');
}
});
};`
I want to send the result re.category to app.userdetails page.kindly anyone help me.
using $stateParams
$state.go('app.userdetails',{'category': res.category});