NCMainTabBar.swift 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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 UIKit
  24. import NextcloudKit
  25. class NCMainTabBar: UITabBar {
  26. private var fillColor: UIColor!
  27. private var shapeLayer: CALayer?
  28. private let appDelegate = UIApplication.shared.delegate as! AppDelegate
  29. private let centerButtonY: CGFloat = -28
  30. public var menuRect: CGRect {
  31. get {
  32. let tabBarItemWidth = Int(self.frame.size.width) / (self.items?.count ?? 0)
  33. let rect = CGRect(x: 0, y: -5, width: tabBarItemWidth, height: Int(self.frame.size.height))
  34. return rect
  35. }
  36. }
  37. // MARK: - Life Cycle
  38. required init?(coder: NSCoder) {
  39. super.init(coder: coder)
  40. appDelegate.mainTabBar = self
  41. NotificationCenter.default.addObserver(self, selector: #selector(changeTheming), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterChangeTheming), object: nil)
  42. NotificationCenter.default.addObserver(self, selector: #selector(updateBadgeNumber(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterUpdateBadgeNumber), object: nil)
  43. barTintColor = .secondarySystemBackground
  44. backgroundColor = .secondarySystemBackground
  45. changeTheming()
  46. }
  47. @objc func changeTheming() {
  48. tintColor = NCBrandColor.shared.brandElement
  49. if let centerButton = self.viewWithTag(99) {
  50. centerButton.backgroundColor = NCBrandColor.shared.brandElement
  51. }
  52. }
  53. override var backgroundColor: UIColor? {
  54. get {
  55. return self.fillColor
  56. }
  57. set {
  58. fillColor = newValue
  59. self.setNeedsDisplay()
  60. }
  61. }
  62. override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
  63. let button = self.viewWithTag(99)
  64. if self.bounds.contains(point) || (button != nil && button!.frame.contains(point)) {
  65. return true
  66. } else {
  67. return false
  68. }
  69. }
  70. override func layoutSubviews() {
  71. super.layoutSubviews()
  72. layer.shadowPath = createPath()
  73. layer.shadowRadius = 5
  74. layer.shadowOffset = .zero
  75. layer.shadowOpacity = 0.25
  76. }
  77. override func draw(_ rect: CGRect) {
  78. addShape()
  79. createButtons()
  80. }
  81. private func addShape() {
  82. let shapeLayer = CAShapeLayer()
  83. shapeLayer.path = createPath()
  84. shapeLayer.fillColor = backgroundColor?.cgColor
  85. shapeLayer.strokeColor = UIColor.clear.cgColor
  86. if let oldShapeLayer = self.shapeLayer {
  87. self.layer.replaceSublayer(oldShapeLayer, with: shapeLayer)
  88. } else {
  89. self.layer.insertSublayer(shapeLayer, at: 0)
  90. }
  91. self.shapeLayer = shapeLayer
  92. }
  93. private func createPath() -> CGPath {
  94. let height: CGFloat = 28
  95. let margin: CGFloat = 6
  96. let path = UIBezierPath()
  97. let centerWidth = self.frame.width / 2
  98. path.move(to: CGPoint(x: 0, y: 0)) // start top left
  99. path.addLine(to: CGPoint(x: (centerWidth - height - margin), y: 0)) // the beginning of the trough
  100. // first curve down
  101. 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)
  102. // complete the rect
  103. path.addLine(to: CGPoint(x: self.frame.width, y: 0))
  104. path.addLine(to: CGPoint(x: self.frame.width, y: self.frame.height))
  105. path.addLine(to: CGPoint(x: 0, y: self.frame.height))
  106. path.close()
  107. return path.cgPath
  108. }
  109. private func createButtons() {
  110. // File
  111. if let item = items?[0] {
  112. item.title = NSLocalizedString("_home_", comment: "")
  113. item.image = UIImage(named: "tabBarFiles")?.image(color: NCBrandColor.shared.brandElement, size: 25)
  114. item.selectedImage = item.image
  115. }
  116. // Favorite
  117. if let item = items?[1] {
  118. item.title = NSLocalizedString("_favorites_", comment: "")
  119. item.image = UIImage(named: "star.fill")?.image(color: NCBrandColor.shared.brandElement, size: 25)
  120. item.selectedImage = item.image
  121. }
  122. // +
  123. if let item = items?[2] {
  124. item.title = ""
  125. item.image = nil
  126. item.isEnabled = false
  127. }
  128. // Media
  129. if let item = items?[3] {
  130. item.title = NSLocalizedString("_media_", comment: "")
  131. item.image = UIImage(named: "media")?.image(color: NCBrandColor.shared.brandElement, size: 25)
  132. item.selectedImage = item.image
  133. }
  134. // More
  135. if let item = items?[4] {
  136. item.title = NSLocalizedString("_more_", comment: "")
  137. item.image = UIImage(named: "tabBarMore")?.image(color: NCBrandColor.shared.brandElement, size: 25)
  138. item.selectedImage = item.image
  139. }
  140. // Center button
  141. if let centerButton = self.viewWithTag(99) {
  142. centerButton.removeFromSuperview()
  143. }
  144. let centerButtonHeight: CGFloat = 57
  145. let centerButton = UIButton(frame: CGRect(x: (self.bounds.width / 2)-(centerButtonHeight/2), y: centerButtonY, width: centerButtonHeight, height: centerButtonHeight))
  146. centerButton.setTitle("", for: .normal)
  147. centerButton.setImage(UIImage(named: "tabBarPlus")?.image(color: .white, size: 100), for: .normal)
  148. centerButton.backgroundColor = NCBrandColor.shared.brandElement
  149. centerButton.tintColor = UIColor.white
  150. centerButton.tag = 99
  151. centerButton.accessibilityLabel = NSLocalizedString("_accessibility_add_upload_", comment: "")
  152. centerButton.layer.cornerRadius = centerButton.frame.size.width / 2.0
  153. centerButton.layer.masksToBounds = false
  154. centerButton.layer.shadowOffset = CGSize(width: 0, height: 0)
  155. centerButton.layer.shadowRadius = 3.0
  156. centerButton.layer.shadowOpacity = 0.5
  157. centerButton.action(for: .touchUpInside) { _ in
  158. if let directory = NCManageDatabase.shared.getTableDirectory(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@", self.appDelegate.account, self.appDelegate.activeServerUrl)) {
  159. if !directory.permissions.contains("CK") {
  160. let error = NKError(errorCode: NCGlobal.shared.errorInternalError, errorDescription: "_no_permission_add_file_")
  161. NCContentPresenter.shared.showWarning(error: error)
  162. return
  163. }
  164. }
  165. if let viewController = self.window?.rootViewController {
  166. self.appDelegate.toggleMenu(viewController: viewController)
  167. }
  168. }
  169. self.addSubview(centerButton)
  170. }
  171. @objc func updateBadgeNumber(_ notification: NSNotification) {
  172. guard let userInfo = notification.userInfo as NSDictionary?,
  173. let counter = userInfo["counter"] as? Int
  174. else { return }
  175. UIApplication.shared.applicationIconBadgeNumber = counter
  176. if let item = self.items?[0] {
  177. if counter > 0 {
  178. item.badgeValue = String(counter)
  179. } else {
  180. item.badgeValue = nil
  181. }
  182. }
  183. }
  184. func getCenterButton() -> UIView? {
  185. if let centerButton = self.viewWithTag(99) {
  186. return centerButton
  187. } else {
  188. return nil
  189. }
  190. }
  191. func getHight() -> CGFloat {
  192. return (frame.size.height - centerButtonY)
  193. }
  194. }