FloatingButton.swift 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799
  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. DispatchQueue.global().async {
  329. let urlString = Utils.getURLBase() + "get_file_from_path?img=" + icon
  330. if let cachedImage = ImageCache.shared.image(forKey: urlString) {
  331. DispatchQueue.main.async() {
  332. newButton.setImage(cachedImage, for: .normal)
  333. }
  334. return
  335. }
  336. Utils.fetchDataWithCookiesAndUserAgent(from: URL(string: urlString)!) { data, response, error in
  337. guard let data = data, error == nil else { return }
  338. // always update the UI from the main thread
  339. DispatchQueue.main.async() {
  340. if let image = UIImage(data: data) {
  341. newButton.setImage(image, for: .normal)
  342. ImageCache.shared.save(image: UIImage(data: data)!, forKey: urlString)
  343. }
  344. }
  345. }
  346. }
  347. groupView.addArrangedSubview(newButton)
  348. newButton.restorationIdentifier = package_id
  349. newButton.accessibilityIdentifier = app_id
  350. newButton.addTarget(self, action: #selector(fbTap), for: .touchUpInside)
  351. let qmeraLongPress = UILongPressGestureRecognizer(target: self, action: #selector(qmeraLongPress(gestureRecognizer:)))
  352. newButton.addGestureRecognizer(qmeraLongPress)
  353. }
  354. }
  355. } else {
  356. if !Utils.getHistoryPullFB().isEmpty {
  357. setFBFromPull()
  358. }
  359. if let response = Nexilis.writeSync(message: CoreMessage_TMessageBank.pullFloatingButton(), timeout: 30 * 1000){
  360. if response.isOk() {
  361. Utils.setHistoryPullFB(value: response.getBody(key: CoreMessage_TMessageKey.DATA, default_value: ""))
  362. setFBFromPull()
  363. }
  364. }
  365. }
  366. }
  367. }
  368. private func setFBFromPull() {
  369. DispatchQueue.main.async {
  370. let data = Utils.getHistoryPullFB()
  371. if !data.isEmpty {
  372. if let jsonArray = try! JSONSerialization.jsonObject(with: data.data(using: String.Encoding.utf8)!, options: JSONSerialization.ReadingOptions()) as? [AnyObject] {
  373. DispatchQueue.main.async { [self] in
  374. let filteredData = jsonArray.filter({ $0["mode"] as? Int == Int(configModeFB) })
  375. if filteredData.count != 0 {
  376. groupView.subviews.forEach({ $0.removeFromSuperview() })
  377. countMenuFB = CGFloat(filteredData.count > 5 ? 5 : filteredData.count)
  378. for json in filteredData {
  379. let package_id = json["package_id"] as! String
  380. let app_id = (json["app_id"] as? String) ?? ""
  381. let icon = (json["icon"] as? String) ?? ""
  382. let mode = "\((json["mode"] as? Int) ?? 1)"
  383. let newButton = UIButton()
  384. if mode != configModeFB {
  385. continue
  386. }
  387. if mode == MODE_HORIZONTAL_SIDE_TAB {
  388. newButton.widthAnchor.constraint(equalToConstant: defaultHeightFB - 10).isActive = true
  389. newButton.heightAnchor.constraint(equalToConstant: defaultHeightFB - 10).isActive = true
  390. } else if mode == MODE_HORIZONTAL_ANIMATION {
  391. newButton.widthAnchor.constraint(equalToConstant: defaultWidthHeightMenuFB).isActive = true
  392. newButton.heightAnchor.constraint(equalToConstant: defaultWidthHeightMenuFB).isActive = true
  393. } else if mode == MODE_VERTICAL_SIDE_TAB {
  394. newButton.widthAnchor.constraint(equalToConstant: 30).isActive = true
  395. newButton.heightAnchor.constraint(equalToConstant: 25).isActive = true
  396. newButton.contentMode = .scaleAspectFit
  397. newButton.clipsToBounds = true
  398. } else {
  399. newButton.heightAnchor.constraint(equalToConstant: defaultWidthHeightMenuFB).isActive = true
  400. }
  401. newButton.translatesAutoresizingMaskIntoConstraints = false
  402. var indexTap = 0
  403. if package_id.contains("_fb"){
  404. let listSplit = package_id.split(separator: "_", maxSplits: 1)
  405. let idxFB = listSplit.firstIndex(where: { $0.contains("fb") }) ?? 0
  406. let numIdx = listSplit[idxFB]
  407. indexTap = Int(String(numIdx).substring(from: 2, to: numIdx.count)) ?? 0
  408. }
  409. if indexTap == Nexilis.IDX_CHAT {
  410. 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)
  411. } else if indexTap == Nexilis.IDX_CONVERSATION {
  412. 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)
  413. } else if indexTap == Nexilis.IDX_CALL {
  414. 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)
  415. } else if indexTap == Nexilis.IDX_CC {
  416. 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)
  417. } else if indexTap == Nexilis.IDX_STREAM {
  418. 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)
  419. } else if indexTap == Nexilis.IDX_SOCIAL_COMMERCE {
  420. 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)
  421. } else if indexTap == Nexilis.IDX_NEWS {
  422. 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)
  423. } else if indexTap == Nexilis.IDX_POST {
  424. 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)
  425. } else if indexTap == Nexilis.IDX_NOTIF_CENTER {
  426. 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)
  427. } else {
  428. 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)
  429. }
  430. if !icon.isEmpty {
  431. DispatchQueue.global().async {
  432. let urlString = Utils.getURLBase() + "get_file_from_path?img=" + icon
  433. if let cachedImage = ImageCache.shared.image(forKey: urlString) {
  434. DispatchQueue.main.async() {
  435. newButton.setImage(cachedImage, for: .normal)
  436. }
  437. return
  438. }
  439. Utils.fetchDataWithCookiesAndUserAgent(from: URL(string: urlString)!) { data, response, error in
  440. guard let data = data, error == nil else { return }
  441. // always update the UI from the main thread
  442. DispatchQueue.main.async() {
  443. if let image = UIImage(data: data) {
  444. newButton.setImage(image, for: .normal)
  445. ImageCache.shared.save(image: UIImage(data: data)!, forKey: urlString)
  446. }
  447. }
  448. }
  449. }
  450. }
  451. groupView.addArrangedSubview(newButton)
  452. newButton.restorationIdentifier = package_id
  453. newButton.accessibilityIdentifier = app_id
  454. newButton.addTarget(self, action: #selector(fbTap), for: .touchUpInside)
  455. let qmeraLongPress = UILongPressGestureRecognizer(target: self, action: #selector(qmeraLongPress(gestureRecognizer:)))
  456. newButton.addGestureRecognizer(qmeraLongPress)
  457. }
  458. }
  459. }
  460. }
  461. }
  462. }
  463. }
  464. func getDefaultButton() {
  465. let mode = configModeFB
  466. var data = [Nexilis.IDX_NOTIF_CENTER, Nexilis.IDX_CC, Nexilis.IDX_CONVERSATION, Nexilis.IDX_CALL, Nexilis.IDX_STREAM]
  467. if Nexilis.defaultFloatingButton.count > 0 {
  468. data = Nexilis.defaultFloatingButton
  469. }
  470. if mode == MODE_VERTICAL_SIDE_TAB {
  471. data = [Nexilis.IDX_NOTIF_CENTER, Nexilis.IDX_CC, Nexilis.IDX_CONVERSATION, Nexilis.IDX_STREAM, Nexilis.IDX_SOCIAL_COMMERCE]
  472. } else if mode == MODE_HORIZONTAL_ANIMATION {
  473. data = [Nexilis.IDX_NOTIF_CENTER, Nexilis.IDX_CC, Nexilis.IDX_CONVERSATION, Nexilis.IDX_SOCIAL_COMMERCE, Nexilis.IDX_STREAM]
  474. }
  475. countMenuFB = CGFloat(data.count > 5 ? 5 : data.count)
  476. for i in 0..<data.count {
  477. let newButton = UIButton()
  478. if mode == MODE_HORIZONTAL_SIDE_TAB {
  479. newButton.widthAnchor.constraint(equalToConstant: defaultHeightFB - 10).isActive = true
  480. newButton.heightAnchor.constraint(equalToConstant: defaultHeightFB - 10).isActive = true
  481. } else if mode == MODE_HORIZONTAL_ANIMATION {
  482. newButton.widthAnchor.constraint(equalToConstant: defaultWidthHeightMenuFB).isActive = true
  483. newButton.heightAnchor.constraint(equalToConstant: defaultWidthHeightMenuFB).isActive = true
  484. } else if mode == MODE_VERTICAL_SIDE_TAB {
  485. newButton.imageView?.contentMode = .scaleAspectFit
  486. } else {
  487. newButton.heightAnchor.constraint(equalToConstant: defaultWidthHeightMenuFB).isActive = true
  488. }
  489. newButton.translatesAutoresizingMaskIntoConstraints = false
  490. let indexTap = data[i]
  491. if indexTap == Nexilis.IDX_CHAT {
  492. 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)
  493. } else if indexTap == Nexilis.IDX_CONVERSATION {
  494. 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)
  495. } else if indexTap == Nexilis.IDX_CALL {
  496. 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)
  497. } else if indexTap == Nexilis.IDX_CC {
  498. 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)
  499. } else if indexTap == Nexilis.IDX_STREAM {
  500. 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)
  501. } else if indexTap == Nexilis.IDX_SOCIAL_COMMERCE {
  502. 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)
  503. } else if indexTap == Nexilis.IDX_NEWS {
  504. 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)
  505. } else if indexTap == Nexilis.IDX_POST {
  506. 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)
  507. } else if indexTap == Nexilis.IDX_NOTIF_CENTER {
  508. 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)
  509. if mode == MODE_VERTICAL_SIDE_TAB {
  510. newButton.imageView?.contentMode = .scaleAspectFit
  511. }
  512. } else {
  513. 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)
  514. }
  515. groupView.addArrangedSubview(newButton)
  516. newButton.restorationIdentifier = "default_fb\(data[i])"
  517. newButton.accessibilityIdentifier = ""
  518. newButton.addTarget(self, action: #selector(fbTap), for: .touchUpInside)
  519. let qmeraLongPress = UILongPressGestureRecognizer(target: self, action: #selector(qmeraLongPress(gestureRecognizer:)))
  520. newButton.addGestureRecognizer(qmeraLongPress)
  521. }
  522. }
  523. @objc func draggedView(_ sender:UIPanGestureRecognizer){
  524. let size = UIScreen.main.bounds
  525. let widthScreen = size.width
  526. let heightScreen = size.height
  527. let minimumx: CGFloat = configModeFB == MODE_HORIZONTAL_ANIMATION && isShow ? widthFBAnim + defaultWidthHeightMenuFB : configModeFB == MODE_VERTICAL_ANIMATION && isShow ? 60 : 40
  528. let maximumx = configModeFB == MODE_HORIZONTAL_ANIMATION && isShow ? widthScreen - widthFBAnim - defaultWidthHeightMenuFB : configModeFB == MODE_VERTICAL_ANIMATION && isShow ? widthScreen - 10 - defaultWidthHeightMenuFB : widthScreen - 40
  529. let maxMinXSideTab = isShow ? center.x : widthScreen + (center.x - widthScreen)
  530. let translation = sender.translation(in: self)
  531. var xPos = center.x + translation.x
  532. var yPos = center.y + translation.y
  533. bringSubviewToFront(self)
  534. if configModeFB == MODE_HORIZONTAL_SIDE_TAB || configModeFB == MODE_VERTICAL_SIDE_TAB {
  535. xPos = maxMinXSideTab
  536. } else {
  537. if (xPos < minimumx) {
  538. xPos = minimumx
  539. }
  540. if (xPos > maximumx) {
  541. xPos = maximumx
  542. }
  543. }
  544. if(isShow) {
  545. 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)
  546. 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
  547. if(yPos < minimumy) {
  548. yPos = minimumy
  549. }
  550. if(yPos > maximumy) {
  551. yPos = maximumy
  552. }
  553. } else {
  554. let minimumy: CGFloat = 50
  555. let maximumy = heightScreen - 50
  556. if(yPos < minimumy) {
  557. yPos = minimumy
  558. }
  559. if(yPos > maximumy) {
  560. yPos = maximumy
  561. }
  562. }
  563. center = CGPoint(x: xPos, y: yPos)
  564. sender.setTranslation(CGPoint.zero, in: self)
  565. if lastPosY != nil {
  566. lastPosY = nil
  567. }
  568. // SecureUserDefaults.shared.set(center.x, forKey: "xlastPosFB")
  569. // SecureUserDefaults.shared.set(center.y, forKey: "ylastPosFB")
  570. }
  571. @objc func imageFBUpdate(notification: NSNotification) {
  572. }
  573. @objc func checkCounter() {
  574. DispatchQueue.global().async { [self] in
  575. let counter = queryCountCounter()
  576. if counter > 0 {
  577. DispatchQueue.main.async { [self] in
  578. if button_fb2 != nil && !indicatorCounterFB.isDescendant(of: button_fb2) {
  579. button_fb2.addSubview(indicatorCounterFB)
  580. indicatorCounterFB.layer.cornerRadius = 7.5
  581. indicatorCounterFB.layer.masksToBounds = true
  582. indicatorCounterFB.backgroundColor = .systemRed
  583. indicatorCounterFB.anchor(top: button_fb2.topAnchor, left: button_fb2.leftAnchor, height: 15, minWidth: 15, maxWidth: 20)
  584. indicatorCounterFB.addSubview(labelCounterFB)
  585. labelCounterFB.anchor(left: indicatorCounterFB.leftAnchor, right: indicatorCounterFB.rightAnchor, paddingLeft: 5, paddingRight: 5, centerX: indicatorCounterFB.centerXAnchor, centerY: indicatorCounterFB.centerYAnchor)
  586. labelCounterFB.font = .systemFont(ofSize: 10)
  587. labelCounterFB.textColor = .white
  588. }
  589. if !indicatorCounterFBBig.isDescendant(of: nexilis_button){
  590. nexilis_button.addSubview(indicatorCounterFBBig)
  591. indicatorCounterFBBig.tintColor = .systemRed
  592. indicatorCounterFBBig.image = UIImage(systemName: "staroflife.circle.fill")
  593. indicatorCounterFBBig.anchor(top: nexilis_button.topAnchor, left: nexilis_button.leftAnchor, paddingTop: 5, paddingLeft: 5, width: 15, height: 15)
  594. }
  595. labelCounterFB.text = "\(counter)"
  596. }
  597. } else {
  598. DispatchQueue.main.async { [self] in
  599. if button_fb2 != nil && indicatorCounterFB.isDescendant(of: button_fb2) {
  600. indicatorCounterFB.removeFromSuperview()
  601. }
  602. if indicatorCounterFBBig.isDescendant(of: nexilis_button) {
  603. indicatorCounterFBBig.removeFromSuperview()
  604. }
  605. }
  606. }
  607. }
  608. }
  609. private func queryCountCounter() -> Int32 {
  610. var counter: Int32?
  611. Database.shared.database?.inTransaction({ (fmdb, rollback) in
  612. do {
  613. if let cursor = Database.shared.getRecords(fmdb: fmdb, query: "SELECT SUM(counter) FROM MESSAGE_SUMMARY"), cursor.next() {
  614. counter = cursor.int(forColumnIndex: 0)
  615. cursor.close()
  616. }
  617. } catch {
  618. rollback.pointee = true
  619. print("Access database error: \(error.localizedDescription)")
  620. }
  621. })
  622. return counter ?? 0
  623. }
  624. @objc func qmeraTap() {
  625. show(isShow: !isShow)
  626. }
  627. @objc func fbTap(_ sender: UIButton) {
  628. let package_id = sender.restorationIdentifier!
  629. var app_id = sender.accessibilityIdentifier!
  630. var indexTap = 0
  631. if package_id.contains("_fb"){
  632. let listSplit = package_id.split(separator: "_", maxSplits: 1)
  633. let numIdx = listSplit[listSplit.firstIndex(where: { $0.contains("fb") }) ?? 0]
  634. indexTap = Int(String(numIdx).substring(from: 2, to: numIdx.count))!
  635. if listSplit.count == 2 {
  636. app_id = String(listSplit[1])
  637. }
  638. }
  639. Nexilis.buttonClicked(index: indexTap, id: app_id)
  640. hideButton()
  641. }
  642. @objc func qmeraLongPress(gestureRecognizer: UILongPressGestureRecognizer) {
  643. if gestureRecognizer.state == .began {
  644. if mySettingDelegate != nil {
  645. mySettingDelegate?.settingDelegate()
  646. } else {
  647. APIS.openSetting()
  648. }
  649. hideButton()
  650. }
  651. }
  652. @objc public func hideButton() {
  653. animationTimer.invalidate()
  654. if isShow {
  655. show(isShow: false)
  656. }
  657. if self.frame.origin.x < UIScreen.main.bounds.width / 2 - 30 {
  658. self.frame.origin.x = 0
  659. } else {
  660. self.frame.origin.x = UIScreen.main.bounds.width - defaultWidthFB
  661. }
  662. }
  663. public func show(isShow: Bool) {
  664. self.isShow = isShow
  665. if isShow {
  666. animationTimer.invalidate()
  667. pullButton()
  668. if indicatorCounterFBBig.isDescendant(of: nexilis_button) {
  669. indicatorCounterFBBig.isHidden = true
  670. }
  671. var height = CGFloat((defaultWidthHeightMenuFB * countMenuFB) + defaultHeightFB + 5) //defaultWidthHeightMenuFB
  672. var width = frame.width
  673. var xPosition = frame.origin.x
  674. if configModeFB == MODE_VERTICAL_ANIMATION {
  675. height = CGFloat((defaultWidthHeightMenuFB * (countMenuFB - 2)) + defaultHeightFB - 5)
  676. width = frame.width + defaultWidthHeightMenuFB
  677. if xPosition > UIScreen.main.bounds.width - defaultWidthFB - defaultWidthHeightMenuFB {
  678. xPosition = UIScreen.main.bounds.width - defaultWidthFB - defaultWidthHeightMenuFB
  679. }
  680. scrollView.isHidden = false
  681. } else if configModeFB == MODE_HORIZONTAL_ANIMATION {
  682. height = defaultHeightFB + defaultWidthHeightMenuFB
  683. width = defaultWidthHeightMenuFB * (countMenuFB - 1)
  684. if xPosition > UIScreen.main.bounds.width - width {
  685. xPosition = UIScreen.main.bounds.width - width
  686. }
  687. }
  688. var yPosition = frame.origin.y - height + defaultHeightFB
  689. if yPosition <= 25 {
  690. lastPosY = frame.origin.y
  691. yPosition = 25
  692. }
  693. if configModeFB != MODE_HORIZONTAL_SIDE_TAB && configModeFB != MODE_VERTICAL_SIDE_TAB {
  694. frame = CGRect(x: xPosition, y: yPosition, width: width, height: height)
  695. } else {
  696. UIView.animate(withDuration: 0.5, animations: { [self] in
  697. var vst: CGFloat = 0.0
  698. if configModeFB == MODE_VERTICAL_SIDE_TAB {
  699. vst = UIScreen.main.bounds.width - defaultWidthFB - widthVerticalSideTab
  700. let size = UIScreen.main.bounds
  701. let heightScreen = size.height
  702. if frame.origin.y < 20 {
  703. frame.origin.y = 20
  704. } else if frame.origin.y > heightScreen - heightVerticalSideTab {
  705. frame.origin.y = heightScreen - heightVerticalSideTab
  706. }
  707. }
  708. frame.origin.x = 0 + vst
  709. })
  710. }
  711. DispatchQueue.main.asyncAfter(deadline: .now() + 0.5, execute: { [self] in
  712. if isShow {
  713. let countSubviewsAfter = groupView.subviews.count
  714. if countSubviewsAfter > 5 {
  715. scrollView.flashScrollIndicators()
  716. }
  717. }
  718. })
  719. } else {
  720. if indicatorCounterFBBig.isDescendant(of: nexilis_button) {
  721. indicatorCounterFBBig.isHidden = false
  722. }
  723. var height = CGFloat((defaultWidthHeightMenuFB * countMenuFB) + defaultHeightFB + 5) //defaultWidthHeightMenuFB
  724. var width = defaultWidthFB
  725. if configModeFB == MODE_VERTICAL_ANIMATION {
  726. height = CGFloat((defaultWidthHeightMenuFB * 3) + defaultHeightFB - 5)
  727. scrollView.isHidden = true
  728. } else if configModeFB == MODE_HORIZONTAL_ANIMATION {
  729. height = defaultHeightFB + defaultWidthHeightMenuFB
  730. width = defaultWidthFB
  731. }
  732. var yPosition = frame.origin.y + height - defaultHeightFB
  733. if lastPosY != nil {
  734. yPosition = lastPosY!
  735. }
  736. if configModeFB != MODE_HORIZONTAL_SIDE_TAB && configModeFB != MODE_VERTICAL_SIDE_TAB {
  737. frame = CGRect(x: frame.origin.x, y: yPosition, width: width, height: defaultHeightFB)
  738. } else {
  739. UIView.animate(withDuration: 0.5, animations: { [self] in
  740. frame.origin.x = UIScreen.main.bounds.width - defaultWidthFB
  741. })
  742. }
  743. if configModeFB == MODE_VERTICAL_ANIMATION || configModeFB == MODE_HORIZONTAL_ANIMATION {
  744. checkDelayAnimation()
  745. }
  746. }
  747. }
  748. }
  749. public protocol SettingMABDelegate: AnyObject {
  750. func settingDelegate()
  751. }