NCShare.swift 8.7 KB

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