Utils.swift 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. public static func sGetCurrentDateTime(sFormat: String!) -> String! {
  45. let todaysDate = NSDate()
  46. let dateFormatter = DateFormatter()
  47. dateFormatter.dateFormat = sFormat
  48. return dateFormatter.string(from: todaysDate as Date)
  49. }
  50. public static func getMD5(string: String) -> Data {
  51. let length = Int(CC_MD5_DIGEST_LENGTH)
  52. let messageData = string.data(using:.utf8)!
  53. var digestData = Data(count: length)
  54. _ = digestData.withUnsafeMutableBytes { digestBytes -> UInt8 in
  55. messageData.withUnsafeBytes { messageBytes -> UInt8 in
  56. if let messageBytesBaseAddress = messageBytes.baseAddress, let digestBytesBlindMemory = digestBytes.bindMemory(to: UInt8.self).baseAddress {
  57. let messageLength = CC_LONG(messageData.count)
  58. CC_MD5(messageBytesBaseAddress, messageLength, digestBytesBlindMemory)
  59. }
  60. return 0
  61. }
  62. }
  63. return digestData
  64. }
  65. static let callDurationFormatter: DateComponentsFormatter = {
  66. let dateFormatter: DateComponentsFormatter
  67. dateFormatter = DateComponentsFormatter()
  68. dateFormatter.unitsStyle = .positional
  69. dateFormatter.allowedUnits = [.minute, .second]
  70. dateFormatter.zeroFormattingBehavior = .pad
  71. return dateFormatter
  72. }()
  73. public static func previewMessageText(chat: Chat) -> Any {
  74. if chat.credential == "1" && chat.lock == "2" {
  75. return ("🚫 _"+"Message has expired".localized()+"_").richText()
  76. } else if chat.credential == "1" {
  77. return "Confidential Message".localized().richText()
  78. } else if chat.attachmentFlag == "27" {
  79. return ("📄 " + "Live Streaming".localized()).richText()
  80. } else if !chat.image.isEmpty {
  81. if !chat.messageText.isEmpty {
  82. return "📷 \(chat.messageText)".richText()
  83. } else {
  84. return ("📷 " + "Photo".localized()).richText()
  85. }
  86. }
  87. else if !chat.video.isEmpty {
  88. if !chat.messageText.isEmpty {
  89. return "📹 \(chat.messageText)".richText()
  90. } else {
  91. return ("📹 " + "Video".localized()).richText()
  92. }
  93. }
  94. else if !chat.file.isEmpty {
  95. if chat.messageScope == "18" {
  96. return ("📄 Form").richText()
  97. }
  98. return ("📄 " + chat.messageText.components(separatedBy: "|")[0]).richText()
  99. } else if chat.attachmentFlag == "11" {
  100. return ("❤️ " + "Sticker".localized()).richText()
  101. }
  102. else {
  103. return chat.messageText.richText()
  104. }
  105. }
  106. static func getURLBase() -> String? {
  107. return UserDefaults.standard.string(forKey: "app_builder_url_base")
  108. }
  109. static func getIconDock() -> String? {
  110. return UserDefaults.standard.string(forKey: "app_builder_icon_dock")
  111. }
  112. static func getUrlDock() -> String? {
  113. return Utils.getURLBase()! + "dashboardv2/uploads/fb_icon/" + Utils.getIconDock()!
  114. }
  115. }
  116. public extension UIImage {
  117. var jpeg: Data? { jpegData(compressionQuality: 1) } // QUALITY min = 0 / max = 1
  118. var png: Data? { pngData() }
  119. }
  120. public extension Data {
  121. var uiImage: UIImage? { UIImage(data: self) }
  122. }
  123. public extension UIDevice {
  124. var modelName: String {
  125. var systemInfo = utsname()
  126. uname(&systemInfo)
  127. let machineMirror = Mirror(reflecting: systemInfo.machine)
  128. let identifier = machineMirror.children.reduce("") { identifier, element in
  129. guard let value = element.value as? Int8, value != 0 else { return identifier }
  130. return identifier + String(UnicodeScalar(UInt8(value)))
  131. }
  132. switch identifier {
  133. case "iPod5,1": return "iPod Touch 5"
  134. case "iPod7,1": return "iPod Touch 6"
  135. case "iPhone3,1", "iPhone3,2", "iPhone3,3": return "iPhone 4"
  136. case "iPhone4,1": return "iPhone 4s"
  137. case "iPhone5,1", "iPhone5,2": return "iPhone 5"
  138. case "iPhone5,3", "iPhone5,4": return "iPhone 5c"
  139. case "iPhone6,1", "iPhone6,2": return "iPhone 5s"
  140. case "iPhone7,2": return "iPhone 6"
  141. case "iPhone7,1": return "iPhone 6 Plus"
  142. case "iPhone8,1": return "iPhone 6s"
  143. case "iPhone8,2": return "iPhone 6s Plus"
  144. case "iPhone9,1", "iPhone9,3": return "iPhone 7"
  145. case "iPhone9,2", "iPhone9,4": return "iPhone 7 Plus"
  146. case "iPhone8,4": return "iPhone SE"
  147. case "iPad2,1", "iPad2,2", "iPad2,3", "iPad2,4":return "iPad 2"
  148. case "iPad3,1", "iPad3,2", "iPad3,3": return "iPad 3"
  149. case "iPad3,4", "iPad3,5", "iPad3,6": return "iPad 4"
  150. case "iPad4,1", "iPad4,2", "iPad4,3": return "iPad Air"
  151. case "iPad5,3", "iPad5,4": return "iPad Air 2"
  152. case "iPad2,5", "iPad2,6", "iPad2,7": return "iPad Mini"
  153. case "iPad4,4", "iPad4,5", "iPad4,6": return "iPad Mini 2"
  154. case "iPad4,7", "iPad4,8", "iPad4,9": return "iPad Mini 3"
  155. case "iPad5,1", "iPad5,2": return "iPad Mini 4"
  156. case "iPad6,3", "iPad6,4", "iPad6,7", "iPad6,8":return "iPad Pro"
  157. case "AppleTV5,3": return "Apple TV"
  158. case "i386", "x86_64": return "Simulator"
  159. default: return identifier
  160. }
  161. }
  162. }