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