Skip to content

Commit

Permalink
feat(ios): show toast when loading url in debug mode (#2871)
Browse files Browse the repository at this point in the history
  • Loading branch information
Heerschop authored Jun 8, 2020
1 parent a2f2e4f commit 171870b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
4 changes: 4 additions & 0 deletions ios/Capacitor/Capacitor/CAPBridgeViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>

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!)!)
Expand Down
29 changes: 17 additions & 12 deletions ios/Capacitor/Capacitor/Plugins/Toast.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
Expand All @@ -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()
Expand Down Expand Up @@ -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);
}
})
})
}
}
Expand Down

0 comments on commit 171870b

Please sign in to comment.