FloatingButton.swift 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690
  1. //
  2. // FloatingButton.swift
  3. // Qmera
  4. //
  5. // Created by Yayan Dwi on 03/09/21.
  6. //
  7. import UIKit
  8. import nuSDKService
  9. import NotificationBannerSwift
  10. public class FloatingButton: UIView {
  11. var groupView: UIStackView!
  12. var scrollView: UIScrollView!
  13. var button_fb1: UIButton!
  14. var button_fb2: UIButton!
  15. var button_fb3: UIButton!
  16. var button_fb4: UIButton!
  17. var nexilis_button: UIImageView!
  18. var nexilis_pin: UIImageView!
  19. var leadingConstraintPin: NSLayoutConstraint!
  20. var bottomConstraintPin: NSLayoutConstraint!
  21. var trailingConstraintPin: NSLayoutConstraint!
  22. var topConstraintPin: NSLayoutConstraint!
  23. var lastPosY: CGFloat?
  24. var lastImageButton = ""
  25. var iconCC = ""
  26. let indicatorCounterFB = UIView()
  27. let labelCounterFB = UILabel()
  28. let indicatorCounterFBBig = UIImageView()
  29. var datePull: Date?
  30. var animationTimer = Timer()
  31. var configAnim: Int = Int(Utils.getFloatingAnim().components(separatedBy: "~")[0]) ?? 1
  32. var isLoopingAnim = (Int(Utils.getFloatingAnim().components(separatedBy: "~")[1]) ?? 1) == 1 ? true : false
  33. var lastRunAnimationHrz = -1
  34. var lastRunAnimationVrt = -1
  35. var panGesture: UIPanGestureRecognizer?
  36. var defaultWidthFB = (UIScreen.main.bounds.height * 0.5) / 7.5
  37. var defaultHeightFB = (UIScreen.main.bounds.height * 0.5) / 7.5
  38. let defaultWidthHeightMenuFB = (UIScreen.main.bounds.height * 0.45) / 7.5
  39. let widthFBAnim = (UIScreen.main.bounds.height * 0.8) / 7.5
  40. let heightFBAnim = (UIScreen.main.bounds.height * 1) / 7.5
  41. let heightFBSideTab = (UIScreen.main.bounds.height * 1.05) / 7.5
  42. let widthFBSideTab: CGFloat = 18
  43. final let MODE_VERTICAL_FLOATING_BUTTON = "1"
  44. final let MODE_VERTICAL_ANIMATION = "2"
  45. final let MODE_HORIZONTAL_ANIMATION = "3"
  46. final let MODE_HORIZONTAL_SIDE_TAB = "4"
  47. var countMenuFB: CGFloat = 5 {
  48. didSet {
  49. if isShow {
  50. show(isShow: isShow)
  51. }
  52. }
  53. }
  54. public weak var mySettingDelegate: SettingMABDelegate?
  55. public var isShow: Bool = false
  56. override init(frame: CGRect) {
  57. super.init(frame: frame)
  58. commonInit()
  59. }
  60. required init?(coder: NSCoder) {
  61. super.init(coder: coder)
  62. commonInit()
  63. }
  64. private func commonInit() {
  65. panGesture = UIPanGestureRecognizer(target: self, action: #selector(draggedView(_:)))
  66. addGestureRecognizer(panGesture!)
  67. nexilis_button = UIImageView()
  68. nexilis_button.translatesAutoresizingMaskIntoConstraints = false
  69. nexilis_button.isUserInteractionEnabled = true
  70. var dataImage: Data?
  71. if Utils.getConfigModeFB() == MODE_VERTICAL_ANIMATION || Utils.getConfigModeFB() == MODE_HORIZONTAL_ANIMATION {
  72. defaultWidthFB = widthFBAnim
  73. defaultHeightFB = heightFBAnim
  74. nexilis_button.sd_setImage(with: URL(string: Utils.getConfigModeFB() == MODE_VERTICAL_ANIMATION ? Utils.getIconCenterAnim2()! : Utils.getIconCenterAnim4()!)) { [self] (image, error, cacheType, imageURL) in
  75. if error != nil {
  76. //print("Error loading GIF: \(error.localizedDescription)")
  77. } else {
  78. // Configure the animation
  79. nexilis_button.animationImages = image?.images
  80. nexilis_button.animationDuration = image?.duration ?? 0.0
  81. nexilis_button.animationRepeatCount = 0
  82. nexilis_button.startAnimating()
  83. }
  84. }
  85. } else {
  86. if !Utils.getIconDock().isEmpty && Utils.getConfigModeFB() == MODE_VERTICAL_FLOATING_BUTTON {
  87. dataImage = try? Data(contentsOf: URL(string: Utils.getUrlDock()!)!)
  88. if dataImage != nil {
  89. nexilis_button.image = UIImage(data: dataImage!)
  90. }
  91. } else {
  92. if Utils.getConfigModeFB() == MODE_HORIZONTAL_SIDE_TAB {
  93. defaultWidthFB = widthFBSideTab
  94. defaultHeightFB = heightFBSideTab
  95. }
  96. nexilis_button.image = UIImage(named: Utils.getConfigModeFB() != MODE_HORIZONTAL_SIDE_TAB ? "pb_button" : "pb_side_tab", in: Bundle.resourceBundle(for: DigiX.self), with: nil)
  97. }
  98. }
  99. backgroundColor = .clear
  100. frame = CGRect(x: UIScreen.main.bounds.width - defaultWidthFB, y: (UIScreen.main.bounds.height / 2) - defaultHeightFB, width: Utils.getConfigModeFB() == MODE_HORIZONTAL_SIDE_TAB ? UIScreen.main.bounds.width - defaultWidthFB : defaultWidthFB, height: defaultHeightFB)
  101. if Utils.getConfigModeFB() == MODE_VERTICAL_ANIMATION || Utils.getConfigModeFB() == MODE_HORIZONTAL_ANIMATION {
  102. if configAnim == 0 { //left to right
  103. lastRunAnimationHrz = 1
  104. } else if configAnim == 1 { //right to left
  105. lastRunAnimationHrz = -1
  106. } else if configAnim == 2 { //top to bottom
  107. lastRunAnimationVrt = 1
  108. } else if configAnim == 3 { //top to bottom
  109. lastRunAnimationVrt = -1
  110. }
  111. if configAnim >= 0 && configAnim <= 3 {
  112. checkDelayAnimation()
  113. }
  114. }
  115. let qmeraTap = UITapGestureRecognizer(target: self, action: #selector(qmeraTap))
  116. qmeraTap.numberOfTouchesRequired = 1
  117. nexilis_button.addGestureRecognizer(qmeraTap)
  118. let qmeraLongPress = UILongPressGestureRecognizer(target: self, action: #selector(qmeraLongPress(gestureRecognizer:)))
  119. nexilis_button.addGestureRecognizer(qmeraLongPress)
  120. addSubview(nexilis_button)
  121. nexilis_button.widthAnchor.constraint(equalToConstant: defaultWidthFB).isActive = true
  122. nexilis_button.heightAnchor.constraint(equalToConstant: defaultHeightFB).isActive = true
  123. nexilis_button.leftAnchor.constraint(equalTo: leftAnchor).isActive = true
  124. nexilis_button.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true
  125. scrollView = UIScrollView()
  126. scrollView.translatesAutoresizingMaskIntoConstraints = false
  127. // scrollView.layer.borderWidth = 1.0
  128. // scrollView.layer.borderColor = UIColor.white.cgColor
  129. // scrollView.layer.cornerRadius = 8.0
  130. // scrollView.layer.masksToBounds = true
  131. // scrollView.backgroundColor = .black.withAlphaComponent(0.25)
  132. addSubview(scrollView)
  133. if Utils.getConfigModeFB() != MODE_HORIZONTAL_SIDE_TAB {
  134. scrollView.topAnchor.constraint(equalTo: topAnchor).isActive = true
  135. if Utils.getConfigModeFB() == MODE_VERTICAL_ANIMATION {
  136. scrollView.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true
  137. scrollView.leftAnchor.constraint(equalTo: nexilis_button.rightAnchor, constant: -20).isActive = true
  138. scrollView.widthAnchor.constraint(equalToConstant: defaultWidthHeightMenuFB + 10).isActive = true
  139. scrollView.isHidden = true
  140. } else if Utils.getConfigModeFB() == MODE_HORIZONTAL_ANIMATION {
  141. scrollView.bottomAnchor.constraint(equalTo: nexilis_button.topAnchor).isActive = true
  142. scrollView.leftAnchor.constraint(equalTo: nexilis_button.leftAnchor).isActive = true
  143. scrollView.widthAnchor.constraint(equalToConstant: defaultWidthHeightMenuFB * (countMenuFB - 1)).isActive = true
  144. } else {
  145. scrollView.widthAnchor.constraint(equalToConstant: defaultWidthHeightMenuFB + 10).isActive = true
  146. scrollView.centerXAnchor.constraint(equalTo: centerXAnchor).isActive = true
  147. scrollView.bottomAnchor.constraint(equalTo: nexilis_button.topAnchor).isActive = true
  148. }
  149. } else {
  150. scrollView.widthAnchor.constraint(equalToConstant: UIScreen.main.bounds.width - defaultWidthFB).isActive = true
  151. scrollView.heightAnchor.constraint(equalToConstant: defaultHeightFB).isActive = true
  152. scrollView.leftAnchor.constraint(equalTo: nexilis_button.rightAnchor).isActive = true
  153. scrollView.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true
  154. scrollView.backgroundColor = .black.withAlphaComponent(0.25)
  155. }
  156. groupView = UIStackView()
  157. groupView.translatesAutoresizingMaskIntoConstraints = false
  158. groupView.axis = (Utils.getConfigModeFB() == MODE_HORIZONTAL_SIDE_TAB || Utils.getConfigModeFB() == MODE_HORIZONTAL_ANIMATION) ? .horizontal : .vertical
  159. if Utils.getConfigModeFB() != MODE_HORIZONTAL_SIDE_TAB && Utils.getConfigModeFB() != MODE_HORIZONTAL_ANIMATION {
  160. groupView.distribution = .fillEqually
  161. }
  162. scrollView.addSubview(groupView)
  163. if Utils.getConfigModeFB() == MODE_HORIZONTAL_SIDE_TAB {
  164. groupView.leftAnchor.constraint(equalTo: scrollView.leftAnchor).isActive = true
  165. groupView.rightAnchor.constraint(equalTo: scrollView.rightAnchor).isActive = true
  166. groupView.centerYAnchor.constraint(equalTo: scrollView.centerYAnchor).isActive = true
  167. groupView.heightAnchor.constraint(equalToConstant: defaultHeightFB - 10).isActive = true
  168. } else {
  169. groupView.widthAnchor.constraint(equalToConstant: Utils.getConfigModeFB() == MODE_HORIZONTAL_ANIMATION ? defaultWidthHeightMenuFB * (countMenuFB - 1) : defaultWidthHeightMenuFB).isActive = true
  170. groupView.topAnchor.constraint(equalTo: scrollView.topAnchor, constant: Utils.getConfigModeFB() == MODE_HORIZONTAL_ANIMATION ? 0 : 5).isActive = true
  171. groupView.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor, constant: Utils.getConfigModeFB() == MODE_HORIZONTAL_ANIMATION ? 0 : -5).isActive = true
  172. groupView.leftAnchor.constraint(equalTo: scrollView.leftAnchor, constant: Utils.getConfigModeFB() == MODE_HORIZONTAL_ANIMATION ? 0 : 6).isActive = true
  173. }
  174. pullButton()
  175. let center: NotificationCenter = NotificationCenter.default
  176. center.addObserver(self, selector: #selector(imageFBUpdate(notification:)), name: NSNotification.Name(rawValue: "imageFBUpdate"), object: nil)
  177. center.addObserver(self, selector: #selector(checkCounter), name: NSNotification.Name(rawValue: DigiX.listenerReceiveChat), object: nil)
  178. center.addObserver(self, selector: #selector(checkCounter), name: NSNotification.Name(rawValue: "reloadTabChats"), object: nil)
  179. let tapGesture = UITapGestureRecognizer(target: self, action: #selector(hideButton))
  180. tapGesture.cancelsTouchesInView = false
  181. UIApplication.shared.windows.first?.rootViewController?.view.addGestureRecognizer(tapGesture)
  182. }
  183. private func checkDelayAnimation() {
  184. DispatchQueue.main.asyncAfter(deadline: .now() + 3, execute: { [self] in
  185. if !isShow {
  186. animationTimer = Timer.scheduledTimer(timeInterval: 0.001, target: self, selector: #selector(runAnimation), userInfo: nil, repeats: true)
  187. }
  188. })
  189. }
  190. @objc func runAnimation(){
  191. DispatchQueue.main.async { [self] in
  192. if configAnim == 0 || configAnim == 1 {
  193. if (lastRunAnimationHrz == -1 && frame.origin.x >= 0) || lastRunAnimationHrz == 1 && frame.origin.x <= UIScreen.main.bounds.width - defaultWidthFB {
  194. if lastRunAnimationHrz == -1 {
  195. frame.origin.x-=0.1
  196. } else {
  197. frame.origin.x+=0.1
  198. }
  199. } else {
  200. lastRunAnimationHrz = lastRunAnimationHrz == 1 ? -1 : 1
  201. if (lastRunAnimationHrz == -1 && configAnim == 1) || (lastRunAnimationHrz == 1 && configAnim == 0) {
  202. animationTimer.invalidate()
  203. if isLoopingAnim {
  204. checkDelayAnimation()
  205. }
  206. }
  207. }
  208. } else {
  209. if (lastRunAnimationVrt == -1 && frame.origin.y >= 0) || lastRunAnimationVrt == 1 && frame.origin.y <= UIScreen.main.bounds.height - defaultHeightFB {
  210. if lastRunAnimationVrt == -1 {
  211. frame.origin.y-=0.1
  212. } else {
  213. frame.origin.y+=0.1
  214. }
  215. } else {
  216. lastRunAnimationVrt = lastRunAnimationVrt == 1 ? -1 : 1
  217. if (lastRunAnimationVrt == -1 && configAnim == 3) || (lastRunAnimationVrt == 1 && configAnim == 2) {
  218. animationTimer.invalidate()
  219. if isLoopingAnim {
  220. checkDelayAnimation()
  221. }
  222. }
  223. }
  224. }
  225. }
  226. }
  227. private func pullButton() {
  228. if datePull == nil || Int(Date().timeIntervalSince(datePull!)) >= 60 {
  229. datePull = Date()
  230. } else if Int(Date().timeIntervalSince(datePull!)) < 60 {
  231. return
  232. }
  233. if !CheckConnection.isConnectedToNetwork() || API.nGetCLXConnState() == 0 {
  234. let imageView = UIImageView(image: UIImage(systemName: "xmark.circle.fill"))
  235. imageView.tintColor = .white
  236. let banner = FloatingNotificationBanner(title: "Check your connection".localized(), subtitle: nil, titleFont: UIFont.systemFont(ofSize: 16), titleColor: nil, titleTextAlign: .left, subtitleFont: nil, subtitleColor: nil, subtitleTextAlign: nil, leftView: imageView, rightView: nil, style: .danger, colors: nil, iconPosition: .center)
  237. banner.show()
  238. return
  239. }
  240. if groupView.subviews.count == 0 {
  241. getDefaultButton()
  242. }
  243. DispatchQueue.global().async { [self] in
  244. if !Utils.getCustomButtons().isEmpty && Utils.getConfigModeFB() != MODE_HORIZONTAL_SIDE_TAB && Utils.getConfigModeFB() != MODE_HORIZONTAL_ANIMATION {
  245. DispatchQueue.main.async { [self] in
  246. groupView.subviews.forEach({ $0.removeFromSuperview() })
  247. let customButtons = Utils.getCustomButtons().components(separatedBy: ",")
  248. let customIcons = Utils.getCustomFBIcon().components(separatedBy: ",")
  249. countMenuFB = CGFloat(customButtons.count > 5 ? 5 : customButtons.count)
  250. for i in 0..<customButtons.count {
  251. let package_id = customButtons[i]
  252. let app_id = ""
  253. let icon = customIcons[i]
  254. let newButton = UIButton()
  255. newButton.heightAnchor.constraint(equalToConstant: defaultWidthHeightMenuFB).isActive = true
  256. newButton.translatesAutoresizingMaskIntoConstraints = false
  257. DispatchQueue.global().async {
  258. let data = try? Data(contentsOf: URL(string: "https://DigiX.io/dashboardv2/uploads/fb_icon/\(icon)")!) //make sure your image in this url does exist, otherwise unwrap in a if let check / try-catch
  259. DispatchQueue.main.async {
  260. if data != nil {
  261. newButton.setImage(UIImage(data: data!), for: .normal)
  262. }
  263. }
  264. }
  265. groupView.addArrangedSubview(newButton)
  266. newButton.restorationIdentifier = package_id
  267. newButton.accessibilityIdentifier = app_id
  268. newButton.addTarget(self, action: #selector(fbTap), for: .touchUpOutside)
  269. }
  270. }
  271. return
  272. }
  273. if let response = DigiX.writeSync(message: CoreMessage_TMessageBank.pullFloatingButton(), timeout: 30 * 1000){
  274. if response.isOk() {
  275. let data = response.getBody(key: CoreMessage_TMessageKey.DATA, default_value: "")
  276. if !data.isEmpty {
  277. if let jsonArray = try! JSONSerialization.jsonObject(with: data.data(using: String.Encoding.utf8)!, options: JSONSerialization.ReadingOptions()) as? [AnyObject] {
  278. DispatchQueue.main.async { [self] in
  279. groupView.subviews.forEach({ $0.removeFromSuperview() })
  280. if jsonArray.count == 0 {
  281. countMenuFB = CGFloat(4)
  282. getDefaultButton()
  283. } else {
  284. if Utils.getConfigModeFB() != MODE_HORIZONTAL_ANIMATION {
  285. countMenuFB = CGFloat(jsonArray.count > 5 ? 5 : jsonArray.count)
  286. } else {
  287. countMenuFB = CGFloat(jsonArray.filter({ $0["mode"] as? Int == Int(MODE_HORIZONTAL_ANIMATION) }).count)
  288. }
  289. for json in jsonArray {
  290. let package_id = json["package_id"] as! String
  291. let app_id = (json["app_id"] as? String) ?? ""
  292. let icon = (json["icon"] as? String) ?? ""
  293. let mode = "\((json["mode"] as? Int) ?? 1)"
  294. let newButton = UIButton()
  295. if mode != Utils.getConfigModeFB() {
  296. continue
  297. }
  298. if mode == MODE_HORIZONTAL_SIDE_TAB {
  299. newButton.widthAnchor.constraint(equalToConstant: defaultHeightFB - 10).isActive = true
  300. newButton.heightAnchor.constraint(equalToConstant: defaultHeightFB - 10).isActive = true
  301. } else if mode == MODE_HORIZONTAL_ANIMATION {
  302. newButton.widthAnchor.constraint(equalToConstant: defaultWidthHeightMenuFB).isActive = true
  303. newButton.heightAnchor.constraint(equalToConstant: defaultWidthHeightMenuFB).isActive = true
  304. } else {
  305. newButton.heightAnchor.constraint(equalToConstant: defaultWidthHeightMenuFB).isActive = true
  306. }
  307. newButton.translatesAutoresizingMaskIntoConstraints = false
  308. var indexTap = 0
  309. if package_id.contains("_fb"){
  310. let numIdx = package_id.split(separator: "_")[1]
  311. indexTap = Int(String(numIdx).substring(from: 2, to: numIdx.count))!
  312. }
  313. if indexTap == DigiX.IDX_CHAT {
  314. newButton.setImage(UIImage(named: mode == MODE_HORIZONTAL_SIDE_TAB ? "pb_button_hrz_chat" : mode == MODE_HORIZONTAL_ANIMATION ? "pb_button_hrz_anim_chat" : "pb_button_chat", in: Bundle.resourceBundle(for: DigiX.self), with: nil), for: .normal)
  315. } else if indexTap == DigiX.IDX_CONVERSATION {
  316. newButton.setImage(UIImage(named: mode == MODE_HORIZONTAL_SIDE_TAB ? "pb_button_hrz_conversation" : mode == MODE_HORIZONTAL_ANIMATION ? "pb_button_hrz_anim_conversation" : "pb_button_chat", in: Bundle.resourceBundle(for: DigiX.self), with: nil), for: .normal)
  317. } else if indexTap == DigiX.IDX_CALL {
  318. newButton.setImage(UIImage(named: mode == MODE_HORIZONTAL_SIDE_TAB ? "pb_button_hrz_call" : mode == MODE_HORIZONTAL_ANIMATION ? "pb_button_hrz_anim_call" : "pb_button_call", in: Bundle.resourceBundle(for: DigiX.self), with: nil), for: .normal)
  319. } else if indexTap == DigiX.IDX_CC {
  320. newButton.setImage(UIImage(named: mode == MODE_HORIZONTAL_SIDE_TAB ? "pb_button_hrz_cc" : mode == MODE_HORIZONTAL_ANIMATION ? "pb_button_hrz_anim_cc" : "pb_button_cc", in: Bundle.resourceBundle(for: DigiX.self), with: nil), for: .normal)
  321. } else if indexTap == DigiX.IDX_STREAM {
  322. newButton.setImage(UIImage(named: mode == MODE_HORIZONTAL_SIDE_TAB ? "pb_button_hrz_stream" : mode == MODE_HORIZONTAL_ANIMATION ? "pb_button_hrz_anim_stream" : "pb_button_stream", in: Bundle.resourceBundle(for: DigiX.self), with: nil), for: .normal)
  323. } else if indexTap == DigiX.IDX_SOCIAL_COMMERCE {
  324. newButton.setImage(UIImage(named: mode == MODE_HORIZONTAL_SIDE_TAB ? "pb_button_hrz_social_commerce" : mode == MODE_HORIZONTAL_ANIMATION ? "pb_button_hrz_anim_commerce" : "pb_button_commerce", in: Bundle.resourceBundle(for: DigiX.self), with: nil), for: .normal)
  325. } else if indexTap == DigiX.IDX_NEWS {
  326. newButton.setImage(UIImage(named: mode == MODE_HORIZONTAL_SIDE_TAB ? "pb_button_hrz_news" : mode == MODE_HORIZONTAL_ANIMATION ? "pb_button_hrz_anim_news" : "pb_button_news", in: Bundle.resourceBundle(for: DigiX.self), with: nil), for: .normal)
  327. } else if indexTap == DigiX.IDX_POST {
  328. newButton.setImage(UIImage(named: mode == MODE_HORIZONTAL_SIDE_TAB ? "pb_button_hrz_post" : mode == MODE_HORIZONTAL_ANIMATION ? "pb_button_hrz_anim_post" : "pb_button_post", in: Bundle.resourceBundle(for: DigiX.self), with: nil), for: .normal)
  329. } else if indexTap == DigiX.IDX_NOTIF_CENTER {
  330. newButton.setImage(UIImage(named: mode == MODE_HORIZONTAL_SIDE_TAB ? "pb_button_hrz_notif_center" : mode == MODE_HORIZONTAL_ANIMATION ? "pb_button_hrz_anim_notif_center" : "pb_button_notification", in: Bundle.resourceBundle(for: DigiX.self), with: nil), for: .normal)
  331. } else {
  332. newButton.setImage(UIImage(named: mode == MODE_HORIZONTAL_SIDE_TAB ? "pb_button_hrz_more" : mode == MODE_HORIZONTAL_ANIMATION ? "pb_button_hrz_anim_more" : "pb_button_others", in: Bundle.resourceBundle(for: DigiX.self), with: nil), for: .normal)
  333. }
  334. if !icon.isEmpty {
  335. DispatchQueue.global().async {
  336. let data = try? Data(contentsOf: URL(string: "https://newuniverse.io/get_file?account=\(DigiX.sAPIKey)&image=\(icon)")!) //make sure your image in this url does exist, otherwise unwrap in a if let check / try-catch
  337. DispatchQueue.main.async {
  338. if data != nil {
  339. newButton.setImage(UIImage(data: data!), for: .normal)
  340. }
  341. }
  342. }
  343. }
  344. groupView.addArrangedSubview(newButton)
  345. newButton.restorationIdentifier = package_id
  346. newButton.accessibilityIdentifier = app_id
  347. newButton.addTarget(self, action: #selector(fbTap), for: .touchUpOutside)
  348. }
  349. }
  350. }
  351. }
  352. } else {
  353. groupView.subviews.forEach({ $0.removeFromSuperview() })
  354. getDefaultButton()
  355. }
  356. } else {
  357. groupView.subviews.forEach({ $0.removeFromSuperview() })
  358. getDefaultButton()
  359. }
  360. }
  361. }
  362. }
  363. func getDefaultButton() {
  364. let data = [DigiX.IDX_NOTIF_CENTER, DigiX.IDX_CC, DigiX.IDX_CONVERSATION, DigiX.IDX_CALL, DigiX.IDX_STREAM]
  365. for i in 0..<data.count {
  366. let newButton = UIButton()
  367. let mode = Utils.getConfigModeFB()
  368. if mode == MODE_HORIZONTAL_SIDE_TAB {
  369. newButton.widthAnchor.constraint(equalToConstant: defaultHeightFB - 10).isActive = true
  370. newButton.heightAnchor.constraint(equalToConstant: defaultHeightFB - 10).isActive = true
  371. } else if mode == MODE_HORIZONTAL_ANIMATION {
  372. newButton.widthAnchor.constraint(equalToConstant: defaultWidthHeightMenuFB).isActive = true
  373. newButton.heightAnchor.constraint(equalToConstant: defaultWidthHeightMenuFB).isActive = true
  374. } else {
  375. newButton.heightAnchor.constraint(equalToConstant: defaultWidthHeightMenuFB).isActive = true
  376. }
  377. newButton.translatesAutoresizingMaskIntoConstraints = false
  378. let indexTap = data[i]
  379. if indexTap == DigiX.IDX_CHAT {
  380. newButton.setImage(UIImage(named: mode == MODE_HORIZONTAL_SIDE_TAB ? "pb_button_hrz_chat" : mode == MODE_HORIZONTAL_ANIMATION ? "pb_button_hrz_anim_chat" : "pb_button_chat", in: Bundle.resourceBundle(for: DigiX.self), with: nil), for: .normal)
  381. } else if indexTap == DigiX.IDX_CONVERSATION {
  382. newButton.setImage(UIImage(named: mode == MODE_HORIZONTAL_SIDE_TAB ? "pb_button_hrz_conversation" : mode == MODE_HORIZONTAL_ANIMATION ? "pb_button_hrz_anim_conversation" : "pb_button_chat", in: Bundle.resourceBundle(for: DigiX.self), with: nil), for: .normal)
  383. } else if indexTap == DigiX.IDX_CALL {
  384. newButton.setImage(UIImage(named: mode == MODE_HORIZONTAL_SIDE_TAB ? "pb_button_hrz_call" : mode == MODE_HORIZONTAL_ANIMATION ? "pb_button_hrz_anim_call" : "pb_button_call", in: Bundle.resourceBundle(for: DigiX.self), with: nil), for: .normal)
  385. } else if indexTap == DigiX.IDX_CC {
  386. newButton.setImage(UIImage(named: mode == MODE_HORIZONTAL_SIDE_TAB ? "pb_button_hrz_cc" : mode == MODE_HORIZONTAL_ANIMATION ? "pb_button_hrz_anim_cc" : "pb_button_cc", in: Bundle.resourceBundle(for: DigiX.self), with: nil), for: .normal)
  387. } else if indexTap == DigiX.IDX_STREAM {
  388. newButton.setImage(UIImage(named: mode == MODE_HORIZONTAL_SIDE_TAB ? "pb_button_hrz_stream" : mode == MODE_HORIZONTAL_ANIMATION ? "pb_button_hrz_anim_stream" : "pb_button_stream", in: Bundle.resourceBundle(for: DigiX.self), with: nil), for: .normal)
  389. } else if indexTap == DigiX.IDX_SOCIAL_COMMERCE {
  390. newButton.setImage(UIImage(named: mode == MODE_HORIZONTAL_SIDE_TAB ? "pb_button_hrz_social_commerce" : mode == MODE_HORIZONTAL_ANIMATION ? "pb_button_hrz_anim_commerce" : "pb_button_commerce", in: Bundle.resourceBundle(for: DigiX.self), with: nil), for: .normal)
  391. } else if indexTap == DigiX.IDX_NEWS {
  392. newButton.setImage(UIImage(named: mode == MODE_HORIZONTAL_SIDE_TAB ? "pb_button_hrz_news" : mode == MODE_HORIZONTAL_ANIMATION ? "pb_button_hrz_anim_news" : "pb_button_news", in: Bundle.resourceBundle(for: DigiX.self), with: nil), for: .normal)
  393. } else if indexTap == DigiX.IDX_POST {
  394. newButton.setImage(UIImage(named: mode == MODE_HORIZONTAL_SIDE_TAB ? "pb_button_hrz_post" : mode == MODE_HORIZONTAL_ANIMATION ? "pb_button_hrz_anim_post" : "pb_button_post", in: Bundle.resourceBundle(for: DigiX.self), with: nil), for: .normal)
  395. } else if indexTap == DigiX.IDX_NOTIF_CENTER {
  396. newButton.setImage(UIImage(named: mode == MODE_HORIZONTAL_SIDE_TAB ? "pb_button_hrz_notif_center" : mode == MODE_HORIZONTAL_ANIMATION ? "pb_button_hrz_anim_notif_center" : "pb_button_notification", in: Bundle.resourceBundle(for: DigiX.self), with: nil), for: .normal)
  397. } else {
  398. newButton.setImage(UIImage(named: mode == MODE_HORIZONTAL_SIDE_TAB ? "pb_button_hrz_more" : mode == MODE_HORIZONTAL_ANIMATION ? "pb_button_hrz_anim_more" : "pb_button_others", in: Bundle.resourceBundle(for: DigiX.self), with: nil), for: .normal)
  399. }
  400. groupView.addArrangedSubview(newButton)
  401. newButton.restorationIdentifier = "default_fb\(data[i])"
  402. newButton.accessibilityIdentifier = ""
  403. newButton.addTarget(self, action: #selector(fbTap), for: .touchUpOutside)
  404. }
  405. }
  406. @objc func draggedView(_ sender:UIPanGestureRecognizer){
  407. let size = UIScreen.main.bounds
  408. let widthScreen = size.width
  409. let heightScreen = size.height
  410. let minimumx: CGFloat = Utils.getConfigModeFB() == MODE_HORIZONTAL_ANIMATION && isShow ? 80 : Utils.getConfigModeFB() == MODE_VERTICAL_ANIMATION && isShow ? 60 : 40
  411. let maximumx = Utils.getConfigModeFB() == MODE_HORIZONTAL_ANIMATION && isShow ? widthScreen - frame.width + 20 : Utils.getConfigModeFB() == MODE_VERTICAL_ANIMATION && isShow ? widthScreen - 10 - defaultWidthHeightMenuFB : widthScreen - 40
  412. let maxMinXSideTab = isShow ? center.x : widthScreen + (center.x - widthScreen)
  413. let translation = sender.translation(in: self)
  414. var xPos = center.x + translation.x
  415. var yPos = center.y + translation.y
  416. bringSubviewToFront(self)
  417. if Utils.getConfigModeFB() == MODE_HORIZONTAL_SIDE_TAB {
  418. xPos = maxMinXSideTab
  419. } else {
  420. if (xPos < minimumx) {
  421. xPos = minimumx
  422. }
  423. if (xPos > maximumx) {
  424. xPos = maximumx
  425. }
  426. }
  427. if(isShow) {
  428. let minimumy = Utils.getConfigModeFB() == MODE_HORIZONTAL_SIDE_TAB ? 50 : (defaultWidthHeightMenuFB * countMenuFB) - defaultHeightFB - 10
  429. let maximumy = Utils.getConfigModeFB() == MODE_HORIZONTAL_SIDE_TAB ? heightScreen - 50 : heightScreen - defaultHeightFB - 10
  430. if(yPos < minimumy) {
  431. yPos = minimumy
  432. }
  433. if(yPos > maximumy) {
  434. yPos = maximumy
  435. }
  436. } else {
  437. let minimumy: CGFloat = 50
  438. let maximumy = heightScreen - 50
  439. if(yPos < minimumy) {
  440. yPos = minimumy
  441. }
  442. if(yPos > maximumy) {
  443. yPos = maximumy
  444. }
  445. }
  446. center = CGPoint(x: xPos, y: yPos)
  447. sender.setTranslation(CGPoint.zero, in: self)
  448. if lastPosY != nil {
  449. lastPosY = nil
  450. }
  451. UserDefaults.standard.set(center.x, forKey: "xlastPosFB")
  452. UserDefaults.standard.set(center.y, forKey: "ylastPosFB")
  453. }
  454. @objc func imageFBUpdate(notification: NSNotification) {
  455. }
  456. @objc func checkCounter() {
  457. let counter = queryCountCounter()
  458. if counter > 0 {
  459. DispatchQueue.main.async { [self] in
  460. if button_fb2 != nil && !indicatorCounterFB.isDescendant(of: button_fb2) {
  461. button_fb2.addSubview(indicatorCounterFB)
  462. indicatorCounterFB.layer.cornerRadius = 7.5
  463. indicatorCounterFB.layer.masksToBounds = true
  464. indicatorCounterFB.backgroundColor = .systemRed
  465. indicatorCounterFB.anchor(top: button_fb2.topAnchor, left: button_fb2.leftAnchor, height: 15, minWidth: 15, maxWidth: 20)
  466. indicatorCounterFB.addSubview(labelCounterFB)
  467. labelCounterFB.anchor(left: indicatorCounterFB.leftAnchor, right: indicatorCounterFB.rightAnchor, paddingLeft: 5, paddingRight: 5, centerX: indicatorCounterFB.centerXAnchor, centerY: indicatorCounterFB.centerYAnchor)
  468. labelCounterFB.font = .systemFont(ofSize: 10)
  469. labelCounterFB.textColor = .white
  470. }
  471. if !indicatorCounterFBBig.isDescendant(of: nexilis_button){
  472. nexilis_button.addSubview(indicatorCounterFBBig)
  473. indicatorCounterFBBig.tintColor = .systemRed
  474. indicatorCounterFBBig.image = UIImage(systemName: "staroflife.circle.fill")
  475. indicatorCounterFBBig.anchor(top: nexilis_button.topAnchor, left: nexilis_button.leftAnchor, paddingTop: 5, paddingLeft: 5, width: 15, height: 15)
  476. }
  477. labelCounterFB.text = "\(counter)"
  478. }
  479. } else {
  480. DispatchQueue.main.async { [self] in
  481. if button_fb2 != nil && indicatorCounterFB.isDescendant(of: button_fb2) {
  482. indicatorCounterFB.removeFromSuperview()
  483. }
  484. if indicatorCounterFBBig.isDescendant(of: nexilis_button) {
  485. indicatorCounterFBBig.removeFromSuperview()
  486. }
  487. }
  488. }
  489. }
  490. private func queryCountCounter() -> Int32 {
  491. var counter: Int32?
  492. Database.shared.database?.inTransaction({ (fmdb, rollback) in
  493. if let cursor = Database.shared.getRecords(fmdb: fmdb, query: "SELECT SUM(counter) FROM MESSAGE_SUMMARY"), cursor.next() {
  494. counter = cursor.int(forColumnIndex: 0)
  495. cursor.close()
  496. }
  497. })
  498. return counter ?? 0
  499. }
  500. @objc func qmeraTap() {
  501. show(isShow: !isShow)
  502. }
  503. @objc func fbTap(_ sender: UIButton) {
  504. let package_id = sender.restorationIdentifier!
  505. var app_id = sender.accessibilityIdentifier!
  506. var indexTap = 0
  507. if package_id.contains("_fb"){
  508. let numIdx = package_id.split(separator: "_")[1]
  509. indexTap = Int(String(numIdx).substring(from: 2, to: numIdx.count))!
  510. }
  511. if indexTap == 2 {
  512. app_id = package_id.components(separatedBy: "_")[2]
  513. }
  514. DigiX.buttonClicked(index: indexTap, id: app_id)
  515. hideButton()
  516. }
  517. @objc func fb1Tap() {
  518. APIS.openContactCenter()
  519. hideButton()
  520. }
  521. @objc func fb2Tap() {
  522. APIS.startConversation()
  523. hideButton()
  524. }
  525. @objc func fb3Tap() {
  526. APIS.openCall()
  527. hideButton()
  528. }
  529. @objc func fb4Tap() {
  530. APIS.openStreaming()
  531. hideButton()
  532. }
  533. @objc func qmeraLongPress(gestureRecognizer: UILongPressGestureRecognizer) {
  534. if gestureRecognizer.state == .began {
  535. if mySettingDelegate != nil {
  536. mySettingDelegate?.settingDelegate()
  537. } else {
  538. let navigationController = AppStoryBoard.Palio.instance.instantiateViewController(withIdentifier: "settingNav") as! UINavigationController
  539. navigationController.modalPresentationStyle = .fullScreen
  540. navigationController.navigationBar.tintColor = .white
  541. navigationController.navigationBar.barTintColor = .mainColor
  542. navigationController.navigationBar.isTranslucent = false
  543. navigationController.navigationBar.overrideUserInterfaceStyle = .dark
  544. navigationController.navigationBar.barStyle = .black
  545. let cancelButtonAttributes: [NSAttributedString.Key: Any] = [NSAttributedString.Key.foregroundColor: UIColor.white, NSAttributedString.Key.font : UIFont.systemFont(ofSize: 16)]
  546. UIBarButtonItem.appearance().setTitleTextAttributes(cancelButtonAttributes, for: .normal)
  547. let textAttributes = [NSAttributedString.Key.foregroundColor:UIColor.white]
  548. navigationController.navigationBar.titleTextAttributes = textAttributes
  549. navigationController.view.backgroundColor = .mainColor
  550. UIApplication.shared.rootViewController?.present(navigationController, animated: true, completion: nil)
  551. }
  552. hideButton()
  553. }
  554. }
  555. @objc public func hideButton() {
  556. animationTimer.invalidate()
  557. if isShow {
  558. show(isShow: false)
  559. }
  560. if self.frame.origin.x < UIScreen.main.bounds.width / 2 - 30 {
  561. self.frame.origin.x = 0
  562. } else {
  563. self.frame.origin.x = UIScreen.main.bounds.width - defaultWidthFB
  564. }
  565. }
  566. public func show(isShow: Bool) {
  567. self.isShow = isShow
  568. if isShow {
  569. animationTimer.invalidate()
  570. pullButton()
  571. if indicatorCounterFBBig.isDescendant(of: nexilis_button) {
  572. indicatorCounterFBBig.isHidden = true
  573. }
  574. var height = CGFloat((defaultWidthHeightMenuFB * countMenuFB) + defaultHeightFB + 5) //defaultWidthHeightMenuFB
  575. var width = frame.width
  576. var xPosition = frame.origin.x
  577. if Utils.getConfigModeFB() == MODE_VERTICAL_ANIMATION {
  578. height = CGFloat((defaultWidthHeightMenuFB * (countMenuFB - 2)) + defaultHeightFB - 5)
  579. width = frame.width + defaultWidthHeightMenuFB
  580. if xPosition > UIScreen.main.bounds.width - defaultWidthFB - defaultWidthHeightMenuFB {
  581. xPosition = UIScreen.main.bounds.width - defaultWidthFB - defaultWidthHeightMenuFB
  582. }
  583. scrollView.isHidden = false
  584. } else if Utils.getConfigModeFB() == MODE_HORIZONTAL_ANIMATION {
  585. height = defaultHeightFB + defaultWidthHeightMenuFB
  586. width = defaultWidthHeightMenuFB * (countMenuFB - 1)
  587. if xPosition > UIScreen.main.bounds.width - defaultWidthHeightMenuFB * (countMenuFB) {
  588. xPosition = UIScreen.main.bounds.width - defaultWidthHeightMenuFB * (countMenuFB)
  589. }
  590. }
  591. var yPosition = frame.origin.y - height + defaultHeightFB
  592. if yPosition <= 25 {
  593. lastPosY = frame.origin.y
  594. yPosition = 25
  595. }
  596. if Utils.getConfigModeFB() != MODE_HORIZONTAL_SIDE_TAB {
  597. frame = CGRect(x: xPosition, y: yPosition, width: width, height: height)
  598. } else {
  599. UIView.animate(withDuration: 0.5, animations: {
  600. self.frame.origin.x = 0
  601. })
  602. }
  603. DispatchQueue.main.asyncAfter(deadline: .now() + 0.5, execute: { [self] in
  604. if isShow {
  605. let countSubviewsAfter = groupView.subviews.count
  606. if countSubviewsAfter > 5 {
  607. scrollView.flashScrollIndicators()
  608. }
  609. }
  610. })
  611. } else {
  612. if indicatorCounterFBBig.isDescendant(of: nexilis_button) {
  613. indicatorCounterFBBig.isHidden = false
  614. }
  615. var height = CGFloat((defaultWidthHeightMenuFB * countMenuFB) + defaultHeightFB + 5) //defaultWidthHeightMenuFB
  616. var width = defaultWidthFB
  617. if Utils.getConfigModeFB() == MODE_VERTICAL_ANIMATION {
  618. height = CGFloat((defaultWidthHeightMenuFB * 3) + defaultHeightFB - 5)
  619. scrollView.isHidden = true
  620. } else if Utils.getConfigModeFB() == MODE_HORIZONTAL_ANIMATION {
  621. height = defaultHeightFB + defaultWidthHeightMenuFB
  622. width = defaultWidthFB
  623. }
  624. var yPosition = frame.origin.y + height - defaultHeightFB
  625. if lastPosY != nil {
  626. yPosition = lastPosY!
  627. }
  628. if Utils.getConfigModeFB() != MODE_HORIZONTAL_SIDE_TAB {
  629. frame = CGRect(x: frame.origin.x, y: yPosition, width: width, height: defaultHeightFB)
  630. } else {
  631. UIView.animate(withDuration: 0.5, animations: { [self] in
  632. frame.origin.x = UIScreen.main.bounds.width - defaultWidthFB
  633. })
  634. }
  635. if Utils.getConfigModeFB() == MODE_VERTICAL_ANIMATION || Utils.getConfigModeFB() == MODE_HORIZONTAL_ANIMATION {
  636. checkDelayAnimation()
  637. }
  638. }
  639. }
  640. }
  641. public protocol SettingMABDelegate: AnyObject {
  642. func settingDelegate()
  643. }