NCSharePaging.swift 12 KB

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