NCMainTabBar.swift 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. NotificationCenter.default.addObserver(self, selector: #selector(changeTheming), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterChangeTheming), object: nil)
  39. NotificationCenter.default.addObserver(self, selector: #selector(updateBadgeNumber(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterUpdateBadgeNumber), object: nil)
  40. changeTheming()
  41. }
  42. @objc func changeTheming() {
  43. tintColor = NCBrandColor.shared.brandElement
  44. if let centerButton = self.viewWithTag(99) {
  45. centerButton.backgroundColor = NCBrandColor.shared.brandElement
  46. }
  47. }
  48. override var backgroundColor: UIColor? {
  49. get {
  50. return self.fillColor
  51. }
  52. set {
  53. fillColor = newValue
  54. self.setNeedsDisplay()
  55. }
  56. }
  57. override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
  58. let button = self.viewWithTag(99)
  59. if self.bounds.contains(point) || (button != nil && button!.frame.contains(point)) {
  60. return true
  61. } else {
  62. return false
  63. }
  64. }
  65. override func draw(_ rect: CGRect) {
  66. self.subviews.forEach({ $0.removeFromSuperview() })
  67. addShape()
  68. createButtons()
  69. }
  70. private func addShape() {
  71. let blurEffect = UIBlurEffect(style: .systemThinMaterial)
  72. let blurView = UIVisualEffectView(effect: blurEffect)
  73. blurView.frame = self.bounds
  74. let maskLayer = CAShapeLayer()
  75. maskLayer.path = createPath()
  76. blurView.layer.mask = maskLayer
  77. let border = CALayer()
  78. border.backgroundColor = UIColor.separator.cgColor
  79. border.frame = CGRect(x: 0, y: 0, width: blurView.frame.width, height: 0.5)
  80. blurView.layer.addSublayer(border)
  81. self.addSubview(blurView)
  82. }
  83. private func createPath() -> CGPath {
  84. let height: CGFloat = 28
  85. let margin: CGFloat = 6
  86. let path = UIBezierPath()
  87. let centerWidth = self.frame.width / 2
  88. path.move(to: CGPoint(x: 0, y: 0)) // start top left
  89. path.addLine(to: CGPoint(x: (centerWidth - height - margin), y: 0)) // the beginning of the trough
  90. // first curve down
  91. 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)
  92. // complete the rect
  93. path.addLine(to: CGPoint(x: self.frame.width, y: 0))
  94. path.addLine(to: CGPoint(x: self.frame.width, y: self.frame.height))
  95. path.addLine(to: CGPoint(x: 0, y: self.frame.height))
  96. path.close()
  97. return path.cgPath
  98. }
  99. private func createButtons() {
  100. // File
  101. if let item = items?[0] {
  102. item.title = NSLocalizedString("_home_", comment: "")
  103. item.image = UIImage(systemName: "folder.fill")
  104. item.selectedImage = item.image
  105. }
  106. // Favorite
  107. if let item = items?[1] {
  108. item.title = NSLocalizedString("_favorites_", comment: "")
  109. item.image = UIImage(systemName: "star.fill")
  110. item.selectedImage = item.image
  111. }
  112. // +
  113. let imagePlus = UIImage(systemName: "plus", withConfiguration: UIImage.SymbolConfiguration(scale: .large))?.applyingSymbolConfiguration(UIImage.SymbolConfiguration(paletteColors: [.white]))
  114. if let item = items?[2] {
  115. item.title = ""
  116. item.image = nil
  117. item.isEnabled = false
  118. }
  119. // Media
  120. if let item = items?[3] {
  121. item.title = NSLocalizedString("_media_", comment: "")
  122. item.image = UIImage(systemName: "photo")
  123. item.selectedImage = item.image
  124. }
  125. // More
  126. if let item = items?[4] {
  127. item.title = NSLocalizedString("_more_", comment: "")
  128. item.image = UIImage(systemName: "line.3.horizontal")
  129. item.image = UIImage(systemName: "ellipsis")
  130. item.selectedImage = item.image
  131. }
  132. // Center button
  133. if let centerButton = self.viewWithTag(99) {
  134. centerButton.removeFromSuperview()
  135. }
  136. let centerButtonHeight: CGFloat = 57
  137. let centerButton = UIButton(frame: CGRect(x: (self.bounds.width / 2) - (centerButtonHeight / 2), y: centerButtonY, width: centerButtonHeight, height: centerButtonHeight))
  138. centerButton.setTitle("", for: .normal)
  139. centerButton.setImage(imagePlus, for: .normal)
  140. centerButton.backgroundColor = NCBrandColor.shared.brandElement
  141. centerButton.tintColor = UIColor.white
  142. centerButton.tag = 99
  143. centerButton.accessibilityLabel = NSLocalizedString("_accessibility_add_upload_", comment: "")
  144. centerButton.layer.cornerRadius = centerButton.frame.size.width / 2.0
  145. centerButton.layer.masksToBounds = false
  146. centerButton.layer.shadowOffset = CGSize(width: 0, height: 0)
  147. centerButton.layer.shadowRadius = 3.0
  148. centerButton.layer.shadowOpacity = 0.5
  149. centerButton.action(for: .touchUpInside) { _ in
  150. if let controller = self.window?.rootViewController as? NCMainTabBarController {
  151. let serverUrl = controller.currentServerUrl()
  152. if let directory = NCManageDatabase.shared.getTableDirectory(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@", self.appDelegate.account, serverUrl)) {
  153. if !directory.permissions.contains("CK") {
  154. let error = NKError(errorCode: NCGlobal.shared.errorInternalError, errorDescription: "_no_permission_add_file_")
  155. NCContentPresenter().showWarning(error: error)
  156. return
  157. }
  158. }
  159. self.appDelegate.toggleMenu(controller: controller)
  160. }
  161. }
  162. self.addSubview(centerButton)
  163. }
  164. @objc func updateBadgeNumber(_ notification: NSNotification) {
  165. DispatchQueue.main.async {
  166. guard let userInfo = notification.userInfo as NSDictionary?,
  167. let counterDownload = userInfo["counterDownload"] as? Int,
  168. let counterUpload = userInfo["counterUpload"] as? Int
  169. else { return }
  170. self.updateBadgeNumberUI(counterDownload: counterDownload, counterUpload: counterUpload)
  171. }
  172. }
  173. func updateBadgeNumberUI(counterDownload: Int, counterUpload: Int) {
  174. UIApplication.shared.applicationIconBadgeNumber = counterDownload + 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. let badgeValue = String("↓ \(counterDownload)")
  180. item.badgeValue = badgeValue
  181. } else if counterDownload == 0, counterUpload > 0 {
  182. let badgeValue = String("↑ \(counterUpload)")
  183. item.badgeValue = badgeValue
  184. } else {
  185. let badgeValueDownload = String("↓ \(counterDownload)")
  186. let badgeValueUpload = String("↑ \(counterUpload)")
  187. item.badgeValue = badgeValueDownload + " " + badgeValueUpload
  188. }
  189. }
  190. }
  191. func getCenterButton() -> UIView? {
  192. if let centerButton = self.viewWithTag(99) {
  193. return centerButton
  194. } else {
  195. return nil
  196. }
  197. }
  198. func getHeight() -> CGFloat {
  199. return (frame.size.height - centerButtonY)
  200. }
  201. }