NCMainTabBar.swift 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. class NCMainTabBar: UITabBar {
  25. private var fillColor: UIColor!
  26. private var shapeLayer: CALayer?
  27. private let appDelegate = UIApplication.shared.delegate as! AppDelegate
  28. private var timer: Timer?
  29. // override var traitCollection: UITraitCollection {
  30. // return UITraitCollection(horizontalSizeClass: .compact)
  31. // }
  32. required init?(coder: NSCoder) {
  33. super.init(coder: coder)
  34. timer = Timer.scheduledTimer(timeInterval: 3, target: self, selector: (#selector(updateBadgeNumber)), userInfo: nil, repeats: true)
  35. NotificationCenter.default.addObserver(self, selector: #selector(changeTheming), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterChangeTheming), object: nil)
  36. NotificationCenter.default.addObserver(self, selector: #selector(updateBadgeNumber), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterUpdateBadgeNumber), object: nil)
  37. barTintColor = NCBrandColor.shared.secondarySystemBackground
  38. backgroundColor = NCBrandColor.shared.secondarySystemBackground
  39. changeTheming()
  40. }
  41. override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
  42. super.traitCollectionDidChange(previousTraitCollection)
  43. }
  44. @objc func changeTheming() {
  45. tintColor = NCBrandColor.shared.brandElement
  46. if let centerButton = self.viewWithTag(99) {
  47. centerButton.backgroundColor = NCBrandColor.shared.brandElement
  48. }
  49. }
  50. override var backgroundColor: UIColor? {
  51. get {
  52. return self.fillColor
  53. }
  54. set {
  55. fillColor = newValue
  56. self.setNeedsDisplay()
  57. }
  58. }
  59. override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
  60. let button = self.viewWithTag(99)
  61. if self.bounds.contains(point) || (button != nil && button!.frame.contains(point)) {
  62. return true
  63. } else {
  64. return false
  65. }
  66. }
  67. override func layoutSubviews() {
  68. super.layoutSubviews()
  69. layer.shadowPath = createPath()
  70. layer.shadowRadius = 5
  71. layer.shadowOffset = .zero
  72. layer.shadowOpacity = 0.25
  73. }
  74. override func draw(_ rect: CGRect) {
  75. addShape()
  76. createButtons()
  77. }
  78. private func addShape() {
  79. let shapeLayer = CAShapeLayer()
  80. shapeLayer.path = createPath()
  81. shapeLayer.fillColor = backgroundColor?.cgColor
  82. shapeLayer.strokeColor = UIColor.clear.cgColor
  83. if let oldShapeLayer = self.shapeLayer {
  84. self.layer.replaceSublayer(oldShapeLayer, with: shapeLayer)
  85. } else {
  86. self.layer.insertSublayer(shapeLayer, at: 0)
  87. }
  88. self.shapeLayer = shapeLayer
  89. }
  90. private func createPath() -> CGPath {
  91. let height: CGFloat = 28
  92. let margin: CGFloat = 8
  93. let path = UIBezierPath()
  94. let centerWidth = self.frame.width / 2
  95. path.move(to: CGPoint(x: 0, y: 0)) // start top left
  96. path.addLine(to: CGPoint(x: (centerWidth - height - margin), y: 0)) // the beginning of the trough
  97. // first curve down
  98. 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)
  99. // complete the rect
  100. path.addLine(to: CGPoint(x: self.frame.width, y: 0))
  101. path.addLine(to: CGPoint(x: self.frame.width, y: self.frame.height))
  102. path.addLine(to: CGPoint(x: 0, y: self.frame.height))
  103. path.close()
  104. return path.cgPath
  105. }
  106. private func createButtons() {
  107. // File
  108. if let item = items?[0] {
  109. item.title = NSLocalizedString("_home_", comment: "")
  110. item.image = UIImage(named: "tabBarFiles")?.image(color: NCBrandColor.shared.brandElement, size: 25)
  111. item.selectedImage = item.image
  112. }
  113. // Favorite
  114. if let item = items?[1] {
  115. item.title = NSLocalizedString("_favorites_", comment: "")
  116. item.image = UIImage(named: "tabBarFavorites")?.image(color: NCBrandColor.shared.brandElement, size: 25)
  117. item.selectedImage = item.image
  118. }
  119. // +
  120. if let item = items?[2] {
  121. item.title = ""
  122. item.image = nil
  123. item.isEnabled = false
  124. }
  125. // Media
  126. if let item = items?[3] {
  127. item.title = NSLocalizedString("_media_", comment: "")
  128. item.image = UIImage(named: "tabBarMedia")?.image(color: NCBrandColor.shared.brandElement, size: 25)
  129. item.selectedImage = item.image
  130. }
  131. // More
  132. if let item = items?[4] {
  133. item.title = NSLocalizedString("_more_", comment: "")
  134. item.image = UIImage(named: "tabBarMore")?.image(color: NCBrandColor.shared.brandElement, size: 25)
  135. item.selectedImage = item.image
  136. }
  137. // Center button
  138. if let centerButton = self.viewWithTag(99) {
  139. centerButton.removeFromSuperview()
  140. }
  141. let centerButtonHeight: CGFloat = 57
  142. let centerButtonY: CGFloat = -28
  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.addTarget(self, action: #selector(self.centerButtonAction), for: .touchUpInside)
  156. self.addSubview(centerButton)
  157. }
  158. // Menu Button Touch Action
  159. @objc func centerButtonAction(sender: UIButton) {
  160. if let directory = NCManageDatabase.shared.getTableDirectory(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@", appDelegate.account, appDelegate.activeServerUrl)) {
  161. if !directory.permissions.contains("CK") {
  162. NCContentPresenter.shared.messageNotification("_warning_", description: "_no_permission_add_file_", delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.info, errorCode: NCGlobal.shared.errorInternalError)
  163. return
  164. }
  165. }
  166. if let viewController = self.window?.rootViewController {
  167. appDelegate.toggleMenu(viewController: viewController)
  168. }
  169. }
  170. @objc func updateBadgeNumber() {
  171. if appDelegate.account == "" { return }
  172. let counterDownload = NCOperationQueue.shared.downloadCount()
  173. 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
  174. let total = counterDownload + counterUpload
  175. UIApplication.shared.applicationIconBadgeNumber = total
  176. if let item = items?[0] {
  177. if total > 0 {
  178. item.badgeValue = String(total)
  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. }