// // GlobalHelper.swift // Runner // // Created by ZiKambrani on 29/03/1442 AH. // import UIKit func dictionaryArray(from:String) -> [[String:Any]]{ if let data = from.data(using: .utf8) { do { return try JSONSerialization.jsonObject(with: data, options: []) as? [[String: Any]] ?? [] } catch { print(error.localizedDescription) } } return [] } func dictionary(from:String) -> [String:Any]?{ if let data = from.data(using: .utf8) { do { return try? JSONSerialization.jsonObject(with: data, options: []) as? [String: Any] } catch { print(error.localizedDescription) } } return nil } let HmgLocalNotificationCategoryIdentifier = "hmg.local.notification" func showNotification(identifier:String? = nil, title:String?, subtitle:String?, message:String?, sound:UNNotificationSound = UNNotificationSound.default, categoryIdentifier:String = HmgLocalNotificationCategoryIdentifier){ DispatchQueue.main.async { let notificationContent = UNMutableNotificationContent() notificationContent.categoryIdentifier = categoryIdentifier if identifier != nil { notificationContent.categoryIdentifier = identifier! } if title != nil { notificationContent.title = title! } if subtitle != nil { notificationContent.body = message! } if message != nil { notificationContent.subtitle = subtitle! } notificationContent.sound = UNNotificationSound.default let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 1, repeats: false) let request = UNNotificationRequest(identifier: "\(Date().timeIntervalSinceNow)", content: notificationContent, trigger: trigger) UNUserNotificationCenter.current().add(request) { error in if let error = error { print("Error: \(error)") } } } } func appLanguageCode() -> Int{ let lang = UserDefaults.standard.string(forKey: "language") ?? "ar" return lang == "ar" ? 2 : 1 } func userProfile() -> [String:Any?]?{ var userProf = UserDefaults.standard.string(forKey: "flutter.imei-user-data") if(userProf == nil){ userProf = UserDefaults.standard.string(forKey: "flutter.user-profile") } return dictionary(from: userProf ?? "{}") } fileprivate let defaultHTTPParams:[String : Any?] = [ "ZipCode" : "966", "VersionID" : 5.8, "Channel" : 3, "LanguageID" : appLanguageCode(), "IPAdress" : "10.20.10.20", "generalid" : "Cs2020@2016$2958", "PatientOutSA" : 0, "SessionID" : nil, "isDentalAllowedBackend" : false, "DeviceTypeID" : 2 ] func httpPostRequest(urlString:String, jsonBody:[String:Any?], completion:((Bool,[String:Any]?)->Void)?){ var json: [String: Any?] = jsonBody json = json.merge(dict: defaultHTTPParams) let jsonData = try? JSONSerialization.data(withJSONObject: json) // create post request let url = URL(string: urlString)! var request = URLRequest(url: url) request.addValue("application/json", forHTTPHeaderField: "Content-Type") request.addValue("*/*", forHTTPHeaderField: "Accept") request.httpMethod = "POST" request.httpBody = jsonData let task = URLSession.shared.dataTask(with: request) { data, response, error in guard let data = data, error == nil else { print(error?.localizedDescription ?? "No data") return } let responseJSON = try? JSONSerialization.jsonObject(with: data, options: []) if let responseJSON = responseJSON as? [String: Any], let status = responseJSON["MessageStatus"] as? Int{ print(responseJSON) if status == 1{ completion?(true,responseJSON) }else{ completion?(false,responseJSON) } }else{ completion?(false,nil) } } task.resume() }