NCSharePaging.swift 16 KB

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