NCSharePaging.swift 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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 NextcloudKit
  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.orientationDidChange), name: UIDevice.orientationDidChangeNotification, object: nil)
  83. pagingViewController.indicatorColor = NCBrandColor.shared.brandElement
  84. (pagingViewController.view as? NCSharePagingView)?.setupConstraints()
  85. pagingViewController.reloadMenu()
  86. }
  87. func setupCapabilities() {
  88. guard let appDelegate = appDelegate else { return }
  89. // Verify Comments & Sharing enabled
  90. let serverVersionMajor = NCManageDatabase.shared.getCapabilitiesServerInt(account: appDelegate.account, elements: NCElementsJSON.shared.capabilitiesVersionMajor)
  91. let comments = NCManageDatabase.shared.getCapabilitiesServerBool(account: appDelegate.account, elements: NCElementsJSON.shared.capabilitiesFilesComments, exists: false)
  92. if serverVersionMajor >= NCGlobal.shared.nextcloudVersion20 && comments == false {
  93. commentsEnabled = false
  94. }
  95. let sharing = NCManageDatabase.shared.getCapabilitiesServerBool(account: appDelegate.account, elements: NCElementsJSON.shared.capabilitiesFileSharingApiEnabled, exists: false)
  96. sharingEnabled = sharing
  97. let activity = NCManageDatabase.shared.getCapabilitiesServerArray(account: appDelegate.account, elements: NCElementsJSON.shared.capabilitiesActivity)
  98. activityEnabled = activity != nil
  99. if indexPage == .sharing && !sharingEnabled {
  100. indexPage = .activity
  101. }
  102. if indexPage == .activity && !activityEnabled && sharingEnabled {
  103. indexPage = .sharing
  104. }
  105. }
  106. override func viewDidAppear(_ animated: Bool) {
  107. super.viewDidAppear(animated)
  108. currentVC = pagingViewController.pageViewController.selectedViewController as? NCSharePagingContent
  109. }
  110. override func viewWillAppear(_ animated: Bool) {
  111. super.viewWillAppear(animated)
  112. if appDelegate?.disableSharesView == true {
  113. self.dismiss(animated: false, completion: nil)
  114. }
  115. pagingViewController.menuItemSize = .fixed(
  116. width: self.view.bounds.width / CGFloat(NCGlobal.NCSharePagingIndex.allCases.count),
  117. height: 40)
  118. }
  119. override func viewWillDisappear(_ animated: Bool) {
  120. super.viewWillDisappear(animated)
  121. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterReloadDataSource, userInfo: ["serverUrl": metadata.serverUrl])
  122. }
  123. deinit {
  124. NotificationCenter.default.removeObserver(self, name: UIDevice.orientationDidChangeNotification, object: nil)
  125. NotificationCenter.default.removeObserver(self, name: UIResponder.keyboardDidShowNotification, object: nil)
  126. NotificationCenter.default.removeObserver(self, name: UIResponder.keyboardWillHideNotification, object: nil)
  127. }
  128. // MARK: - NotificationCenter
  129. @objc func orientationDidChange() {
  130. pagingViewController.menuItemSize = .fixed(
  131. width: self.view.bounds.width / CGFloat(NCGlobal.NCSharePagingIndex.allCases.count),
  132. height: 40)
  133. currentVC?.textField?.resignFirstResponder()
  134. }
  135. // MARK: - Keyboard & TextField
  136. @objc func keyboardWillShow(notification: Notification) {
  137. let frameEndUserInfoKey = UIResponder.keyboardFrameEndUserInfoKey
  138. guard let info = notification.userInfo,
  139. let textField = currentVC?.textField,
  140. let centerObject = textField.superview?.convert(textField.center, to: nil),
  141. let keyboardFrame = info[frameEndUserInfoKey] as? CGRect
  142. else { return }
  143. let diff = keyboardFrame.origin.y - centerObject.y - textField.frame.height
  144. if diff < 0 {
  145. view.frame.origin.y = diff
  146. }
  147. }
  148. @objc func keyboardWillHide(notification: NSNotification) {
  149. view.frame.origin.y = 0
  150. }
  151. @objc func exitTapped() {
  152. self.dismiss(animated: true, completion: nil)
  153. }
  154. }
  155. // MARK: - PagingViewController Delegate
  156. extension NCSharePaging: PagingViewControllerDelegate {
  157. func pagingViewController(_ pagingViewController: PagingViewController, willScrollToItem pagingItem: PagingItem, startingViewController: UIViewController, destinationViewController: UIViewController) {
  158. guard
  159. let item = pagingItem as? PagingIndexItem,
  160. let itemIndex = NCGlobal.NCSharePagingIndex(rawValue: item.index)
  161. else { return }
  162. if itemIndex == .activity && !activityEnabled || itemIndex == .sharing && !sharingEnabled {
  163. pagingViewController.contentInteraction = .none
  164. } else {
  165. self.title = item.title
  166. }
  167. currentVC?.textField?.resignFirstResponder()
  168. self.currentVC = destinationViewController as? NCSharePagingContent
  169. }
  170. }
  171. // MARK: - PagingViewController DataSource
  172. extension NCSharePaging: PagingViewControllerDataSource {
  173. func pagingViewController(_: PagingViewController, viewControllerAt index: Int) -> UIViewController {
  174. let height = pagingViewController.options.menuHeight + NCSharePagingView.headerHeight
  175. switch NCGlobal.NCSharePagingIndex(rawValue: index) {
  176. case .activity:
  177. guard let viewController = UIStoryboard(name: "NCActivity", bundle: nil).instantiateInitialViewController() as? NCActivity else {
  178. return UIViewController()
  179. }
  180. viewController.height = height
  181. viewController.showComments = true
  182. viewController.didSelectItemEnable = false
  183. viewController.metadata = metadata
  184. viewController.objectType = "files"
  185. return viewController
  186. case .sharing:
  187. guard let viewController = UIStoryboard(name: "NCShare", bundle: nil).instantiateViewController(withIdentifier: "sharing") as? NCShare else {
  188. return UIViewController()
  189. }
  190. viewController.sharingEnabled = sharingEnabled
  191. viewController.metadata = metadata
  192. viewController.height = height
  193. return viewController
  194. default:
  195. return UIViewController()
  196. }
  197. }
  198. func pagingViewController(_: PagingViewController, pagingItemAt index: Int) -> PagingItem {
  199. switch NCGlobal.NCSharePagingIndex(rawValue: index) {
  200. case .activity:
  201. return PagingIndexItem(index: index, title: NSLocalizedString("_activity_", comment: ""))
  202. case .sharing:
  203. return PagingIndexItem(index: index, title: NSLocalizedString("_sharing_", comment: ""))
  204. default:
  205. return PagingIndexItem(index: index, title: "")
  206. }
  207. }
  208. func numberOfViewControllers(in pagingViewController: PagingViewController) -> Int {
  209. return 2
  210. }
  211. }
  212. // MARK: - Header
  213. class NCShareHeaderViewController: PagingViewController {
  214. public var image: UIImage?
  215. public var metadata = tableMetadata()
  216. public var activityEnabled = true
  217. public var commentsEnabled = true
  218. public var sharingEnabled = true
  219. override func loadView() {
  220. view = NCSharePagingView(
  221. options: options,
  222. collectionView: collectionView,
  223. pageView: pageViewController.view,
  224. metadata: metadata
  225. )
  226. }
  227. override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  228. guard NCGlobal.NCSharePagingIndex(rawValue: indexPath.item) != .activity || activityEnabled,
  229. NCGlobal.NCSharePagingIndex(rawValue: indexPath.item) != .sharing || sharingEnabled
  230. else { return }
  231. super.collectionView(collectionView, didSelectItemAt: indexPath)
  232. }
  233. }
  234. class NCSharePagingView: PagingView {
  235. static let headerHeight: CGFloat = 100
  236. var metadata = tableMetadata()
  237. var headerHeightConstraint: NSLayoutConstraint?
  238. // MARK: - View Life Cycle
  239. public init(options: Parchment.PagingOptions, collectionView: UICollectionView, pageView: UIView, metadata: tableMetadata) {
  240. super.init(options: options, collectionView: collectionView, pageView: pageView)
  241. self.metadata = metadata
  242. }
  243. required init?(coder: NSCoder) {
  244. fatalError("init(coder:) has not been implemented")
  245. }
  246. override func setupConstraints() {
  247. guard let headerView = Bundle.main.loadNibNamed("NCShareHeaderView", owner: self, options: nil)?.first as? NCShareHeaderView else { return }
  248. headerView.backgroundColor = NCBrandColor.shared.systemBackground
  249. headerView.ocId = metadata.ocId
  250. if FileManager.default.fileExists(atPath: CCUtility.getDirectoryProviderStorageIconOcId(metadata.ocId, etag: metadata.etag)) {
  251. headerView.imageView.image = UIImage(contentsOfFile: CCUtility.getDirectoryProviderStorageIconOcId(metadata.ocId, etag: metadata.etag))
  252. } else {
  253. if metadata.directory {
  254. let image = UIImage(named: "folder")
  255. headerView.imageView.image = image?.image(color: NCBrandColor.shared.brandElement, size: image?.size.width ?? 0)
  256. headerView.imageView.image = headerView.imageView.image?.colorizeFolder(metadata: metadata)
  257. } else if !metadata.iconName.isEmpty {
  258. headerView.imageView.image = UIImage(named: metadata.iconName)
  259. } else {
  260. headerView.imageView.image = UIImage(named: "file")
  261. }
  262. }
  263. headerView.path.text = NCUtilityFileSystem.shared.getPath(path: metadata.path, user: metadata.user, fileName: metadata.fileName)
  264. headerView.path.textColor = NCBrandColor.shared.label
  265. headerView.path.trailingBuffer = headerView.path.frame.width
  266. if metadata.favorite {
  267. headerView.favorite.setImage(NCUtility.shared.loadImage(named: "star.fill", color: NCBrandColor.shared.yellowFavorite, size: 20), for: .normal)
  268. } else {
  269. headerView.favorite.setImage(NCUtility.shared.loadImage(named: "star.fill", color: NCBrandColor.shared.systemGray, size: 20), for: .normal)
  270. }
  271. headerView.info.text = CCUtility.transformedSize(metadata.size) + ", " + CCUtility.dateDiff(metadata.date as Date)
  272. addSubview(headerView)
  273. pageView.translatesAutoresizingMaskIntoConstraints = false
  274. collectionView.translatesAutoresizingMaskIntoConstraints = false
  275. headerView.translatesAutoresizingMaskIntoConstraints = false
  276. headerHeightConstraint = headerView.heightAnchor.constraint(equalToConstant: NCSharePagingView.headerHeight)
  277. headerHeightConstraint?.isActive = true
  278. NSLayoutConstraint.activate([
  279. collectionView.leadingAnchor.constraint(equalTo: leadingAnchor),
  280. collectionView.trailingAnchor.constraint(equalTo: trailingAnchor),
  281. collectionView.heightAnchor.constraint(equalToConstant: options.menuHeight),
  282. collectionView.topAnchor.constraint(equalTo: headerView.bottomAnchor),
  283. headerView.topAnchor.constraint(equalTo: topAnchor),
  284. headerView.leadingAnchor.constraint(equalTo: leadingAnchor),
  285. headerView.trailingAnchor.constraint(equalTo: trailingAnchor),
  286. pageView.leadingAnchor.constraint(equalTo: leadingAnchor),
  287. pageView.trailingAnchor.constraint(equalTo: trailingAnchor),
  288. pageView.bottomAnchor.constraint(equalTo: bottomAnchor),
  289. pageView.topAnchor.constraint(equalTo: topAnchor, constant: 10)
  290. ])
  291. }
  292. }
  293. class NCShareHeaderView: UIView {
  294. @IBOutlet weak var imageView: UIImageView!
  295. @IBOutlet weak var path: MarqueeLabel!
  296. @IBOutlet weak var info: UILabel!
  297. @IBOutlet weak var favorite: UIButton!
  298. var ocId = ""
  299. override func awakeFromNib() {
  300. super.awakeFromNib()
  301. let longGesture = UILongPressGestureRecognizer(target: self, action: #selector(self.longTap))
  302. path.addGestureRecognizer(longGesture)
  303. }
  304. @IBAction func touchUpInsideFavorite(_ sender: UIButton) {
  305. guard let metadata = NCManageDatabase.shared.getMetadataFromOcId(ocId) else { return }
  306. NCNetworking.shared.favoriteMetadata(metadata) { error in
  307. if error == .success {
  308. self.favorite.setImage(NCUtility.shared.loadImage(
  309. named: "star.fill",
  310. color: metadata.favorite ? NCBrandColor.shared.yellowFavorite : NCBrandColor.shared.systemGray,
  311. size: 20), for: .normal)
  312. } else {
  313. NCContentPresenter.shared.showError(error: error)
  314. }
  315. }
  316. }
  317. @objc func longTap(sender: UIGestureRecognizer) {
  318. UIPasteboard.general.string = path.text
  319. let error = NKError(errorCode: NCGlobal.shared.errorNoError, errorDescription: "_copied_path_")
  320. NCContentPresenter.shared.showInfo(error: error)
  321. }
  322. }