FloatingButton.swift 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646
  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 panGesture: UIPanGestureRecognizer?
  31. public var isShow: Bool = false
  32. override init(frame: CGRect) {
  33. super.init(frame: frame)
  34. commonInit()
  35. }
  36. required init?(coder: NSCoder) {
  37. super.init(coder: coder)
  38. commonInit()
  39. }
  40. private func commonInit() {
  41. backgroundColor = .clear
  42. frame = CGRect(x: UIScreen.main.bounds.width - 50, y: (UIScreen.main.bounds.height / 2) - 50, width: 50.0, height: 50.0)
  43. panGesture = UIPanGestureRecognizer(target: self, action: #selector(draggedView(_:)))
  44. addGestureRecognizer(panGesture!)
  45. nexilis_button = UIImageView()
  46. nexilis_button.translatesAutoresizingMaskIntoConstraints = false
  47. nexilis_button.isUserInteractionEnabled = true
  48. nexilis_button.image = UIImage(named: "pb_button", in: Bundle.resourceBundle(for: Nexilis.self), with: nil)
  49. let qmeraTap = UITapGestureRecognizer(target: self, action: #selector(qmeraTap))
  50. qmeraTap.numberOfTouchesRequired = 1
  51. nexilis_button.addGestureRecognizer(qmeraTap)
  52. let qmeraLongPress = UILongPressGestureRecognizer(target: self, action: #selector(qmeraLongPress))
  53. nexilis_button.addGestureRecognizer(qmeraLongPress)
  54. addSubview(nexilis_button)
  55. nexilis_button.widthAnchor.constraint(equalToConstant: 50.0).isActive = true
  56. nexilis_button.heightAnchor.constraint(equalToConstant: 50.0).isActive = true
  57. nexilis_button.centerXAnchor.constraint(equalTo: centerXAnchor).isActive = true
  58. nexilis_button.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true
  59. scrollView = UIScrollView()
  60. scrollView.translatesAutoresizingMaskIntoConstraints = false
  61. scrollView.layer.borderWidth = 1.0
  62. scrollView.layer.borderColor = UIColor.white.cgColor
  63. scrollView.layer.cornerRadius = 8.0
  64. scrollView.layer.masksToBounds = true
  65. scrollView.backgroundColor = .black.withAlphaComponent(0.25)
  66. addSubview(scrollView)
  67. scrollView.widthAnchor.constraint(equalTo: widthAnchor).isActive = true
  68. scrollView.centerXAnchor.constraint(equalTo: centerXAnchor).isActive = true
  69. scrollView.topAnchor.constraint(equalTo: topAnchor).isActive = true
  70. scrollView.bottomAnchor.constraint(equalTo: nexilis_button.topAnchor).isActive = true
  71. groupView = UIStackView()
  72. groupView.translatesAutoresizingMaskIntoConstraints = false
  73. groupView.axis = .vertical
  74. groupView.distribution = .fillEqually
  75. scrollView.addSubview(groupView)
  76. groupView.widthAnchor.constraint(equalToConstant: 40).isActive = true
  77. groupView.topAnchor.constraint(equalTo: scrollView.topAnchor, constant: 5).isActive = true
  78. groupView.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor, constant: -5).isActive = true
  79. groupView.leftAnchor.constraint(equalTo: scrollView.leftAnchor, constant: 6).isActive = true
  80. pullButton()
  81. let center: NotificationCenter = NotificationCenter.default
  82. center.addObserver(self, selector: #selector(imageFBUpdate(notification:)), name: NSNotification.Name(rawValue: "imageFBUpdate"), object: nil)
  83. center.addObserver(self, selector: #selector(checkCounter), name: NSNotification.Name(rawValue: "onReceiveChat"), object: nil)
  84. center.addObserver(self, selector: #selector(checkCounter), name: NSNotification.Name(rawValue: "reloadTabChats"), object: nil)
  85. let tapGesture = UITapGestureRecognizer(target: self, action: #selector(hideButton))
  86. tapGesture.cancelsTouchesInView = false
  87. UIApplication.shared.windows.first?.rootViewController?.view.addGestureRecognizer(tapGesture)
  88. }
  89. private func pullButton() {
  90. if datePull == nil || Int(Date().timeIntervalSince(datePull!)) >= 60 {
  91. datePull = Date()
  92. } else if Int(Date().timeIntervalSince(datePull!)) < 60 {
  93. return
  94. }
  95. if !CheckConnection.isConnectedToNetwork() || API.nGetCLXConnState() == 0 {
  96. let imageView = UIImageView(image: UIImage(systemName: "xmark.circle.fill"))
  97. imageView.tintColor = .white
  98. 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)
  99. banner.show()
  100. return
  101. }
  102. DispatchQueue.global().async { [self] in
  103. if let response = Nexilis.writeSync(message: CoreMessage_TMessageBank.pullFloatingButton(), timeout: 30 * 1000){
  104. if response.isOk() {
  105. let data = response.getBody(key: CoreMessage_TMessageKey.DATA, default_value: "")
  106. if !data.isEmpty {
  107. if let jsonArray = try! JSONSerialization.jsonObject(with: data.data(using: String.Encoding.utf8)!, options: JSONSerialization.ReadingOptions()) as? [AnyObject] {
  108. DispatchQueue.main.async { [self] in
  109. groupView.subviews.forEach({ $0.removeFromSuperview() })
  110. if jsonArray.count == 0 {
  111. getDefaultButton()
  112. } else {
  113. for json in jsonArray {
  114. if (json["package_id"] as! String).contains("_fb1") {
  115. button_fb1 = UIButton()
  116. button_fb1.heightAnchor.constraint(equalToConstant: 40).isActive = true
  117. button_fb1.translatesAutoresizingMaskIntoConstraints = false
  118. DispatchQueue.global().async {
  119. let data = try? Data(contentsOf: URL(string: "https://qmera.io/filepalio/image/\(json["icon"]!!)")!) //make sure your image in this url does exist, otherwise unwrap in a if let check / try-catch
  120. DispatchQueue.main.async { [self] in
  121. if data != nil {
  122. button_fb1.setImage(UIImage(data: data!), for: .normal)
  123. }
  124. }
  125. }
  126. groupView.addArrangedSubview(button_fb1)
  127. button_fb1.addTarget(self, action: #selector(fb1Tap), for: .touchUpOutside)
  128. } else if (json["package_id"] as! String).contains("_fb2") {
  129. button_fb2 = UIButton()
  130. button_fb2.heightAnchor.constraint(equalToConstant: 40).isActive = true
  131. button_fb2.translatesAutoresizingMaskIntoConstraints = false
  132. DispatchQueue.global().async {
  133. let data = try? Data(contentsOf: URL(string: "https://qmera.io/filepalio/image/\(json["icon"]!!)")!) //make sure your image in this url does exist, otherwise unwrap in a if let check / try-catch
  134. DispatchQueue.main.async { [self] in
  135. if data != nil {
  136. button_fb2.setImage(UIImage(data: data!), for: .normal)
  137. }
  138. }
  139. }
  140. groupView.addArrangedSubview(button_fb2)
  141. button_fb2.addTarget(self, action: #selector(fb2Tap), for: .touchUpOutside)
  142. checkCounter()
  143. } else if (json["package_id"] as! String).contains("_fb3") {
  144. button_fb3 = UIButton()
  145. button_fb3.heightAnchor.constraint(equalToConstant: 40).isActive = true
  146. button_fb3.translatesAutoresizingMaskIntoConstraints = false
  147. DispatchQueue.global().async {
  148. let data = try? Data(contentsOf: URL(string: "https://qmera.io/filepalio/image/\(json["icon"]!!)")!) //make sure your image in this url does exist, otherwise unwrap in a if let check / try-catch
  149. DispatchQueue.main.async { [self] in
  150. if data != nil {
  151. button_fb3.setImage(UIImage(data: data!), for: .normal)
  152. }
  153. }
  154. }
  155. groupView.addArrangedSubview(button_fb3)
  156. button_fb3.addTarget(self, action: #selector(fb3Tap), for: .touchUpOutside)
  157. } else if (json["package_id"] as! String).contains("_fb4") {
  158. button_fb4 = UIButton()
  159. button_fb4.heightAnchor.constraint(equalToConstant: 40).isActive = true
  160. button_fb4.translatesAutoresizingMaskIntoConstraints = false
  161. iconCC = json["icon"] as! String
  162. DispatchQueue.global().async {
  163. let data = try? Data(contentsOf: URL(string: "https://qmera.io/filepalio/image/\(self.iconCC)")!) //make sure your image in this url does exist, otherwise unwrap in a if let check / try-catch
  164. DispatchQueue.main.async { [self] in
  165. if data != nil {
  166. button_fb4.setImage(UIImage(data: data!), for: .normal)
  167. }
  168. }
  169. }
  170. groupView.addArrangedSubview(button_fb4)
  171. button_fb4.addTarget(self, action: #selector(fb4Tap), for: .touchUpOutside)
  172. } else {
  173. let newButton = UIButton()
  174. newButton.heightAnchor.constraint(equalToConstant: 40).isActive = true
  175. newButton.translatesAutoresizingMaskIntoConstraints = false
  176. DispatchQueue.global().async {
  177. let data = try? Data(contentsOf: URL(string: "https://qmera.io/filepalio/image/\(json["icon"]!!)")!) //make sure your image in this url does exist, otherwise unwrap in a if let check / try-catch
  178. DispatchQueue.main.async {
  179. if data != nil {
  180. newButton.setImage(UIImage(data: data!), for: .normal)
  181. }
  182. }
  183. }
  184. groupView.addArrangedSubview(newButton)
  185. newButton.restorationIdentifier = json["app_id"] as? String
  186. newButton.addTarget(self, action: #selector(tapMoreApp(_:)), for: .touchUpOutside)
  187. }
  188. }
  189. }
  190. let countSubviewsAfter = groupView.subviews.count
  191. if countSubviewsAfter <= 4 {
  192. scrollView.isScrollEnabled = false
  193. }
  194. }
  195. }
  196. } else {
  197. groupView.subviews.forEach({ $0.removeFromSuperview() })
  198. getDefaultButton()
  199. }
  200. } else {
  201. groupView.subviews.forEach({ $0.removeFromSuperview() })
  202. getDefaultButton()
  203. }
  204. }
  205. }
  206. }
  207. func getDefaultButton() {
  208. button_fb1 = UIButton()
  209. button_fb1.heightAnchor.constraint(equalToConstant: 40).isActive = true
  210. button_fb1.translatesAutoresizingMaskIntoConstraints = false
  211. button_fb1.setImage(UIImage(named: "pb_button_cc", in: Bundle.resourceBundle(for: Nexilis.self), with: nil), for: .normal)
  212. groupView.addArrangedSubview(button_fb1)
  213. button_fb1.addTarget(self, action: #selector(fb1Tap), for: .touchUpOutside)
  214. button_fb2 = UIButton()
  215. button_fb2.heightAnchor.constraint(equalToConstant: 40).isActive = true
  216. button_fb2.translatesAutoresizingMaskIntoConstraints = false
  217. button_fb2.setImage(UIImage(named: "pb_button_chat", in: Bundle.resourceBundle(for: Nexilis.self), with: nil), for: .normal)
  218. groupView.addArrangedSubview(button_fb2)
  219. button_fb2.addTarget(self, action: #selector(fb2Tap), for: .touchUpOutside)
  220. checkCounter()
  221. button_fb3 = UIButton()
  222. button_fb3.heightAnchor.constraint(equalToConstant: 40).isActive = true
  223. button_fb3.translatesAutoresizingMaskIntoConstraints = false
  224. button_fb3.setImage(UIImage(named: "pb_button_call", in: Bundle.resourceBundle(for: Nexilis.self), with: nil), for: .normal)
  225. groupView.addArrangedSubview(button_fb3)
  226. button_fb3.addTarget(self, action: #selector(fb3Tap), for: .touchUpOutside)
  227. button_fb4 = UIButton()
  228. button_fb4.heightAnchor.constraint(equalToConstant: 40).isActive = true
  229. button_fb4.translatesAutoresizingMaskIntoConstraints = false
  230. button_fb4.setImage(UIImage(named: "pb_button_stream", in: Bundle.resourceBundle(for: Nexilis.self), with: nil), for: .normal)
  231. groupView.addArrangedSubview(button_fb4)
  232. button_fb4.addTarget(self, action: #selector(fb4Tap), for: .touchUpOutside)
  233. }
  234. @objc func draggedView(_ sender:UIPanGestureRecognizer){
  235. let size = UIScreen.main.bounds
  236. let widthScreen = size.width
  237. let heightScreen = size.height
  238. let minimumx = (widthScreen + 30) - widthScreen
  239. let maximumx = widthScreen - 30
  240. let translation = sender.translation(in: self)
  241. var xPos = center.x + translation.x
  242. var yPos = center.y + translation.y
  243. bringSubviewToFront(self)
  244. if (xPos < minimumx) {
  245. xPos = minimumx
  246. }
  247. if (xPos > maximumx) {
  248. xPos = maximumx
  249. }
  250. if(isShow) {
  251. let minimumy = CGFloat(120.5) //30
  252. let maximumy = heightScreen - 100
  253. if(yPos < minimumy) {
  254. yPos = minimumy
  255. }
  256. if(yPos > maximumy) {
  257. yPos = maximumy
  258. }
  259. } else {
  260. let minimumy = (heightScreen + 50) - heightScreen
  261. let maximumy = heightScreen - 20
  262. if(yPos < minimumy) {
  263. yPos = minimumy
  264. }
  265. if(yPos > maximumy) {
  266. yPos = maximumy
  267. }
  268. }
  269. center = CGPoint(x: xPos, y: yPos)
  270. sender.setTranslation(CGPoint.zero, in: self)
  271. if lastPosY != nil {
  272. lastPosY = nil
  273. }
  274. UserDefaults.standard.set(center.x, forKey: "xlastPosFB")
  275. UserDefaults.standard.set(center.y, forKey: "ylastPosFB")
  276. }
  277. @objc func imageFBUpdate(notification: NSNotification) {
  278. }
  279. @objc func checkCounter() {
  280. let counter = queryCountCounter()
  281. if counter > 0 {
  282. DispatchQueue.main.async { [self] in
  283. if !indicatorCounterFB.isDescendant(of: button_fb2) {
  284. button_fb2.addSubview(indicatorCounterFB)
  285. indicatorCounterFB.layer.cornerRadius = 7.5
  286. indicatorCounterFB.layer.masksToBounds = true
  287. indicatorCounterFB.backgroundColor = .systemRed
  288. indicatorCounterFB.anchor(top: button_fb2.topAnchor, left: button_fb2.leftAnchor, height: 15, minWidth: 15, maxWidth: 20)
  289. indicatorCounterFB.addSubview(labelCounterFB)
  290. labelCounterFB.anchor(left: indicatorCounterFB.leftAnchor, right: indicatorCounterFB.rightAnchor, paddingLeft: 5, paddingRight: 5, centerX: indicatorCounterFB.centerXAnchor, centerY: indicatorCounterFB.centerYAnchor)
  291. labelCounterFB.font = .systemFont(ofSize: 10)
  292. labelCounterFB.textColor = .white
  293. }
  294. if !indicatorCounterFBBig.isDescendant(of: nexilis_button){
  295. nexilis_button.addSubview(indicatorCounterFBBig)
  296. indicatorCounterFBBig.tintColor = .systemRed
  297. indicatorCounterFBBig.image = UIImage(systemName: "staroflife.circle.fill")
  298. indicatorCounterFBBig.anchor(top: nexilis_button.topAnchor, left: nexilis_button.leftAnchor, paddingTop: 5, paddingLeft: 5, width: 15, height: 15)
  299. }
  300. labelCounterFB.text = "\(counter)"
  301. }
  302. } else {
  303. DispatchQueue.main.async { [self] in
  304. if indicatorCounterFB.isDescendant(of: button_fb2) {
  305. indicatorCounterFB.removeFromSuperview()
  306. }
  307. if indicatorCounterFBBig.isDescendant(of: nexilis_button) {
  308. indicatorCounterFBBig.removeFromSuperview()
  309. }
  310. }
  311. }
  312. }
  313. private func queryCountCounter() -> Int32 {
  314. var counter: Int32?
  315. Database.shared.database?.inTransaction({ (fmdb, rollback) in
  316. if let cursor = Database.shared.getRecords(fmdb: fmdb, query: "SELECT SUM(counter) FROM MESSAGE_SUMMARY"), cursor.next() {
  317. counter = cursor.int(forColumnIndex: 0)
  318. cursor.close()
  319. }
  320. })
  321. return counter ?? 0
  322. }
  323. @objc func qmeraTap() {
  324. show(isShow: !isShow)
  325. }
  326. @objc func tapMoreApp(_ sender: UIButton) {
  327. let id = sender.restorationIdentifier
  328. let isChangeProfile = UserDefaults.standard.bool(forKey: "is_change_profile")
  329. if !isChangeProfile {
  330. let alert = UIAlertController(title: "Change Profile".localized(), message: "You must change your name to use this feature".localized(), preferredStyle: .alert)
  331. alert.addAction(UIAlertAction(title: "Ok".localized(), style: UIAlertAction.Style.default, handler: {(_) in
  332. let controller = AppStoryBoard.Palio.instance.instantiateViewController(withIdentifier: "changeNS") as! ChangeNamePassswordViewController
  333. let navigationController = UINavigationController(rootViewController: controller)
  334. navigationController.modalPresentationStyle = .custom
  335. navigationController.navigationBar.tintColor = .white
  336. navigationController.navigationBar.barTintColor = .mainColor
  337. navigationController.navigationBar.isTranslucent = false
  338. let textAttributes = [NSAttributedString.Key.foregroundColor:UIColor.white]
  339. navigationController.navigationBar.titleTextAttributes = textAttributes
  340. navigationController.view.backgroundColor = .mainColor
  341. if UIApplication.shared.visibleViewController?.navigationController != nil {
  342. UIApplication.shared.visibleViewController?.navigationController?.present(navigationController, animated: true, completion: nil)
  343. } else {
  344. UIApplication.shared.visibleViewController?.present(navigationController, animated: true, completion: nil)
  345. }
  346. }))
  347. if UIApplication.shared.visibleViewController?.navigationController != nil {
  348. UIApplication.shared.visibleViewController?.navigationController?.present(alert, animated: true, completion: nil)
  349. } else {
  350. UIApplication.shared.visibleViewController?.present(alert, animated: true, completion: nil)
  351. }
  352. hideButton()
  353. return
  354. }
  355. if id == nil {
  356. return
  357. }
  358. if let url = URL(string: "itms-apps://apple.com/app/\(id!)") {
  359. UIApplication.shared.open(url)
  360. }
  361. hideButton()
  362. }
  363. @objc func fb1Tap() {
  364. let isChangeProfile = UserDefaults.standard.bool(forKey: "is_change_profile")
  365. if !isChangeProfile {
  366. let alert = UIAlertController(title: "Change Profile".localized(), message: "You must change your name to use this feature".localized(), preferredStyle: .alert)
  367. alert.addAction(UIAlertAction(title: "Ok".localized(), style: UIAlertAction.Style.default, handler: {(_) in
  368. let controller = AppStoryBoard.Palio.instance.instantiateViewController(withIdentifier: "changeNS") as! ChangeNamePassswordViewController
  369. let navigationController = UINavigationController(rootViewController: controller)
  370. navigationController.modalPresentationStyle = .custom
  371. navigationController.navigationBar.tintColor = .white
  372. navigationController.navigationBar.barTintColor = .mainColor
  373. navigationController.navigationBar.isTranslucent = false
  374. let textAttributes = [NSAttributedString.Key.foregroundColor:UIColor.white]
  375. navigationController.navigationBar.titleTextAttributes = textAttributes
  376. navigationController.view.backgroundColor = .mainColor
  377. if UIApplication.shared.visibleViewController?.navigationController != nil {
  378. UIApplication.shared.visibleViewController?.navigationController?.present(navigationController, animated: true, completion: nil)
  379. } else {
  380. UIApplication.shared.visibleViewController?.present(navigationController, animated: true, completion: nil)
  381. }
  382. }))
  383. if UIApplication.shared.visibleViewController?.navigationController != nil {
  384. UIApplication.shared.visibleViewController?.navigationController?.present(alert, animated: true, completion: nil)
  385. } else {
  386. UIApplication.shared.visibleViewController?.present(alert, animated: true, completion: nil)
  387. }
  388. hideButton()
  389. return
  390. }
  391. let isWaitingRequestCC = UserDefaults.standard.bool(forKey: "waitingRequestCC")
  392. if isWaitingRequestCC {
  393. let imageView = UIImageView(image: UIImage(systemName: "info.circle"))
  394. imageView.tintColor = .white
  395. let banner = FloatingNotificationBanner(title: "You have requested Call Center, please wait for response.".localized(), subtitle: nil, titleFont: UIFont.systemFont(ofSize: 16), titleColor: nil, titleTextAlign: .left, subtitleFont: nil, subtitleColor: nil, subtitleTextAlign: nil, leftView: imageView, rightView: nil, style: .info, colors: nil, iconPosition: .center)
  396. banner.show()
  397. return
  398. }
  399. let controller = AppStoryBoard.Palio.instance.instantiateViewController(identifier: "editorPersonalVC") as! EditorPersonal
  400. controller.isContactCenter = true
  401. let navigationController = UINavigationController(rootViewController: controller)
  402. navigationController.modalPresentationStyle = .custom
  403. navigationController.navigationBar.tintColor = .white
  404. navigationController.navigationBar.barTintColor = .mainColor
  405. navigationController.navigationBar.isTranslucent = false
  406. let textAttributes = [NSAttributedString.Key.foregroundColor:UIColor.white]
  407. navigationController.navigationBar.titleTextAttributes = textAttributes
  408. navigationController.view.backgroundColor = .mainColor
  409. if UIApplication.shared.visibleViewController?.navigationController != nil {
  410. UIApplication.shared.visibleViewController?.navigationController?.present(navigationController, animated: true, completion: nil)
  411. } else {
  412. UIApplication.shared.visibleViewController?.present(navigationController, animated: true, completion: nil)
  413. }
  414. hideButton()
  415. }
  416. @objc func fb2Tap() {
  417. let isChangeProfile = UserDefaults.standard.bool(forKey: "is_change_profile")
  418. if !isChangeProfile {
  419. let alert = UIAlertController(title: "Change Profile".localized(), message: "You must change your name to use this feature".localized(), preferredStyle: .alert)
  420. alert.addAction(UIAlertAction(title: "Ok".localized(), style: UIAlertAction.Style.default, handler: {(_) in
  421. let controller = AppStoryBoard.Palio.instance.instantiateViewController(withIdentifier: "changeNS") as! ChangeNamePassswordViewController
  422. let navigationController = UINavigationController(rootViewController: controller)
  423. navigationController.modalPresentationStyle = .custom
  424. navigationController.navigationBar.tintColor = .white
  425. navigationController.navigationBar.barTintColor = .mainColor
  426. navigationController.navigationBar.isTranslucent = false
  427. let textAttributes = [NSAttributedString.Key.foregroundColor:UIColor.white]
  428. navigationController.navigationBar.titleTextAttributes = textAttributes
  429. navigationController.view.backgroundColor = .mainColor
  430. if UIApplication.shared.visibleViewController?.navigationController != nil {
  431. UIApplication.shared.visibleViewController?.navigationController?.present(navigationController, animated: true, completion: nil)
  432. } else {
  433. UIApplication.shared.visibleViewController?.present(navigationController, animated: true, completion: nil)
  434. }
  435. }))
  436. if UIApplication.shared.visibleViewController?.navigationController != nil {
  437. UIApplication.shared.visibleViewController?.navigationController?.present(alert, animated: true, completion: nil)
  438. } else {
  439. UIApplication.shared.visibleViewController?.present(alert, animated: true, completion: nil)
  440. }
  441. hideButton()
  442. return
  443. }
  444. let navigationController = AppStoryBoard.Palio.instance.instantiateViewController(withIdentifier: "contactChatNav") as! UINavigationController
  445. navigationController.modalPresentationStyle = .fullScreen
  446. navigationController.navigationBar.tintColor = .white
  447. navigationController.navigationBar.barTintColor = .mainColor
  448. navigationController.navigationBar.isTranslucent = false
  449. let textAttributes = [NSAttributedString.Key.foregroundColor:UIColor.white]
  450. navigationController.navigationBar.titleTextAttributes = textAttributes
  451. navigationController.view.backgroundColor = .mainColor
  452. if UIApplication.shared.visibleViewController?.navigationController != nil {
  453. UIApplication.shared.visibleViewController?.navigationController?.present(navigationController, animated: true, completion: nil)
  454. } else {
  455. UIApplication.shared.visibleViewController?.present(navigationController, animated: true, completion: nil)
  456. }
  457. hideButton()
  458. }
  459. @objc func fb3Tap() {
  460. let isChangeProfile = UserDefaults.standard.bool(forKey: "is_change_profile")
  461. if !isChangeProfile {
  462. let alert = UIAlertController(title: "Change Profile".localized(), message: "You must change your name to use this feature".localized(), preferredStyle: .alert)
  463. alert.addAction(UIAlertAction(title: "Ok".localized(), style: UIAlertAction.Style.default, handler: {(_) in
  464. let controller = AppStoryBoard.Palio.instance.instantiateViewController(withIdentifier: "changeNS") as! ChangeNamePassswordViewController
  465. let navigationController = UINavigationController(rootViewController: controller)
  466. navigationController.modalPresentationStyle = .custom
  467. navigationController.navigationBar.tintColor = .white
  468. navigationController.navigationBar.barTintColor = .mainColor
  469. navigationController.navigationBar.isTranslucent = false
  470. let textAttributes = [NSAttributedString.Key.foregroundColor:UIColor.white]
  471. navigationController.navigationBar.titleTextAttributes = textAttributes
  472. navigationController.view.backgroundColor = .mainColor
  473. if UIApplication.shared.visibleViewController?.navigationController != nil {
  474. UIApplication.shared.visibleViewController?.navigationController?.present(navigationController, animated: true, completion: nil)
  475. } else {
  476. UIApplication.shared.visibleViewController?.present(navigationController, animated: true, completion: nil)
  477. }
  478. }))
  479. if UIApplication.shared.visibleViewController?.navigationController != nil {
  480. UIApplication.shared.visibleViewController?.navigationController?.present(alert, animated: true, completion: nil)
  481. } else {
  482. UIApplication.shared.visibleViewController?.present(alert, animated: true, completion: nil)
  483. }
  484. hideButton()
  485. return
  486. }
  487. let callContact = AppStoryBoard.Palio.instance.instantiateViewController(withIdentifier: "contactSID")
  488. let navigationController = UINavigationController(rootViewController: callContact)
  489. navigationController.modalPresentationStyle = .fullScreen
  490. navigationController.navigationBar.tintColor = .white
  491. navigationController.navigationBar.barTintColor = .mainColor
  492. navigationController.navigationBar.isTranslucent = false
  493. let textAttributes = [NSAttributedString.Key.foregroundColor:UIColor.white]
  494. navigationController.navigationBar.titleTextAttributes = textAttributes
  495. navigationController.view.backgroundColor = .mainColor
  496. if UIApplication.shared.visibleViewController?.navigationController != nil {
  497. UIApplication.shared.visibleViewController?.navigationController?.present(navigationController, animated: true, completion: nil)
  498. } else {
  499. UIApplication.shared.visibleViewController?.present(navigationController, animated: true, completion: nil)
  500. }
  501. hideButton()
  502. }
  503. @objc func fb4Tap() {
  504. let isChangeProfile = UserDefaults.standard.bool(forKey: "is_change_profile")
  505. if !isChangeProfile {
  506. let alert = UIAlertController(title: "Change Profile".localized(), message: "You must change your name to use this feature".localized(), preferredStyle: .alert)
  507. alert.addAction(UIAlertAction(title: "Ok".localized(), style: UIAlertAction.Style.default, handler: {(_) in
  508. let controller = AppStoryBoard.Palio.instance.instantiateViewController(withIdentifier: "changeNS") as! ChangeNamePassswordViewController
  509. let navigationController = UINavigationController(rootViewController: controller)
  510. navigationController.modalPresentationStyle = .custom
  511. navigationController.navigationBar.tintColor = .white
  512. navigationController.navigationBar.barTintColor = .mainColor
  513. navigationController.navigationBar.isTranslucent = false
  514. let textAttributes = [NSAttributedString.Key.foregroundColor:UIColor.white]
  515. navigationController.navigationBar.titleTextAttributes = textAttributes
  516. navigationController.view.backgroundColor = .mainColor
  517. if UIApplication.shared.visibleViewController?.navigationController != nil {
  518. UIApplication.shared.visibleViewController?.navigationController?.present(navigationController, animated: true, completion: nil)
  519. } else {
  520. UIApplication.shared.visibleViewController?.present(navigationController, animated: true, completion: nil)
  521. }
  522. }))
  523. if UIApplication.shared.visibleViewController?.navigationController != nil {
  524. UIApplication.shared.visibleViewController?.navigationController?.present(alert, animated: true, completion: nil)
  525. } else {
  526. UIApplication.shared.visibleViewController?.present(alert, animated: true, completion: nil)
  527. }
  528. hideButton()
  529. return
  530. }
  531. let navigationController = UINavigationController(rootViewController: QmeraCreateStreamingViewController())
  532. navigationController.modalPresentationStyle = .custom
  533. navigationController.navigationBar.tintColor = .white
  534. navigationController.navigationBar.barTintColor = .mainColor
  535. navigationController.navigationBar.isTranslucent = false
  536. let textAttributes = [NSAttributedString.Key.foregroundColor:UIColor.white]
  537. navigationController.navigationBar.titleTextAttributes = textAttributes
  538. navigationController.view.backgroundColor = .mainColor
  539. if UIApplication.shared.visibleViewController?.navigationController != nil {
  540. UIApplication.shared.visibleViewController?.navigationController?.present(navigationController, animated: true, completion: nil)
  541. } else {
  542. UIApplication.shared.visibleViewController?.present(navigationController, animated: true, completion: nil)
  543. }
  544. hideButton()
  545. }
  546. @objc func qmeraLongPress() {
  547. let navigationController = AppStoryBoard.Palio.instance.instantiateViewController(withIdentifier: "settingNav") as! UINavigationController
  548. navigationController.modalPresentationStyle = .custom
  549. navigationController.navigationBar.tintColor = .white
  550. navigationController.navigationBar.barTintColor = .mainColor
  551. navigationController.navigationBar.isTranslucent = false
  552. let textAttributes = [NSAttributedString.Key.foregroundColor:UIColor.white]
  553. navigationController.navigationBar.titleTextAttributes = textAttributes
  554. navigationController.view.backgroundColor = .mainColor
  555. UIApplication.shared.rootViewController?.present(navigationController, animated: true, completion: nil)
  556. hideButton()
  557. }
  558. @objc func hideButton() {
  559. if isShow {
  560. show(isShow: false)
  561. }
  562. if self.frame.origin.x < UIScreen.main.bounds.width / 2 - 30 {
  563. self.frame.origin.x = 0
  564. } else {
  565. self.frame.origin.x = UIScreen.main.bounds.width - 50
  566. }
  567. }
  568. public func show(isShow: Bool) {
  569. self.isShow = isShow
  570. if isShow {
  571. pullButton()
  572. if indicatorCounterFBBig.isDescendant(of: nexilis_button) {
  573. indicatorCounterFBBig.isHidden = true
  574. }
  575. let height = CGFloat(217) //40
  576. var yPosition = frame.origin.y - height + 50
  577. if yPosition <= 25 {
  578. lastPosY = frame.origin.y
  579. yPosition = 25
  580. }
  581. frame = CGRect(x: frame.origin.x, y: yPosition, width: frame.width, height: height)
  582. DispatchQueue.main.asyncAfter(deadline: .now() + 0.5, execute: { [self] in
  583. if isShow {
  584. let countSubviewsAfter = groupView.subviews.count
  585. if countSubviewsAfter > 5 {
  586. scrollView.flashScrollIndicators()
  587. }
  588. }
  589. })
  590. } else {
  591. if indicatorCounterFBBig.isDescendant(of: nexilis_button) {
  592. indicatorCounterFBBig.isHidden = false
  593. }
  594. let height = CGFloat(217) //40
  595. var yPosition = frame.origin.y + height - 50
  596. if lastPosY != nil {
  597. yPosition = lastPosY!
  598. }
  599. frame = CGRect(x: frame.origin.x, y: yPosition, width: frame.width, height: frame.width)
  600. }
  601. }
  602. }