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