FloatingButton.swift 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806
  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, UIGestureRecognizerDelegate {
  11. var groupView: UIStackView!
  12. var scrollView: UIScrollView!
  13. var imageScrollView: UIImageView!
  14. var button_fb1: UIButton!
  15. var button_fb2: UIButton!
  16. var button_fb3: UIButton!
  17. var button_fb4: UIButton!
  18. var nexilis_button: UIImageView!
  19. var nexilis_pin: UIImageView!
  20. var leadingConstraintPin: NSLayoutConstraint!
  21. var bottomConstraintPin: NSLayoutConstraint!
  22. var trailingConstraintPin: NSLayoutConstraint!
  23. var topConstraintPin: NSLayoutConstraint!
  24. var lastPosY: CGFloat?
  25. var lastImageButton = ""
  26. var iconCC = ""
  27. let indicatorCounterFB = UIView()
  28. let labelCounterFB = UILabel()
  29. let indicatorCounterFBBig = UIImageView()
  30. var datePull: Date?
  31. var animationTimer = Timer()
  32. var configAnim: Int = Int(Utils.getFloatingAnim().components(separatedBy: "~")[0]) ?? 1
  33. var isLoopingAnim = (Int(Utils.getFloatingAnim().components(separatedBy: "~")[1]) ?? 1) == 1 ? true : false
  34. var lastRunAnimationHrz = -1
  35. var lastRunAnimationVrt = -1
  36. var panGesture: UIPanGestureRecognizer?
  37. var defaultWidthFB = (UIScreen.main.bounds.height * 0.5) / 7.5
  38. var defaultHeightFB = (UIScreen.main.bounds.height * 0.5) / 7.5
  39. let defaultWidthHeightMenuFB = (UIScreen.main.bounds.height * 0.45) / 7.5
  40. let widthFBAnim = (UIScreen.main.bounds.height * 0.8) / 7.5
  41. let heightFBAnim = (UIScreen.main.bounds.height * 1) / 7.5
  42. let heightFBSideTab = (UIScreen.main.bounds.height * 1.05) / 7.5
  43. let widthFBSideTab: CGFloat = 18
  44. let widthVerticalSideTab: CGFloat = 50
  45. let heightVerticalSideTab: CGFloat = 220
  46. final let MODE_VERTICAL_FLOATING_BUTTON = "1"
  47. final let MODE_VERTICAL_ANIMATION = "2"
  48. final let MODE_HORIZONTAL_ANIMATION = "3"
  49. final let MODE_HORIZONTAL_SIDE_TAB = "4"
  50. final let MODE_VERTICAL_SIDE_TAB = "5"
  51. var configModeFB = "1"
  52. var countMenuFB: CGFloat = 6 {
  53. didSet {
  54. if isShow {
  55. show(isShow: isShow)
  56. }
  57. }
  58. }
  59. public weak var mySettingDelegate: SettingMABDelegate?
  60. public var isShow: Bool = false
  61. override init(frame: CGRect) {
  62. super.init(frame: frame)
  63. commonInit()
  64. }
  65. required init?(coder: NSCoder) {
  66. super.init(coder: coder)
  67. commonInit()
  68. }
  69. private func commonInit() {
  70. panGesture = UIPanGestureRecognizer(target: self, action: #selector(draggedView(_:)))
  71. addGestureRecognizer(panGesture!)
  72. configModeFB = Utils.getConfigModeFB()
  73. nexilis_button = UIImageView()
  74. nexilis_button.translatesAutoresizingMaskIntoConstraints = false
  75. nexilis_button.isUserInteractionEnabled = true
  76. if configModeFB == MODE_VERTICAL_ANIMATION || configModeFB == MODE_HORIZONTAL_ANIMATION {
  77. defaultWidthFB = widthFBAnim
  78. if configModeFB == MODE_HORIZONTAL_ANIMATION {
  79. defaultHeightFB = heightFBAnim + 30
  80. } else {
  81. defaultHeightFB = heightFBAnim
  82. }
  83. var urlGif = URL(string: configModeFB == MODE_VERTICAL_ANIMATION ? Utils.getIconCenterAnim2() : Utils.getIconCenterAnim4())
  84. if (urlGif == nil) {
  85. urlGif = Bundle.resourceBundle(for: Nexilis.self).url(forResource: configModeFB == MODE_VERTICAL_ANIMATION ? "pb_def_icon_mode2" : "pb_def_icon_mode4", withExtension: "gif")! //resourcesMediaBundle
  86. if urlGif == nil {
  87. urlGif = Bundle.resourcesMediaBundle(for: Nexilis.self).url(forResource: configModeFB == MODE_VERTICAL_ANIMATION ? "pb_def_icon_mode2" : "pb_def_icon_mode4", withExtension: "gif")!
  88. }
  89. }
  90. nexilis_button.sd_setImage(with: urlGif) { [self] (image, error, cacheType, imageURL) in
  91. if error == nil {
  92. nexilis_button.animationImages = image?.images
  93. nexilis_button.animationDuration = image?.duration ?? 0.0
  94. nexilis_button.animationRepeatCount = 0
  95. nexilis_button.startAnimating()
  96. }
  97. }
  98. } else {
  99. if !Utils.getIconDock().isEmpty && configModeFB == MODE_VERTICAL_FLOATING_BUTTON && Nexilis.fromMAB {
  100. setImageWithURL(true)
  101. } else {
  102. if configModeFB == MODE_HORIZONTAL_SIDE_TAB || configModeFB == MODE_VERTICAL_SIDE_TAB {
  103. defaultWidthFB = widthFBSideTab
  104. defaultHeightFB = heightFBSideTab
  105. }
  106. if !Utils.getIconCenter().isEmpty && configModeFB == MODE_VERTICAL_FLOATING_BUTTON {
  107. setImageWithURL(false)
  108. } else {
  109. nexilis_button.image = UIImage(named: configModeFB == MODE_VERTICAL_SIDE_TAB ? "pb_side_tab_vtc" : configModeFB == MODE_HORIZONTAL_SIDE_TAB ? "pb_side_tab" : "pb_button", in: Bundle.resourceBundle(for: Nexilis.self), with: nil)
  110. }
  111. }
  112. }
  113. backgroundColor = .clear
  114. frame = CGRect(x: UIScreen.main.bounds.width - defaultWidthFB, y: (UIScreen.main.bounds.height / 2) - defaultHeightFB, width: configModeFB == MODE_VERTICAL_SIDE_TAB ? 50 + defaultWidthFB : configModeFB == MODE_HORIZONTAL_SIDE_TAB ? UIScreen.main.bounds.width - defaultWidthFB : defaultWidthFB, height: configModeFB == MODE_VERTICAL_SIDE_TAB ? heightVerticalSideTab : defaultHeightFB)
  115. if configModeFB == MODE_VERTICAL_ANIMATION || configModeFB == MODE_HORIZONTAL_ANIMATION {
  116. if configAnim == 0 { //left to right
  117. lastRunAnimationHrz = 1
  118. } else if configAnim == 1 { //right to left
  119. lastRunAnimationHrz = -1
  120. } else if configAnim == 2 { //top to bottom
  121. lastRunAnimationVrt = 1
  122. } else if configAnim == 3 { //top to bottom
  123. lastRunAnimationVrt = -1
  124. }
  125. if configAnim >= 0 && configAnim <= 3 {
  126. checkDelayAnimation()
  127. }
  128. }
  129. let qmeraTap = UITapGestureRecognizer(target: self, action: #selector(qmeraTap))
  130. qmeraTap.numberOfTouchesRequired = 1
  131. nexilis_button.addGestureRecognizer(qmeraTap)
  132. qmeraTap.delegate = self
  133. let qmeraLongPress = UILongPressGestureRecognizer(target: self, action: #selector(qmeraLongPress(gestureRecognizer:)))
  134. self.addGestureRecognizer(qmeraLongPress)
  135. addSubview(nexilis_button)
  136. nexilis_button.widthAnchor.constraint(equalToConstant: defaultWidthFB).isActive = true
  137. nexilis_button.heightAnchor.constraint(equalToConstant: defaultHeightFB).isActive = true
  138. nexilis_button.leftAnchor.constraint(equalTo: leftAnchor).isActive = true
  139. if configModeFB == MODE_VERTICAL_SIDE_TAB {
  140. nexilis_button.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true
  141. } else {
  142. nexilis_button.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true
  143. }
  144. scrollView = UIScrollView()
  145. scrollView.translatesAutoresizingMaskIntoConstraints = false
  146. if Utils.getFBItemBg() == "1" && configModeFB == MODE_VERTICAL_FLOATING_BUTTON {
  147. scrollView.layer.borderWidth = 1.0
  148. scrollView.layer.borderColor = UIColor.white.cgColor
  149. scrollView.layer.cornerRadius = 8.0
  150. scrollView.layer.masksToBounds = true
  151. scrollView.backgroundColor = .black.withAlphaComponent(0.25)
  152. }
  153. addSubview(scrollView)
  154. if configModeFB == MODE_VERTICAL_SIDE_TAB {
  155. scrollView.backgroundColor = UIColor.white.withAlphaComponent(0.8)
  156. scrollView.layer.borderColor = UIColor.gray.cgColor
  157. scrollView.layer.borderWidth = 0.2
  158. scrollView.layer.cornerRadius = 5.0
  159. scrollView.layer.masksToBounds = true
  160. scrollView.leftAnchor.constraint(equalTo: nexilis_button.rightAnchor).isActive = true
  161. scrollView.centerYAnchor.constraint(equalTo: nexilis_button.centerYAnchor).isActive = true
  162. scrollView.widthAnchor.constraint(equalToConstant: widthVerticalSideTab).isActive = true
  163. scrollView.heightAnchor.constraint(equalToConstant: heightVerticalSideTab).isActive = true
  164. } else if configModeFB != MODE_HORIZONTAL_SIDE_TAB {
  165. scrollView.topAnchor.constraint(equalTo: topAnchor).isActive = true
  166. if configModeFB == MODE_VERTICAL_ANIMATION {
  167. scrollView.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true
  168. scrollView.leftAnchor.constraint(equalTo: nexilis_button.rightAnchor, constant: -20).isActive = true
  169. scrollView.widthAnchor.constraint(equalToConstant: defaultWidthHeightMenuFB + 10).isActive = true
  170. scrollView.isHidden = true
  171. } else if configModeFB == MODE_HORIZONTAL_ANIMATION {
  172. scrollView.bottomAnchor.constraint(equalTo: nexilis_button.topAnchor).isActive = true
  173. scrollView.leftAnchor.constraint(equalTo: nexilis_button.leftAnchor).isActive = true
  174. scrollView.widthAnchor.constraint(equalToConstant: defaultWidthHeightMenuFB * (countMenuFB - 1)).isActive = true
  175. } else {
  176. scrollView.widthAnchor.constraint(equalToConstant: defaultWidthHeightMenuFB + 10).isActive = true
  177. scrollView.centerXAnchor.constraint(equalTo: centerXAnchor).isActive = true
  178. scrollView.bottomAnchor.constraint(equalTo: nexilis_button.topAnchor).isActive = true
  179. }
  180. } else {
  181. scrollView.widthAnchor.constraint(equalToConstant: UIScreen.main.bounds.width - defaultWidthFB).isActive = true
  182. scrollView.heightAnchor.constraint(equalToConstant: defaultHeightFB).isActive = true
  183. scrollView.leftAnchor.constraint(equalTo: nexilis_button.rightAnchor).isActive = true
  184. scrollView.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true
  185. scrollView.backgroundColor = .black.withAlphaComponent(0.25)
  186. }
  187. groupView = UIStackView()
  188. groupView.translatesAutoresizingMaskIntoConstraints = false
  189. groupView.axis = (configModeFB == MODE_HORIZONTAL_SIDE_TAB || configModeFB == MODE_HORIZONTAL_ANIMATION) ? .horizontal : .vertical
  190. if configModeFB != MODE_HORIZONTAL_SIDE_TAB && configModeFB != MODE_HORIZONTAL_ANIMATION {
  191. groupView.distribution = .fillEqually
  192. }
  193. scrollView.addSubview(groupView)
  194. if configModeFB == MODE_VERTICAL_SIDE_TAB {
  195. groupView.spacing = 20.0
  196. groupView.topAnchor.constraint(equalTo: scrollView.topAnchor, constant: 10).isActive = true
  197. groupView.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor, constant: -10).isActive = true
  198. groupView.centerXAnchor.constraint(equalTo: scrollView.centerXAnchor).isActive = true
  199. } else if configModeFB == MODE_HORIZONTAL_SIDE_TAB {
  200. groupView.leftAnchor.constraint(equalTo: scrollView.leftAnchor).isActive = true
  201. groupView.rightAnchor.constraint(equalTo: scrollView.rightAnchor).isActive = true
  202. groupView.centerYAnchor.constraint(equalTo: scrollView.centerYAnchor).isActive = true
  203. groupView.heightAnchor.constraint(equalToConstant: defaultHeightFB - 10).isActive = true
  204. } else {
  205. groupView.widthAnchor.constraint(equalToConstant: configModeFB == MODE_HORIZONTAL_ANIMATION ? defaultWidthHeightMenuFB * (countMenuFB - 1) : defaultWidthHeightMenuFB).isActive = true
  206. groupView.topAnchor.constraint(equalTo: scrollView.topAnchor, constant: configModeFB == MODE_HORIZONTAL_ANIMATION ? 0 : 5).isActive = true
  207. groupView.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor, constant: configModeFB == MODE_HORIZONTAL_ANIMATION ? 0 : -5).isActive = true
  208. groupView.leftAnchor.constraint(equalTo: scrollView.leftAnchor, constant: configModeFB == MODE_HORIZONTAL_ANIMATION ? 0 : 6).isActive = true
  209. }
  210. pullButton()
  211. let center: NotificationCenter = NotificationCenter.default
  212. center.addObserver(self, selector: #selector(imageFBUpdate(notification:)), name: NSNotification.Name(rawValue: "imageFBUpdate"), object: nil)
  213. center.addObserver(self, selector: #selector(checkCounter), name: NSNotification.Name(rawValue: Nexilis.listenerReceiveChat), object: nil)
  214. center.addObserver(self, selector: #selector(checkCounter), name: NSNotification.Name(rawValue: "reloadTabChats"), object: nil)
  215. let tapGesture = UITapGestureRecognizer(target: self, action: #selector(hideButton))
  216. tapGesture.cancelsTouchesInView = false
  217. UIApplication.shared.windows.first?.rootViewController?.view.addGestureRecognizer(tapGesture)
  218. }
  219. @objc func handleTap(_ sender: UITapGestureRecognizer) {
  220. let location = sender.location(in: self)
  221. print("Tap location: \(location)")
  222. }
  223. public func setImageWithURL(_ isDocked: Bool) {
  224. if configModeFB != MODE_VERTICAL_FLOATING_BUTTON {
  225. return
  226. }
  227. var urlFb = Utils.getIconCenter()
  228. if isDocked {
  229. urlFb = Utils.getUrlDock() ?? ""
  230. }
  231. if urlFb.isEmpty {
  232. return
  233. }
  234. let task = URLSession.shared.dataTask(with: URL(string: (urlFb))!) { dataImage, response, error in
  235. if let error = error {
  236. print("Failed to load data: \(error)")
  237. return
  238. }
  239. DispatchQueue.main.async { [self] in
  240. if dataImage != nil && UIImage(data: dataImage!) != nil {
  241. if let image = UIImage(data: dataImage!) {
  242. nexilis_button.image = image
  243. } else {
  244. nexilis_button.image = UIImage(named: Utils.getFBIconBg() == "1" ? "pb_button" : "pb_ball", in: Bundle.resourceBundle(for: Nexilis.self), with: nil)
  245. }
  246. } else {
  247. nexilis_button.image = UIImage(named: Utils.getFBIconBg() == "1" ? "pb_button" : "pb_ball", in: Bundle.resourceBundle(for: Nexilis.self), with: nil)
  248. }
  249. }
  250. }
  251. task.resume()
  252. }
  253. private func checkDelayAnimation() {
  254. DispatchQueue.main.asyncAfter(deadline: .now() + 3, execute: { [self] in
  255. if !isShow {
  256. animationTimer.invalidate()
  257. animationTimer = Timer.scheduledTimer(timeInterval: 0.001, target: self, selector: #selector(runAnimation), userInfo: nil, repeats: true)
  258. }
  259. })
  260. }
  261. @objc func runAnimation(){
  262. DispatchQueue.main.async { [self] in
  263. if configAnim == 0 || configAnim == 1 {
  264. if (lastRunAnimationHrz == -1 && frame.origin.x >= 0) || lastRunAnimationHrz == 1 && frame.origin.x <= UIScreen.main.bounds.width - defaultWidthFB {
  265. if lastRunAnimationHrz == -1 {
  266. frame.origin.x-=0.1
  267. } else {
  268. frame.origin.x+=0.1
  269. }
  270. } else {
  271. lastRunAnimationHrz = lastRunAnimationHrz == 1 ? -1 : 1
  272. if (lastRunAnimationHrz == -1 && configAnim == 1) || (lastRunAnimationHrz == 1 && configAnim == 0) {
  273. animationTimer.invalidate()
  274. if isLoopingAnim {
  275. checkDelayAnimation()
  276. }
  277. }
  278. }
  279. } else {
  280. if (lastRunAnimationVrt == -1 && frame.origin.y >= 0) || lastRunAnimationVrt == 1 && frame.origin.y <= UIScreen.main.bounds.height - defaultHeightFB {
  281. if lastRunAnimationVrt == -1 {
  282. frame.origin.y-=0.1
  283. } else {
  284. frame.origin.y+=0.1
  285. }
  286. } else {
  287. lastRunAnimationVrt = lastRunAnimationVrt == 1 ? -1 : 1
  288. if (lastRunAnimationVrt == -1 && configAnim == 3) || (lastRunAnimationVrt == 1 && configAnim == 2) {
  289. animationTimer.invalidate()
  290. if isLoopingAnim {
  291. checkDelayAnimation()
  292. }
  293. }
  294. }
  295. }
  296. }
  297. }
  298. private func pullButton() {
  299. if datePull == nil || Int(Date().timeIntervalSince(datePull!)) >= 60 {
  300. datePull = Date()
  301. } else if Int(Date().timeIntervalSince(datePull!)) < 60 {
  302. return
  303. }
  304. if !CheckConnection.isConnectedToNetwork() || API.nGetCLXConnState() == 0 {
  305. let imageView = UIImageView(image: UIImage(systemName: "xmark.circle.fill"))
  306. imageView.tintColor = .white
  307. 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)
  308. banner.show()
  309. return
  310. }
  311. if groupView.subviews.count == 0 {
  312. getDefaultButton()
  313. }
  314. DispatchQueue.global().async { [self] in
  315. if !Utils.getCustomButtons().isEmpty && configModeFB != MODE_HORIZONTAL_SIDE_TAB && configModeFB != MODE_HORIZONTAL_ANIMATION && configModeFB != MODE_VERTICAL_SIDE_TAB && Nexilis.fromMAB {
  316. DispatchQueue.main.async { [self] in
  317. groupView.subviews.forEach({ $0.removeFromSuperview() })
  318. let customButtons = Utils.getCustomButtons().components(separatedBy: ",")
  319. let customIcons = Utils.getCustomFBIcon().components(separatedBy: ",")
  320. countMenuFB = CGFloat(customButtons.count > 5 ? 5 : customButtons.count)
  321. for i in 0..<customButtons.count {
  322. let package_id = customButtons[i]
  323. let app_id = ""
  324. let icon = customIcons[i]
  325. let newButton = UIButton()
  326. newButton.heightAnchor.constraint(equalToConstant: defaultWidthHeightMenuFB).isActive = true
  327. newButton.translatesAutoresizingMaskIntoConstraints = false
  328. if !APIS.checkAppStateisBackground() {
  329. DispatchQueue.global().async {
  330. let urlString = Utils.getURLBase() + "get_file_from_path?img=" + icon
  331. if let cachedImage = ImageCache.shared.image(forKey: urlString) {
  332. DispatchQueue.main.async() {
  333. newButton.setImage(cachedImage, for: .normal)
  334. }
  335. return
  336. }
  337. Utils.fetchDataWithCookiesAndUserAgent(from: URL(string: urlString)!) { data, response, error in
  338. guard let data = data, error == nil else { return }
  339. // always update the UI from the main thread
  340. DispatchQueue.main.async() {
  341. if let image = UIImage(data: data) {
  342. newButton.setImage(image, for: .normal)
  343. ImageCache.shared.save(image: UIImage(data: data)!, forKey: urlString)
  344. }
  345. }
  346. }
  347. }
  348. }
  349. groupView.addArrangedSubview(newButton)
  350. newButton.restorationIdentifier = package_id
  351. newButton.accessibilityIdentifier = app_id
  352. newButton.addTarget(self, action: #selector(fbTap), for: .touchUpInside)
  353. let qmeraLongPress = UILongPressGestureRecognizer(target: self, action: #selector(qmeraLongPress(gestureRecognizer:)))
  354. newButton.addGestureRecognizer(qmeraLongPress)
  355. }
  356. }
  357. } else {
  358. if !Utils.getHistoryPullFB().isEmpty {
  359. setFBFromPull()
  360. }
  361. while Nexilis.isProcessWriteSync {
  362. Thread.sleep(forTimeInterval: 0.5)
  363. }
  364. if let response = Nexilis.writeSync(message: CoreMessage_TMessageBank.pullFloatingButton(), timeout: 30 * 1000){
  365. if response.isOk() {
  366. Utils.setHistoryPullFB(value: response.getBody(key: CoreMessage_TMessageKey.DATA, default_value: ""))
  367. setFBFromPull()
  368. }
  369. }
  370. }
  371. }
  372. }
  373. private func setFBFromPull() {
  374. DispatchQueue.main.async {
  375. let data = Utils.getHistoryPullFB()
  376. if !data.isEmpty {
  377. if let jsonArray = try! JSONSerialization.jsonObject(with: data.data(using: String.Encoding.utf8)!, options: JSONSerialization.ReadingOptions()) as? [AnyObject] {
  378. DispatchQueue.main.async { [self] in
  379. let filteredData = jsonArray.filter({ $0["mode"] as? Int == Int(configModeFB) })
  380. if filteredData.count != 0 {
  381. groupView.subviews.forEach({ $0.removeFromSuperview() })
  382. countMenuFB = CGFloat(filteredData.count > 5 ? 5 : filteredData.count)
  383. for json in filteredData {
  384. let package_id = json["package_id"] as! String
  385. let app_id = (json["app_id"] as? String) ?? ""
  386. let icon = (json["icon"] as? String) ?? ""
  387. let mode = "\((json["mode"] as? Int) ?? 1)"
  388. let newButton = UIButton()
  389. if mode != configModeFB {
  390. continue
  391. }
  392. if mode == MODE_HORIZONTAL_SIDE_TAB {
  393. newButton.widthAnchor.constraint(equalToConstant: defaultHeightFB - 10).isActive = true
  394. newButton.heightAnchor.constraint(equalToConstant: defaultHeightFB - 10).isActive = true
  395. } else if mode == MODE_HORIZONTAL_ANIMATION {
  396. newButton.widthAnchor.constraint(equalToConstant: defaultWidthHeightMenuFB).isActive = true
  397. newButton.heightAnchor.constraint(equalToConstant: defaultWidthHeightMenuFB).isActive = true
  398. } else if mode == MODE_VERTICAL_SIDE_TAB {
  399. newButton.widthAnchor.constraint(equalToConstant: 30).isActive = true
  400. newButton.heightAnchor.constraint(equalToConstant: 25).isActive = true
  401. newButton.contentMode = .scaleAspectFit
  402. newButton.clipsToBounds = true
  403. } else {
  404. newButton.heightAnchor.constraint(equalToConstant: defaultWidthHeightMenuFB).isActive = true
  405. }
  406. newButton.translatesAutoresizingMaskIntoConstraints = false
  407. var indexTap = 0
  408. if package_id.contains("_fb"){
  409. let listSplit = package_id.split(separator: "_", maxSplits: 1)
  410. let idxFB = listSplit.firstIndex(where: { $0.contains("fb") }) ?? 0
  411. let numIdx = listSplit[idxFB]
  412. indexTap = Int(String(numIdx).substring(from: 2, to: numIdx.count)) ?? 0
  413. }
  414. if indexTap == Nexilis.IDX_CHAT {
  415. newButton.setImage(UIImage(named: mode == MODE_HORIZONTAL_SIDE_TAB ? "pb_button_hrz_chat" : 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)
  416. } else if indexTap == Nexilis.IDX_CONVERSATION {
  417. newButton.setImage(UIImage(named: mode == MODE_VERTICAL_SIDE_TAB ? "pb_button_vtcst_chat" : 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)
  418. } else if indexTap == Nexilis.IDX_CALL {
  419. 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)
  420. } else if indexTap == Nexilis.IDX_CC {
  421. newButton.setImage(UIImage(named: mode == MODE_VERTICAL_SIDE_TAB ? "pb_button_vtcst_cc" : 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)
  422. } else if indexTap == Nexilis.IDX_STREAM {
  423. newButton.setImage(UIImage(named: mode == MODE_VERTICAL_SIDE_TAB ? "pb_button_vtcst_stream" : 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)
  424. } else if indexTap == Nexilis.IDX_SOCIAL_COMMERCE {
  425. newButton.setImage(UIImage(named: mode == MODE_VERTICAL_SIDE_TAB ? "pb_button_vtcst_commerce" : 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)
  426. } else if indexTap == Nexilis.IDX_NEWS {
  427. 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)
  428. } else if indexTap == Nexilis.IDX_POST {
  429. 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)
  430. } else if indexTap == Nexilis.IDX_NOTIF_CENTER {
  431. newButton.setImage(UIImage(named: mode == MODE_VERTICAL_SIDE_TAB ? "pb_button_vtcst_notif_center" : 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)
  432. } else {
  433. 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)
  434. }
  435. if !icon.isEmpty {
  436. if !APIS.checkAppStateisBackground() {
  437. DispatchQueue.global().async {
  438. let urlString = Utils.getURLBase() + "get_file_from_path?img=" + icon
  439. if let cachedImage = ImageCache.shared.image(forKey: urlString) {
  440. DispatchQueue.main.async() {
  441. newButton.setImage(cachedImage, for: .normal)
  442. }
  443. return
  444. }
  445. Utils.fetchDataWithCookiesAndUserAgent(from: URL(string: urlString)!) { data, response, error in
  446. guard let data = data, error == nil else { return }
  447. // always update the UI from the main thread
  448. DispatchQueue.main.async() {
  449. if let image = UIImage(data: data) {
  450. newButton.setImage(image, for: .normal)
  451. ImageCache.shared.save(image: UIImage(data: data)!, forKey: urlString)
  452. }
  453. }
  454. }
  455. }
  456. }
  457. }
  458. groupView.addArrangedSubview(newButton)
  459. newButton.restorationIdentifier = package_id
  460. newButton.accessibilityIdentifier = app_id
  461. newButton.addTarget(self, action: #selector(fbTap), for: .touchUpInside)
  462. let qmeraLongPress = UILongPressGestureRecognizer(target: self, action: #selector(qmeraLongPress(gestureRecognizer:)))
  463. newButton.addGestureRecognizer(qmeraLongPress)
  464. }
  465. }
  466. }
  467. }
  468. }
  469. }
  470. }
  471. func getDefaultButton() {
  472. let mode = configModeFB
  473. var data = [Nexilis.IDX_NOTIF_CENTER, Nexilis.IDX_CC, Nexilis.IDX_CONVERSATION, Nexilis.IDX_CALL, Nexilis.IDX_STREAM]
  474. if Nexilis.defaultFloatingButton.count > 0 {
  475. data = Nexilis.defaultFloatingButton
  476. }
  477. if mode == MODE_VERTICAL_SIDE_TAB {
  478. data = [Nexilis.IDX_NOTIF_CENTER, Nexilis.IDX_CC, Nexilis.IDX_CONVERSATION, Nexilis.IDX_STREAM, Nexilis.IDX_SOCIAL_COMMERCE]
  479. } else if mode == MODE_HORIZONTAL_ANIMATION {
  480. data = [Nexilis.IDX_NOTIF_CENTER, Nexilis.IDX_CC, Nexilis.IDX_CONVERSATION, Nexilis.IDX_SOCIAL_COMMERCE, Nexilis.IDX_STREAM]
  481. }
  482. countMenuFB = CGFloat(data.count > 5 ? 5 : data.count)
  483. for i in 0..<data.count {
  484. let newButton = UIButton()
  485. if mode == MODE_HORIZONTAL_SIDE_TAB {
  486. newButton.widthAnchor.constraint(equalToConstant: defaultHeightFB - 10).isActive = true
  487. newButton.heightAnchor.constraint(equalToConstant: defaultHeightFB - 10).isActive = true
  488. } else if mode == MODE_HORIZONTAL_ANIMATION {
  489. newButton.widthAnchor.constraint(equalToConstant: defaultWidthHeightMenuFB).isActive = true
  490. newButton.heightAnchor.constraint(equalToConstant: defaultWidthHeightMenuFB).isActive = true
  491. } else if mode == MODE_VERTICAL_SIDE_TAB {
  492. newButton.imageView?.contentMode = .scaleAspectFit
  493. } else {
  494. newButton.heightAnchor.constraint(equalToConstant: defaultWidthHeightMenuFB).isActive = true
  495. }
  496. newButton.translatesAutoresizingMaskIntoConstraints = false
  497. let indexTap = data[i]
  498. if indexTap == Nexilis.IDX_CHAT {
  499. 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)
  500. } else if indexTap == Nexilis.IDX_CONVERSATION {
  501. newButton.setImage(UIImage(named: mode == MODE_VERTICAL_SIDE_TAB ? "pb_button_vtcst_chat" : 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)
  502. } else if indexTap == Nexilis.IDX_CALL {
  503. 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)
  504. } else if indexTap == Nexilis.IDX_CC {
  505. newButton.setImage(UIImage(named: mode == MODE_VERTICAL_SIDE_TAB ? "pb_button_vtcst_cc" : 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)
  506. } else if indexTap == Nexilis.IDX_STREAM {
  507. newButton.setImage(UIImage(named: mode == MODE_VERTICAL_SIDE_TAB ? "pb_button_vtcst_stream" : 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)
  508. } else if indexTap == Nexilis.IDX_SOCIAL_COMMERCE {
  509. newButton.setImage(UIImage(named: mode == MODE_VERTICAL_SIDE_TAB ? "pb_button_vtcst_commerce" : 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)
  510. } else if indexTap == Nexilis.IDX_NEWS {
  511. 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)
  512. } else if indexTap == Nexilis.IDX_POST {
  513. 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)
  514. } else if indexTap == Nexilis.IDX_NOTIF_CENTER {
  515. newButton.setImage(UIImage(named: mode == MODE_VERTICAL_SIDE_TAB ? "pb_button_vtcst_notif_center" : 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)
  516. if mode == MODE_VERTICAL_SIDE_TAB {
  517. newButton.imageView?.contentMode = .scaleAspectFit
  518. }
  519. } else {
  520. 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)
  521. }
  522. groupView.addArrangedSubview(newButton)
  523. newButton.restorationIdentifier = "default_fb\(data[i])"
  524. newButton.accessibilityIdentifier = ""
  525. newButton.addTarget(self, action: #selector(fbTap), for: .touchUpInside)
  526. let qmeraLongPress = UILongPressGestureRecognizer(target: self, action: #selector(qmeraLongPress(gestureRecognizer:)))
  527. newButton.addGestureRecognizer(qmeraLongPress)
  528. }
  529. }
  530. @objc func draggedView(_ sender:UIPanGestureRecognizer){
  531. let size = UIScreen.main.bounds
  532. let widthScreen = size.width
  533. let heightScreen = size.height
  534. let minimumx: CGFloat = configModeFB == MODE_HORIZONTAL_ANIMATION && isShow ? widthFBAnim + defaultWidthHeightMenuFB : configModeFB == MODE_VERTICAL_ANIMATION && isShow ? 60 : 40
  535. let maximumx = configModeFB == MODE_HORIZONTAL_ANIMATION && isShow ? widthScreen - widthFBAnim - defaultWidthHeightMenuFB : configModeFB == MODE_VERTICAL_ANIMATION && isShow ? widthScreen - 10 - defaultWidthHeightMenuFB : widthScreen - 40
  536. let maxMinXSideTab = isShow ? center.x : widthScreen + (center.x - widthScreen)
  537. let translation = sender.translation(in: self)
  538. var xPos = center.x + translation.x
  539. var yPos = center.y + translation.y
  540. bringSubviewToFront(self)
  541. if configModeFB == MODE_HORIZONTAL_SIDE_TAB || configModeFB == MODE_VERTICAL_SIDE_TAB {
  542. xPos = maxMinXSideTab
  543. } else {
  544. if (xPos < minimumx) {
  545. xPos = minimumx
  546. }
  547. if (xPos > maximumx) {
  548. xPos = maximumx
  549. }
  550. }
  551. if(isShow) {
  552. let minimumy = configModeFB == MODE_VERTICAL_SIDE_TAB ? heightVerticalSideTab - 100 : configModeFB == MODE_HORIZONTAL_SIDE_TAB ? 50 : configModeFB == MODE_HORIZONTAL_ANIMATION ? defaultWidthHeightMenuFB * 2 + 10 : configModeFB == MODE_VERTICAL_ANIMATION ? defaultHeightFB + defaultWidthHeightMenuFB : self.frame.size.height - 120 + ((5 - countMenuFB) * 25)
  553. let maximumy = configModeFB == MODE_VERTICAL_SIDE_TAB ? heightScreen - (heightVerticalSideTab - 100) : configModeFB == MODE_HORIZONTAL_SIDE_TAB ? heightScreen - 50 : configModeFB == MODE_HORIZONTAL_ANIMATION ? heightScreen - defaultWidthHeightMenuFB - 30 : configModeFB == MODE_VERTICAL_ANIMATION ? heightScreen - defaultHeightFB - 30 : heightScreen - 50
  554. if(yPos < minimumy) {
  555. yPos = minimumy
  556. }
  557. if(yPos > maximumy) {
  558. yPos = maximumy
  559. }
  560. } else {
  561. let minimumy: CGFloat = 50
  562. let maximumy = heightScreen - 50
  563. if(yPos < minimumy) {
  564. yPos = minimumy
  565. }
  566. if(yPos > maximumy) {
  567. yPos = maximumy
  568. }
  569. }
  570. center = CGPoint(x: xPos, y: yPos)
  571. sender.setTranslation(CGPoint.zero, in: self)
  572. if lastPosY != nil {
  573. lastPosY = nil
  574. }
  575. // SecureUserDefaults.shared.set(center.x, forKey: "xlastPosFB")
  576. // SecureUserDefaults.shared.set(center.y, forKey: "ylastPosFB")
  577. }
  578. @objc func imageFBUpdate(notification: NSNotification) {
  579. }
  580. @objc func checkCounter() {
  581. DispatchQueue.global().async { [self] in
  582. let counter = queryCountCounter()
  583. if counter > 0 {
  584. DispatchQueue.main.async { [self] in
  585. if button_fb2 != nil && !indicatorCounterFB.isDescendant(of: button_fb2) {
  586. button_fb2.addSubview(indicatorCounterFB)
  587. indicatorCounterFB.layer.cornerRadius = 7.5
  588. indicatorCounterFB.layer.masksToBounds = true
  589. indicatorCounterFB.backgroundColor = .systemRed
  590. indicatorCounterFB.anchor(top: button_fb2.topAnchor, left: button_fb2.leftAnchor, height: 15, minWidth: 15, maxWidth: 20)
  591. indicatorCounterFB.addSubview(labelCounterFB)
  592. labelCounterFB.anchor(left: indicatorCounterFB.leftAnchor, right: indicatorCounterFB.rightAnchor, paddingLeft: 5, paddingRight: 5, centerX: indicatorCounterFB.centerXAnchor, centerY: indicatorCounterFB.centerYAnchor)
  593. labelCounterFB.font = .systemFont(ofSize: 10)
  594. labelCounterFB.textColor = .white
  595. }
  596. if !indicatorCounterFBBig.isDescendant(of: nexilis_button){
  597. nexilis_button.addSubview(indicatorCounterFBBig)
  598. indicatorCounterFBBig.tintColor = .systemRed
  599. indicatorCounterFBBig.image = UIImage(systemName: "staroflife.circle.fill")
  600. indicatorCounterFBBig.anchor(top: nexilis_button.topAnchor, left: nexilis_button.leftAnchor, paddingTop: 5, paddingLeft: 5, width: 15, height: 15)
  601. }
  602. labelCounterFB.text = "\(counter)"
  603. }
  604. } else {
  605. DispatchQueue.main.async { [self] in
  606. if button_fb2 != nil && indicatorCounterFB.isDescendant(of: button_fb2) {
  607. indicatorCounterFB.removeFromSuperview()
  608. }
  609. if indicatorCounterFBBig.isDescendant(of: nexilis_button) {
  610. indicatorCounterFBBig.removeFromSuperview()
  611. }
  612. }
  613. }
  614. }
  615. }
  616. private func queryCountCounter() -> Int32 {
  617. var counter: Int32?
  618. Database.shared.database?.inTransaction({ (fmdb, rollback) in
  619. do {
  620. if let cursor = Database.shared.getRecords(fmdb: fmdb, query: "SELECT SUM(counter) FROM MESSAGE_SUMMARY"), cursor.next() {
  621. counter = cursor.int(forColumnIndex: 0)
  622. cursor.close()
  623. }
  624. } catch {
  625. rollback.pointee = true
  626. print("Access database error: \(error.localizedDescription)")
  627. }
  628. })
  629. return counter ?? 0
  630. }
  631. @objc func qmeraTap() {
  632. show(isShow: !isShow)
  633. }
  634. @objc func fbTap(_ sender: UIButton) {
  635. let package_id = sender.restorationIdentifier!
  636. var app_id = sender.accessibilityIdentifier!
  637. var indexTap = 0
  638. if package_id.contains("_fb"){
  639. let listSplit = package_id.split(separator: "_", maxSplits: 1)
  640. let numIdx = listSplit[listSplit.firstIndex(where: { $0.contains("fb") }) ?? 0]
  641. indexTap = Int(String(numIdx).substring(from: 2, to: numIdx.count))!
  642. if listSplit.count == 2 {
  643. app_id = String(listSplit[1])
  644. }
  645. }
  646. Nexilis.buttonClicked(index: indexTap, id: app_id)
  647. hideButton()
  648. }
  649. @objc func qmeraLongPress(gestureRecognizer: UILongPressGestureRecognizer) {
  650. if gestureRecognizer.state == .began {
  651. if mySettingDelegate != nil {
  652. mySettingDelegate?.settingDelegate()
  653. } else {
  654. APIS.openSetting()
  655. }
  656. hideButton()
  657. }
  658. }
  659. @objc public func hideButton() {
  660. animationTimer.invalidate()
  661. if isShow {
  662. show(isShow: false)
  663. }
  664. if self.frame.origin.x < UIScreen.main.bounds.width / 2 - 30 {
  665. self.frame.origin.x = 0
  666. } else {
  667. self.frame.origin.x = UIScreen.main.bounds.width - defaultWidthFB
  668. }
  669. }
  670. public func show(isShow: Bool) {
  671. self.isShow = isShow
  672. if isShow {
  673. animationTimer.invalidate()
  674. pullButton()
  675. if indicatorCounterFBBig.isDescendant(of: nexilis_button) {
  676. indicatorCounterFBBig.isHidden = true
  677. }
  678. var height = CGFloat((defaultWidthHeightMenuFB * countMenuFB) + defaultHeightFB + 5) //defaultWidthHeightMenuFB
  679. var width = frame.width
  680. var xPosition = frame.origin.x
  681. if configModeFB == MODE_VERTICAL_ANIMATION {
  682. height = CGFloat((defaultWidthHeightMenuFB * (countMenuFB - 2)) + defaultHeightFB - 5)
  683. width = frame.width + defaultWidthHeightMenuFB
  684. if xPosition > UIScreen.main.bounds.width - defaultWidthFB - defaultWidthHeightMenuFB {
  685. xPosition = UIScreen.main.bounds.width - defaultWidthFB - defaultWidthHeightMenuFB
  686. }
  687. scrollView.isHidden = false
  688. } else if configModeFB == MODE_HORIZONTAL_ANIMATION {
  689. height = defaultHeightFB + defaultWidthHeightMenuFB
  690. width = defaultWidthHeightMenuFB * (countMenuFB - 1)
  691. if xPosition > UIScreen.main.bounds.width - width {
  692. xPosition = UIScreen.main.bounds.width - width
  693. }
  694. }
  695. var yPosition = frame.origin.y - height + defaultHeightFB
  696. if yPosition <= 25 {
  697. lastPosY = frame.origin.y
  698. yPosition = 25
  699. }
  700. if configModeFB != MODE_HORIZONTAL_SIDE_TAB && configModeFB != MODE_VERTICAL_SIDE_TAB {
  701. frame = CGRect(x: xPosition, y: yPosition, width: width, height: height)
  702. } else {
  703. UIView.animate(withDuration: 0.5, animations: { [self] in
  704. var vst: CGFloat = 0.0
  705. if configModeFB == MODE_VERTICAL_SIDE_TAB {
  706. vst = UIScreen.main.bounds.width - defaultWidthFB - widthVerticalSideTab
  707. let size = UIScreen.main.bounds
  708. let heightScreen = size.height
  709. if frame.origin.y < 20 {
  710. frame.origin.y = 20
  711. } else if frame.origin.y > heightScreen - heightVerticalSideTab {
  712. frame.origin.y = heightScreen - heightVerticalSideTab
  713. }
  714. }
  715. frame.origin.x = 0 + vst
  716. })
  717. }
  718. DispatchQueue.main.asyncAfter(deadline: .now() + 0.5, execute: { [self] in
  719. if isShow {
  720. let countSubviewsAfter = groupView.subviews.count
  721. if countSubviewsAfter > 5 {
  722. scrollView.flashScrollIndicators()
  723. }
  724. }
  725. })
  726. } else {
  727. if indicatorCounterFBBig.isDescendant(of: nexilis_button) {
  728. indicatorCounterFBBig.isHidden = false
  729. }
  730. var height = CGFloat((defaultWidthHeightMenuFB * countMenuFB) + defaultHeightFB + 5) //defaultWidthHeightMenuFB
  731. var width = defaultWidthFB
  732. if configModeFB == MODE_VERTICAL_ANIMATION {
  733. height = CGFloat((defaultWidthHeightMenuFB * 3) + defaultHeightFB - 5)
  734. scrollView.isHidden = true
  735. } else if configModeFB == MODE_HORIZONTAL_ANIMATION {
  736. height = defaultHeightFB + defaultWidthHeightMenuFB
  737. width = defaultWidthFB
  738. }
  739. var yPosition = frame.origin.y + height - defaultHeightFB
  740. if lastPosY != nil {
  741. yPosition = lastPosY!
  742. }
  743. if configModeFB != MODE_HORIZONTAL_SIDE_TAB && configModeFB != MODE_VERTICAL_SIDE_TAB {
  744. frame = CGRect(x: frame.origin.x, y: yPosition, width: width, height: defaultHeightFB)
  745. } else {
  746. UIView.animate(withDuration: 0.5, animations: { [self] in
  747. frame.origin.x = UIScreen.main.bounds.width - defaultWidthFB
  748. })
  749. }
  750. if configModeFB == MODE_VERTICAL_ANIMATION || configModeFB == MODE_HORIZONTAL_ANIMATION {
  751. checkDelayAnimation()
  752. }
  753. }
  754. }
  755. }
  756. public protocol SettingMABDelegate: AnyObject {
  757. func settingDelegate()
  758. }