NCSharePaging.swift 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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. import TagListView
  29. protocol NCSharePagingContent {
  30. var textField: UITextField? { get }
  31. }
  32. class NCSharePaging: UIViewController {
  33. private let pagingViewController = NCShareHeaderViewController()
  34. private weak var appDelegate = UIApplication.shared.delegate as? AppDelegate
  35. private var activityEnabled = true
  36. private var commentsEnabled = true
  37. private var sharingEnabled = true
  38. private var currentVC: NCSharePagingContent?
  39. private let applicationHandle = NCApplicationHandle()
  40. @objc var metadata = tableMetadata()
  41. var indexPage = NCBrandOptions.NCInfoPagingIndex.activity
  42. // MARK: - View Life Cycle
  43. override func viewDidLoad() {
  44. super.viewDidLoad()
  45. view.backgroundColor = .systemBackground
  46. navigationItem.leftBarButtonItem = UIBarButtonItem(title: NSLocalizedString("_close_", comment: ""), style: .done, target: self, action: #selector(exitTapped))
  47. NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(notification:)), name: UIResponder.keyboardWillShowNotification, object: nil)
  48. NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(notification:)), name: UIResponder.keyboardWillHideNotification, object: nil)
  49. setupCapabilities()
  50. // *** MUST BE THE FIRST ONE ***
  51. pagingViewController.metadata = metadata
  52. pagingViewController.activityEnabled = activityEnabled
  53. pagingViewController.commentsEnabled = commentsEnabled
  54. pagingViewController.sharingEnabled = sharingEnabled
  55. pagingViewController.backgroundColor = .systemBackground
  56. pagingViewController.menuBackgroundColor = .systemBackground
  57. pagingViewController.selectedBackgroundColor = .systemBackground
  58. pagingViewController.textColor = .label
  59. pagingViewController.selectedTextColor = .label
  60. // Pagination
  61. addChild(pagingViewController)
  62. view.addSubview(pagingViewController.view)
  63. pagingViewController.didMove(toParent: self)
  64. // Customization
  65. pagingViewController.indicatorOptions = .visible(
  66. height: 1,
  67. zIndex: Int.max,
  68. spacing: .zero,
  69. insets: UIEdgeInsets(top: 0, left: 5, bottom: 0, right: 5)
  70. )
  71. // Contrain the paging view to all edges.
  72. pagingViewController.view.translatesAutoresizingMaskIntoConstraints = false
  73. NSLayoutConstraint.activate([
  74. pagingViewController.view.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
  75. pagingViewController.view.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor),
  76. pagingViewController.view.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor),
  77. pagingViewController.view.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor)
  78. ])
  79. pagingViewController.dataSource = self
  80. pagingViewController.delegate = self
  81. pagingViewController.select(index: indexPage.rawValue)
  82. let pagingIndexItem = self.pagingViewController(pagingViewController, pagingItemAt: indexPage.rawValue) as? PagingIndexItem
  83. self.title = pagingIndexItem?.title
  84. if sharingEnabled {
  85. pagingViewController.indicatorColor = NCBrandColor.shared.brandElement
  86. } else {
  87. pagingViewController.indicatorColor = .clear
  88. }
  89. (pagingViewController.view as? NCSharePagingView)?.setupConstraints()
  90. pagingViewController.reloadMenu()
  91. }
  92. func setupCapabilities() {
  93. guard let appDelegate = appDelegate else { return }
  94. // Verify Comments & Sharing enabled
  95. let serverVersionMajor = NCManageDatabase.shared.getCapabilitiesServerInt(account: appDelegate.account, elements: NCElementsJSON.shared.capabilitiesVersionMajor)
  96. let comments = NCManageDatabase.shared.getCapabilitiesServerBool(account: appDelegate.account, elements: NCElementsJSON.shared.capabilitiesFilesComments, exists: false)
  97. if serverVersionMajor >= NCGlobal.shared.nextcloudVersion20 && comments == false {
  98. commentsEnabled = false
  99. }
  100. sharingEnabled = metadata.isSharable
  101. let activity = NCManageDatabase.shared.getCapabilitiesServerArray(account: appDelegate.account, elements: NCElementsJSON.shared.capabilitiesActivity)
  102. activityEnabled = activity != nil
  103. if indexPage == .sharing && !sharingEnabled {
  104. indexPage = .activity
  105. }
  106. if indexPage == .activity && !activityEnabled && sharingEnabled {
  107. indexPage = .sharing
  108. }
  109. }
  110. override func viewDidAppear(_ animated: Bool) {
  111. super.viewDidAppear(animated)
  112. currentVC = pagingViewController.pageViewController.selectedViewController as? NCSharePagingContent
  113. }
  114. override func viewWillAppear(_ animated: Bool) {
  115. super.viewWillAppear(animated)
  116. if appDelegate?.disableSharesView == true {
  117. self.dismiss(animated: false, completion: nil)
  118. }
  119. pagingViewController.menuItemSize = .fixed(
  120. width: self.view.bounds.width / CGFloat(NCBrandOptions.NCInfoPagingIndex.allCases.count),
  121. height: 40)
  122. }
  123. override func viewWillDisappear(_ animated: Bool) {
  124. super.viewWillDisappear(animated)
  125. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterReloadDataSource, userInfo: ["serverUrl": metadata.serverUrl])
  126. }
  127. deinit {
  128. NotificationCenter.default.removeObserver(self, name: UIResponder.keyboardDidShowNotification, object: nil)
  129. NotificationCenter.default.removeObserver(self, name: UIResponder.keyboardWillHideNotification, object: nil)
  130. }
  131. override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
  132. super.viewWillTransition(to: size, with: coordinator)
  133. coordinator.animate(alongsideTransition: nil) { _ in
  134. self.pagingViewController.menuItemSize = .fixed(
  135. width: self.view.bounds.width / CGFloat(NCBrandOptions.NCInfoPagingIndex.allCases.count),
  136. height: 40)
  137. self.currentVC?.textField?.resignFirstResponder()
  138. }
  139. }
  140. // MARK: - Keyboard & TextField
  141. @objc func keyboardWillShow(notification: Notification) {
  142. let frameEndUserInfoKey = UIResponder.keyboardFrameEndUserInfoKey
  143. guard let info = notification.userInfo,
  144. let textField = currentVC?.textField,
  145. let centerObject = textField.superview?.convert(textField.center, to: nil),
  146. let keyboardFrame = info[frameEndUserInfoKey] as? CGRect
  147. else { return }
  148. let diff = keyboardFrame.origin.y - centerObject.y - textField.frame.height
  149. if diff < 0 {
  150. view.frame.origin.y = diff
  151. }
  152. }
  153. @objc func keyboardWillHide(notification: NSNotification) {
  154. view.frame.origin.y = 0
  155. }
  156. @objc func exitTapped() {
  157. self.dismiss(animated: true, completion: nil)
  158. }
  159. }
  160. // MARK: - PagingViewController Delegate
  161. extension NCSharePaging: PagingViewControllerDelegate {
  162. func pagingViewController(_ pagingViewController: PagingViewController, willScrollToItem pagingItem: PagingItem, startingViewController: UIViewController, destinationViewController: UIViewController) {
  163. guard
  164. let item = pagingItem as? PagingIndexItem,
  165. let itemIndex = NCBrandOptions.NCInfoPagingIndex(rawValue: item.index)
  166. else { return }
  167. if itemIndex == .activity && !activityEnabled || itemIndex == .sharing && !sharingEnabled {
  168. pagingViewController.contentInteraction = .none
  169. } else {
  170. self.title = item.title
  171. }
  172. currentVC?.textField?.resignFirstResponder()
  173. self.currentVC = destinationViewController as? NCSharePagingContent
  174. }
  175. }
  176. // MARK: - PagingViewController DataSource
  177. extension NCSharePaging: PagingViewControllerDataSource {
  178. func pagingViewController(_: PagingViewController, viewControllerAt index: Int) -> UIViewController {
  179. let height = pagingViewController.options.menuHeight + NCSharePagingView.headerHeight + NCSharePagingView.tagHeaderHeight
  180. switch NCBrandOptions.NCInfoPagingIndex(rawValue: index) {
  181. case .activity:
  182. guard let viewController = UIStoryboard(name: "NCActivity", bundle: nil).instantiateInitialViewController() as? NCActivity else {
  183. return UIViewController()
  184. }
  185. viewController.height = height
  186. viewController.showComments = true
  187. viewController.didSelectItemEnable = false
  188. viewController.metadata = metadata
  189. viewController.objectType = "files"
  190. return viewController
  191. case .sharing:
  192. guard let viewController = UIStoryboard(name: "NCShare", bundle: nil).instantiateViewController(withIdentifier: "sharing") as? NCShare else {
  193. return UIViewController()
  194. }
  195. viewController.sharingEnabled = sharingEnabled
  196. viewController.metadata = metadata
  197. viewController.height = height
  198. return viewController
  199. default:
  200. return applicationHandle.pagingViewController(pagingViewController, viewControllerAt: index, metadata: metadata)
  201. }
  202. }
  203. func pagingViewController(_: PagingViewController, pagingItemAt index: Int) -> PagingItem {
  204. if NCBrandOptions.NCInfoPagingIndex.allCases.count == 2 && !sharingEnabled {
  205. self.title = NSLocalizedString("_activity_", comment: "")
  206. return PagingIndexItem(index: index, title: "")
  207. }
  208. switch NCBrandOptions.NCInfoPagingIndex(rawValue: index) {
  209. case .activity:
  210. return PagingIndexItem(index: index, title: NSLocalizedString("_activity_", comment: ""))
  211. case .sharing:
  212. return PagingIndexItem(index: index, title: NSLocalizedString("_sharing_", comment: ""))
  213. default:
  214. return applicationHandle.pagingViewController(pagingViewController, pagingItemAt: index)
  215. }
  216. }
  217. func numberOfViewControllers(in pagingViewController: PagingViewController) -> Int {
  218. return NCBrandOptions.NCInfoPagingIndex.allCases.count
  219. }
  220. }
  221. // MARK: - Header
  222. class NCShareHeaderViewController: PagingViewController {
  223. public var image: UIImage?
  224. public var metadata = tableMetadata()
  225. public var activityEnabled = true
  226. public var commentsEnabled = true
  227. public var sharingEnabled = true
  228. override func loadView() {
  229. view = NCSharePagingView(
  230. options: options,
  231. collectionView: collectionView,
  232. pageView: pageViewController.view,
  233. metadata: metadata
  234. )
  235. }
  236. override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  237. guard NCBrandOptions.NCInfoPagingIndex(rawValue: indexPath.item) != .activity || activityEnabled,
  238. NCBrandOptions.NCInfoPagingIndex(rawValue: indexPath.item) != .sharing || sharingEnabled
  239. else { return }
  240. super.collectionView(collectionView, didSelectItemAt: indexPath)
  241. }
  242. }
  243. class NCSharePagingView: PagingView {
  244. static let headerHeight: CGFloat = 90
  245. static var tagHeaderHeight: CGFloat = 0
  246. var metadata = tableMetadata()
  247. public var headerHeightConstraint: NSLayoutConstraint?
  248. // MARK: - View Life Cycle
  249. public init(options: Parchment.PagingOptions, collectionView: UICollectionView, pageView: UIView, metadata: tableMetadata) {
  250. super.init(options: options, collectionView: collectionView, pageView: pageView)
  251. self.metadata = metadata
  252. }
  253. required init?(coder: NSCoder) {
  254. fatalError("init(coder:) has not been implemented")
  255. }
  256. override func setupConstraints() {
  257. guard let headerView = Bundle.main.loadNibNamed("NCShareHeaderView", owner: self, options: nil)?.first as? NCShareHeaderView else { return }
  258. headerView.backgroundColor = .systemBackground
  259. headerView.ocId = metadata.ocId
  260. let dateFormatter = DateFormatter()
  261. dateFormatter.dateStyle = .short
  262. dateFormatter.timeStyle = .short
  263. dateFormatter.locale = Locale.current
  264. if FileManager.default.fileExists(atPath: CCUtility.getDirectoryProviderStorageIconOcId(metadata.ocId, etag: metadata.etag)) {
  265. headerView.imageView.image = UIImage(contentsOfFile: CCUtility.getDirectoryProviderStorageIconOcId(metadata.ocId, etag: metadata.etag))
  266. } else {
  267. if metadata.directory {
  268. let image = metadata.e2eEncrypted ? UIImage(named: "folderEncrypted") : UIImage(named: "folder")
  269. headerView.imageView.image = image?.image(color: NCBrandColor.shared.brandElement, size: image?.size.width ?? 0)
  270. headerView.imageView.image = headerView.imageView.image?.colorizeFolder(metadata: metadata)
  271. } else if !metadata.iconName.isEmpty {
  272. headerView.imageView.image = UIImage(named: metadata.iconName)
  273. } else {
  274. headerView.imageView.image = UIImage(named: "file")
  275. }
  276. }
  277. headerView.path.text = NCUtilityFileSystem.shared.getPath(path: metadata.path, user: metadata.user, fileName: metadata.fileName)
  278. headerView.path.textColor = .label
  279. headerView.path.trailingBuffer = headerView.path.frame.width
  280. if metadata.favorite {
  281. headerView.favorite.setImage(NCUtility.shared.loadImage(named: "star.fill", color: NCBrandColor.shared.yellowFavorite, size: 20), for: .normal)
  282. } else {
  283. headerView.favorite.setImage(NCUtility.shared.loadImage(named: "star.fill", color: .systemGray, size: 20), for: .normal)
  284. }
  285. headerView.info.text = CCUtility.transformedSize(metadata.size) + ", " + NSLocalizedString("_modified_", comment: "") + " " + dateFormatter.string(from: metadata.date as Date)
  286. headerView.info.textColor = .systemGray
  287. headerView.creation.text = NSLocalizedString("_creation_", comment: "") + " " + dateFormatter.string(from: metadata.creationDate as Date)
  288. headerView.creation.textColor = .systemGray
  289. headerView.upload.text = NSLocalizedString("_upload_", comment: "") + " " + dateFormatter.string(from: metadata.uploadDate as Date)
  290. headerView.upload.textColor = .systemGray
  291. headerView.details.setTitleColor(.label, for: .normal)
  292. headerView.details.setTitle(NSLocalizedString("_details_", comment: ""), for: .normal)
  293. headerView.details.layer.cornerRadius = 9
  294. headerView.details.layer.masksToBounds = true
  295. headerView.details.layer.backgroundColor = UIColor(red: 152.0 / 255.0, green: 167.0 / 255.0, blue: 181.0 / 255.0, alpha: 0.8).cgColor
  296. for tag in metadata.tags {
  297. headerView.tagListView.addTag(tag)
  298. }
  299. if metadata.tags.isEmpty {
  300. NCSharePagingView.tagHeaderHeight = 0
  301. } else {
  302. NCSharePagingView.tagHeaderHeight = headerView.tagListView.intrinsicContentSize.height + 10
  303. }
  304. addSubview(headerView)
  305. collectionView.translatesAutoresizingMaskIntoConstraints = false
  306. headerView.translatesAutoresizingMaskIntoConstraints = false
  307. pageView.translatesAutoresizingMaskIntoConstraints = false
  308. NSLayoutConstraint.activate([
  309. collectionView.leadingAnchor.constraint(equalTo: leadingAnchor),
  310. collectionView.trailingAnchor.constraint(equalTo: trailingAnchor),
  311. collectionView.heightAnchor.constraint(equalToConstant: options.menuHeight),
  312. collectionView.topAnchor.constraint(equalTo: headerView.bottomAnchor),
  313. headerView.topAnchor.constraint(equalTo: topAnchor),
  314. headerView.leadingAnchor.constraint(equalTo: leadingAnchor),
  315. headerView.trailingAnchor.constraint(equalTo: trailingAnchor),
  316. headerView.heightAnchor.constraint(equalToConstant: NCSharePagingView.headerHeight + NCSharePagingView.tagHeaderHeight),
  317. pageView.leadingAnchor.constraint(equalTo: leadingAnchor),
  318. pageView.trailingAnchor.constraint(equalTo: trailingAnchor),
  319. pageView.bottomAnchor.constraint(equalTo: bottomAnchor),
  320. pageView.topAnchor.constraint(equalTo: topAnchor, constant: 10)
  321. ])
  322. }
  323. }
  324. class NCShareHeaderView: UIView {
  325. @IBOutlet weak var imageView: UIImageView!
  326. @IBOutlet weak var path: MarqueeLabel!
  327. @IBOutlet weak var info: UILabel!
  328. @IBOutlet weak var creation: UILabel!
  329. @IBOutlet weak var upload: UILabel!
  330. @IBOutlet weak var favorite: UIButton!
  331. @IBOutlet weak var details: UIButton!
  332. @IBOutlet weak var tagListView: TagListView!
  333. var ocId = ""
  334. override func awakeFromNib() {
  335. super.awakeFromNib()
  336. let longGesture = UILongPressGestureRecognizer(target: self, action: #selector(self.longTap))
  337. path.addGestureRecognizer(longGesture)
  338. }
  339. @IBAction func touchUpInsideFavorite(_ sender: UIButton) {
  340. guard let metadata = NCManageDatabase.shared.getMetadataFromOcId(ocId) else { return }
  341. NCNetworking.shared.favoriteMetadata(metadata) { error in
  342. if error == .success {
  343. guard let metadata = NCManageDatabase.shared.getMetadataFromOcId(metadata.ocId) else { return }
  344. self.favorite.setImage(NCUtility.shared.loadImage(
  345. named: "star.fill",
  346. color: metadata.favorite ? NCBrandColor.shared.yellowFavorite : .systemGray,
  347. size: 20), for: .normal)
  348. } else {
  349. NCContentPresenter.shared.showError(error: error)
  350. }
  351. }
  352. }
  353. @IBAction func touchUpInsideDetails(_ sender: UIButton) {
  354. creation.isHidden = !creation.isHidden
  355. upload.isHidden = !upload.isHidden
  356. }
  357. @objc func longTap(sender: UIGestureRecognizer) {
  358. UIPasteboard.general.string = path.text
  359. let error = NKError(errorCode: NCGlobal.shared.errorInternalError, errorDescription: "_copied_path_")
  360. NCContentPresenter.shared.showInfo(error: error)
  361. }
  362. }