NCShare.swift 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. //
  2. // NCShare.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 17/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 NCShare: UIViewController {
  26. private let pagingViewController = NCShareHeaderViewController()
  27. @objc var metadata: tableMetadata?
  28. @objc var shareLink: String = ""
  29. @objc var shareUserAndGroup: String = ""
  30. override func viewDidLoad() {
  31. super.viewDidLoad()
  32. // load preview
  33. if FileManager.default.fileExists(atPath: CCUtility.getDirectoryProviderStorageIconFileID(metadata!.fileID, fileNameView: metadata!.fileNameView)) {
  34. pagingViewController.image = UIImage.init(contentsOfFile: CCUtility.getDirectoryProviderStorageIconFileID(metadata!.fileID, fileNameView: metadata!.fileNameView))
  35. } else {
  36. if metadata!.iconName.count > 0 {
  37. pagingViewController.image = UIImage.init(named: metadata!.iconName)
  38. } else if metadata!.directory {
  39. let image = UIImage.init(named: "folder")!
  40. pagingViewController.image = CCGraphics.changeThemingColorImage(image, width: image.size.width*2, height: image.size.height*2, color: NCBrandColor.sharedInstance.brandElement)
  41. } else {
  42. pagingViewController.image = UIImage.init(named: "file")
  43. }
  44. }
  45. // Navigation Controller
  46. var image = CCGraphics.changeThemingColorImage(UIImage(named: "exit")!, width: 40, height: 40, color: UIColor.gray)
  47. image = image?.withRenderingMode(.alwaysOriginal)
  48. self.navigationItem.rightBarButtonItem = UIBarButtonItem(image: image, style:.plain, target: self, action: #selector(exitTapped))
  49. // Pagination
  50. addChild(pagingViewController)
  51. view.addSubview(pagingViewController.view)
  52. pagingViewController.didMove(toParent: self)
  53. pagingViewController.selectedTextColor = .black
  54. pagingViewController.indicatorColor = .black
  55. pagingViewController.indicatorOptions = .visible(
  56. height: 1,
  57. zIndex: Int.max,
  58. spacing: .zero,
  59. insets: .zero
  60. )
  61. // Contrain the paging view to all edges.
  62. pagingViewController.view.translatesAutoresizingMaskIntoConstraints = false
  63. NSLayoutConstraint.activate([
  64. pagingViewController.view.topAnchor.constraint(equalTo: view.topAnchor),
  65. pagingViewController.view.bottomAnchor.constraint(equalTo: view.bottomAnchor),
  66. pagingViewController.view.leadingAnchor.constraint(equalTo: view.leadingAnchor),
  67. pagingViewController.view.trailingAnchor.constraint(equalTo: view.trailingAnchor),
  68. ])
  69. // Set our data source and delegate.
  70. pagingViewController.dataSource = self
  71. }
  72. @objc func exitTapped() {
  73. self.dismiss(animated: true, completion: nil)
  74. }
  75. }
  76. extension NCShare: PagingViewControllerDataSource {
  77. func pagingViewController<T>(_ pagingViewController: PagingViewController<T>, viewControllerForIndex index: Int) -> UIViewController {
  78. var viewController: UIViewController?
  79. let storyboard = UIStoryboard(name: "NCShare", bundle: nil)
  80. switch index {
  81. case 0:
  82. viewController = storyboard.instantiateViewController(withIdentifier: "activity")
  83. case 1:
  84. viewController = storyboard.instantiateViewController(withIdentifier: "comments")
  85. case 2:
  86. viewController = storyboard.instantiateViewController(withIdentifier: "sharing")
  87. default:
  88. print("")
  89. }
  90. return viewController!
  91. }
  92. func pagingViewController<T>(_ pagingViewController: PagingViewController<T>, pagingItemForIndex index: Int) -> T {
  93. switch index {
  94. case 0:
  95. return PagingIndexItem(index: index, title: "activity") as! T
  96. case 1:
  97. return PagingIndexItem(index: index, title: "comments") as! T
  98. case 2:
  99. return PagingIndexItem(index: index, title: "sharing") as! T
  100. default:
  101. return PagingIndexItem(index: index, title: "") as! T
  102. }
  103. }
  104. func numberOfViewControllers<T>(in: PagingViewController<T>) -> Int{
  105. return 3
  106. }
  107. }
  108. class NCShareHeaderViewController: PagingViewController<PagingIndexItem> {
  109. public var image: UIImage?
  110. override func loadView() {
  111. view = NCShareHeader(
  112. options: options,
  113. collectionView: collectionView,
  114. pageView: pageViewController.view,
  115. image: image
  116. )
  117. }
  118. }
  119. class NCShareHeader: PagingView {
  120. static let HeaderHeight: CGFloat = 200
  121. var image: UIImage?
  122. var headerHeightConstraint: NSLayoutConstraint?
  123. private lazy var headerView: UIImageView = {
  124. let view = UIImageView(image: self.image!)
  125. view.contentMode = .scaleAspectFit
  126. view.clipsToBounds = true
  127. return view
  128. }()
  129. public init(options: Parchment.PagingOptions, collectionView: UICollectionView, pageView: UIView, image: UIImage?) {
  130. super.init(options: options, collectionView: collectionView, pageView: pageView)
  131. self.image = image
  132. }
  133. required init?(coder: NSCoder) {
  134. fatalError("init(coder:) has not been implemented")
  135. }
  136. override func setupConstraints() {
  137. addSubview(headerView)
  138. pageView.translatesAutoresizingMaskIntoConstraints = false
  139. collectionView.translatesAutoresizingMaskIntoConstraints = false
  140. headerView.translatesAutoresizingMaskIntoConstraints = false
  141. headerHeightConstraint = headerView.heightAnchor.constraint(
  142. equalToConstant: NCShareHeader.HeaderHeight
  143. )
  144. headerHeightConstraint?.isActive = true
  145. NSLayoutConstraint.activate([
  146. collectionView.leadingAnchor.constraint(equalTo: leadingAnchor),
  147. collectionView.trailingAnchor.constraint(equalTo: trailingAnchor),
  148. collectionView.heightAnchor.constraint(equalToConstant: options.menuHeight),
  149. collectionView.topAnchor.constraint(equalTo: headerView.bottomAnchor),
  150. headerView.topAnchor.constraint(equalTo: topAnchor),
  151. headerView.leadingAnchor.constraint(equalTo: leadingAnchor),
  152. headerView.trailingAnchor.constraint(equalTo: trailingAnchor),
  153. pageView.leadingAnchor.constraint(equalTo: leadingAnchor),
  154. pageView.trailingAnchor.constraint(equalTo: trailingAnchor),
  155. pageView.bottomAnchor.constraint(equalTo: bottomAnchor),
  156. pageView.topAnchor.constraint(equalTo: topAnchor)
  157. ])
  158. }
  159. }