NCSharePaging.swift 13 KB

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