NCSharePaging.swift 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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. class NCSharePaging: UIViewController {
  29. private let pagingViewController = NCShareHeaderViewController()
  30. private let appDelegate = UIApplication.shared.delegate as! AppDelegate
  31. private var activityEnabled = true
  32. private var commentsEnabled = true
  33. private var sharingEnabled = true
  34. @objc var metadata = tableMetadata()
  35. var indexPage = NCGlobal.NCSharePagingIndex.activity
  36. // MARK: - View Life Cycle
  37. override func viewDidLoad() {
  38. super.viewDidLoad()
  39. view.backgroundColor = NCBrandColor.shared.systemBackground
  40. navigationItem.leftBarButtonItem = UIBarButtonItem(title: NSLocalizedString("_close_", comment: ""), style: .done, target: self, action: #selector(exitTapped))
  41. // Verify Comments & Sharing enabled
  42. let serverVersionMajor = NCManageDatabase.shared.getCapabilitiesServerInt(account: appDelegate.account, elements: NCElementsJSON.shared.capabilitiesVersionMajor)
  43. let comments = NCManageDatabase.shared.getCapabilitiesServerBool(account: appDelegate.account, elements: NCElementsJSON.shared.capabilitiesFilesComments, exists: false)
  44. if serverVersionMajor >= NCGlobal.shared.nextcloudVersion20 && comments == false {
  45. commentsEnabled = false
  46. }
  47. let sharing = NCManageDatabase.shared.getCapabilitiesServerBool(account: appDelegate.account, elements: NCElementsJSON.shared.capabilitiesFileSharingApiEnabled, exists: false)
  48. if sharing == false {
  49. sharingEnabled = false
  50. }
  51. let activity = NCManageDatabase.shared.getCapabilitiesServerArray(account: appDelegate.account, elements: NCElementsJSON.shared.capabilitiesActivity)
  52. if activity == nil {
  53. activityEnabled = false
  54. }
  55. if indexPage == .sharing && !sharingEnabled {
  56. indexPage = .activity
  57. }
  58. if indexPage == .activity && !activityEnabled && sharingEnabled {
  59. indexPage = .sharing
  60. }
  61. // *** MUST BE THE FIRST ONE ***
  62. pagingViewController.metadata = metadata
  63. pagingViewController.activityEnabled = activityEnabled
  64. pagingViewController.commentsEnabled = commentsEnabled
  65. pagingViewController.sharingEnabled = sharingEnabled
  66. pagingViewController.backgroundColor = NCBrandColor.shared.systemBackground
  67. pagingViewController.menuBackgroundColor = NCBrandColor.shared.systemBackground
  68. pagingViewController.selectedBackgroundColor = NCBrandColor.shared.systemBackground
  69. pagingViewController.textColor = NCBrandColor.shared.label
  70. pagingViewController.selectedTextColor = NCBrandColor.shared.label
  71. // Pagination
  72. addChild(pagingViewController)
  73. view.addSubview(pagingViewController.view)
  74. pagingViewController.didMove(toParent: self)
  75. // Customization
  76. pagingViewController.indicatorOptions = .visible(
  77. height: 1,
  78. zIndex: Int.max,
  79. spacing: .zero,
  80. insets: UIEdgeInsets(top: 0, left: 5, bottom: 0, right: 5)
  81. )
  82. // Contrain the paging view to all edges.
  83. pagingViewController.view.translatesAutoresizingMaskIntoConstraints = false
  84. NSLayoutConstraint.activate([
  85. pagingViewController.view.topAnchor.constraint(equalTo: view.topAnchor),
  86. pagingViewController.view.bottomAnchor.constraint(equalTo: view.bottomAnchor),
  87. pagingViewController.view.leadingAnchor.constraint(equalTo: view.leadingAnchor),
  88. pagingViewController.view.trailingAnchor.constraint(equalTo: view.trailingAnchor)
  89. ])
  90. pagingViewController.dataSource = self
  91. pagingViewController.delegate = self
  92. pagingViewController.select(index: indexPage.rawValue)
  93. let pagingIndexItem = self.pagingViewController(pagingViewController, pagingItemAt: indexPage.rawValue) as! PagingIndexItem
  94. self.title = pagingIndexItem.title
  95. NotificationCenter.default.addObserver(self, selector: #selector(self.changeTheming), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterChangeTheming), object: nil)
  96. changeTheming()
  97. }
  98. override func viewWillAppear(_ animated: Bool) {
  99. super.viewWillAppear(animated)
  100. if appDelegate.disableSharesView {
  101. self.dismiss(animated: false, completion: nil)
  102. }
  103. pagingViewController.menuItemSize = .fixed(
  104. width: self.view.bounds.width / CGFloat(NCGlobal.NCSharePagingIndex.allCases.count),
  105. height: 40)
  106. }
  107. override func viewWillDisappear(_ animated: Bool) {
  108. super.viewWillDisappear(animated)
  109. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterReloadDataSource, userInfo: ["ocId": metadata.ocId, "serverUrl": metadata.serverUrl])
  110. }
  111. @objc func exitTapped() {
  112. self.dismiss(animated: true, completion: nil)
  113. }
  114. // MARK: - NotificationCenter
  115. @objc func changeTheming() {
  116. pagingViewController.indicatorColor = NCBrandColor.shared.brandElement
  117. (pagingViewController.view as! NCSharePagingView).setupConstraints()
  118. pagingViewController.reloadMenu()
  119. }
  120. }
  121. // MARK: - PagingViewController Delegate
  122. extension NCSharePaging: PagingViewControllerDelegate {
  123. func pagingViewController(_ pagingViewController: PagingViewController, willScrollToItem pagingItem: PagingItem, startingViewController: UIViewController, destinationViewController: UIViewController) {
  124. guard
  125. let item = pagingItem as? PagingIndexItem,
  126. let itemIndex = NCGlobal.NCSharePagingIndex(rawValue: item.index)
  127. else { return }
  128. if itemIndex == .activity && !activityEnabled {
  129. pagingViewController.contentInteraction = .none
  130. } else if itemIndex == .sharing && !sharingEnabled {
  131. pagingViewController.contentInteraction = .none
  132. } else {
  133. self.title = item.title
  134. }
  135. }
  136. }
  137. // MARK: - PagingViewController DataSource
  138. extension NCSharePaging: PagingViewControllerDataSource {
  139. func pagingViewController(_: PagingViewController, viewControllerAt index: Int) -> UIViewController {
  140. let height = pagingViewController.options.menuHeight + NCSharePagingView.HeaderHeight
  141. switch NCGlobal.NCSharePagingIndex(rawValue: index) {
  142. case .activity:
  143. let viewController = UIStoryboard(name: "NCActivity", bundle: nil).instantiateInitialViewController() as! NCActivity
  144. viewController.height = height
  145. viewController.showComments = true
  146. viewController.didSelectItemEnable = false
  147. viewController.metadata = metadata
  148. viewController.objectType = "files"
  149. return viewController
  150. case .sharing:
  151. let viewController = UIStoryboard(name: "NCShare", bundle: nil).instantiateViewController(withIdentifier: "sharing") as! NCShare
  152. viewController.sharingEnabled = sharingEnabled
  153. viewController.metadata = metadata
  154. viewController.height = height
  155. return viewController
  156. default:
  157. return UIViewController()
  158. }
  159. }
  160. func pagingViewController(_: PagingViewController, pagingItemAt index: Int) -> PagingItem {
  161. switch NCGlobal.NCSharePagingIndex(rawValue: index) {
  162. case .activity:
  163. return PagingIndexItem(index: index, title: NSLocalizedString("_activity_", comment: ""))
  164. case .sharing:
  165. return PagingIndexItem(index: index, title: NSLocalizedString("_sharing_", comment: ""))
  166. default:
  167. return PagingIndexItem(index: index, title: "")
  168. }
  169. }
  170. func numberOfViewControllers(in pagingViewController: PagingViewController) -> Int {
  171. return 2
  172. }
  173. }
  174. // MARK: - Header
  175. class NCShareHeaderViewController: PagingViewController {
  176. public var image: UIImage?
  177. public var metadata = tableMetadata()
  178. public var activityEnabled = true
  179. public var commentsEnabled = true
  180. public var sharingEnabled = true
  181. override func loadView() {
  182. view = NCSharePagingView(
  183. options: options,
  184. collectionView: collectionView,
  185. pageView: pageViewController.view,
  186. metadata: metadata
  187. )
  188. }
  189. override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  190. if NCGlobal.NCSharePagingIndex(rawValue: indexPath.item) == .activity && !activityEnabled {
  191. return
  192. } else if NCGlobal.NCSharePagingIndex(rawValue: indexPath.item) == .sharing && !sharingEnabled {
  193. return
  194. }
  195. super.collectionView(collectionView, didSelectItemAt: indexPath)
  196. }
  197. }
  198. class NCSharePagingView: PagingView {
  199. static let HeaderHeight: CGFloat = 250
  200. var metadata = tableMetadata()
  201. var headerHeightConstraint: NSLayoutConstraint?
  202. // MARK: - View Life Cycle
  203. public init(options: Parchment.PagingOptions, collectionView: UICollectionView, pageView: UIView, metadata: tableMetadata) {
  204. super.init(options: options, collectionView: collectionView, pageView: pageView)
  205. self.metadata = metadata
  206. }
  207. required init?(coder: NSCoder) {
  208. fatalError("init(coder:) has not been implemented")
  209. }
  210. override func setupConstraints() {
  211. let headerView = Bundle.main.loadNibNamed("NCShareHeaderView", owner: self, options: nil)?.first as! NCShareHeaderView
  212. headerView.backgroundColor = NCBrandColor.shared.systemBackground
  213. headerView.ocId = metadata.ocId
  214. if FileManager.default.fileExists(atPath: CCUtility.getDirectoryProviderStorageIconOcId(metadata.ocId, etag: metadata.etag)) {
  215. headerView.imageView.image = UIImage(contentsOfFile: CCUtility.getDirectoryProviderStorageIconOcId(metadata.ocId, etag: metadata.etag))
  216. } else {
  217. if metadata.directory {
  218. let image = UIImage(named: "folder")!
  219. headerView.imageView.image = image.image(color: NCBrandColor.shared.brandElement, size: image.size.width)
  220. } else if metadata.iconName.count > 0 {
  221. headerView.imageView.image = UIImage(named: metadata.iconName)
  222. } else {
  223. headerView.imageView.image = UIImage(named: "file")
  224. }
  225. }
  226. headerView.path.text = NCUtilityFileSystem.shared.getPath(metadata: metadata)
  227. headerView.path.textColor = NCBrandColor.shared.label
  228. headerView.path.trailingBuffer = headerView.path.frame.width
  229. if metadata.favorite {
  230. headerView.favorite.setImage(NCUtility.shared.loadImage(named: "star.fill", color: NCBrandColor.shared.yellowFavorite, size: 20), for: .normal)
  231. } else {
  232. headerView.favorite.setImage(NCUtility.shared.loadImage(named: "star.fill", color: NCBrandColor.shared.systemGray, size: 20), for: .normal)
  233. }
  234. headerView.info.text = CCUtility.transformedSize(metadata.size) + ", " + CCUtility.dateDiff(metadata.date as Date)
  235. addSubview(headerView)
  236. pageView.translatesAutoresizingMaskIntoConstraints = false
  237. collectionView.translatesAutoresizingMaskIntoConstraints = false
  238. headerView.translatesAutoresizingMaskIntoConstraints = false
  239. headerHeightConstraint = headerView.heightAnchor.constraint(
  240. equalToConstant: NCSharePagingView.HeaderHeight
  241. )
  242. headerHeightConstraint?.isActive = true
  243. NSLayoutConstraint.activate([
  244. collectionView.leadingAnchor.constraint(equalTo: leadingAnchor),
  245. collectionView.trailingAnchor.constraint(equalTo: trailingAnchor),
  246. collectionView.heightAnchor.constraint(equalToConstant: options.menuHeight),
  247. collectionView.topAnchor.constraint(equalTo: headerView.bottomAnchor),
  248. headerView.topAnchor.constraint(equalTo: topAnchor),
  249. headerView.leadingAnchor.constraint(equalTo: leadingAnchor),
  250. headerView.trailingAnchor.constraint(equalTo: trailingAnchor),
  251. pageView.leadingAnchor.constraint(equalTo: leadingAnchor),
  252. pageView.trailingAnchor.constraint(equalTo: trailingAnchor),
  253. pageView.bottomAnchor.constraint(equalTo: bottomAnchor),
  254. pageView.topAnchor.constraint(equalTo: topAnchor, constant: 10)
  255. ])
  256. }
  257. }
  258. class NCShareHeaderView: UIView {
  259. @IBOutlet weak var imageView: UIImageView!
  260. @IBOutlet weak var path: MarqueeLabel!
  261. @IBOutlet weak var info: UILabel!
  262. @IBOutlet weak var favorite: UIButton!
  263. var ocId = ""
  264. override func awakeFromNib() {
  265. super.awakeFromNib()
  266. let longGesture = UILongPressGestureRecognizer(target: self, action: #selector(self.longTap))
  267. path.addGestureRecognizer(longGesture)
  268. }
  269. @IBAction func touchUpInsideFavorite(_ sender: UIButton) {
  270. if let metadata = NCManageDatabase.shared.getMetadataFromOcId(ocId) {
  271. NCNetworking.shared.favoriteMetadata(metadata) { errorCode, errorDescription in
  272. if errorCode == 0 {
  273. if !metadata.favorite {
  274. self.favorite.setImage(NCUtility.shared.loadImage(named: "star.fill", color: NCBrandColor.shared.yellowFavorite, size: 20), for: .normal)
  275. } else {
  276. self.favorite.setImage(NCUtility.shared.loadImage(named: "star.fill", color: NCBrandColor.shared.systemGray, size: 20), for: .normal)
  277. }
  278. } else {
  279. NCContentPresenter.shared.messageNotification("_error_", description: errorDescription, delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, errorCode: errorCode)
  280. }
  281. }
  282. }
  283. }
  284. @objc func longTap(sender: UIGestureRecognizer) {
  285. let board = UIPasteboard.general
  286. board.string = path.text
  287. NCContentPresenter.shared.messageNotification("", description: "_copied_path_", delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.info, errorCode: NCGlobal.shared.errorNoError)
  288. }
  289. }