NCMainTabBar.swift 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. required init?(coder: NSCoder) {
  32. super.init(coder: coder)
  33. NotificationCenter.default.addObserver(self, selector: #selector(changeTheming), name: NSNotification.Name(rawValue: k_notificationCenter_changeTheming), object: nil)
  34. }
  35. override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
  36. super.traitCollectionDidChange(previousTraitCollection)
  37. if #available(iOS 13.0, *) {
  38. if CCUtility.getDarkModeDetect() {
  39. if traitCollection.userInterfaceStyle == .dark {
  40. CCUtility.setDarkMode(true)
  41. } else {
  42. CCUtility.setDarkMode(false)
  43. }
  44. }
  45. changeTheming()
  46. }
  47. }
  48. @objc func changeTheming() {
  49. barTintColor = NCBrandColor.sharedInstance.backgroundView
  50. backgroundColor = NCBrandColor.sharedInstance.tabBar
  51. tintColor = NCBrandColor.sharedInstance.brandElement
  52. if let centerButton = self.viewWithTag(99) {
  53. centerButton.backgroundColor = NCBrandColor.sharedInstance.brandElement
  54. }
  55. }
  56. override var backgroundColor: UIColor? {
  57. get {
  58. return self.fillColor
  59. }
  60. set {
  61. fillColor = newValue
  62. self.setNeedsDisplay()
  63. }
  64. }
  65. override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
  66. let button = self.viewWithTag(99)
  67. if self.bounds.contains(point) || (button != nil && button!.frame.contains(point)) {
  68. return true
  69. } else {
  70. return false
  71. }
  72. }
  73. override func layoutSubviews() {
  74. super.layoutSubviews()
  75. layer.shadowPath = createPath()
  76. layer.shadowRadius = 5
  77. layer.shadowOffset = .zero
  78. layer.shadowOpacity = 0.25
  79. }
  80. override func draw(_ rect: CGRect) {
  81. addShape()
  82. createButtons()
  83. }
  84. private func addShape() {
  85. let shapeLayer = CAShapeLayer()
  86. shapeLayer.path = createPath()
  87. shapeLayer.fillColor = backgroundColor?.cgColor
  88. if let oldShapeLayer = self.shapeLayer {
  89. self.layer.replaceSublayer(oldShapeLayer, with: shapeLayer)
  90. } else {
  91. self.layer.insertSublayer(shapeLayer, at: 0)
  92. }
  93. self.shapeLayer = shapeLayer
  94. }
  95. private func createPath() -> CGPath {
  96. let height: CGFloat = 28
  97. let margin: CGFloat = 8
  98. let path = UIBezierPath()
  99. let centerWidth = self.frame.width / 2
  100. path.move(to: CGPoint(x: 0, y: 0)) // start top left
  101. path.addLine(to: CGPoint(x: (centerWidth - height - margin), y: 0)) // the beginning of the trough
  102. // first curve down
  103. 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)
  104. // complete the rect
  105. path.addLine(to: CGPoint(x: self.frame.width, y: 0))
  106. path.addLine(to: CGPoint(x: self.frame.width, y: self.frame.height))
  107. path.addLine(to: CGPoint(x: 0, y: self.frame.height))
  108. path.close()
  109. return path.cgPath
  110. }
  111. private func createButtons() {
  112. // File
  113. if let item = items?[Int(k_tabBarApplicationIndexFile)] {
  114. item.title = NSLocalizedString("_home_", comment: "")
  115. item.image = CCGraphics.changeThemingColorImage(UIImage(named: "tabBarFiles"), width: 50, height: 50, color: NCBrandColor.sharedInstance.brandElement)
  116. item.selectedImage = item.image
  117. }
  118. // Favorite
  119. if let item = items?[Int(k_tabBarApplicationIndexFavorite)] {
  120. item.title = NSLocalizedString("_favorites_", comment: "")
  121. item.image = CCGraphics.changeThemingColorImage(UIImage(named: "favorite"), width: 50, height: 50, color: NCBrandColor.sharedInstance.brandElement)
  122. item.selectedImage = item.image
  123. }
  124. // +
  125. if let item = items?[Int(k_tabBarApplicationIndexPlusHide)] {
  126. item.title = ""
  127. item.image = nil
  128. item.isEnabled = false
  129. }
  130. // Media
  131. if let item = items?[Int(k_tabBarApplicationIndexMedia)] {
  132. item.title = NSLocalizedString("_media_", comment: "")
  133. item.image = CCGraphics.changeThemingColorImage(UIImage(named: "media"), width: 50, height: 50, color: NCBrandColor.sharedInstance.brandElement)
  134. item.selectedImage = item.image
  135. }
  136. // More
  137. if let item = items?[Int(k_tabBarApplicationIndexMore)] {
  138. item.title = NSLocalizedString("_more_", comment: "")
  139. item.image = CCGraphics.changeThemingColorImage(UIImage(named: "tabBarMore"), width: 50, height: 50, color: NCBrandColor.sharedInstance.brandElement)
  140. item.selectedImage = item.image
  141. }
  142. // Center button
  143. if let centerButton = self.viewWithTag(99) {
  144. centerButton.removeFromSuperview()
  145. }
  146. let centerButtonHeight: CGFloat = 57
  147. let centerButtonY: CGFloat = -28
  148. let centerButton = UIButton(frame: CGRect(x: (self.bounds.width / 2)-(centerButtonHeight/2), y: centerButtonY, width: centerButtonHeight, height: centerButtonHeight))
  149. centerButton.setTitle("", for: .normal)
  150. centerButton.setImage(CCGraphics.changeThemingColorImage(UIImage(named: "tabBarPlus"), width: 100, height: 100, color: .white), for: .normal)
  151. centerButton.backgroundColor = NCBrandColor.sharedInstance.brandElement
  152. centerButton.tintColor = UIColor.white
  153. centerButton.tag = 99
  154. centerButton.accessibilityLabel = NSLocalizedString("_accessibility_add_upload_", comment: "")
  155. centerButton.layer.cornerRadius = centerButton.frame.size.width / 2.0
  156. centerButton.layer.masksToBounds = false
  157. centerButton.layer.shadowOffset = CGSize(width: 0, height: 0)
  158. centerButton.layer.shadowRadius = 3.0
  159. centerButton.layer.shadowOpacity = 0.5
  160. centerButton.addTarget(self, action: #selector(self.centerButtonAction), for: .touchUpInside)
  161. self.addSubview(centerButton)
  162. }
  163. // Menu Button Touch Action
  164. @objc func centerButtonAction(sender: UIButton) {
  165. if appDelegate.maintenanceMode { return }
  166. if let directory = NCManageDatabase.sharedInstance.getTableDirectory(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@", appDelegate.account, appDelegate.activeServerUrl)) {
  167. if !directory.permissions.contains("CK") {
  168. NCContentPresenter.shared.messageNotification("_warning_", description: "_no_permission_add_file_", delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.info, errorCode: Int(k_CCErrorInternalError))
  169. return
  170. }
  171. }
  172. if let viewController = self.window?.rootViewController {
  173. appDelegate.showMenuIn(viewController: viewController)
  174. }
  175. }
  176. }