NCMainTabBar.swift 8.2 KB

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