NCSharePaging.swift 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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. private var activityEnabled = true
  30. private var commentsEnabled = true
  31. private var sharingEnabled = true
  32. @objc var metadata = tableMetadata()
  33. @objc var indexPage: Int = 0
  34. override func viewDidLoad() {
  35. super.viewDidLoad()
  36. // Verify Comments & Sharing enabled
  37. let serverVersionMajor = NCManageDatabase.shared.getCapabilitiesServerInt(account: appDelegate.account, elements: NCElementsJSON.shared.capabilitiesVersionMajor)
  38. let comments = NCManageDatabase.shared.getCapabilitiesServerBool(account: appDelegate.account, elements: NCElementsJSON.shared.capabilitiesFilesComments, exists: false)
  39. if serverVersionMajor >= k_files_comments && comments == false {
  40. commentsEnabled = false
  41. }
  42. let sharing = NCManageDatabase.shared.getCapabilitiesServerBool(account: appDelegate.account, elements: NCElementsJSON.shared.capabilitiesFileSharingApiEnabled, exists: false)
  43. if sharing == false {
  44. sharingEnabled = false
  45. }
  46. let activity = NCManageDatabase.shared.getCapabilitiesServerArray(account: appDelegate.account, elements: NCElementsJSON.shared.capabilitiesActivity)
  47. if activity == nil {
  48. activityEnabled = false
  49. }
  50. if indexPage == NCBrandGlobal.shared.indexPageComments && !commentsEnabled {
  51. indexPage = NCBrandGlobal.shared.indexPageActivity
  52. }
  53. if indexPage == NCBrandGlobal.shared.indexPageSharing && !sharingEnabled {
  54. indexPage = NCBrandGlobal.shared.indexPageActivity
  55. }
  56. if indexPage == NCBrandGlobal.shared.indexPageActivity && !activityEnabled {
  57. if sharingEnabled {
  58. indexPage = NCBrandGlobal.shared.indexPageSharing
  59. } else if commentsEnabled {
  60. indexPage = NCBrandGlobal.shared.indexPageComments
  61. }
  62. }
  63. pagingViewController.activityEnabled = activityEnabled
  64. pagingViewController.commentsEnabled = commentsEnabled
  65. pagingViewController.sharingEnabled = sharingEnabled
  66. pagingViewController.metadata = metadata
  67. NotificationCenter.default.addObserver(self, selector: #selector(self.changeTheming), name: NSNotification.Name(rawValue: k_notificationCenter_changeTheming), object: nil)
  68. self.navigationItem.leftBarButtonItem = UIBarButtonItem(title: NSLocalizedString("_cancel_", comment: ""), style: .done, target: self, action: #selector(exitTapped))
  69. // Pagination
  70. addChild(pagingViewController)
  71. view.addSubview(pagingViewController.view)
  72. pagingViewController.didMove(toParent: self)
  73. // Customization
  74. pagingViewController.indicatorOptions = .visible(
  75. height: 1,
  76. zIndex: Int.max,
  77. spacing: .zero,
  78. insets: UIEdgeInsets(top: 0, left: 5, bottom: 0, right: 5)
  79. )
  80. // Contrain the paging view to all edges.
  81. pagingViewController.view.translatesAutoresizingMaskIntoConstraints = false
  82. NSLayoutConstraint.activate([
  83. pagingViewController.view.topAnchor.constraint(equalTo: view.topAnchor),
  84. pagingViewController.view.bottomAnchor.constraint(equalTo: view.bottomAnchor),
  85. pagingViewController.view.leadingAnchor.constraint(equalTo: view.leadingAnchor),
  86. pagingViewController.view.trailingAnchor.constraint(equalTo: view.trailingAnchor),
  87. ])
  88. pagingViewController.dataSource = self
  89. pagingViewController.delegate = self
  90. pagingViewController.select(index: indexPage)
  91. let pagingIndexItem = self.pagingViewController(pagingViewController, pagingItemAt: indexPage) as! PagingIndexItem
  92. self.title = pagingIndexItem.title
  93. changeTheming()
  94. }
  95. override func viewWillAppear(_ animated: Bool) {
  96. super.viewWillAppear(animated)
  97. if appDelegate.disableSharesView {
  98. self.dismiss(animated: false, completion: nil)
  99. }
  100. pagingViewController.menuItemSize = .fixed(width: self.view.bounds.width/3, height: 40)
  101. }
  102. override func viewWillDisappear(_ animated: Bool) {
  103. super.viewWillDisappear(animated)
  104. NotificationCenter.default.postOnMainThread(name: k_notificationCenter_reloadDataSource, userInfo: ["ocId":metadata.ocId, "serverUrl":metadata.serverUrl])
  105. }
  106. @objc func exitTapped() {
  107. self.dismiss(animated: true, completion: nil)
  108. }
  109. //MARK: - NotificationCenter
  110. @objc func changeTheming() {
  111. view.backgroundColor = NCBrandColor.shared.backgroundForm
  112. pagingViewController.backgroundColor = NCBrandColor.shared.backgroundForm
  113. pagingViewController.menuBackgroundColor = NCBrandColor.shared.backgroundForm
  114. pagingViewController.selectedBackgroundColor = NCBrandColor.shared.backgroundForm
  115. pagingViewController.textColor = NCBrandColor.shared.textView
  116. pagingViewController.selectedTextColor = NCBrandColor.shared.textView
  117. pagingViewController.indicatorColor = NCBrandColor.shared.brandElement
  118. (pagingViewController.view as! NCSharePagingView).setupConstraints()
  119. pagingViewController.reloadMenu()
  120. }
  121. }
  122. // MARK: - PagingViewController Delegate
  123. extension NCSharePaging: PagingViewControllerDelegate {
  124. func pagingViewController(_ pagingViewController: PagingViewController, willScrollToItem pagingItem: PagingItem, startingViewController: UIViewController, destinationViewController: UIViewController) {
  125. guard let item = pagingItem as? PagingIndexItem else { return }
  126. if item.index == NCBrandGlobal.shared.indexPageActivity && !activityEnabled {
  127. pagingViewController.contentInteraction = .none
  128. } else if item.index == NCBrandGlobal.shared.indexPageComments && !commentsEnabled {
  129. pagingViewController.contentInteraction = .none
  130. } else if item.index == NCBrandGlobal.shared.indexPageSharing && !sharingEnabled {
  131. pagingViewController.contentInteraction = .none
  132. } else {
  133. self.title = item.title
  134. }
  135. }
  136. }
  137. // MARK: - PagingViewController DataSource
  138. extension NCSharePaging: PagingViewControllerDataSource {
  139. func pagingViewController(_: PagingViewController, viewControllerAt index: Int) -> UIViewController {
  140. let height = pagingViewController.options.menuHeight + NCSharePagingView.HeaderHeight
  141. let topSafeArea = UIApplication.shared.keyWindow?.safeAreaInsets.top ?? 0
  142. switch index {
  143. case NCBrandGlobal.shared.indexPageActivity:
  144. let viewController = UIStoryboard(name: "NCActivity", bundle: nil).instantiateInitialViewController() as! NCActivity
  145. viewController.insets = UIEdgeInsets(top: height - topSafeArea, left: 0, bottom: 0, right: 0)
  146. viewController.didSelectItemEnable = false
  147. viewController.filterFileId = metadata.fileId
  148. viewController.objectType = "files"
  149. return viewController
  150. case NCBrandGlobal.shared.indexPageComments:
  151. let viewController = UIStoryboard(name: "NCShare", bundle: nil).instantiateViewController(withIdentifier: "comments") as! NCShareComments
  152. viewController.metadata = metadata
  153. viewController.height = height
  154. return viewController
  155. case NCBrandGlobal.shared.indexPageSharing:
  156. let viewController = UIStoryboard(name: "NCShare", bundle: nil).instantiateViewController(withIdentifier: "sharing") as! NCShare
  157. viewController.sharingEnabled = sharingEnabled
  158. viewController.metadata = metadata
  159. viewController.height = height
  160. return viewController
  161. default:
  162. return UIViewController()
  163. }
  164. }
  165. func pagingViewController(_: PagingViewController, pagingItemAt index: Int) -> PagingItem {
  166. switch index {
  167. case NCBrandGlobal.shared.indexPageActivity:
  168. return PagingIndexItem(index: index, title: NSLocalizedString("_activity_", comment: ""))
  169. case NCBrandGlobal.shared.indexPageComments:
  170. return PagingIndexItem(index: index, title: NSLocalizedString("_comments_", comment: ""))
  171. case NCBrandGlobal.shared.indexPageSharing:
  172. return PagingIndexItem(index: index, title: NSLocalizedString("_sharing_", comment: ""))
  173. default:
  174. return PagingIndexItem(index: index, title: "")
  175. }
  176. }
  177. func numberOfViewControllers(in pagingViewController: PagingViewController) -> Int {
  178. return 3
  179. }
  180. }
  181. // MARK: - Header
  182. class NCShareHeaderViewController: PagingViewController {
  183. public var image: UIImage?
  184. public var metadata: tableMetadata?
  185. public var activityEnabled = true
  186. public var commentsEnabled = true
  187. public var sharingEnabled = true
  188. override func loadView() {
  189. view = NCSharePagingView(
  190. options: options,
  191. collectionView: collectionView,
  192. pageView: pageViewController.view,
  193. metadata: metadata
  194. )
  195. }
  196. override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  197. if indexPath.item == NCBrandGlobal.shared.indexPageActivity && !activityEnabled {
  198. return
  199. }
  200. if indexPath.item == NCBrandGlobal.shared.indexPageComments && !commentsEnabled {
  201. return
  202. }
  203. if indexPath.item == NCBrandGlobal.shared.indexPageSharing && !sharingEnabled {
  204. return
  205. }
  206. super.collectionView(collectionView, didSelectItemAt: indexPath)
  207. }
  208. }
  209. class NCSharePagingView: PagingView {
  210. static let HeaderHeight: CGFloat = 250
  211. var metadata: tableMetadata?
  212. var headerHeightConstraint: NSLayoutConstraint?
  213. public init(options: Parchment.PagingOptions, collectionView: UICollectionView, pageView: UIView, metadata: tableMetadata?) {
  214. super.init(options: options, collectionView: collectionView, pageView: pageView)
  215. self.metadata = metadata
  216. }
  217. required init?(coder: NSCoder) {
  218. fatalError("init(coder:) has not been implemented")
  219. }
  220. override func setupConstraints() {
  221. let headerView = Bundle.main.loadNibNamed("NCShareHeaderView", owner: self, options: nil)?.first as! NCShareHeaderView
  222. headerView.backgroundColor = NCBrandColor.shared.backgroundForm
  223. headerView.ocId = metadata!.ocId
  224. if FileManager.default.fileExists(atPath: CCUtility.getDirectoryProviderStorageIconOcId(metadata!.ocId, etag: metadata!.etag)) {
  225. headerView.imageView.image = UIImage.init(contentsOfFile: CCUtility.getDirectoryProviderStorageIconOcId(metadata!.ocId, etag: metadata!.etag))
  226. } else {
  227. if metadata!.directory {
  228. let image = UIImage.init(named: "folder")!
  229. headerView.imageView.image = CCGraphics.changeThemingColorImage(image, width: image.size.width*2, height: image.size.height*2, color: NCBrandColor.shared.brandElement)
  230. } else if metadata!.iconName.count > 0 {
  231. headerView.imageView.image = UIImage.init(named: metadata!.iconName)
  232. } else {
  233. headerView.imageView.image = UIImage.init(named: "file")
  234. }
  235. }
  236. headerView.fileName.text = metadata?.fileNameView
  237. headerView.fileName.textColor = NCBrandColor.shared.textView
  238. if metadata!.favorite {
  239. headerView.favorite.setImage(CCGraphics.changeThemingColorImage(UIImage.init(named: "favorite"), width: 40, height: 40, color: NCBrandColor.shared.yellowFavorite), for: .normal)
  240. } else {
  241. headerView.favorite.setImage(CCGraphics.changeThemingColorImage(UIImage.init(named: "favorite"), width: 40, height: 40, color: NCBrandColor.shared.textInfo), for: .normal)
  242. }
  243. headerView.info.text = CCUtility.transformedSize(metadata!.size) + ", " + CCUtility.dateDiff(metadata!.date as Date)
  244. addSubview(headerView)
  245. pageView.translatesAutoresizingMaskIntoConstraints = false
  246. collectionView.translatesAutoresizingMaskIntoConstraints = false
  247. headerView.translatesAutoresizingMaskIntoConstraints = false
  248. headerHeightConstraint = headerView.heightAnchor.constraint(
  249. equalToConstant: NCSharePagingView.HeaderHeight
  250. )
  251. headerHeightConstraint?.isActive = true
  252. NSLayoutConstraint.activate([
  253. collectionView.leadingAnchor.constraint(equalTo: leadingAnchor),
  254. collectionView.trailingAnchor.constraint(equalTo: trailingAnchor),
  255. collectionView.heightAnchor.constraint(equalToConstant: options.menuHeight),
  256. collectionView.topAnchor.constraint(equalTo: headerView.bottomAnchor),
  257. headerView.topAnchor.constraint(equalTo: topAnchor),
  258. headerView.leadingAnchor.constraint(equalTo: leadingAnchor),
  259. headerView.trailingAnchor.constraint(equalTo: trailingAnchor),
  260. pageView.leadingAnchor.constraint(equalTo: leadingAnchor),
  261. pageView.trailingAnchor.constraint(equalTo: trailingAnchor),
  262. pageView.bottomAnchor.constraint(equalTo: bottomAnchor),
  263. pageView.topAnchor.constraint(equalTo: topAnchor, constant: 10)
  264. ])
  265. }
  266. }
  267. class NCShareHeaderView: UIView {
  268. @IBOutlet weak var imageView: UIImageView!
  269. @IBOutlet weak var fileName: UILabel!
  270. @IBOutlet weak var info: UILabel!
  271. @IBOutlet weak var favorite: UIButton!
  272. private let appDelegate = UIApplication.shared.delegate as! AppDelegate
  273. var ocId = ""
  274. @IBAction func touchUpInsideFavorite(_ sender: UIButton) {
  275. if let metadata = NCManageDatabase.shared.getMetadataFromOcId(ocId) {
  276. NCNetworking.shared.favoriteMetadata(metadata, urlBase: appDelegate.urlBase) { (errorCode, errorDescription) in
  277. if errorCode == 0 {
  278. if !metadata.favorite {
  279. self.favorite.setImage(CCGraphics.changeThemingColorImage(UIImage.init(named: "favorite"), width: 40, height: 40, color: NCBrandColor.shared.yellowFavorite), for: .normal)
  280. } else {
  281. self.favorite.setImage(CCGraphics.changeThemingColorImage(UIImage.init(named: "favorite"), width: 40, height: 40, color: NCBrandColor.shared.textInfo), for: .normal)
  282. }
  283. } else {
  284. NCContentPresenter.shared.messageNotification("_error_", description: errorDescription, delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
  285. }
  286. }
  287. }
  288. }
  289. }