Skip to content

armgit / core

Public Imported from GitHub
0

100% open-source IoT Platform - Integrate your devices, add intelligence, build custom apps

Added progress indicators for loading between pages

Michael Rademakerf14b85d56d16
diff --git a/console/android/src/main/java/org/openremote/android/MainActivity.java b/console/android/src/main/java/org/openremote/android/MainActivity.java
index 9f09494c9..c5797ad2f 100644
--- a/console/android/src/main/java/org/openremote/android/MainActivity.java
+++ b/console/android/src/main/java/org/openremote/android/MainActivity.java
@@ -41,6 +41,7 @@ import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
+import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
@@ -64,7 +65,6 @@ import java.util.logging.Level;
import java.util.logging.Logger;
import androidx.annotation.NonNull;
-import androidx.annotation.Nullable;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
@@ -82,6 +82,7 @@ public class MainActivity extends Activity {
protected final ConnectivityChangeReceiver connectivityChangeReceiver = new ConnectivityChangeReceiver();
protected WebView webView;
+ protected ProgressBar progressBar;
protected int webViewTimeout = WEBVIEW_LOAD_TIMEOUT_DEFAULT;
protected boolean webViewLoaded;
protected ErrorViewHolder errorViewHolder;
@@ -201,6 +202,10 @@ public class MainActivity extends Activity {
}
openIntentUrl(getIntent());
+ progressBar = findViewById(R.id.webProgressBar);
+ progressBar.setMax(100);
+ progressBar.setProgress(1);
+
errorViewHolder = new ErrorViewHolder(findViewById(R.id.errorView));
}
@@ -367,6 +372,8 @@ public class MainActivity extends Activity {
@Override
public void onPageStarted(WebView view, final String url, Bitmap favicon) {
+ progressBar.setVisibility(View.VISIBLE);
+
Runnable run = new Runnable() {
public void run() {
if (!webViewLoaded) {
@@ -381,6 +388,7 @@ public class MainActivity extends Activity {
@Override
public void onPageFinished(WebView view, String url) {
webViewLoaded = true;
+ progressBar.setVisibility(View.GONE);
}
@Override
@@ -446,6 +454,10 @@ public class MainActivity extends Activity {
}
return true;
}
+
+ public void onProgressChanged(WebView view, int progress) {
+ progressBar.setProgress(progress);
+ }
});
webView.setDownloadListener(new DownloadListener() {
diff --git a/console/android/src/main/res/layout/activity_web.xml b/console/android/src/main/res/layout/activity_web.xml
index b38c039c1..80f165057 100644
--- a/console/android/src/main/res/layout/activity_web.xml
+++ b/console/android/src/main/res/layout/activity_web.xml
@@ -1,11 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:id="@+id/activity_web"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context=".MainActivity"
- android:padding="0dp">
+ xmlns:tools="http://schemas.android.com/tools"
+ android:id="@+id/activity_web"
+ android:layout_width="match_parent"
+ android:layout_height="match_parent"
+ android:padding="0dp"
+ tools:context=".MainActivity">
<WebView
android:id="@+id/webview"
@@ -16,5 +16,17 @@
</WebView>
+ <ProgressBar
+ android:id="@+id/webProgressBar"
+ style="?android:attr/progressBarStyleHorizontal"
+ android:layout_width="match_parent"
+ android:layout_height="wrap_content"
+ android:layout_alignParentTop="true"
+ android:layout_marginTop="-7dp"
+ android:background="@android:color/transparent"
+ android:indeterminate="false"
+ android:max="100"
+ android:progress="1"/>
+
<include layout="@layout/error_layout"/>
</RelativeLayout>
diff --git a/console/iOS/ORLib/ORLib/ORViewController.swift b/console/iOS/ORLib/ORLib/ORViewController.swift
index d1f814d18..40a34ecd0 100644
--- a/console/iOS/ORLib/ORLib/ORViewController.swift
+++ b/console/iOS/ORLib/ORLib/ORViewController.swift
@@ -26,6 +26,7 @@ open class ORViewcontroller : UIViewController, URLSessionDelegate, WKScriptMess
public var data : Data?
public var myWebView : WKWebView?
+ public var webProgressBar: UIProgressView?
public var defaults : UserDefaults?
public var webCfg : WKWebViewConfiguration?
public var isLoading = false
@@ -177,6 +178,10 @@ open class ORViewcontroller : UIViewController, URLSessionDelegate, WKScriptMess
self.dismiss(animated: true, completion: nil)
completionHandler()
}
+
+ public func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
+ showProgressView()
+ }
open func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
if (navigationAction.request.url?.absoluteString.contains("logout"))! {
@@ -253,6 +258,27 @@ open class ORViewcontroller : UIViewController, URLSessionDelegate, WKScriptMess
open func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
UIApplication.shared.isNetworkActivityIndicatorVisible = false
+ hideProgressView()
+ }
+
+ public func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
+ NSLog("error %@", error.localizedDescription)
+ UIApplication.shared.isNetworkActivityIndicatorVisible = false
+ if let err = error as? URLError {
+
+ let httpCode: Int
+ switch(err.code) {
+ case .cannotFindHost:
+ httpCode = 404
+ default:
+ httpCode = 500
+ }
+
+ handleError(errorCode: httpCode, description: err.localizedDescription, failingUrl: err.failureURLString ?? "", isForMainFrame: true)
+ } else {
+ handleError(errorCode: 0, description: error.localizedDescription, failingUrl: webView.url?.absoluteString ?? "", isForMainFrame: true)
+ }
+ hideProgressView()
}
open func urlSession(_ session: URLSession, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
@@ -283,12 +309,50 @@ open class ORViewcontroller : UIViewController, URLSessionDelegate, WKScriptMess
sbHeight = UIApplication.shared.statusBarFrame.height
}
webCfg.setURLSchemeHandler(ORSchemeHandler(), forURLScheme: ORSchemeHandler.browserScheme)
- let webFrame = CGRect(x : 0,y : sbHeight,width : view.frame.size.width,height : view.frame.size.height - sbHeight)
+ let webFrame = CGRect(x: 0, y: sbHeight, width: view.frame.size.width, height: view.frame.size.height - sbHeight)
myWebView = WKWebView(frame: webFrame, configuration: webCfg)
myWebView?.autoresizingMask = [.flexibleWidth, .flexibleHeight];
myWebView?.uiDelegate = self;
myWebView?.navigationDelegate = self;
+ //add observer to get estimated progress value
+ myWebView?.addObserver(self, forKeyPath: "estimatedProgress", options: .new, context: nil);
+
+ webProgressBar = UIProgressView(progressViewStyle: .bar)
+ webProgressBar?.progressTintColor = UIColor.mainColorAccent
+
view.addSubview(myWebView!)
+ view.addSubview(webProgressBar!)
+ view.bringSubviewToFront(webProgressBar!)
+
+ webProgressBar?.translatesAutoresizingMaskIntoConstraints = false
+ webProgressBar?.leadingAnchor.constraint(equalTo: myWebView!.leadingAnchor).isActive = true
+ webProgressBar?.trailingAnchor.constraint(equalTo: myWebView!.trailingAnchor).isActive = true
+ webProgressBar?.topAnchor.constraint(equalTo: myWebView!.topAnchor, constant: -2).isActive = true
+ webProgressBar?.heightAnchor.constraint(equalToConstant: 2).isActive = true
+ }
+
+ override open func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
+ if keyPath == "estimatedProgress" {
+ if let webView = myWebView {
+ webProgressBar?.progress = Float(webView.estimatedProgress);
+ }
+ }
+ }
+
+ func showProgressView() {
+ if let progressBar = webProgressBar {
+ UIView.animate(withDuration: 0.5, delay: 0, options: .curveEaseInOut, animations: {
+ progressBar.alpha = 1
+ }, completion: nil)
+ }
+ }
+
+ func hideProgressView() {
+ if let progressBar = webProgressBar {
+ UIView.animate(withDuration: 0.5, delay: 0, options: .curveEaseInOut, animations: {
+ progressBar.alpha = 0
+ }, completion: nil)
+ }
}
open func loadURL(url : URL) {
diff --git a/console/iOS/ORLib/ORLib/ProjectConstants.swift b/console/iOS/ORLib/ORLib/ProjectConstants.swift
index fb88011e4..2ebce5e77 100644
--- a/console/iOS/ORLib/ORLib/ProjectConstants.swift
+++ b/console/iOS/ORLib/ORLib/ProjectConstants.swift
@@ -46,3 +46,10 @@ public enum ORFCM {
public enum ORAppGroup {
public static var entitlement = "group.org.openremote.console"
}
+
+extension UIColor {
+ public static var mainColor = UIColor(red:0.22, green:0.30, blue:0.31, alpha:1.0)
+ public static var mainColorDark = UIColor(red:0.15, green:0.20, blue:0.22, alpha:1.0)
+ public static var mainColorAccent = UIColor(red:0.75, green:0.84, blue:0.18, alpha:1.0)
+}
+