NCMainTabBar.swift 9.4 KB

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