ConfigureFloatingButton.swift 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. //
  2. // ConfigureFloatingButton.swift
  3. // NexilisLite
  4. //
  5. // Created by Akhmad Al Qindi Irsyam on 19/09/23.
  6. //
  7. import UIKit
  8. public class ConfigureFloatingButton: UIViewController {
  9. let subContainerView = UIView()
  10. let titleConfigureFB = UILabel()
  11. var chosenMode: String = "1"
  12. var fullMode: [String] = ["1", "2", "3", "4", "5"]
  13. var dataMode: [String] = ["1. Floating Button (Vertical Mode)".localized(),"2. Animation (Vertical Mode)".localized(),"3. Animation (Horizontal Mode)".localized(),"4. Side-Tab (Horizontal Mode)".localized(), "5. Side-Tab (Vertical Mode)".localized()]
  14. var paddingTopFinishButton = NSLayoutConstraint()
  15. let buttonSet = UIButton()
  16. let modeButton = UIView()
  17. var paddingBottomSLButton = NSLayoutConstraint()
  18. var showPickerInt = 0
  19. let chosenModeTitle = UILabel()
  20. public override func viewDidLoad() {
  21. super.viewDidLoad()
  22. getInisialData()
  23. view.backgroundColor = .black.withAlphaComponent(0.3)
  24. let containerView = UIView()
  25. view.addSubview(containerView)
  26. containerView.anchor(centerX: view.centerXAnchor, centerY: view.centerYAnchor, width: view.bounds.width - 40, minHeight: 100, maxHeight: view.bounds.height - 100)
  27. containerView.backgroundColor = .white.withAlphaComponent(0.9)
  28. containerView.layer.cornerRadius = 15.0
  29. containerView.clipsToBounds = true
  30. subContainerView.backgroundColor = .clear
  31. containerView.addSubview(subContainerView)
  32. subContainerView.anchor(top: containerView.topAnchor, left: containerView.leftAnchor, bottom: containerView.bottomAnchor, right: containerView.rightAnchor, paddingTop: 20.0, paddingLeft: 10.0, paddingBottom: 20.0, paddingRight: 10.0)
  33. let buttonClose = UIButton(type: .close)
  34. buttonClose.frame.size = CGSize(width: 30, height: 30)
  35. buttonClose.layer.cornerRadius = 15.0
  36. buttonClose.clipsToBounds = true
  37. buttonClose.backgroundColor = .secondaryColor.withAlphaComponent(0.5)
  38. buttonClose.addTarget(self, action: #selector(close), for: .touchUpInside)
  39. containerView.addSubview(buttonClose)
  40. buttonClose.anchor(top: containerView.topAnchor, right: containerView.rightAnchor, width: 30, height: 30)
  41. titleConfigureFB.font = .systemFont(ofSize: 18, weight: .bold)
  42. titleConfigureFB.text = "Configure Floating Button".localized()
  43. titleConfigureFB.textAlignment = .center
  44. subContainerView.addSubview(titleConfigureFB)
  45. titleConfigureFB.anchor(top: subContainerView.topAnchor, left: subContainerView.leftAnchor, right: subContainerView.rightAnchor)
  46. makeFinish()
  47. makeButtonMode()
  48. }
  49. private func getInisialData() {
  50. chosenMode = Utils.getConfigModeFB()
  51. }
  52. @objc func close() {
  53. self.dismiss(animated: true)
  54. }
  55. func makeFinish() {
  56. buttonSet.backgroundColor = .black
  57. buttonSet.setImage(UIImage(systemName: "checkmark"), for: .normal)
  58. buttonSet.tintColor = .white
  59. buttonSet.setTitle("Set".localized(), for: .normal)
  60. buttonSet.titleLabel?.font = .boldSystemFont(ofSize: 15)
  61. buttonSet.layer.cornerRadius = 8
  62. subContainerView.addSubview(buttonSet)
  63. buttonSet.anchor(bottom: subContainerView.bottomAnchor, right: subContainerView.rightAnchor, width: (self.view.bounds.width / 2) - 55, height: 40)
  64. paddingTopFinishButton = buttonSet.topAnchor.constraint(equalTo: titleConfigureFB.bottomAnchor, constant: 10)
  65. paddingTopFinishButton.isActive = true
  66. buttonSet.addTarget(self, action: #selector(setFB), for: .touchUpInside)
  67. }
  68. func makeButtonMode() {
  69. modeButton.backgroundColor = .white
  70. modeButton.isUserInteractionEnabled = true
  71. subContainerView.addSubview(modeButton)
  72. modeButton.anchor(top: titleConfigureFB.bottomAnchor, left: subContainerView.leftAnchor, right: subContainerView.rightAnchor, paddingTop: 10, height: 40)
  73. modeButton.layer.cornerRadius = 10
  74. modeButton.layer.masksToBounds = true
  75. let titleTypeButton = UILabel()
  76. titleTypeButton.font = .systemFont(ofSize: 14, weight: .bold)
  77. titleTypeButton.text = "New Mode".localized()
  78. titleTypeButton.textColor = .gray
  79. modeButton.addSubview(titleTypeButton)
  80. titleTypeButton.anchor(left: modeButton.leftAnchor, paddingLeft: 10, centerY: modeButton.centerYAnchor)
  81. let accessoryType = UIImageView()
  82. accessoryType.image = UIImage(systemName: "chevron.right")
  83. accessoryType.tintColor = .gray
  84. modeButton.addSubview(accessoryType)
  85. accessoryType.anchor(right: modeButton.rightAnchor, paddingRight: 10, centerY: modeButton.centerYAnchor)
  86. chosenModeTitle.font = .systemFont(ofSize: 14)
  87. chosenModeTitle.text = dataMode[Int(chosenMode)! - 1]
  88. chosenModeTitle.textColor = .gray
  89. modeButton.addSubview(chosenModeTitle)
  90. modeButton.tag = 0
  91. chosenModeTitle.anchor(right: accessoryType.leftAnchor, paddingLeft: 15, centerY: modeButton.centerYAnchor)
  92. let tap = UITapGestureRecognizer(target: self, action: #selector(showPicker(sender:)))
  93. modeButton.addGestureRecognizer(tap)
  94. let constPaddingTop = 55
  95. paddingTopFinishButton.constant = CGFloat(constPaddingTop)
  96. }
  97. @objc func setFB() {
  98. Utils.setConfigModeFB(value: chosenMode)
  99. DispatchQueue.main.async {
  100. Nexilis.floatingButton.removeFromSuperview()
  101. Nexilis.floatingButton = FloatingButton()
  102. let viewController = (UIApplication.shared.windows.first?.rootViewController)!
  103. Nexilis.addFB(viewController: viewController, fromMAB: true)
  104. }
  105. close()
  106. }
  107. @objc func showPicker(sender: UITapGestureRecognizer) {
  108. showPickerInt = sender.view!.tag
  109. let index = fullMode.firstIndex(of: chosenMode)!
  110. let titleChooser = "New Mode".localized()
  111. let pickerView = UIPickerView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width - 10, height: 100))
  112. pickerView.dataSource = self
  113. pickerView.delegate = self
  114. let vc = UIViewController()
  115. vc.preferredContentSize = CGSize(width: UIScreen.main.bounds.width - 10, height: 100)
  116. pickerView.selectRow(index, inComponent: 0, animated: false)
  117. vc.view.addSubview(pickerView)
  118. pickerView.translatesAutoresizingMaskIntoConstraints = false
  119. pickerView.centerXAnchor.constraint(equalTo: vc.view.centerXAnchor).isActive = true
  120. pickerView.centerYAnchor.constraint(equalTo: vc.view.centerYAnchor).isActive = true
  121. let alert = LibAlertController(title: titleChooser, message: "", preferredStyle: .actionSheet)
  122. alert.setValue(vc, forKey: "contentViewController")
  123. alert.addAction(UIAlertAction(title: "Cancel".localized(), style: .cancel, handler: { (UIAlertAction) in
  124. }))
  125. alert.addAction(UIAlertAction(title: "Select".localized(), style: .default, handler: { [self] (UIAlertAction) in
  126. let selectedIndex = pickerView.selectedRow(inComponent: 0)
  127. chosenMode = fullMode[selectedIndex]
  128. chosenModeTitle.text = dataMode[selectedIndex]
  129. }))
  130. self.present(alert, animated: true, completion: nil)
  131. }
  132. }
  133. extension ConfigureFloatingButton: UIPickerViewDelegate, UIPickerViewDataSource {
  134. public func numberOfComponents(in pickerView: UIPickerView) -> Int {
  135. 1
  136. }
  137. public func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
  138. return dataMode.count
  139. }
  140. public func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
  141. return dataMode[row]
  142. }
  143. }