// // Extension.swift // LMS // // Created by Suraj Kumar Mandal on 17/08/22. // import Foundation import UIKit extension UIApplication { class func isFirstLaunch() -> Bool { if !UserDefaults.standard.bool(forKey: "hasBeenLaunchedBeforeFlag") { UserDefaults.standard.set(true, forKey: "hasBeenLaunchedBeforeFlag") UserDefaults.standard.synchronize() return true } return false } } extension UIWindow { static var key: UIWindow? { if #available(iOS 13, *) { return UIApplication.shared.windows.first { $0.isKeyWindow } } else { return UIApplication.shared.keyWindow } } } extension UIApplication { static var release: String { return Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") as! String? ?? "x.x" } static var build: String { return Bundle.main.object(forInfoDictionaryKey: "CFBundleVersion") as! String? ?? "x" } static var version: String { return "\(release).\(build)" } } extension UIView { func dropShadow(scale: Bool = true) { layer.masksToBounds = false layer.shadowColor = UIColor.black.cgColor layer.shadowOpacity = 0.2 layer.shadowOffset = .zero layer.shadowRadius = 1 layer.shouldRasterize = true layer.rasterizationScale = scale ? UIScreen.main.scale : 1 } func addShadow() { self.layer.shadowColor = UIColor.gray.cgColor self.layer.shadowOffset = CGSize(width: 1, height: 1) self.layer.shadowOpacity = 1 } } extension UIScrollView { func scrollToTop() { let desiredOffset = CGPoint(x: 0, y: -contentInset.top) setContentOffset(desiredOffset, animated: true) } } extension UIScrollView { func scrollViewToTop( _ someView:UIView){ let targetViewTop = someView.frame.origin.y let viewToTop = targetViewTop - self.contentInset.top self.setContentOffset(CGPoint(x: 0, y: viewToTop), animated: true) } } extension Bundle { // Name of the app - title under the icon. var displayName: String? { return object(forInfoDictionaryKey: "CFBundleDisplayName") as? String ?? object(forInfoDictionaryKey: "CFBundleName") as? String } } extension UIDatePicker { func setMonthYearPicker() { self.datePickerMode = .date let currentDate = Date() self.maximumDate = currentDate let calendar = Calendar(identifier: .gregorian) var components = calendar.dateComponents([.year, .month], from: currentDate) components.calendar = calendar self.date = components.date! } } extension Encodable { func toDictionary() -> [String: Any]? { guard let data = try? JSONEncoder().encode(self) else { return nil } return (try? JSONSerialization.jsonObject(with: data, options: .allowFragments)) as? [String: Any] } } extension UIViewController { func dismissAllAlerts() { if let rootViewController = UIApplication.shared.windows.first?.rootViewController { var topViewController = rootViewController while let presentedViewController = topViewController.presentedViewController { if let alert = presentedViewController as? UIAlertController { alert.dismiss(animated: false, completion: nil) } topViewController = presentedViewController } } } }