NCSharePaging.swift 15 KB

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