NCSharePaging.swift 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. class NCSharePaging: UIViewController {
  26. private let pagingViewController = NCShareHeaderViewController()
  27. @objc var metadata: tableMetadata?
  28. override func viewDidLoad() {
  29. super.viewDidLoad()
  30. pagingViewController.metadata = metadata
  31. // Navigation Controller
  32. var image = CCGraphics.changeThemingColorImage(UIImage(named: "exit")!, width: 40, height: 40, color: UIColor.gray)
  33. image = image?.withRenderingMode(.alwaysOriginal)
  34. self.navigationItem.rightBarButtonItem = UIBarButtonItem(image: image, style:.plain, target: self, action: #selector(exitTapped))
  35. // Pagination
  36. addChild(pagingViewController)
  37. view.addSubview(pagingViewController.view)
  38. pagingViewController.didMove(toParent: self)
  39. // Customization
  40. pagingViewController.selectedTextColor = .black
  41. pagingViewController.indicatorColor = .black
  42. pagingViewController.indicatorOptions = .visible(
  43. height: 0.5,
  44. zIndex: Int.max,
  45. spacing: .zero,
  46. insets: .zero
  47. )
  48. pagingViewController.menuItemSize = .fixed(width: self.view.bounds.width/3, height: 40)
  49. // Contrain the paging view to all edges.
  50. pagingViewController.view.translatesAutoresizingMaskIntoConstraints = false
  51. NSLayoutConstraint.activate([
  52. pagingViewController.view.topAnchor.constraint(equalTo: view.topAnchor),
  53. pagingViewController.view.bottomAnchor.constraint(equalTo: view.bottomAnchor),
  54. pagingViewController.view.leadingAnchor.constraint(equalTo: view.leadingAnchor),
  55. pagingViewController.view.trailingAnchor.constraint(equalTo: view.trailingAnchor),
  56. ])
  57. pagingViewController.dataSource = self
  58. pagingViewController.select(index: 2)
  59. }
  60. @objc func exitTapped() {
  61. self.dismiss(animated: true, completion: nil)
  62. }
  63. }
  64. extension NCSharePaging: PagingViewControllerDataSource {
  65. func pagingViewController<T>(_ pagingViewController: PagingViewController<T>, viewControllerForIndex index: Int) -> UIViewController {
  66. let height = pagingViewController.options.menuHeight + NCSharePagingView.HeaderHeight
  67. switch index {
  68. case 0:
  69. let viewController = UIStoryboard(name: "NCActivity", bundle: nil).instantiateInitialViewController() as! NCActivity
  70. viewController.insets = UIEdgeInsets(top: height, left: 0, bottom: 0, right: 0)
  71. viewController.refreshControlEnable = false
  72. viewController.didSelectItemEnable = false
  73. viewController.filterFileID = metadata!.fileID
  74. return viewController
  75. case 1:
  76. let viewController = UIStoryboard(name: "NCShare", bundle: nil).instantiateViewController(withIdentifier: "comments") as! NCShareComments
  77. viewController.metadata = metadata!
  78. return viewController
  79. case 2:
  80. let viewController = UIStoryboard(name: "NCShare", bundle: nil).instantiateViewController(withIdentifier: "sharing") as! NCShare
  81. viewController.metadata = metadata!
  82. viewController.height = height
  83. return viewController
  84. default:
  85. return UIViewController()
  86. }
  87. }
  88. func pagingViewController<T>(_ pagingViewController: PagingViewController<T>, pagingItemForIndex index: Int) -> T {
  89. switch index {
  90. case 0:
  91. return PagingIndexItem(index: index, title: NSLocalizedString("_activity_", comment: "")) as! T
  92. case 1:
  93. return PagingIndexItem(index: index, title: NSLocalizedString("_comments_", comment: "")) as! T
  94. case 2:
  95. return PagingIndexItem(index: index, title: NSLocalizedString("_sharing_", comment: "")) as! T
  96. default:
  97. return PagingIndexItem(index: index, title: "") as! T
  98. }
  99. }
  100. func numberOfViewControllers<T>(in: PagingViewController<T>) -> Int{
  101. return 3
  102. }
  103. }
  104. class NCShareHeaderViewController: PagingViewController<PagingIndexItem> {
  105. public var image: UIImage?
  106. public var metadata: tableMetadata?
  107. override func loadView() {
  108. view = NCSharePagingView(
  109. options: options,
  110. collectionView: collectionView,
  111. pageView: pageViewController.view,
  112. metadata: metadata
  113. )
  114. }
  115. }
  116. class NCSharePagingView: PagingView {
  117. static let HeaderHeight: CGFloat = 200
  118. var metadata: tableMetadata?
  119. var headerHeightConstraint: NSLayoutConstraint?
  120. public init(options: Parchment.PagingOptions, collectionView: UICollectionView, pageView: UIView, metadata: tableMetadata?) {
  121. super.init(options: options, collectionView: collectionView, pageView: pageView)
  122. self.metadata = metadata
  123. }
  124. required init?(coder: NSCoder) {
  125. fatalError("init(coder:) has not been implemented")
  126. }
  127. override func setupConstraints() {
  128. let headerView = Bundle.main.loadNibNamed("NCShareHeaderView", owner: self, options: nil)?.first as! NCShareHeaderView
  129. if FileManager.default.fileExists(atPath: CCUtility.getDirectoryProviderStorageIconFileID(metadata!.fileID, fileNameView: metadata!.fileNameView)) {
  130. headerView.imageView.image = UIImage.init(contentsOfFile: CCUtility.getDirectoryProviderStorageIconFileID(metadata!.fileID, fileNameView: metadata!.fileNameView))
  131. } else {
  132. if metadata!.iconName.count > 0 {
  133. headerView.imageView.image = UIImage.init(named: metadata!.iconName)
  134. } else if metadata!.directory {
  135. let image = UIImage.init(named: "folder")!
  136. headerView.imageView.image = CCGraphics.changeThemingColorImage(image, width: image.size.width*2, height: image.size.height*2, color: NCBrandColor.sharedInstance.brandElement)
  137. } else {
  138. headerView.imageView.image = UIImage.init(named: "file")
  139. }
  140. }
  141. headerView.fileName.text = metadata?.fileNameView
  142. if metadata!.favorite {
  143. headerView.favorite.setImage(CCGraphics.changeThemingColorImage(UIImage.init(named: "favorite"), width: 40, height: 40, color: NCBrandColor.sharedInstance.yellowFavorite), for: .normal)
  144. } else {
  145. headerView.favorite.setImage(CCGraphics.changeThemingColorImage(UIImage.init(named: "favorite"), width: 40, height: 40, color: NCBrandColor.sharedInstance.textInfo), for: .normal)
  146. }
  147. headerView.info.text = CCUtility.transformedSize(metadata!.size) + ", " + CCUtility.dateDiff(metadata!.date as Date)
  148. addSubview(headerView)
  149. pageView.translatesAutoresizingMaskIntoConstraints = false
  150. collectionView.translatesAutoresizingMaskIntoConstraints = false
  151. headerView.translatesAutoresizingMaskIntoConstraints = false
  152. headerHeightConstraint = headerView.heightAnchor.constraint(
  153. equalToConstant: NCSharePagingView.HeaderHeight
  154. )
  155. headerHeightConstraint?.isActive = true
  156. NSLayoutConstraint.activate([
  157. collectionView.leadingAnchor.constraint(equalTo: leadingAnchor),
  158. collectionView.trailingAnchor.constraint(equalTo: trailingAnchor),
  159. collectionView.heightAnchor.constraint(equalToConstant: options.menuHeight),
  160. collectionView.topAnchor.constraint(equalTo: headerView.bottomAnchor),
  161. headerView.topAnchor.constraint(equalTo: topAnchor),
  162. headerView.leadingAnchor.constraint(equalTo: leadingAnchor),
  163. headerView.trailingAnchor.constraint(equalTo: trailingAnchor),
  164. pageView.leadingAnchor.constraint(equalTo: leadingAnchor),
  165. pageView.trailingAnchor.constraint(equalTo: trailingAnchor),
  166. pageView.bottomAnchor.constraint(equalTo: bottomAnchor),
  167. pageView.topAnchor.constraint(equalTo: topAnchor)
  168. ])
  169. }
  170. }
  171. class NCShareHeaderView: UIView {
  172. @IBOutlet weak var imageView: UIImageView!
  173. @IBOutlet weak var fileName: UILabel!
  174. @IBOutlet weak var info: UILabel!
  175. @IBOutlet weak var favorite: UIButton!
  176. }