NCSharePaging.swift 11 KB

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