FloatingButton.swift 41 KB

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