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