NCSharePaging.swift 14 KB

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