Utils.swift 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. //
  2. // Utils.swift
  3. // Runner
  4. //
  5. // Created by Rifqy Fakhrul Rijal on 13/08/20.
  6. // Copyright © 2020 The Chromium Authors. All rights reserved.
  7. //
  8. import Foundation
  9. import UIKit
  10. //import var CommonCrypto.CC_MD5_DIGEST_LENGTH
  11. //import func CommonCrypto.CC_MD5
  12. //import typealias CommonCrypto.CC_LONG
  13. public final class Utils {
  14. public static func getCurrentTime()->Int64 {
  15. return Int64(Date().timeIntervalSince1970)
  16. }
  17. public static func getCurrentTimeMillis()->Int64 {
  18. return Int64(Date().timeIntervalSince1970 * 1000)
  19. }
  20. public static func getCurrentTimeNanos()->Int64 {
  21. return Int64(Date().timeIntervalSince1970 * 1000_000_000)
  22. }
  23. public static func getElapsedRealtime() -> Int64 {
  24. return Int64((ProcessInfo().systemUptime).rounded()) // SystemClock.elapsedRealtime();
  25. }
  26. public static func getElapsedRealtimeMillis() -> Int64 {
  27. return Int64((ProcessInfo().systemUptime * 1000).rounded()) // SystemClock.elapsedRealtime();
  28. }
  29. public static func getElapsedRealtimeNanos() -> Int64 {
  30. return Int64((ProcessInfo().systemUptime * 1000_000_000).rounded()) // SystemClock.elapsedRealtimeNano();
  31. }
  32. public static func getForceAnonymous() -> Bool {
  33. return UserDefaults.standard.bool(forKey: "force_anonymous")
  34. }
  35. public static func setForceAnonymous(value: Bool){
  36. UserDefaults.standard.set(value, forKey: "force_anonymous")
  37. }
  38. public static func getSetProfile() -> Bool {
  39. return UserDefaults.standard.bool(forKey: "is_change_profile")
  40. }
  41. public static func setProfile(value: Bool){
  42. UserDefaults.standard.set(value, forKey: "is_change_profile")
  43. }
  44. static func setIconCenterAnim2(value: String){
  45. UserDefaults.standard.set(value, forKey: "pb_fb_icon_center_self_mode2")
  46. }
  47. static func getIconCenterAnim2() -> String? {
  48. UserDefaults.standard.string(forKey: "pb_fb_icon_center_self_mode2") ?? "https://nexilis.io/dashboardv2/uploads/logofloat/525_411217560576.gif"
  49. }
  50. static func setIconCenterAnim4(value: String){
  51. UserDefaults.standard.set(value, forKey: "pb_fb_icon_center_self_mode4")
  52. }
  53. static func getIconCenterAnim4() -> String? {
  54. UserDefaults.standard.string(forKey: "pb_fb_icon_center_self_mode4") ?? "https://nexilis.io/dashboardv2/uploads/logofloat/semut1.gif"
  55. }
  56. static func setURLFirstTab(value: String) {
  57. UserDefaults.standard.set(value, forKey: "app_builder_url_first_tab")
  58. }
  59. static func setURLThirdTab(value: String) {
  60. UserDefaults.standard.set(value, forKey: "app_builder_url_third_tab")
  61. }
  62. static func setURLBase(value: String) {
  63. UserDefaults.standard.set(value, forKey: "app_builder_url_base")
  64. }
  65. static func setURLQMS(value: String) {
  66. UserDefaults.standard.set(value, forKey: "app_builder_url_qms")
  67. }
  68. static func setIconDock(value: String) {
  69. UserDefaults.standard.set(value, forKey: "app_builder_icon_dock")
  70. }
  71. static func setIconSS(value: String) {
  72. UserDefaults.standard.set(value, forKey: "app_builder_icon_ss")
  73. }
  74. static func setBackground(value: String) {
  75. UserDefaults.standard.set(value, forKey: "app_builder_background")
  76. }
  77. static func setURLPrivacyPolicy(value: String){
  78. UserDefaults.standard.set(value, forKey: "app_builder_url_privacy_policy")
  79. }
  80. static func setEnablePrivacyPolicy(value: Bool){
  81. UserDefaults.standard.set(value, forKey: "app_builder_enable_privacy_policy")
  82. }
  83. static func setCustomTab(cust: String){
  84. UserDefaults.standard.set(cust, forKey: "custom_tab")
  85. }
  86. static func setACTheme(value: String){
  87. UserDefaults.standard.set(value, forKey: "app_builder_ac_theme")
  88. }
  89. static func setButtonURL(value: String){
  90. UserDefaults.standard.set(value, forKey: "app_builder_button_url")
  91. }
  92. static func setCustomButtons(value: String){
  93. UserDefaults.standard.set(value, forKey: "app_builder_custom_buttons")
  94. }
  95. static func getCustomButtons() -> String {
  96. UserDefaults.standard.string(forKey: "app_builder_button_icon") ?? ""
  97. }
  98. static func setCustomFBIcon(value: String){
  99. UserDefaults.standard.set(value, forKey: "app_builder_button_icon")
  100. }
  101. static func getCustomFBIcon() -> String {
  102. UserDefaults.standard.string(forKey: "app_builder_button_icon") ?? ""
  103. }
  104. static func setEnableMobileBuilder(value: String){
  105. UserDefaults.standard.set(value, forKey: "app_builder_enable_mobile_builder")
  106. }
  107. static func setFinishInitPrefs(value: Bool){
  108. UserDefaults.standard.set(value, forKey: "finish_init_prefs")
  109. }
  110. static func getFinishInitPrefsr() -> Bool {
  111. UserDefaults.standard.bool(forKey: "finish_init_prefs")
  112. }
  113. static func setConfigModeFB(value: String) {
  114. UserDefaults.standard.set(value, forKey: "fb_config_mode")
  115. }
  116. static func getConfigModeFB() -> String {
  117. UserDefaults.standard.string(forKey: "fb_config_mode") ?? "1"
  118. }
  119. public static func sGetCurrentDateTime(sFormat: String!) -> String! {
  120. let todaysDate = NSDate()
  121. let dateFormatter = DateFormatter()
  122. dateFormatter.dateFormat = sFormat
  123. return dateFormatter.string(from: todaysDate as Date)
  124. }
  125. // public static func getMD5(string: String) -> Data {
  126. // let length = Int(CC_MD5_DIGEST_LENGTH)
  127. // let messageData = string.data(using:.utf8)!
  128. // var digestData = Data(count: length)
  129. //
  130. // _ = digestData.withUnsafeMutableBytes { digestBytes -> UInt8 in
  131. // messageData.withUnsafeBytes { messageBytes -> UInt8 in
  132. // if let messageBytesBaseAddress = messageBytes.baseAddress, let digestBytesBlindMemory = digestBytes.bindMemory(to: UInt8.self).baseAddress {
  133. // let messageLength = CC_LONG(messageData.count)
  134. // CC_MD5(messageBytesBaseAddress, messageLength, digestBytesBlindMemory)
  135. // }
  136. // return 0
  137. // }
  138. // }
  139. // return digestData
  140. // }
  141. public static let callDurationFormatter: DateComponentsFormatter = {
  142. let dateFormatter: DateComponentsFormatter
  143. dateFormatter = DateComponentsFormatter()
  144. dateFormatter.unitsStyle = .positional
  145. dateFormatter.allowedUnits = [.minute, .second]
  146. dateFormatter.zeroFormattingBehavior = .pad
  147. return dateFormatter
  148. }()
  149. public static func previewMessageText(chat: Chat) -> Any {
  150. if chat.credential == "1" && chat.lock == "2" {
  151. return ("🚫 _"+"Message has expired".localized()+"_").richText(group_id: chat.pin)
  152. } else if chat.credential == "1" {
  153. return "Confidential Message".localized().richText(group_id: chat.pin)
  154. } else if chat.attachmentFlag == "27" {
  155. return ("📄 " + "Live Streaming".localized()).richText(group_id: chat.pin)
  156. } else if chat.attachmentFlag == "26" {
  157. return ("📄 " + "Seminar".localized()).richText(group_id: chat.pin)
  158. } else if !chat.image.isEmpty {
  159. if !chat.messageText.isEmpty {
  160. return "📷 \(chat.messageText)".richText(group_id: chat.pin)
  161. } else {
  162. return ("📷 " + "Photo".localized()).richText(group_id: chat.pin)
  163. }
  164. }
  165. else if !chat.video.isEmpty {
  166. if !chat.messageText.isEmpty {
  167. return "📹 \(chat.messageText)".richText(group_id: chat.pin)
  168. } else {
  169. return ("📹 " + "Video".localized()).richText(group_id: chat.pin)
  170. }
  171. }
  172. else if !chat.file.isEmpty {
  173. if chat.messageScope == "18" {
  174. return ("📄 Form").richText(group_id: chat.pin)
  175. }
  176. return ("📄 " + chat.messageText.components(separatedBy: "|")[0]).richText(group_id: chat.pin)
  177. } else if chat.attachmentFlag == "11" {
  178. return ("❤️ " + "Sticker".localized()).richText(group_id: chat.pin)
  179. }
  180. else {
  181. return chat.messageText.richText(group_id: chat.pin)
  182. }
  183. }
  184. static func getURLBase() -> String? {
  185. return UserDefaults.standard.string(forKey: "app_builder_url_base") ?? "https://nexilis.io/"
  186. }
  187. static func getIconDock() -> String {
  188. return UserDefaults.standard.string(forKey: "app_builder_icon_dock") ?? ""
  189. }
  190. static func getUrlDock() -> String? {
  191. return Utils.getURLBase()! + "dashboardv2/uploads/logofloat/" + Utils.getIconDock()
  192. }
  193. static func setDefaultCC(value: String){
  194. UserDefaults.standard.set(value, forKey: "default_cc")
  195. }
  196. static func getDefaultCC() -> String? {
  197. return UserDefaults.standard.string(forKey: "default_cc")
  198. }
  199. public static var inTabChats = false
  200. }
  201. public extension UIImage {
  202. var jpeg: Data? { jpegData(compressionQuality: 1) } // QUALITY min = 0 / max = 1
  203. var png: Data? { pngData() }
  204. }
  205. public extension Data {
  206. var uiImage: UIImage? { UIImage(data: self) }
  207. }
  208. public extension UIDevice {
  209. var modelName: String {
  210. var systemInfo = utsname()
  211. uname(&systemInfo)
  212. let machineMirror = Mirror(reflecting: systemInfo.machine)
  213. let identifier = machineMirror.children.reduce("") { identifier, element in
  214. guard let value = element.value as? Int8, value != 0 else { return identifier }
  215. return identifier + String(UnicodeScalar(UInt8(value)))
  216. }
  217. switch identifier {
  218. case "iPod5,1": return "iPod Touch 5"
  219. case "iPod7,1": return "iPod Touch 6"
  220. case "iPhone3,1", "iPhone3,2", "iPhone3,3": return "iPhone 4"
  221. case "iPhone4,1": return "iPhone 4s"
  222. case "iPhone5,1", "iPhone5,2": return "iPhone 5"
  223. case "iPhone5,3", "iPhone5,4": return "iPhone 5c"
  224. case "iPhone6,1", "iPhone6,2": return "iPhone 5s"
  225. case "iPhone7,2": return "iPhone 6"
  226. case "iPhone7,1": return "iPhone 6 Plus"
  227. case "iPhone8,1": return "iPhone 6s"
  228. case "iPhone8,2": return "iPhone 6s Plus"
  229. case "iPhone9,1", "iPhone9,3": return "iPhone 7"
  230. case "iPhone9,2", "iPhone9,4": return "iPhone 7 Plus"
  231. case "iPhone8,4": return "iPhone SE"
  232. case "iPad2,1", "iPad2,2", "iPad2,3", "iPad2,4":return "iPad 2"
  233. case "iPad3,1", "iPad3,2", "iPad3,3": return "iPad 3"
  234. case "iPad3,4", "iPad3,5", "iPad3,6": return "iPad 4"
  235. case "iPad4,1", "iPad4,2", "iPad4,3": return "iPad Air"
  236. case "iPad5,3", "iPad5,4": return "iPad Air 2"
  237. case "iPad2,5", "iPad2,6", "iPad2,7": return "iPad Mini"
  238. case "iPad4,4", "iPad4,5", "iPad4,6": return "iPad Mini 2"
  239. case "iPad4,7", "iPad4,8", "iPad4,9": return "iPad Mini 3"
  240. case "iPad5,1", "iPad5,2": return "iPad Mini 4"
  241. case "iPad6,3", "iPad6,4", "iPad6,7", "iPad6,8":return "iPad Pro"
  242. case "AppleTV5,3": return "Apple TV"
  243. case "i386", "x86_64": return "Simulator"
  244. default: return identifier
  245. }
  246. }
  247. }