NCMainTabBar.swift 8.9 KB

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