NCSharePaging.swift 15 KB

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