Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ios): show toast when loading url in debug mode #2871

Merged
merged 8 commits into from
Jun 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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