Skip to content

Commit ba1f8f7

Browse files
committed
Merge pull request phonegap#909 from guidosabatini/master
Modal dialog to give a waiting feedback to users
2 parents d8d4ace + d937cf2 commit ba1f8f7

File tree

7 files changed

+254
-0
lines changed

7 files changed

+254
-0
lines changed

Android/WaitingDialog/README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Waiting dialog for Android applications
2+
3+
_Created by `Guido Sabatini`_
4+
5+
Creates a modal dialog to give a waiting feedback to users
6+
You can only show and dismiss the dialog, it will block user interactions. You can set the text appearing in the dialog
7+
8+
**NOTE:** this is not a progress dialog, you can not show progress
9+
10+
// To SHOW a modal waiting dialog
11+
window.plugins.waitingDialog.show("Your dialog text");
12+
13+
// To HIDE the dialog
14+
window.plugins.waitingDialog.hide();
15+
16+
If you want to show a waiting dialog for a certain amount of time, you can use javascript setTimeout
17+
18+
// show dialog
19+
window.plugins.waitingDialog.show("Your dialog text");
20+
// automatically hide dialog after 5 seconds
21+
setTimeout(function() {window.plugins.waitingDialog.hide();}, 5000);
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package org.apache.cordova;
2+
3+
import org.apache.cordova.api.CallbackContext;
4+
import org.apache.cordova.api.CordovaPlugin;
5+
import org.apache.cordova.api.LOG;
6+
import org.json.JSONArray;
7+
import org.json.JSONException;
8+
import android.app.ProgressDialog;
9+
10+
public class WaitingDialog extends CordovaPlugin {
11+
12+
private ProgressDialog waitingDialog = null;
13+
14+
@Override
15+
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
16+
if ("show".equals(action)) {
17+
String text = "Please wait";
18+
try {
19+
text = args.getString(0);
20+
} catch (Exception e) {
21+
LOG.d("WaitingDialog", "Text parameter not valid, using default");
22+
}
23+
showWaitingDialog(text);
24+
callbackContext.success();
25+
return true;
26+
} else if ("hide".equals(action)) {
27+
hideWaitingDialog();
28+
callbackContext.success();
29+
return true;
30+
}
31+
return false;
32+
}
33+
34+
public void showWaitingDialog(String text) {
35+
waitingDialog = ProgressDialog.show(this.cordova.getActivity(), "", text);
36+
LOG.d("WaitingDialog", "Dialog shown, waiting hide command");
37+
}
38+
39+
public void hideWaitingDialog() {
40+
if (waitingDialog != null) {
41+
waitingDialog.dismiss();
42+
LOG.d("WaitingDialog", "Dialog dismissed");
43+
waitingDialog = null;
44+
} else {
45+
LOG.d("WaitingDialog", "Nothing to dismiss");
46+
}
47+
}
48+
49+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// window.plugins.waitingDialog
2+
3+
function WaitingDialog() {
4+
}
5+
6+
WaitingDialog.prototype.show = function(text) {
7+
cordova.exec(null, null, "WaitingDialog", "show", [text]);
8+
}
9+
10+
WaitingDialog.prototype.hide = function() {
11+
cordova.exec(null, null, "WaitingDialog", "hide", []);
12+
}
13+
14+
cordova.addConstructor(function() {
15+
if(!window.plugins) {
16+
window.plugins = {};
17+
}
18+
19+
// shim to work in 1.5 and 1.6
20+
if (!window.Cordova) {
21+
window.Cordova = cordova;
22+
};
23+
24+
window.plugins.waitingDialog = new WaitingDialog();
25+
});

iOS/WaitingDialog/README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Waiting dialog for iOS applications
2+
3+
_Created by `Guido Sabatini`_
4+
5+
Creates a modal dialog to give a waiting feedback to users
6+
You can only show and dismiss the dialog, it will block user interactions. You can set the text appearing in the dialog
7+
8+
**NOTE:** this is not a progress dialog, you can not show progress
9+
10+
// To SHOW a modal waiting dialog
11+
window.plugins.waitingDialog.show("Your dialog text");
12+
13+
// To HIDE the dialog
14+
window.plugins.waitingDialog.hide();
15+
16+
If you want to show a waiting dialog for a certain amount of time, you can use javascript setTimeout
17+
18+
// show dialog
19+
window.plugins.waitingDialog.show("Your dialog text");
20+
// automatically hide dialog after 5 seconds
21+
setTimeout(function() {window.plugins.waitingDialog.hide();}, 5000);

