I'm calling alerts, view's colours, spinners and components properties from Shared class. Is it ok. What is the best way to write common code.
Ex: My shared class code is...
import UIKit
class SharedClass: NSObject {
static let sharedInstance = SharedClass()
var spinner = UIActivityIndicatorView()
//Show activity indicator
func activityIndicator() {
DispatchQueue.main.async {
if let window = UIApplication.shared.keyWindow {//Conditionally unwrap it instead of force unwrap
// let window = UIApplication.shared.keyWindow! //Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value
self.transparentView = UIView()
self.transparentView?.frame = CGRect(x: 0, y: 0, width: window.frame.width, height: window.frame.height)
self.transparentView?.backgroundColor = UIColor.black.withAlphaComponent(0.4)
window.addSubview(self.transparentView!)
if UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiom.pad {
// self.spinner = UIActivityIndicatorView(style: .whiteLarge)
self.spinner = UIActivityIndicatorView(style: .whiteLarge)
self.spinner.frame = CGRect(x: 0, y: 0, width: 60, height: 60)
} else {
// self.spinner = UIActivityIndicatorView(style: .white)
self.spinner = UIActivityIndicatorView(style: .white)
self.spinner.frame = CGRect(x: 0, y: 0, width: 40, height: 40)
}
self.spinner.center = window.center
self.transparentView?.addSubview(self.spinner)
self.spinner.startAnimating()
DispatchQueue.main.asyncAfter(deadline: .now() + 40.0) {//Stop spinner after 40 Sec's
self.stopActivityIndicator()
}
}
}
}
//Stop activity indicator
func stopActivityIndicator() {
DispatchQueue.main.async {
self.spinner.stopAnimating()
self.spinner.removeFromSuperview()
self.transparentView?.removeFromSuperview()//Some times getting error here
}
}
//Email validation
func isValidEmail(email: String) -> Bool {
let emailRegex = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}"
var valid = NSPredicate(format: "SELF MATCHES %@", emailRegex).evaluate(with: email)
if valid {
valid = !email.contains("Invalid email id")
}
return valid
}
//Mobile number validation
func isValidPhone(phone: String) -> Bool {
let phoneRegex = "^((0091)|(\\+91)|0?)[6789]{1}\\d{9}$"; // @"^((0091)|(\\+91)|0?)[6789]{1}\\d{9}$" "^[0-9]{6,14}$"
let valid = NSPredicate(format: "SELF MATCHES %@", phoneRegex).evaluate(with: phone)
return valid
}
private override init() {
}
}
//Alert function
extension UIViewController {
func showAlert(title: String, msg: String) {
DispatchQueue.main.async {
let alert = UIAlertController(title: title, message: msg, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
self.present(alert, animated: true, completion: nil)
}
}
}
//Set TF shadow in login VC
extension UITextField {
func TFProperties() {
layer.shadowColor = UIColor(red: 199/255, green: 217/255, blue: 223/255, alpha: 1.0).cgColor
layer.shadowOpacity = 1
layer.shadowOffset = CGSize.zero
layer.shadowRadius = 5
layer.borderColor = UIColor(red: 199/255, green: 217/255, blue: 223/255, alpha: 1.0).cgColor
layer.borderWidth = 1
layer.cornerRadius = 4
}
}
//Set View shadow in OTPVerify VC
extension UIView {
func subViewShadow() {
layer.shadowColor = UIColor.white.cgColor
layer.shadowOpacity = 1
layer.shadowOffset = CGSize.zero
layer.shadowRadius = 3
layer.borderColor = UIColor.gray.cgColor
layer.borderWidth = 1
layer.cornerRadius = 4
}
func viewShadow() {
layer.shadowColor = UIColor.gray.cgColor
layer.shadowOpacity = 1
layer.shadowOffset = CGSize.zero
layer.shadowRadius = 3
}
func collectionViewCellBorder() {
layer.cornerRadius = 5
layer.borderColor = UIColor UIColor.gray.cgColor
layer.borderWidth = 1.0
}
}
Aucun commentaire:
Enregistrer un commentaire