123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682 |
- //
- // FloatingButton.swift
- // Qmera
- //
- // Created by Yayan Dwi on 03/09/21.
- //
- import UIKit
- import nuSDKService
- import NotificationBannerSwift
- public class FloatingButton: UIView {
-
- var groupView: UIStackView!
- var scrollView: UIScrollView!
- var button_fb1: UIButton!
- var button_fb2: UIButton!
- var button_fb3: UIButton!
- var button_fb4: UIButton!
- var nexilis_button: UIImageView!
- var nexilis_pin: UIImageView!
- var leadingConstraintPin: NSLayoutConstraint!
- var bottomConstraintPin: NSLayoutConstraint!
- var trailingConstraintPin: NSLayoutConstraint!
- var topConstraintPin: NSLayoutConstraint!
- var lastPosY: CGFloat?
- var lastImageButton = ""
- var iconCC = ""
-
- let indicatorCounterFB = UIView()
- let labelCounterFB = UILabel()
- let indicatorCounterFBBig = UIImageView()
-
- var datePull: Date?
- var animationTimer = Timer()
- var configAnim: Int = Int(Utils.getFloatingAnim().components(separatedBy: "~")[0]) ?? 1
- var isLoopingAnim = (Int(Utils.getFloatingAnim().components(separatedBy: "~")[1]) ?? 1) == 1 ? true : false
- var lastRunAnimationHrz = -1
- var lastRunAnimationVrt = -1
-
- var panGesture: UIPanGestureRecognizer?
- var defaultWidthFB = (UIScreen.main.bounds.height * 0.5) / 7.5
- var defaultHeightFB = (UIScreen.main.bounds.height * 0.5) / 7.5
- let defaultWidthHeightMenuFB = (UIScreen.main.bounds.height * 0.45) / 7.5
- let widthFBAnim = (UIScreen.main.bounds.height * 0.8) / 7.5
- let heightFBAnim = (UIScreen.main.bounds.height * 1) / 7.5
- let heightFBSideTab = (UIScreen.main.bounds.height * 1.05) / 7.5
- let widthFBSideTab: CGFloat = 18
-
- final let MODE_VERTICAL_FLOATING_BUTTON = "1"
- final let MODE_VERTICAL_ANIMATION = "2"
- final let MODE_HORIZONTAL_ANIMATION = "3"
- final let MODE_HORIZONTAL_SIDE_TAB = "4"
-
- var countMenuFB: CGFloat = 5 {
- didSet {
- if isShow {
- show(isShow: isShow)
- }
- }
- }
-
- public weak var mySettingDelegate: SettingMABDelegate?
-
- public var isShow: Bool = false
-
- override init(frame: CGRect) {
- super.init(frame: frame)
- commonInit()
- }
-
- required init?(coder: NSCoder) {
- super.init(coder: coder)
- commonInit()
- }
-
- private func commonInit() {
- panGesture = UIPanGestureRecognizer(target: self, action: #selector(draggedView(_:)))
- addGestureRecognizer(panGesture!)
-
- nexilis_button = UIImageView()
- nexilis_button.translatesAutoresizingMaskIntoConstraints = false
- nexilis_button.isUserInteractionEnabled = true
- var dataImage: Data?
- if Utils.getConfigModeFB() == MODE_VERTICAL_ANIMATION || Utils.getConfigModeFB() == MODE_HORIZONTAL_ANIMATION {
- defaultWidthFB = widthFBAnim
- defaultHeightFB = heightFBAnim
- nexilis_button.sd_setImage(with: URL(string: Utils.getConfigModeFB() == MODE_VERTICAL_ANIMATION ? Utils.getIconCenterAnim2()! : Utils.getIconCenterAnim4()!)) { [self] (image, error, cacheType, imageURL) in
- if error != nil {
- //print("Error loading GIF: \(error.localizedDescription)")
- } else {
- // Configure the animation
- nexilis_button.animationImages = image?.images
- nexilis_button.animationDuration = image?.duration ?? 0.0
- nexilis_button.animationRepeatCount = 0
- nexilis_button.startAnimating()
- }
- }
- } else {
- if !Utils.getIconDock().isEmpty && Utils.getConfigModeFB() == MODE_VERTICAL_FLOATING_BUTTON {
- dataImage = try? Data(contentsOf: URL(string: Utils.getUrlDock()!)!)
- if dataImage != nil {
- if let image = UIImage(data: dataImage!) {
- nexilis_button.image = image
- } else {
- nexilis_button.image = UIImage(named: Utils.getFBIconBg() == "1" ? "pb_button" : "pb_ball", in: Bundle.resourceBundle(for: Nexilis.self), with: nil)
- }
- } else {
- nexilis_button.image = UIImage(named: Utils.getFBIconBg() == "1" ? "pb_button" : "pb_ball", in: Bundle.resourceBundle(for: Nexilis.self), with: nil)
- }
- } else {
- if Utils.getConfigModeFB() == MODE_HORIZONTAL_SIDE_TAB {
- defaultWidthFB = widthFBSideTab
- defaultHeightFB = heightFBSideTab
- }
- nexilis_button.image = UIImage(named: Utils.getConfigModeFB() != MODE_HORIZONTAL_SIDE_TAB ? "pb_button" : "pb_side_tab", in: Bundle.resourceBundle(for: Nexilis.self), with: nil)
- }
- }
-
- backgroundColor = .clear
- 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)
-
- if Utils.getConfigModeFB() == MODE_VERTICAL_ANIMATION || Utils.getConfigModeFB() == MODE_HORIZONTAL_ANIMATION {
- if configAnim == 0 { //left to right
- lastRunAnimationHrz = 1
- } else if configAnim == 1 { //right to left
- lastRunAnimationHrz = -1
- } else if configAnim == 2 { //top to bottom
- lastRunAnimationVrt = 1
- } else if configAnim == 3 { //top to bottom
- lastRunAnimationVrt = -1
- }
- if configAnim >= 0 && configAnim <= 3 {
- checkDelayAnimation()
- }
- }
-
- let qmeraTap = UITapGestureRecognizer(target: self, action: #selector(qmeraTap))
- qmeraTap.numberOfTouchesRequired = 1
- nexilis_button.addGestureRecognizer(qmeraTap)
-
- let qmeraLongPress = UILongPressGestureRecognizer(target: self, action: #selector(qmeraLongPress(gestureRecognizer:)))
- nexilis_button.addGestureRecognizer(qmeraLongPress)
-
- addSubview(nexilis_button)
-
- nexilis_button.widthAnchor.constraint(equalToConstant: defaultWidthFB).isActive = true
- nexilis_button.heightAnchor.constraint(equalToConstant: defaultHeightFB).isActive = true
- nexilis_button.leftAnchor.constraint(equalTo: leftAnchor).isActive = true
- nexilis_button.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true
-
- scrollView = UIScrollView()
- scrollView.translatesAutoresizingMaskIntoConstraints = false
- if Utils.getFBItemBg() == "1" && Utils.getConfigModeFB() == MODE_VERTICAL_FLOATING_BUTTON {
- scrollView.layer.borderWidth = 1.0
- scrollView.layer.borderColor = UIColor.white.cgColor
- scrollView.layer.cornerRadius = 8.0
- scrollView.layer.masksToBounds = true
- scrollView.backgroundColor = .black.withAlphaComponent(0.25)
- }
- addSubview(scrollView)
-
- if Utils.getConfigModeFB() != MODE_HORIZONTAL_SIDE_TAB {
- scrollView.topAnchor.constraint(equalTo: topAnchor).isActive = true
- if Utils.getConfigModeFB() == MODE_VERTICAL_ANIMATION {
- scrollView.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true
- scrollView.leftAnchor.constraint(equalTo: nexilis_button.rightAnchor, constant: -20).isActive = true
- scrollView.widthAnchor.constraint(equalToConstant: defaultWidthHeightMenuFB + 10).isActive = true
- scrollView.isHidden = true
- } else if Utils.getConfigModeFB() == MODE_HORIZONTAL_ANIMATION {
- scrollView.bottomAnchor.constraint(equalTo: nexilis_button.topAnchor).isActive = true
- scrollView.leftAnchor.constraint(equalTo: nexilis_button.leftAnchor).isActive = true
- scrollView.widthAnchor.constraint(equalToConstant: defaultWidthHeightMenuFB * (countMenuFB - 1)).isActive = true
- } else {
- scrollView.widthAnchor.constraint(equalToConstant: defaultWidthHeightMenuFB + 10).isActive = true
- scrollView.centerXAnchor.constraint(equalTo: centerXAnchor).isActive = true
- scrollView.bottomAnchor.constraint(equalTo: nexilis_button.topAnchor).isActive = true
- }
- } else {
- scrollView.widthAnchor.constraint(equalToConstant: UIScreen.main.bounds.width - defaultWidthFB).isActive = true
- scrollView.heightAnchor.constraint(equalToConstant: defaultHeightFB).isActive = true
- scrollView.leftAnchor.constraint(equalTo: nexilis_button.rightAnchor).isActive = true
- scrollView.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true
- scrollView.backgroundColor = .black.withAlphaComponent(0.25)
- }
-
- groupView = UIStackView()
- groupView.translatesAutoresizingMaskIntoConstraints = false
- groupView.axis = (Utils.getConfigModeFB() == MODE_HORIZONTAL_SIDE_TAB || Utils.getConfigModeFB() == MODE_HORIZONTAL_ANIMATION) ? .horizontal : .vertical
- if Utils.getConfigModeFB() != MODE_HORIZONTAL_SIDE_TAB && Utils.getConfigModeFB() != MODE_HORIZONTAL_ANIMATION {
- groupView.distribution = .fillEqually
- }
- scrollView.addSubview(groupView)
- if Utils.getConfigModeFB() == MODE_HORIZONTAL_SIDE_TAB {
- groupView.leftAnchor.constraint(equalTo: scrollView.leftAnchor).isActive = true
- groupView.rightAnchor.constraint(equalTo: scrollView.rightAnchor).isActive = true
- groupView.centerYAnchor.constraint(equalTo: scrollView.centerYAnchor).isActive = true
- groupView.heightAnchor.constraint(equalToConstant: defaultHeightFB - 10).isActive = true
- } else {
- groupView.widthAnchor.constraint(equalToConstant: Utils.getConfigModeFB() == MODE_HORIZONTAL_ANIMATION ? defaultWidthHeightMenuFB * (countMenuFB - 1) : defaultWidthHeightMenuFB).isActive = true
- groupView.topAnchor.constraint(equalTo: scrollView.topAnchor, constant: Utils.getConfigModeFB() == MODE_HORIZONTAL_ANIMATION ? 0 : 5).isActive = true
- groupView.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor, constant: Utils.getConfigModeFB() == MODE_HORIZONTAL_ANIMATION ? 0 : -5).isActive = true
- groupView.leftAnchor.constraint(equalTo: scrollView.leftAnchor, constant: Utils.getConfigModeFB() == MODE_HORIZONTAL_ANIMATION ? 0 : 6).isActive = true
- }
-
- pullButton()
-
- let center: NotificationCenter = NotificationCenter.default
- center.addObserver(self, selector: #selector(imageFBUpdate(notification:)), name: NSNotification.Name(rawValue: "imageFBUpdate"), object: nil)
- center.addObserver(self, selector: #selector(checkCounter), name: NSNotification.Name(rawValue: Nexilis.listenerReceiveChat), object: nil)
- center.addObserver(self, selector: #selector(checkCounter), name: NSNotification.Name(rawValue: "reloadTabChats"), object: nil)
- let tapGesture = UITapGestureRecognizer(target: self, action: #selector(hideButton))
- tapGesture.cancelsTouchesInView = false
- UIApplication.shared.windows.first?.rootViewController?.view.addGestureRecognizer(tapGesture)
- }
-
- private func checkDelayAnimation() {
- DispatchQueue.main.asyncAfter(deadline: .now() + 3, execute: { [self] in
- if !isShow {
- animationTimer.invalidate()
- animationTimer = Timer.scheduledTimer(timeInterval: 0.001, target: self, selector: #selector(runAnimation), userInfo: nil, repeats: true)
- }
- })
- }
-
- @objc func runAnimation(){
- DispatchQueue.main.async { [self] in
- if configAnim == 0 || configAnim == 1 {
- if (lastRunAnimationHrz == -1 && frame.origin.x >= 0) || lastRunAnimationHrz == 1 && frame.origin.x <= UIScreen.main.bounds.width - defaultWidthFB {
- if lastRunAnimationHrz == -1 {
- frame.origin.x-=0.1
- } else {
- frame.origin.x+=0.1
- }
- } else {
- lastRunAnimationHrz = lastRunAnimationHrz == 1 ? -1 : 1
- if (lastRunAnimationHrz == -1 && configAnim == 1) || (lastRunAnimationHrz == 1 && configAnim == 0) {
- animationTimer.invalidate()
- if isLoopingAnim {
- checkDelayAnimation()
- }
- }
- }
- } else {
- if (lastRunAnimationVrt == -1 && frame.origin.y >= 0) || lastRunAnimationVrt == 1 && frame.origin.y <= UIScreen.main.bounds.height - defaultHeightFB {
- if lastRunAnimationVrt == -1 {
- frame.origin.y-=0.1
- } else {
- frame.origin.y+=0.1
- }
- } else {
- lastRunAnimationVrt = lastRunAnimationVrt == 1 ? -1 : 1
- if (lastRunAnimationVrt == -1 && configAnim == 3) || (lastRunAnimationVrt == 1 && configAnim == 2) {
- animationTimer.invalidate()
- if isLoopingAnim {
- checkDelayAnimation()
- }
- }
- }
- }
- }
- }
-
- private func pullButton() {
- if datePull == nil || Int(Date().timeIntervalSince(datePull!)) >= 60 {
- datePull = Date()
- } else if Int(Date().timeIntervalSince(datePull!)) < 60 {
- return
- }
- if !CheckConnection.isConnectedToNetwork() || API.nGetCLXConnState() == 0 {
- let imageView = UIImageView(image: UIImage(systemName: "xmark.circle.fill"))
- imageView.tintColor = .white
- 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)
- banner.show()
- return
- }
- if groupView.subviews.count == 0 {
- getDefaultButton()
- }
- DispatchQueue.global().async { [self] in
- if !Utils.getCustomButtons().isEmpty && Utils.getConfigModeFB() != MODE_HORIZONTAL_SIDE_TAB && Utils.getConfigModeFB() != MODE_HORIZONTAL_ANIMATION {
- DispatchQueue.main.async { [self] in
- groupView.subviews.forEach({ $0.removeFromSuperview() })
- let customButtons = Utils.getCustomButtons().components(separatedBy: ",")
- let customIcons = Utils.getCustomFBIcon().components(separatedBy: ",")
- countMenuFB = CGFloat(customButtons.count > 5 ? 5 : customButtons.count)
- for i in 0..<customButtons.count {
- let package_id = customButtons[i]
- let app_id = ""
- let icon = customIcons[i]
- let newButton = UIButton()
- newButton.heightAnchor.constraint(equalToConstant: defaultWidthHeightMenuFB).isActive = true
- newButton.translatesAutoresizingMaskIntoConstraints = false
- DispatchQueue.global().async {
- let data = try? Data(contentsOf: URL(string: Utils.getURLBase() + "get_file_from_path?img=\(icon)")!) //make sure your image in this url does exist, otherwise unwrap in a if let check / try-catch
- DispatchQueue.main.async {
- if data != nil {
- if let image = UIImage(data: data!) {
- newButton.setImage(image, for: .normal)
- }
- }
- }
- }
- groupView.addArrangedSubview(newButton)
- newButton.restorationIdentifier = package_id
- newButton.accessibilityIdentifier = app_id
- newButton.addTarget(self, action: #selector(fbTap), for: .touchUpInside)
- }
- }
- return
- }
- if let response = Nexilis.writeSync(message: CoreMessage_TMessageBank.pullFloatingButton(), timeout: 30 * 1000){
- if response.isOk() {
- let data = response.getBody(key: CoreMessage_TMessageKey.DATA, default_value: "")
- if !data.isEmpty {
- if let jsonArray = try! JSONSerialization.jsonObject(with: data.data(using: String.Encoding.utf8)!, options: JSONSerialization.ReadingOptions()) as? [AnyObject] {
- DispatchQueue.main.async { [self] in
- groupView.subviews.forEach({ $0.removeFromSuperview() })
- if jsonArray.count == 0 {
- countMenuFB = CGFloat(4)
- getDefaultButton()
- } else {
- if Utils.getConfigModeFB() != MODE_HORIZONTAL_ANIMATION {
- countMenuFB = CGFloat(jsonArray.count > 5 ? 5 : jsonArray.count)
- } else {
- countMenuFB = CGFloat(jsonArray.filter({ $0["mode"] as? Int == Int(MODE_HORIZONTAL_ANIMATION) }).count)
- }
- for json in jsonArray {
- let package_id = json["package_id"] as! String
- let app_id = (json["app_id"] as? String) ?? ""
- let icon = (json["icon"] as? String) ?? ""
- let mode = "\((json["mode"] as? Int) ?? 1)"
- let newButton = UIButton()
- if mode != Utils.getConfigModeFB() {
- continue
- }
- if mode == MODE_HORIZONTAL_SIDE_TAB {
- newButton.widthAnchor.constraint(equalToConstant: defaultHeightFB - 10).isActive = true
- newButton.heightAnchor.constraint(equalToConstant: defaultHeightFB - 10).isActive = true
- } else if mode == MODE_HORIZONTAL_ANIMATION {
- newButton.widthAnchor.constraint(equalToConstant: defaultWidthHeightMenuFB).isActive = true
- newButton.heightAnchor.constraint(equalToConstant: defaultWidthHeightMenuFB).isActive = true
- } else {
- newButton.heightAnchor.constraint(equalToConstant: defaultWidthHeightMenuFB).isActive = true
- }
- newButton.translatesAutoresizingMaskIntoConstraints = false
- var indexTap = 0
- if package_id.contains("_fb"){
- let numIdx = package_id.split(separator: "_")[1]
- indexTap = Int(String(numIdx).substring(from: 2, to: numIdx.count))!
- }
- if indexTap == Nexilis.IDX_CHAT {
- 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)
- } else if indexTap == Nexilis.IDX_CONVERSATION {
- 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)
- } else if indexTap == Nexilis.IDX_CALL {
- 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)
- } else if indexTap == Nexilis.IDX_CC {
- 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)
- } else if indexTap == Nexilis.IDX_STREAM {
- 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)
- } else if indexTap == Nexilis.IDX_SOCIAL_COMMERCE {
- 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)
- } else if indexTap == Nexilis.IDX_NEWS {
- 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)
- } else if indexTap == Nexilis.IDX_POST {
- 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)
- } else if indexTap == Nexilis.IDX_NOTIF_CENTER {
- 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)
- } else {
- 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)
- }
- if !icon.isEmpty {
- DispatchQueue.global().async {
- let data = try? Data(contentsOf: URL(string: "https://nexilis.io/get_file?account=\(Nexilis.sAPIKey)&image=\(icon)")!) //make sure your image in this url does exist, otherwise unwrap in a if let check / try-catch
- DispatchQueue.main.async {
- if data != nil {
- newButton.setImage(UIImage(data: data!), for: .normal)
- }
- }
- }
- }
- groupView.addArrangedSubview(newButton)
- newButton.restorationIdentifier = package_id
- newButton.accessibilityIdentifier = app_id
- newButton.addTarget(self, action: #selector(fbTap), for: .touchUpInside)
- }
- }
- }
- }
- } else {
- groupView.subviews.forEach({ $0.removeFromSuperview() })
- getDefaultButton()
- }
- } else {
- groupView.subviews.forEach({ $0.removeFromSuperview() })
- getDefaultButton()
- }
- }
- }
- }
-
- func getDefaultButton() {
- let data = [Nexilis.IDX_NOTIF_CENTER, Nexilis.IDX_CC, Nexilis.IDX_CONVERSATION, Nexilis.IDX_CALL, Nexilis.IDX_STREAM]
- for i in 0..<data.count {
- let newButton = UIButton()
- let mode = Utils.getConfigModeFB()
- if mode == MODE_HORIZONTAL_SIDE_TAB {
- newButton.widthAnchor.constraint(equalToConstant: defaultHeightFB - 10).isActive = true
- newButton.heightAnchor.constraint(equalToConstant: defaultHeightFB - 10).isActive = true
- } else if mode == MODE_HORIZONTAL_ANIMATION {
- newButton.widthAnchor.constraint(equalToConstant: defaultWidthHeightMenuFB).isActive = true
- newButton.heightAnchor.constraint(equalToConstant: defaultWidthHeightMenuFB).isActive = true
- } else {
- newButton.heightAnchor.constraint(equalToConstant: defaultWidthHeightMenuFB).isActive = true
- }
- newButton.translatesAutoresizingMaskIntoConstraints = false
- let indexTap = data[i]
- if indexTap == Nexilis.IDX_CHAT {
- 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)
- } else if indexTap == Nexilis.IDX_CONVERSATION {
- 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)
- } else if indexTap == Nexilis.IDX_CALL {
- 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)
- } else if indexTap == Nexilis.IDX_CC {
- 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)
- } else if indexTap == Nexilis.IDX_STREAM {
- 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)
- } else if indexTap == Nexilis.IDX_SOCIAL_COMMERCE {
- 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)
- } else if indexTap == Nexilis.IDX_NEWS {
- 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)
- } else if indexTap == Nexilis.IDX_POST {
- 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)
- } else if indexTap == Nexilis.IDX_NOTIF_CENTER {
- 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)
- } else {
- 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)
- }
- groupView.addArrangedSubview(newButton)
- newButton.restorationIdentifier = "default_fb\(data[i])"
- newButton.accessibilityIdentifier = ""
- newButton.addTarget(self, action: #selector(fbTap), for: .touchUpInside)
- }
- }
-
- @objc func draggedView(_ sender:UIPanGestureRecognizer){
- let size = UIScreen.main.bounds
- let widthScreen = size.width
- let heightScreen = size.height
- let minimumx: CGFloat = Utils.getConfigModeFB() == MODE_HORIZONTAL_ANIMATION && isShow ? 80 : Utils.getConfigModeFB() == MODE_VERTICAL_ANIMATION && isShow ? 60 : 40
- let maximumx = Utils.getConfigModeFB() == MODE_HORIZONTAL_ANIMATION && isShow ? widthScreen - frame.width + 20 : Utils.getConfigModeFB() == MODE_VERTICAL_ANIMATION && isShow ? widthScreen - 10 - defaultWidthHeightMenuFB : widthScreen - 40
- let maxMinXSideTab = isShow ? center.x : widthScreen + (center.x - widthScreen)
- let translation = sender.translation(in: self)
- var xPos = center.x + translation.x
- var yPos = center.y + translation.y
- bringSubviewToFront(self)
- if Utils.getConfigModeFB() == MODE_HORIZONTAL_SIDE_TAB {
- xPos = maxMinXSideTab
- } else {
- if (xPos < minimumx) {
- xPos = minimumx
- }
- if (xPos > maximumx) {
- xPos = maximumx
- }
- }
- if(isShow) {
- let minimumy = Utils.getConfigModeFB() == MODE_HORIZONTAL_SIDE_TAB ? 50 : (defaultWidthHeightMenuFB * countMenuFB) - defaultHeightFB - 10
- let maximumy = Utils.getConfigModeFB() == MODE_HORIZONTAL_SIDE_TAB ? heightScreen - 50 : heightScreen - defaultHeightFB - 10
- if(yPos < minimumy) {
- yPos = minimumy
- }
- if(yPos > maximumy) {
- yPos = maximumy
- }
- } else {
- let minimumy: CGFloat = 50
- let maximumy = heightScreen - 50
- if(yPos < minimumy) {
- yPos = minimumy
- }
- if(yPos > maximumy) {
- yPos = maximumy
- }
- }
- center = CGPoint(x: xPos, y: yPos)
- sender.setTranslation(CGPoint.zero, in: self)
- if lastPosY != nil {
- lastPosY = nil
- }
- UserDefaults.standard.set(center.x, forKey: "xlastPosFB")
- UserDefaults.standard.set(center.y, forKey: "ylastPosFB")
- }
-
- @objc func imageFBUpdate(notification: NSNotification) {
-
- }
-
- @objc func checkCounter() {
- let counter = queryCountCounter()
- if counter > 0 {
- DispatchQueue.main.async { [self] in
- if button_fb2 != nil && !indicatorCounterFB.isDescendant(of: button_fb2) {
- button_fb2.addSubview(indicatorCounterFB)
- indicatorCounterFB.layer.cornerRadius = 7.5
- indicatorCounterFB.layer.masksToBounds = true
- indicatorCounterFB.backgroundColor = .systemRed
- indicatorCounterFB.anchor(top: button_fb2.topAnchor, left: button_fb2.leftAnchor, height: 15, minWidth: 15, maxWidth: 20)
- indicatorCounterFB.addSubview(labelCounterFB)
- labelCounterFB.anchor(left: indicatorCounterFB.leftAnchor, right: indicatorCounterFB.rightAnchor, paddingLeft: 5, paddingRight: 5, centerX: indicatorCounterFB.centerXAnchor, centerY: indicatorCounterFB.centerYAnchor)
- labelCounterFB.font = .systemFont(ofSize: 10)
- labelCounterFB.textColor = .white
- }
- if !indicatorCounterFBBig.isDescendant(of: nexilis_button){
- nexilis_button.addSubview(indicatorCounterFBBig)
- indicatorCounterFBBig.tintColor = .systemRed
- indicatorCounterFBBig.image = UIImage(systemName: "staroflife.circle.fill")
- indicatorCounterFBBig.anchor(top: nexilis_button.topAnchor, left: nexilis_button.leftAnchor, paddingTop: 5, paddingLeft: 5, width: 15, height: 15)
- }
- labelCounterFB.text = "\(counter)"
- }
- } else {
- DispatchQueue.main.async { [self] in
- if button_fb2 != nil && indicatorCounterFB.isDescendant(of: button_fb2) {
- indicatorCounterFB.removeFromSuperview()
- }
- if indicatorCounterFBBig.isDescendant(of: nexilis_button) {
- indicatorCounterFBBig.removeFromSuperview()
- }
- }
- }
- }
-
- private func queryCountCounter() -> Int32 {
- var counter: Int32?
- Database.shared.database?.inTransaction({ (fmdb, rollback) in
- if let cursor = Database.shared.getRecords(fmdb: fmdb, query: "SELECT SUM(counter) FROM MESSAGE_SUMMARY"), cursor.next() {
- counter = cursor.int(forColumnIndex: 0)
- cursor.close()
- }
- })
- return counter ?? 0
- }
-
- @objc func qmeraTap() {
- show(isShow: !isShow)
- }
-
- @objc func fbTap(_ sender: UIButton) {
- let package_id = sender.restorationIdentifier!
- var app_id = sender.accessibilityIdentifier!
- var indexTap = 0
- if package_id.contains("_fb"){
- let listSplit = package_id.split(separator: "_", maxSplits: 1)
- let numIdx = listSplit[listSplit.firstIndex(where: { $0.contains("fb") }) ?? 0]
- indexTap = Int(String(numIdx).substring(from: 2, to: numIdx.count))!
- if listSplit.count == 2 {
- app_id = String(listSplit[1])
- }
- }
- Nexilis.buttonClicked(index: indexTap, id: app_id)
- hideButton()
- }
-
- @objc func qmeraLongPress(gestureRecognizer: UILongPressGestureRecognizer) {
- if gestureRecognizer.state == .began {
- if mySettingDelegate != nil {
- mySettingDelegate?.settingDelegate()
- } else {
- let navigationController = AppStoryBoard.Palio.instance.instantiateViewController(withIdentifier: "settingNav") as! UINavigationController
- navigationController.modalPresentationStyle = .fullScreen
- navigationController.navigationBar.tintColor = .white
- navigationController.navigationBar.barTintColor = self.traitCollection.userInterfaceStyle == .dark ? .blackDarkMode : .mainColor
- navigationController.navigationBar.isTranslucent = false
- navigationController.navigationBar.overrideUserInterfaceStyle = .dark
- navigationController.navigationBar.barStyle = .black
- let cancelButtonAttributes: [NSAttributedString.Key: Any] = [NSAttributedString.Key.foregroundColor: UIColor.white, NSAttributedString.Key.font : UIFont.systemFont(ofSize: 16)]
- UIBarButtonItem.appearance().setTitleTextAttributes(cancelButtonAttributes, for: .normal)
- let textAttributes = [NSAttributedString.Key.foregroundColor:UIColor.white]
- navigationController.navigationBar.titleTextAttributes = textAttributes
- navigationController.view.backgroundColor = self.traitCollection.userInterfaceStyle == .dark ? .blackDarkMode : .mainColor
- UIApplication.shared.rootViewController?.present(navigationController, animated: true, completion: nil)
- }
- hideButton()
- }
- }
-
- @objc public func hideButton() {
- animationTimer.invalidate()
- if isShow {
- show(isShow: false)
- }
- if self.frame.origin.x < UIScreen.main.bounds.width / 2 - 30 {
- self.frame.origin.x = 0
- } else {
- self.frame.origin.x = UIScreen.main.bounds.width - defaultWidthFB
- }
- }
-
- public func show(isShow: Bool) {
- self.isShow = isShow
- if isShow {
- animationTimer.invalidate()
- pullButton()
- if indicatorCounterFBBig.isDescendant(of: nexilis_button) {
- indicatorCounterFBBig.isHidden = true
- }
- var height = CGFloat((defaultWidthHeightMenuFB * countMenuFB) + defaultHeightFB + 5) //defaultWidthHeightMenuFB
- var width = frame.width
- var xPosition = frame.origin.x
- if Utils.getConfigModeFB() == MODE_VERTICAL_ANIMATION {
- height = CGFloat((defaultWidthHeightMenuFB * (countMenuFB - 2)) + defaultHeightFB - 5)
- width = frame.width + defaultWidthHeightMenuFB
- if xPosition > UIScreen.main.bounds.width - defaultWidthFB - defaultWidthHeightMenuFB {
- xPosition = UIScreen.main.bounds.width - defaultWidthFB - defaultWidthHeightMenuFB
- }
- scrollView.isHidden = false
- } else if Utils.getConfigModeFB() == MODE_HORIZONTAL_ANIMATION {
- height = defaultHeightFB + defaultWidthHeightMenuFB
- width = defaultWidthHeightMenuFB * (countMenuFB - 1)
- if xPosition > UIScreen.main.bounds.width - defaultWidthHeightMenuFB * (countMenuFB) {
- xPosition = UIScreen.main.bounds.width - defaultWidthHeightMenuFB * (countMenuFB)
- }
- }
- var yPosition = frame.origin.y - height + defaultHeightFB
- if yPosition <= 25 {
- lastPosY = frame.origin.y
- yPosition = 25
- }
- if Utils.getConfigModeFB() != MODE_HORIZONTAL_SIDE_TAB {
- frame = CGRect(x: xPosition, y: yPosition, width: width, height: height)
- } else {
- UIView.animate(withDuration: 0.5, animations: {
- self.frame.origin.x = 0
- })
- }
- DispatchQueue.main.asyncAfter(deadline: .now() + 0.5, execute: { [self] in
- if isShow {
- let countSubviewsAfter = groupView.subviews.count
- if countSubviewsAfter > 5 {
- scrollView.flashScrollIndicators()
- }
- }
- })
- } else {
- if indicatorCounterFBBig.isDescendant(of: nexilis_button) {
- indicatorCounterFBBig.isHidden = false
- }
- var height = CGFloat((defaultWidthHeightMenuFB * countMenuFB) + defaultHeightFB + 5) //defaultWidthHeightMenuFB
- var width = defaultWidthFB
- if Utils.getConfigModeFB() == MODE_VERTICAL_ANIMATION {
- height = CGFloat((defaultWidthHeightMenuFB * 3) + defaultHeightFB - 5)
- scrollView.isHidden = true
- } else if Utils.getConfigModeFB() == MODE_HORIZONTAL_ANIMATION {
- height = defaultHeightFB + defaultWidthHeightMenuFB
- width = defaultWidthFB
- }
- var yPosition = frame.origin.y + height - defaultHeightFB
- if lastPosY != nil {
- yPosition = lastPosY!
- }
- if Utils.getConfigModeFB() != MODE_HORIZONTAL_SIDE_TAB {
- frame = CGRect(x: frame.origin.x, y: yPosition, width: width, height: defaultHeightFB)
- } else {
- UIView.animate(withDuration: 0.5, animations: { [self] in
- frame.origin.x = UIScreen.main.bounds.width - defaultWidthFB
- })
- }
- if Utils.getConfigModeFB() == MODE_VERTICAL_ANIMATION || Utils.getConfigModeFB() == MODE_HORIZONTAL_ANIMATION {
- checkDelayAnimation()
- }
- }
- }
- }
- public protocol SettingMABDelegate: AnyObject {
- func settingDelegate()
- }
|