NCSharePaging.swift 12 KB

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