NCSharePaging.swift 13 KB

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