NCSharePaging.swift 13 KB

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