NCSharePaging.swift 11 KB

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