NCSharePaging.swift 12 KB

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