From 171870b25562b35dc9e75416764a2c416ca907eb Mon Sep 17 00:00:00 2001 From: Bas Heerschop Date: Mon, 8 Jun 2020 19:45:19 +0200 Subject: [PATCH] feat(ios): show toast when loading url in debug mode (#2871) --- .../Capacitor/CAPBridgeViewController.swift | 4 +++ ios/Capacitor/Capacitor/Plugins/Toast.swift | 29 +++++++++++-------- 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/ios/Capacitor/Capacitor/CAPBridgeViewController.swift b/ios/Capacitor/Capacitor/CAPBridgeViewController.swift index 4c315ccd50..4c89f14171 100644 --- a/ios/Capacitor/Capacitor/CAPBridgeViewController.swift +++ b/ios/Capacitor/Capacitor/CAPBridgeViewController.swift @@ -181,6 +181,10 @@ public class CAPBridgeViewController: UIViewController, CAPBridgeDelegate, WKScr hostname = bridge!.config.getString("server.url") ?? "\(bridge!.getLocalUrl())" allowNavigationConfig = bridge!.config.getValue("server.allowNavigation") as? Array + if bridge!.isDevMode() && bridge!.config.getString("server.url") != nil { + let toastPlugin = bridge!.getOrLoadPlugin(pluginName: "Toast") as? CAPToastPlugin + toastPlugin!.showToast(vc: self, text: "Using app server \(hostname!)", duration: 3500) + } CAPLog.print("⚡️ Loading app at \(hostname!)...") let request = URLRequest(url: URL(string: hostname!)!) diff --git a/ios/Capacitor/Capacitor/Plugins/Toast.swift b/ios/Capacitor/Capacitor/Plugins/Toast.swift index ce0f0c58be..0b39acbac9 100644 --- a/ios/Capacitor/Capacitor/Plugins/Toast.swift +++ b/ios/Capacitor/Capacitor/Plugins/Toast.swift @@ -4,7 +4,7 @@ import Foundation @objc(CAPToastPlugin) public class CAPToastPlugin : CAPPlugin { - var toast: UILabel? + @objc func show(_ call: CAPPluginCall) { guard let text = call.get("text", String.self) else { call.error("text must be provided and must be a string.") @@ -13,10 +13,14 @@ public class CAPToastPlugin : CAPPlugin { let durationType = call.get("duration", String.self, "short")! let duration = durationType == "long" ? 3500 : 2000 let position = call.get("position", String.self, "bottom") - + + showToast(vc: self.bridge!.viewController, text: text, duration: duration, position: position!, completion: {(isCompleted) in + call.success() + }); + } + + public func showToast(vc: UIViewController, text: String, duration: Int = 2000, position: String = "bottom", completion: ((Bool) -> Void)? = nil) { DispatchQueue.main.async { - let vc = self.bridge!.viewController - let maxSizeTitle : CGSize = CGSize(width: vc.view.bounds.size.width-32, height: vc.view.bounds.size.height) let lb = UILabel() @@ -53,21 +57,22 @@ public class CAPToastPlugin : CAPPlugin { height: height) lb.padding = UIEdgeInsets(top: 8, left: 8, bottom: 8, right: 8) - self.toast = lb - + vc.view.addSubview(lb) UIView.animateKeyframes(withDuration: 0.3, delay: 0, animations: { - self.toast!.alpha = 1.0 + lb.alpha = 1.0 }, completion: {(isCompleted) in - + UIView.animate(withDuration: 0.3, delay: (Double(duration) / 1000), options: .curveEaseOut, animations: { - self.toast!.alpha = 0.0 + lb.alpha = 0.0 }, completion: {(isCompleted) in - self.toast!.removeFromSuperview() - call.success() - }) + lb.removeFromSuperview() + if (completion) != nil { + completion?(isCompleted); + } + }) }) } }