NCMainTabBar.swift 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. class NCMainTabBar: UITabBar {
  25. private var fillColor: UIColor!
  26. private var shapeLayer: CALayer?
  27. private let appDelegate = UIApplication.shared.delegate as! AppDelegate
  28. public var menuRect: CGRect {
  29. get {
  30. let tabBarItemWidth = Int(self.frame.size.width) / (self.items?.count ?? 0)
  31. let rect = CGRect(x: 0, y: -5, width: tabBarItemWidth, height: Int(self.frame.size.height))
  32. return rect
  33. }
  34. }
  35. // MARK: - Life Cycle
  36. required init?(coder: NSCoder) {
  37. super.init(coder: coder)
  38. appDelegate.mainTabBar = self
  39. NotificationCenter.default.addObserver(self, selector: #selector(changeTheming), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterChangeTheming), object: nil)
  40. NotificationCenter.default.addObserver(self, selector: #selector(updateBadgeNumber), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterUpdateBadgeNumber), object: nil)
  41. NotificationCenter.default.addObserver(self, selector: #selector(updateBadgeNumber), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterApplicationDidBecomeActive), object: nil)
  42. NotificationCenter.default.addObserver(self, selector: #selector(updateBadgeNumber), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterApplicationWillEnterForeground), object: nil)
  43. NotificationCenter.default.addObserver(self, selector: #selector(updateBadgeNumber), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterUploadStartFile), object: nil)
  44. NotificationCenter.default.addObserver(self, selector: #selector(updateBadgeNumber), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterUploadedFile), object: nil)
  45. NotificationCenter.default.addObserver(self, selector: #selector(updateBadgeNumber), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterUploadCancelFile), object: nil)
  46. barTintColor = NCBrandColor.shared.secondarySystemBackground
  47. backgroundColor = NCBrandColor.shared.secondarySystemBackground
  48. changeTheming()
  49. }
  50. @objc func changeTheming() {
  51. tintColor = NCBrandColor.shared.brandElement
  52. if let centerButton = self.viewWithTag(99) {
  53. centerButton.backgroundColor = NCBrandColor.shared.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. shapeLayer.strokeColor = UIColor.clear.cgColor
  89. if let oldShapeLayer = self.shapeLayer {
  90. self.layer.replaceSublayer(oldShapeLayer, with: shapeLayer)
  91. } else {
  92. self.layer.insertSublayer(shapeLayer, at: 0)
  93. }
  94. self.shapeLayer = shapeLayer
  95. }
  96. private func createPath() -> CGPath {
  97. let height: CGFloat = 28
  98. let margin: CGFloat = 6
  99. let path = UIBezierPath()
  100. let centerWidth = self.frame.width / 2
  101. path.move(to: CGPoint(x: 0, y: 0)) // start top left
  102. path.addLine(to: CGPoint(x: (centerWidth - height - margin), y: 0)) // the beginning of the trough
  103. // first curve down
  104. 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)
  105. // complete the rect
  106. path.addLine(to: CGPoint(x: self.frame.width, y: 0))
  107. path.addLine(to: CGPoint(x: self.frame.width, y: self.frame.height))
  108. path.addLine(to: CGPoint(x: 0, y: self.frame.height))
  109. path.close()
  110. return path.cgPath
  111. }
  112. private func createButtons() {
  113. // File
  114. if let item = items?[0] {
  115. item.title = NSLocalizedString("_home_", comment: "")
  116. item.image = UIImage(named: "tabBarFiles")?.image(color: NCBrandColor.shared.brandElement, size: 25)
  117. item.selectedImage = item.image
  118. }
  119. // Favorite
  120. if let item = items?[1] {
  121. item.title = NSLocalizedString("_favorites_", comment: "")
  122. item.image = UIImage(named: "star.fill")?.image(color: NCBrandColor.shared.brandElement, size: 25)
  123. item.selectedImage = item.image
  124. }
  125. // +
  126. if let item = items?[2] {
  127. item.title = ""
  128. item.image = nil
  129. item.isEnabled = false
  130. }
  131. // Media
  132. if let item = items?[3] {
  133. item.title = NSLocalizedString("_media_", comment: "")
  134. item.image = UIImage(named: "media")?.image(color: NCBrandColor.shared.brandElement, size: 25)
  135. item.selectedImage = item.image
  136. }
  137. // More
  138. if let item = items?[4] {
  139. item.title = NSLocalizedString("_more_", comment: "")
  140. item.image = UIImage(named: "tabBarMore")?.image(color: NCBrandColor.shared.brandElement, size: 25)
  141. item.selectedImage = item.image
  142. }
  143. // Center button
  144. if let centerButton = self.viewWithTag(99) {
  145. centerButton.removeFromSuperview()
  146. }
  147. let centerButtonHeight: CGFloat = 57
  148. let centerButtonY: CGFloat = -28
  149. let centerButton = UIButton(frame: CGRect(x: (self.bounds.width / 2)-(centerButtonHeight/2), y: centerButtonY, width: centerButtonHeight, height: centerButtonHeight))
  150. centerButton.setTitle("", for: .normal)
  151. centerButton.setImage(UIImage(named: "tabBarPlus")?.image(color: .white, size: 100), for: .normal)
  152. centerButton.backgroundColor = NCBrandColor.shared.brandElement
  153. centerButton.tintColor = UIColor.white
  154. centerButton.tag = 99
  155. centerButton.accessibilityLabel = NSLocalizedString("_accessibility_add_upload_", comment: "")
  156. centerButton.layer.cornerRadius = centerButton.frame.size.width / 2.0
  157. centerButton.layer.masksToBounds = false
  158. centerButton.layer.shadowOffset = CGSize(width: 0, height: 0)
  159. centerButton.layer.shadowRadius = 3.0
  160. centerButton.layer.shadowOpacity = 0.5
  161. centerButton.action(for: .touchUpInside) { _ in
  162. if let directory = NCManageDatabase.shared.getTableDirectory(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@", self.appDelegate.account, self.appDelegate.activeServerUrl)) {
  163. if !directory.permissions.contains("CK") {
  164. NCContentPresenter.shared.messageNotification("_warning_", description: "_no_permission_add_file_", delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.info, errorCode: NCGlobal.shared.errorInternalError)
  165. return
  166. }
  167. }
  168. if let viewController = self.window?.rootViewController {
  169. self.appDelegate.toggleMenu(viewController: viewController)
  170. }
  171. }
  172. self.addSubview(centerButton)
  173. }
  174. @objc func updateBadgeNumber() {
  175. guard !appDelegate.account.isEmpty else { return }
  176. let counterUpload = NCManageDatabase.shared.getMetadatas(predicate: NSPredicate(format: "status == %d OR status == %d OR status == %d", NCGlobal.shared.metadataStatusWaitUpload, NCGlobal.shared.metadataStatusInUpload, NCGlobal.shared.metadataStatusUploading)).count
  177. UIApplication.shared.applicationIconBadgeNumber = counterUpload
  178. if let item = items?[0] {
  179. if counterUpload > 0 {
  180. item.badgeValue = String(counterUpload)
  181. } else {
  182. item.badgeValue = nil
  183. }
  184. }
  185. }
  186. func getCenterButton() -> UIView? {
  187. if let centerButton = self.viewWithTag(99) {
  188. return centerButton
  189. } else {
  190. return nil
  191. }
  192. }
  193. }