Skip to content

Commit

Permalink
Including loading when blank page is shown.
Browse files Browse the repository at this point in the history
Adding HideLoading function to close the loading in ionic afterEnter event.
  • Loading branch information
sebalagos90 committed Apr 5, 2016
1 parent bdcd58c commit 705a54b
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 5 deletions.
80 changes: 80 additions & 0 deletions src/android/cl/kunder/webview/WebViewActivity.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,95 @@
package cl.kunder.webview;

import android.app.Activity;
import android.app.Dialog;
import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import org.apache.cordova.CordovaActivity;

public class WebViewActivity extends CordovaActivity {
static Dialog dialog;
static Activity activity2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Aqui debo crear el loading
activity2 = this;
Bundle b = getIntent().getExtras();
String url = b.getString("url");
Boolean shouldShowLoading = false;
try{
shouldShowLoading = b.getBoolean("shouldShowLoading");
}
catch(Exception e){

}
if(shouldShowLoading){
showLoading();
}
loadUrl((url.matches("^(.*://|javascript:)[\\s\\S]*$")?"":"file:///android_asset/www/")+url);
}

public static boolean showLoading() {
// Loading spinner
activity2.runOnUiThread(new Runnable() {
@Override
public void run() {
dialog = new Dialog(activity2,android.R.style.Theme_Translucent_NoTitleBar);
ProgressBar progressBar = new ProgressBar(activity2,null,android.R.attr.progressBarStyle);

LinearLayout linearLayout = new LinearLayout(activity2);
linearLayout.setOrientation(LinearLayout.VERTICAL);
RelativeLayout layoutPrincipal = new RelativeLayout(activity2);
layoutPrincipal.setBackgroundColor(Color.parseColor("#d9000000"));

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.CENTER_IN_PARENT);

linearLayout.addView(progressBar);

linearLayout.setLayoutParams(params);

layoutPrincipal.addView(linearLayout);

dialog.setContentView(layoutPrincipal);
dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialogInterface) {

}
});
dialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
@Override
public boolean onKey(DialogInterface dialogInterface, int i, KeyEvent keyEvent) {
if(keyEvent.getKeyCode() == KeyEvent.KEYCODE_BACK)
return true;
return false;
}
});

dialog.show();
}
});

return true;
}

public static boolean hideLoading() {
// Loading spinner
activity2.runOnUiThread(new Runnable() {
@Override
public void run() {
dialog.hide();
}
});
return true;
}
}
25 changes: 23 additions & 2 deletions src/android/cl/kunder/webview/WebViewPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,15 @@ public boolean execute(String action, JSONArray args, CallbackContext callbackCo
if (action.equals("show") && args.length() > 0) {
LOG.d(LOG_TAG, "Show Web View");
final String url = args.getString(0);
Boolean shouldShowLoading = false;
try{
shouldShowLoading = args.getBoolean(1);
}
catch(Exception e){

}
if(!"".equals(url)) {
showWebView(url);
showWebView(url, shouldShowLoading);
JSONObject r = new JSONObject();
r.put("responseCode", "ok");
callbackContext.success(r);
Expand All @@ -73,6 +79,20 @@ else if(action.equals("hide")) {
callbackContext.success(r);
}

else if(action.equals("hideLoading")) {
LOG.d(LOG_TAG, "Hide Web View Loading");
try{
WebViewActivity.hideLoading();
}
catch(Exception e){
LOG.e(LOG_TAG, "Error in hideLoading");
LOG.e(LOG_TAG, e.toString());
}
JSONObject r = new JSONObject();
r.put("responseCode", "ok");
callbackContext.success(r);
}

else if(action.equals("subscribeCallback")){
LOG.d(LOG_TAG, "Subscribing Cordova CallbackContext");
subscribeCallbackContext = callbackContext;
Expand Down Expand Up @@ -100,10 +120,11 @@ else if(action.equals("exitApp")){
return true;
}

private void showWebView(final String url) {
private void showWebView(final String url, Boolean shouldShowLoading) {
LOG.d(LOG_TAG, "Url: " + url);
Intent i = new Intent(this.cordova.getActivity(), WebViewActivity.class);
i.putExtra("url", url);
i.putExtra("shouldShowLoading", shouldShowLoading);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
this.cordova.getActivity().getApplicationContext().startActivity(i);
}
Expand Down
16 changes: 13 additions & 3 deletions www/webViewPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,23 @@
module.exports = (function() {


var _show = function(url, successCallback, errorCallback) {
cordova.exec(successCallback, errorCallback, 'WebViewPlugin', 'show', [url]);
var _show = function(url, successCallback, errorCallback, loading) {
if(loading){
cordova.exec(successCallback, errorCallback, 'WebViewPlugin', 'show', [url, loading]);
}
else{
cordova.exec(successCallback, errorCallback, 'WebViewPlugin', 'show', [url]);
}
};

var _hide = function(successCallback, errorCallback) {
cordova.exec(successCallback, errorCallback, 'WebViewPlugin', 'hide', []);
};

var _hideLoading = function(successCallback, errorCallback) {
cordova.exec(successCallback, errorCallback, 'WebViewPlugin', 'hideLoading', []);
};

var _subscribeCallback = function(successCallback, errorCallback) {
cordova.exec(successCallback, errorCallback, 'WebViewPlugin', 'subscribeCallback', []);
};
Expand All @@ -30,7 +39,8 @@ module.exports = (function() {
Close: _hide,
SubscribeCallback: _subscribeCallback,
SubscribeExitCallback: _subscribeExitCallback,
ExitApp: _exitApp
ExitApp: _exitApp,
HideLoading: _hideLoading
};

})();

0 comments on commit 705a54b

Please sign in to comment.