NCMainTabBar.swift 9.0 KB

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