NCSharePaging.swift 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. //
  2. // NCSharePaging.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 25/07/2019.
  6. // Copyright © 2019 Marino Faggiana. All rights reserved.
  7. //
  8. // Author Marino Faggiana <marino.faggiana@nextcloud.com>
  9. // Author Henrik Storch <henrik.storch@nextcloud.com>
  10. //
  11. // This program is free software: you can redistribute it and/or modify
  12. // it under the terms of the GNU General Public License as published by
  13. // the Free Software Foundation, either version 3 of the License, or
  14. // (at your option) any later version.
  15. //
  16. // This program is distributed in the hope that it will be useful,
  17. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. // GNU General Public License for more details.
  20. //
  21. // You should have received a copy of the GNU General Public License
  22. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. //
  24. import UIKit
  25. import Parchment
  26. import NCCommunication
  27. import MarqueeLabel
  28. protocol NCSharePagingContent {
  29. var textField: UITextField? { get }
  30. }
  31. class NCSharePaging: UIViewController {
  32. private let pagingViewController = NCShareHeaderViewController()
  33. private weak var appDelegate = UIApplication.shared.delegate as? AppDelegate
  34. private var activityEnabled = true
  35. private var commentsEnabled = true
  36. private var sharingEnabled = true
  37. private var currentVC: NCSharePagingContent?
  38. @objc var metadata = tableMetadata()
  39. var indexPage = NCGlobal.NCSharePagingIndex.activity
  40. // MARK: - View Life Cycle
  41. override func viewDidLoad() {
  42. super.viewDidLoad()
  43. view.backgroundColor = NCBrandColor.shared.systemBackground
  44. navigationItem.leftBarButtonItem = UIBarButtonItem(title: NSLocalizedString("_close_", comment: ""), style: .done, target: self, action: #selector(exitTapped))
  45. NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(notification:)), name: UIResponder.keyboardWillShowNotification, object: nil)
  46. NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(notification:)), name: UIResponder.keyboardWillHideNotification, object: nil)
  47. setupCapabilities()
  48. // *** MUST BE THE FIRST ONE ***
  49. pagingViewController.metadata = metadata
  50. pagingViewController.activityEnabled = activityEnabled
  51. pagingViewController.commentsEnabled = commentsEnabled
  52. pagingViewController.sharingEnabled = sharingEnabled
  53. pagingViewController.backgroundColor = NCBrandColor.shared.systemBackground
  54. pagingViewController.menuBackgroundColor = NCBrandColor.shared.systemBackground
  55. pagingViewController.selectedBackgroundColor = NCBrandColor.shared.systemBackground
  56. pagingViewController.textColor = NCBrandColor.shared.label
  57. pagingViewController.selectedTextColor = NCBrandColor.shared.label
  58. // Pagination
  59. addChild(pagingViewController)
  60. view.addSubview(pagingViewController.view)
  61. pagingViewController.didMove(toParent: self)
  62. // Customization
  63. pagingViewController.indicatorOptions = .visible(
  64. height: 1,
  65. zIndex: Int.max,
  66. spacing: .zero,
  67. insets: UIEdgeInsets(top: 0, left: 5, bottom: 0, right: 5)
  68. )
  69. // Contrain the paging view to all edges.
  70. pagingViewController.view.translatesAutoresizingMaskIntoConstraints = false
  71. NSLayoutConstraint.activate([
  72. pagingViewController.view.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
  73. pagingViewController.view.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor),
  74. pagingViewController.view.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor),
  75. pagingViewController.view.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor)
  76. ])
  77. pagingViewController.dataSource = self
  78. pagingViewController.delegate = self
  79. pagingViewController.select(index: indexPage.rawValue)
  80. let pagingIndexItem = self.pagingViewController(pagingViewController, pagingItemAt: indexPage.rawValue) as? PagingIndexItem
  81. self.title = pagingIndexItem?.title
  82. NotificationCenter.default.addObserver(self, selector: #selector(self.changeTheming), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterChangeTheming), object: nil)
  83. NotificationCenter.default.addObserver(self, selector: #selector(self.orientationDidChange), name: UIDevice.orientationDidChangeNotification, object: nil)
  84. changeTheming()
  85. }
  86. func setupCapabilities() {
  87. guard let appDelegate = appDelegate else { return }
  88. // Verify Comments & Sharing enabled
  89. let serverVersionMajor = NCManageDatabase.shared.getCapabilitiesServerInt(account: appDelegate.account, elements: NCElementsJSON.shared.capabilitiesVersionMajor)
  90. let comments = NCManageDatabase.shared.getCapabilitiesServerBool(account: appDelegate.account, elements: NCElementsJSON.shared.capabilitiesFilesComments, exists: false)
  91. if serverVersionMajor >= NCGlobal.shared.nextcloudVersion20 && comments == false {
  92. commentsEnabled = false
  93. }
  94. let sharing = NCManageDatabase.shared.getCapabilitiesServerBool(account: appDelegate.account, elements: NCElementsJSON.shared.capabilitiesFileSharingApiEnabled, exists: false)
  95. sharingEnabled = sharing
  96. let activity = NCManageDatabase.shared.getCapabilitiesServerArray(account: appDelegate.account, elements: NCElementsJSON.shared.capabilitiesActivity)
  97. activityEnabled = activity != nil
  98. if indexPage == .sharing && !sharingEnabled {
  99. indexPage = .activity
  100. }
  101. if indexPage == .activity && !activityEnabled && sharingEnabled {
  102. indexPage = .sharing
  103. }
  104. }
  105. override func viewDidAppear(_ animated: Bool) {
  106. super.viewDidAppear(animated)
  107. currentVC = pagingViewController.pageViewController.selectedViewController as? NCSharePagingContent
  108. }
  109. override func viewWillAppear(_ animated: Bool) {
  110. super.viewWillAppear(animated)
  111. if appDelegate?.disableSharesView == true {
  112. self.dismiss(animated: false, completion: nil)
  113. }
  114. pagingViewController.menuItemSize = .fixed(
  115. width: self.view.bounds.width / CGFloat(NCGlobal.NCSharePagingIndex.allCases.count),
  116. height: 40)
  117. }
  118. override func viewWillDisappear(_ animated: Bool) {
  119. super.viewWillDisappear(animated)
  120. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterReloadDataSource, userInfo: ["ocId": metadata.ocId, "serverUrl": metadata.serverUrl])
  121. }
  122. deinit {
  123. NotificationCenter.default.removeObserver(self, name: UIDevice.orientationDidChangeNotification, object: nil)
  124. NotificationCenter.default.removeObserver(self, name: UIResponder.keyboardDidShowNotification, object: nil)
  125. NotificationCenter.default.removeObserver(self, name: UIResponder.keyboardWillHideNotification, object: nil)
  126. }
  127. // MARK: - NotificationCenter
  128. @objc func orientationDidChange() {
  129. pagingViewController.menuItemSize = .fixed(
  130. width: self.view.bounds.width / CGFloat(NCGlobal.NCSharePagingIndex.allCases.count),
  131. height: 40)
  132. currentVC?.textField?.resignFirstResponder()
  133. }
  134. @objc func changeTheming() {
  135. pagingViewController.indicatorColor = NCBrandColor.shared.brandElement
  136. (pagingViewController.view as? NCSharePagingView)?.setupConstraints()
  137. pagingViewController.reloadMenu()
  138. }
  139. // MARK: - Keyboard & TextField
  140. @objc func keyboardWillShow(notification: Notification) {
  141. let frameEndUserInfoKey = UIResponder.keyboardFrameEndUserInfoKey
  142. guard let info = notification.userInfo,
  143. let textField = currentVC?.textField,
  144. let centerObject = textField.superview?.convert(textField.center, to: nil),
  145. let keyboardFrame = info[frameEndUserInfoKey] as? CGRect
  146. else { return }
  147. let diff = keyboardFrame.origin.y - centerObject.y - textField.frame.height
  148. if diff < 0 {
  149. view.frame.origin.y = diff
  150. }
  151. }
  152. @objc func keyboardWillHide(notification: NSNotification) {
  153. view.frame.origin.y = 0
  154. }
  155. @objc func exitTapped() {
  156. self.dismiss(animated: true, completion: nil)
  157. }
  158. }
  159. // MARK: - PagingViewController Delegate
  160. extension NCSharePaging: PagingViewControllerDelegate {
  161. func pagingViewController(_ pagingViewController: PagingViewController, willScrollToItem pagingItem: PagingItem, startingViewController: UIViewController, destinationViewController: UIViewController) {
  162. guard
  163. let item = pagingItem as? PagingIndexItem,
  164. let itemIndex = NCGlobal.NCSharePagingIndex(rawValue: item.index)
  165. else { return }
  166. if itemIndex == .activity && !activityEnabled || itemIndex == .sharing && !sharingEnabled {
  167. pagingViewController.contentInteraction = .none
  168. } else {
  169. self.title = item.title
  170. }
  171. currentVC?.textField?.resignFirstResponder()
  172. self.currentVC = destinationViewController as? NCSharePagingContent
  173. }
  174. }
  175. // MARK: - PagingViewController DataSource
  176. extension NCSharePaging: PagingViewControllerDataSource {
  177. func pagingViewController(_: PagingViewController, viewControllerAt index: Int) -> UIViewController {
  178. let height = pagingViewController.options.menuHeight + NCSharePagingView.headerHeight
  179. switch NCGlobal.NCSharePagingIndex(rawValue: index) {
  180. case .activity:
  181. guard let viewController = UIStoryboard(name: "NCActivity", bundle: nil).instantiateInitialViewController() as? NCActivity else {
  182. return UIViewController()
  183. }
  184. viewController.height = height
  185. viewController.showComments = true
  186. viewController.didSelectItemEnable = false
  187. viewController.metadata = metadata
  188. viewController.objectType = "files"
  189. return viewController
  190. case .sharing:
  191. guard let viewController = UIStoryboard(name: "NCShare", bundle: nil).instantiateViewController(withIdentifier: "sharing") as? NCShare else {
  192. return UIViewController()
  193. }
  194. viewController.sharingEnabled = sharingEnabled
  195. viewController.metadata = metadata
  196. viewController.height = height
  197. return viewController
  198. default:
  199. return UIViewController()
  200. }
  201. }
  202. func pagingViewController(_: PagingViewController, pagingItemAt index: Int) -> PagingItem {
  203. switch NCGlobal.NCSharePagingIndex(rawValue: index) {
  204. case .activity:
  205. return PagingIndexItem(index: index, title: NSLocalizedString("_activity_", comment: ""))
  206. case .sharing:
  207. return PagingIndexItem(index: index, title: NSLocalizedString("_sharing_", comment: ""))
  208. default:
  209. return PagingIndexItem(index: index, title: "")
  210. }
  211. }
  212. func numberOfViewControllers(in pagingViewController: PagingViewController) -> Int {
  213. return 2
  214. }
  215. }
  216. // MARK: - Header
  217. class NCShareHeaderViewController: PagingViewController {
  218. public var image: UIImage?
  219. public var metadata = tableMetadata()
  220. public var activityEnabled = true
  221. public var commentsEnabled = true
  222. public var sharingEnabled = true
  223. override func loadView() {
  224. view = NCSharePagingView(
  225. options: options,
  226. collectionView: collectionView,
  227. pageView: pageViewController.view,
  228. metadata: metadata
  229. )
  230. }
  231. override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  232. guard NCGlobal.NCSharePagingIndex(rawValue: indexPath.item) != .activity || activityEnabled,
  233. NCGlobal.NCSharePagingIndex(rawValue: indexPath.item) != .sharing || sharingEnabled
  234. else { return }
  235. super.collectionView(collectionView, didSelectItemAt: indexPath)
  236. }
  237. }
  238. class NCSharePagingView: PagingView {
  239. static let headerHeight: CGFloat = 100
  240. var metadata = tableMetadata()
  241. var headerHeightConstraint: NSLayoutConstraint?
  242. // MARK: - View Life Cycle
  243. public init(options: Parchment.PagingOptions, collectionView: UICollectionView, pageView: UIView, metadata: tableMetadata) {
  244. super.init(options: options, collectionView: collectionView, pageView: pageView)
  245. self.metadata = metadata
  246. }
  247. required init?(coder: NSCoder) {
  248. fatalError("init(coder:) has not been implemented")
  249. }
  250. override func setupConstraints() {
  251. guard let headerView = Bundle.main.loadNibNamed("NCShareHeaderView", owner: self, options: nil)?.first as? NCShareHeaderView else { return }
  252. headerView.backgroundColor = NCBrandColor.shared.systemBackground
  253. headerView.ocId = metadata.ocId
  254. if FileManager.default.fileExists(atPath: CCUtility.getDirectoryProviderStorageIconOcId(metadata.ocId, etag: metadata.etag)) {
  255. headerView.imageView.image = UIImage(contentsOfFile: CCUtility.getDirectoryProviderStorageIconOcId(metadata.ocId, etag: metadata.etag))
  256. } else {
  257. if metadata.directory {
  258. let image = UIImage(named: "folder")
  259. headerView.imageView.image = image?.image(color: NCBrandColor.shared.brandElement, size: image?.size.width ?? 0)
  260. } else if !metadata.iconName.isEmpty {
  261. headerView.imageView.image = UIImage(named: metadata.iconName)
  262. } else {
  263. headerView.imageView.image = UIImage(named: "file")
  264. }
  265. }
  266. headerView.path.text = NCUtilityFileSystem.shared.getPath(metadata: metadata, withFileName: true)
  267. headerView.path.textColor = NCBrandColor.shared.label
  268. headerView.path.trailingBuffer = headerView.path.frame.width
  269. if metadata.favorite {
  270. headerView.favorite.setImage(NCUtility.shared.loadImage(named: "star.fill", color: NCBrandColor.shared.yellowFavorite, size: 20), for: .normal)
  271. } else {
  272. headerView.favorite.setImage(NCUtility.shared.loadImage(named: "star.fill", color: NCBrandColor.shared.systemGray, size: 20), for: .normal)
  273. }
  274. headerView.info.text = CCUtility.transformedSize(metadata.size) + ", " + CCUtility.dateDiff(metadata.date as Date)
  275. addSubview(headerView)
  276. pageView.translatesAutoresizingMaskIntoConstraints = false
  277. collectionView.translatesAutoresizingMaskIntoConstraints = false
  278. headerView.translatesAutoresizingMaskIntoConstraints = false
  279. headerHeightConstraint = headerView.heightAnchor.constraint(equalToConstant: NCSharePagingView.headerHeight)
  280. headerHeightConstraint?.isActive = true
  281. NSLayoutConstraint.activate([
  282. collectionView.leadingAnchor.constraint(equalTo: leadingAnchor),
  283. collectionView.trailingAnchor.constraint(equalTo: trailingAnchor),
  284. collectionView.heightAnchor.constraint(equalToConstant: options.menuHeight),
  285. collectionView.topAnchor.constraint(equalTo: headerView.bottomAnchor),
  286. headerView.topAnchor.constraint(equalTo: topAnchor),
  287. headerView.leadingAnchor.constraint(equalTo: leadingAnchor),
  288. headerView.trailingAnchor.constraint(equalTo: trailingAnchor),
  289. pageView.leadingAnchor.constraint(equalTo: leadingAnchor),
  290. pageView.trailingAnchor.constraint(equalTo: trailingAnchor),
  291. pageView.bottomAnchor.constraint(equalTo: bottomAnchor),
  292. pageView.topAnchor.constraint(equalTo: topAnchor, constant: 10)
  293. ])
  294. }
  295. }
  296. class NCShareHeaderView: UIView {
  297. @IBOutlet weak var imageView: UIImageView!
  298. @IBOutlet weak var path: MarqueeLabel!
  299. @IBOutlet weak var info: UILabel!
  300. @IBOutlet weak var favorite: UIButton!
  301. var ocId = ""
  302. override func awakeFromNib() {
  303. super.awakeFromNib()
  304. let longGesture = UILongPressGestureRecognizer(target: self, action: #selector(self.longTap))
  305. path.addGestureRecognizer(longGesture)
  306. }
  307. @IBAction func touchUpInsideFavorite(_ sender: UIButton) {
  308. guard let metadata = NCManageDatabase.shared.getMetadataFromOcId(ocId) else { return }
  309. NCNetworking.shared.favoriteMetadata(metadata) { errorCode, errorDescription in
  310. if errorCode == 0 {
  311. self.favorite.setImage(NCUtility.shared.loadImage(
  312. named: "star.fill",
  313. color: metadata.favorite ? NCBrandColor.shared.yellowFavorite : NCBrandColor.shared.systemGray,
  314. size: 20), for: .normal)
  315. } else {
  316. NCContentPresenter.shared.messageNotification("_error_", description: errorDescription, delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, errorCode: errorCode)
  317. }
  318. }
  319. }
  320. @objc func longTap(sender: UIGestureRecognizer) {
  321. UIPasteboard.general.string = path.text
  322. NCContentPresenter.shared.messageNotification("", description: "_copied_path_", delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.info, errorCode: NCGlobal.shared.errorNoError)
  323. }
  324. }