FloatingButton.swift 40 KB

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