NCMainTabBar.swift 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. //
  2. // NCMainTabBar.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 06/01/2020.
  6. // Copyright © 2020 Marino Faggiana. All rights reserved.
  7. //
  8. // Author Marino Faggiana <marino.faggiana@nextcloud.com>
  9. //
  10. // This program is free software: you can redistribute it and/or modify
  11. // it under the terms of the GNU General Public License as published by
  12. // the Free Software Foundation, either version 3 of the License, or
  13. // (at your option) any later version.
  14. //
  15. // This program is distributed in the hope that it will be useful,
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. // GNU General Public License for more details.
  19. //
  20. // You should have received a copy of the GNU General Public License
  21. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. //
  23. import Foundation
  24. @IBDesignable class NCMainTabBar: UITabBar {
  25. private var fillColor: UIColor!
  26. private var shapeLayer: CALayer?
  27. private let appDelegate = UIApplication.shared.delegate as! AppDelegate
  28. override var traitCollection: UITraitCollection {
  29. return UITraitCollection(horizontalSizeClass: .compact)
  30. }
  31. override var backgroundColor: UIColor? {
  32. get {
  33. return self.fillColor
  34. }
  35. set {
  36. fillColor = newValue
  37. self.setNeedsDisplay()
  38. }
  39. }
  40. override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
  41. let button = self.viewWithTag(99)
  42. if self.bounds.contains(point) || (button != nil && button!.frame.contains(point)) {
  43. return true
  44. } else {
  45. return false
  46. }
  47. }
  48. override func layoutSubviews() {
  49. super.layoutSubviews()
  50. layer.shadowPath = createPath()
  51. layer.shadowRadius = 5
  52. layer.shadowOffset = .zero
  53. layer.shadowOpacity = 0.25
  54. }
  55. override func draw(_ rect: CGRect) {
  56. addShape()
  57. createButtons()
  58. }
  59. private func addShape() {
  60. let shapeLayer = CAShapeLayer()
  61. shapeLayer.path = createPath()
  62. shapeLayer.fillColor = backgroundColor?.cgColor
  63. if let oldShapeLayer = self.shapeLayer {
  64. self.layer.replaceSublayer(oldShapeLayer, with: shapeLayer)
  65. } else {
  66. self.layer.insertSublayer(shapeLayer, at: 0)
  67. }
  68. self.shapeLayer = shapeLayer
  69. }
  70. private func createPath() -> CGPath {
  71. let height: CGFloat = 28
  72. let margin: CGFloat = 8
  73. let path = UIBezierPath()
  74. let centerWidth = self.frame.width / 2
  75. path.move(to: CGPoint(x: 0, y: 0)) // start top left
  76. path.addLine(to: CGPoint(x: (centerWidth - height - margin), y: 0)) // the beginning of the trough
  77. // first curve down
  78. path.addArc(withCenter: CGPoint(x: centerWidth, y: 0), radius: height + margin, startAngle: CGFloat(180 * Double.pi / 180), endAngle: CGFloat(0 * Double.pi / 180), clockwise: false)
  79. // complete the rect
  80. path.addLine(to: CGPoint(x: self.frame.width, y: 0))
  81. path.addLine(to: CGPoint(x: self.frame.width, y: self.frame.height))
  82. path.addLine(to: CGPoint(x: 0, y: self.frame.height))
  83. path.close()
  84. return path.cgPath
  85. }
  86. private func createButtons() {
  87. // File
  88. if let item = items?[Int(k_tabBarApplicationIndexFile)] {
  89. item.title = NSLocalizedString("_home_", comment: "")
  90. item.image = CCGraphics.changeThemingColorImage(UIImage(named: "tabBarFiles"), width: 50, height: 50, color: NCBrandColor.sharedInstance.brandElement)
  91. item.selectedImage = item.image
  92. }
  93. // Favorite
  94. if let item = items?[Int(k_tabBarApplicationIndexFavorite)] {
  95. item.title = NSLocalizedString("_favorites_", comment: "")
  96. item.image = CCGraphics.changeThemingColorImage(UIImage(named: "favorite"), width: 50, height: 50, color: NCBrandColor.sharedInstance.brandElement)
  97. item.selectedImage = item.image
  98. }
  99. // +
  100. if let item = items?[Int(k_tabBarApplicationIndexPlusHide)] {
  101. item.title = ""
  102. item.image = nil
  103. item.isEnabled = false
  104. }
  105. // Media
  106. if let item = items?[Int(k_tabBarApplicationIndexMedia)] {
  107. item.title = NSLocalizedString("_media_", comment: "")
  108. item.image = CCGraphics.changeThemingColorImage(UIImage(named: "media"), width: 50, height: 50, color: NCBrandColor.sharedInstance.brandElement)
  109. item.selectedImage = item.image
  110. }
  111. // More
  112. if let item = items?[Int(k_tabBarApplicationIndexMore)] {
  113. item.title = NSLocalizedString("_more_", comment: "")
  114. item.image = CCGraphics.changeThemingColorImage(UIImage(named: "tabBarMore"), width: 50, height: 50, color: NCBrandColor.sharedInstance.brandElement)
  115. item.selectedImage = item.image
  116. }
  117. // Center button
  118. if let centerButton = self.viewWithTag(99) {
  119. centerButton.removeFromSuperview()
  120. }
  121. let centerButtonHeight: CGFloat = 57
  122. let centerButtonY: CGFloat = -28
  123. let centerButton = UIButton(frame: CGRect(x: (self.bounds.width / 2)-(centerButtonHeight/2), y: centerButtonY, width: centerButtonHeight, height: centerButtonHeight))
  124. centerButton.setTitle("", for: .normal)
  125. centerButton.setImage(CCGraphics.changeThemingColorImage(UIImage(named: "tabBarPlus"), width: 100, height: 100, color: .white), for: .normal)
  126. centerButton.backgroundColor = NCBrandColor.sharedInstance.brandElement
  127. centerButton.tintColor = UIColor.white
  128. centerButton.tag = 99
  129. centerButton.accessibilityLabel = NSLocalizedString("_accessibility_add_upload_", comment: "")
  130. centerButton.layer.cornerRadius = centerButton.frame.size.width / 2.0
  131. centerButton.layer.masksToBounds = false
  132. centerButton.layer.shadowOffset = CGSize(width: 0, height: 0)
  133. centerButton.layer.shadowRadius = 3.0
  134. centerButton.layer.shadowOpacity = 0.5
  135. centerButton.addTarget(self, action: #selector(self.centerButtonAction), for: .touchUpInside)
  136. self.addSubview(centerButton)
  137. }
  138. // Menu Button Touch Action
  139. @objc func centerButtonAction(sender: UIButton) {
  140. if appDelegate.maintenanceMode { return }
  141. if let directory = NCManageDatabase.sharedInstance.getTableDirectory(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@", appDelegate.account, appDelegate.activeServerUrl)) {
  142. if !directory.permissions.contains("CK") {
  143. NCContentPresenter.shared.messageNotification("_warning_", description: "_no_permission_add_file_", delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.info, errorCode: Int(k_CCErrorInternalError))
  144. return
  145. }
  146. }
  147. if let viewController = self.window?.rootViewController {
  148. appDelegate.showMenuIn(viewController: viewController)
  149. }
  150. }
  151. }