iOS/WaitingDialog/WaitingDialog.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//
2+
// WaitingDialog.h
3+
//
4+
//
5+
// Created by Guido Sabatini in 2012
6+
//
7+
8+
#import <Foundation/Foundation.h>
9+
#import <Cordova/CDVPlugin.h>
10+
11+
12+
@interface WaitingDialog : CDVPlugin {
13+
14+
}
15+
16+
// UNCOMMENT THIS METHOD if you want to use the plugin with versions of cordova < 2.2.0
17+
//- (void) show:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options;
18+
19+
// COMMENT THIS METHOD if you want to use the plugin with versions of cordova < 2.2.0
20+
- (void) show:(CDVInvokedUrlCommand*)command;
21+
- (void) hide:(CDVInvokedUrlCommand*)command;
22+
23+
@end

iOS/WaitingDialog/WaitingDialog.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// window.plugins.waitingDialog
2+
3+
function WaitingDialog() {
4+
}
5+
6+
WaitingDialog.prototype.show = function(text) {
7+
cordova.exec(null, null, "WaitingDialog", "show", [text]);
8+
}
9+
10+
WaitingDialog.prototype.hide = function() {
11+
cordova.exec(null, null, "WaitingDialog", "hide", []);
12+
}
13+
14+
cordova.addConstructor(function() {
15+
if(!window.plugins) {
16+
window.plugins = {};
17+
}
18+
19+
// shim to work in 1.5 and 1.6
20+
if (!window.Cordova) {
21+
window.Cordova = cordova;
22+
};
23+
24+
window.plugins.waitingDialog = new WaitingDialog();
25+
});

iOS/WaitingDialog/WaitingDialog.m

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
//
2+
// WaitingDialog.m
3+
//
4+
//
5+
// Created by Guido Sabatini in 2012
6+
//
7+
8+
#import "WaitingDialog.h"
9+
10+
@interface WaitingDialog () {
11+
UIAlertView *waitingDialog;
12+
}
13+
14+
@property (nonatomic, retain) UIAlertView *waitingDialog;
15+
-(void)showWaitingDialogWithText:(NSString*)text;
16+
-(void)hideWaitingDialog;
17+
18+
@end
19+
20+
@implementation WaitingDialog
21+
22+
@synthesize waitingDialog = _waitingDialog;
23+
24+
-(UIAlertView *)waitingDialog {
25+
if (!_waitingDialog) {
26+
_waitingDialog = [[[UIAlertView alloc] initWithTitle:@"" message:nil delegate:self cancelButtonTitle:nil otherButtonTitles: nil] autorelease];
27+
}
28+
return _waitingDialog;
29+
}
30+
31+
// UNCOMMENT THIS METHOD if you want to use the plugin with versions of cordova < 2.2.0
32+
//- (void) show:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options {
33+
// NSString *text = @"Please wait...";
34+
// @try {
35+
// text = [options valueForKey:@"text"];
36+
// }
37+
// @catch (NSException *exception) {
38+
// DLog(@"Cannot read text argument")
39+
// }
40+
//
41+
// [self showWaitingDialogWithText:text];
42+
//}
43+
44+
// COMMENT THIS METHOD if you want to use the plugin with versions of cordova < 2.2.0
45+
- (void) show:(CDVInvokedUrlCommand*)command {
46+
NSString *text = @"Please wait...";
47+
@try {
48+
text = [command.arguments objectAtIndex:0];
49+
}
50+
@catch (NSException *exception) {
51+
DLog(@"Cannot read text argument")
52+
}
53+
54+
[self showWaitingDialogWithText:text];
55+
}
56+
57+
// UNCOMMENT THIS METHOD if you want to use the plugin with versions of cordova < 2.2.0
58+
//- (void) hide:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options {
59+
// [self hideWaitingDialog];
60+
//}
61+
62+
// COMMENT THIS METHOD if you want to use the plugin with versions of cordova < 2.2.0
63+
- (void) hide:(CDVInvokedUrlCommand*)command {
64+
[self hideWaitingDialog];
65+
}
66+
67+
#pragma mark - PRIVATE METHODS
68+
69+
-(void)showWaitingDialogWithText:(NSString *)text {
70+
[self.waitingDialog setTitle:text];
71+
[self.waitingDialog show];
72+
UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
73+
74+
// Adjust the indicator so it is up a few pixels from the bottom of the alert
75+
indicator.center = CGPointMake(self.waitingDialog.bounds.size.width / 2, self.waitingDialog.bounds.size.height - 50);
76+
[indicator startAnimating];
77+
[self.waitingDialog addSubview:indicator];
78+
[indicator release];
79+
}
80+
81+
-(void)hideWaitingDialog {
82+
if (_waitingDialog) {
83+
[self.waitingDialog dismissWithClickedButtonIndex:0 animated:YES];
84+
_waitingDialog = nil;
85+
}
86+
}
87+
88+
89+
90+
@end

0 commit comments

Comments
 (0)