NCSharePaging.swift 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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. // Author Henrik Storch <henrik.storch@nextcloud.com>
  10. //
  11. // This program is free software: you can redistribute it and/or modify
  12. // it under the terms of the GNU General Public License as published by
  13. // the Free Software Foundation, either version 3 of the License, or
  14. // (at your option) any later version.
  15. //
  16. // This program is distributed in the hope that it will be useful,
  17. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. // GNU General Public License for more details.
  20. //
  21. // You should have received a copy of the GNU General Public License
  22. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. //
  24. import UIKit
  25. import Parchment
  26. import NextcloudKit
  27. import MarqueeLabel
  28. import TagListView
  29. protocol NCSharePagingContent {
  30. var textField: UITextField? { get }
  31. }
  32. class NCSharePaging: UIViewController {
  33. private let pagingViewController = NCShareHeaderViewController()
  34. private weak var appDelegate = UIApplication.shared.delegate as? AppDelegate
  35. private var currentVC: NCSharePagingContent?
  36. private let applicationHandle = NCApplicationHandle()
  37. var metadata = tableMetadata()
  38. var pages: [NCBrandOptions.NCInfoPagingTab] = []
  39. var page: NCBrandOptions.NCInfoPagingTab = .activity
  40. // MARK: - View Life Cycle
  41. override func viewDidLoad() {
  42. super.viewDidLoad()
  43. view.backgroundColor = .systemBackground
  44. title = NSLocalizedString("_details_", comment: "")
  45. navigationItem.leftBarButtonItem = UIBarButtonItem(title: NSLocalizedString("_close_", comment: ""), style: .done, target: self, action: #selector(exitTapped))
  46. NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(notification:)), name: UIResponder.keyboardWillShowNotification, object: nil)
  47. NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(notification:)), name: UIResponder.keyboardWillHideNotification, object: nil)
  48. NotificationCenter.default.addObserver(self, selector: #selector(applicationDidEnterBackground(notification:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterApplicationDidEnterBackground), object: nil)
  49. // *** MUST BE THE FIRST ONE ***
  50. pagingViewController.metadata = metadata
  51. pagingViewController.backgroundColor = .systemBackground
  52. pagingViewController.menuBackgroundColor = .systemBackground
  53. pagingViewController.selectedBackgroundColor = .systemBackground
  54. pagingViewController.textColor = .label
  55. pagingViewController.selectedTextColor = .label
  56. // Pagination
  57. addChild(pagingViewController)
  58. view.addSubview(pagingViewController.view)
  59. pagingViewController.didMove(toParent: self)
  60. // Customization
  61. pagingViewController.indicatorOptions = .visible(
  62. height: 1,
  63. zIndex: Int.max,
  64. spacing: .zero,
  65. insets: UIEdgeInsets(top: 0, left: 5, bottom: 0, right: 5)
  66. )
  67. // Contrain the paging view to all edges.
  68. pagingViewController.view.translatesAutoresizingMaskIntoConstraints = false
  69. NSLayoutConstraint.activate([
  70. pagingViewController.view.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
  71. pagingViewController.view.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor),
  72. pagingViewController.view.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor),
  73. pagingViewController.view.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor)
  74. ])
  75. pagingViewController.dataSource = self
  76. pagingViewController.delegate = self
  77. pagingViewController.select(index: page.rawValue < pages.count ? page.rawValue : max(pages.count - 1, 0))
  78. (pagingViewController.view as? NCSharePagingView)?.setupConstraints()
  79. pagingViewController.reloadMenu()
  80. }
  81. override func viewDidAppear(_ animated: Bool) {
  82. super.viewDidAppear(animated)
  83. currentVC = pagingViewController.pageViewController.selectedViewController as? NCSharePagingContent
  84. }
  85. override func viewWillAppear(_ animated: Bool) {
  86. super.viewWillAppear(animated)
  87. if appDelegate?.disableSharesView == true {
  88. self.dismiss(animated: false, completion: nil)
  89. }
  90. pagingViewController.menuItemSize = .fixed(
  91. width: self.view.bounds.width / CGFloat(self.pages.count),
  92. height: 40)
  93. }
  94. override func viewWillDisappear(_ animated: Bool) {
  95. super.viewWillDisappear(animated)
  96. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterReloadDataSource, userInfo: ["serverUrl": metadata.serverUrl])
  97. }
  98. deinit {
  99. NotificationCenter.default.removeObserver(self, name: UIResponder.keyboardDidShowNotification, object: nil)
  100. NotificationCenter.default.removeObserver(self, name: UIResponder.keyboardWillHideNotification, object: nil)
  101. }
  102. override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
  103. super.viewWillTransition(to: size, with: coordinator)
  104. coordinator.animate(alongsideTransition: nil) { _ in
  105. self.pagingViewController.menuItemSize = .fixed(
  106. width: self.view.bounds.width / CGFloat(self.pages.count),
  107. height: 40)
  108. self.currentVC?.textField?.resignFirstResponder()
  109. }
  110. }
  111. // MARK: - NotificationCenter & Keyboard & TextField
  112. @objc func keyboardWillShow(notification: Notification) {
  113. let frameEndUserInfoKey = UIResponder.keyboardFrameEndUserInfoKey
  114. guard let info = notification.userInfo,
  115. let textField = currentVC?.textField,
  116. let centerObject = textField.superview?.convert(textField.center, to: nil),
  117. let keyboardFrame = info[frameEndUserInfoKey] as? CGRect
  118. else { return }
  119. let diff = keyboardFrame.origin.y - centerObject.y - textField.frame.height
  120. if diff < 0 {
  121. view.frame.origin.y = diff
  122. }
  123. }
  124. @objc func keyboardWillHide(notification: NSNotification) {
  125. view.frame.origin.y = 0
  126. }
  127. @objc func exitTapped() {
  128. self.dismiss(animated: true, completion: nil)
  129. }
  130. @objc func applicationDidEnterBackground(notification: Notification) {
  131. self.dismiss(animated: false, completion: nil)
  132. }
  133. }
  134. // MARK: - PagingViewController Delegate
  135. extension NCSharePaging: PagingViewControllerDelegate {
  136. func pagingViewController(_ pagingViewController: PagingViewController, willScrollToItem pagingItem: PagingItem, startingViewController: UIViewController, destinationViewController: UIViewController) {
  137. currentVC?.textField?.resignFirstResponder()
  138. self.currentVC = destinationViewController as? NCSharePagingContent
  139. }
  140. }
  141. // MARK: - PagingViewController DataSource
  142. extension NCSharePaging: PagingViewControllerDataSource {
  143. func pagingViewController(_: PagingViewController, viewControllerAt index: Int) -> UIViewController {
  144. let height = pagingViewController.options.menuHeight + NCSharePagingView.headerHeight + NCSharePagingView.tagHeaderHeight
  145. if pages[index] == .activity {
  146. guard let viewController = UIStoryboard(name: "NCActivity", bundle: nil).instantiateInitialViewController() as? NCActivity else {
  147. return UIViewController()
  148. }
  149. viewController.height = height
  150. viewController.showComments = true
  151. viewController.didSelectItemEnable = false
  152. viewController.metadata = metadata
  153. viewController.objectType = "files"
  154. return viewController
  155. } else if pages[index] == .sharing {
  156. guard let viewController = UIStoryboard(name: "NCShare", bundle: nil).instantiateViewController(withIdentifier: "sharing") as? NCShare else {
  157. return UIViewController()
  158. }
  159. viewController.metadata = metadata
  160. viewController.height = height
  161. return viewController
  162. } else {
  163. return applicationHandle.pagingViewController(pagingViewController, viewControllerAt: index, metadata: metadata, topHeight: height)
  164. }
  165. }
  166. func pagingViewController(_: PagingViewController, pagingItemAt index: Int) -> PagingItem {
  167. if pages[index] == .activity {
  168. return PagingIndexItem(index: index, title: NSLocalizedString("_activity_", comment: ""))
  169. } else if pages[index] == .sharing {
  170. return PagingIndexItem(index: index, title: NSLocalizedString("_sharing_", comment: ""))
  171. } else {
  172. return applicationHandle.pagingViewController(pagingViewController, pagingItemAt: index)
  173. }
  174. }
  175. func numberOfViewControllers(in pagingViewController: PagingViewController) -> Int {
  176. return self.pages.count
  177. }
  178. }
  179. // MARK: - Header
  180. class NCShareHeaderViewController: PagingViewController {
  181. public var image: UIImage?
  182. public var metadata = tableMetadata()
  183. public var activityEnabled = true
  184. public var commentsEnabled = true
  185. public var sharingEnabled = true
  186. override func loadView() {
  187. view = NCSharePagingView(
  188. options: options,
  189. collectionView: collectionView,
  190. pageView: pageViewController.view,
  191. metadata: metadata
  192. )
  193. }
  194. }
  195. class NCSharePagingView: PagingView {
  196. static let headerHeight: CGFloat = 90
  197. static var tagHeaderHeight: CGFloat = 0
  198. var metadata = tableMetadata()
  199. public var headerHeightConstraint: NSLayoutConstraint?
  200. // MARK: - View Life Cycle
  201. public init(options: Parchment.PagingOptions, collectionView: UICollectionView, pageView: UIView, metadata: tableMetadata) {
  202. super.init(options: options, collectionView: collectionView, pageView: pageView)
  203. self.metadata = metadata
  204. }
  205. required init?(coder: NSCoder) {
  206. fatalError("init(coder:) has not been implemented")
  207. }
  208. override func setupConstraints() {
  209. guard let headerView = Bundle.main.loadNibNamed("NCShareHeaderView", owner: self, options: nil)?.first as? NCShareHeaderView else { return }
  210. headerView.backgroundColor = .systemBackground
  211. headerView.ocId = metadata.ocId
  212. let dateFormatter = DateFormatter()
  213. dateFormatter.dateStyle = .short
  214. dateFormatter.timeStyle = .short
  215. dateFormatter.locale = Locale.current
  216. if FileManager.default.fileExists(atPath: CCUtility.getDirectoryProviderStorageIconOcId(metadata.ocId, etag: metadata.etag)) {
  217. headerView.imageView.image = UIImage(contentsOfFile: CCUtility.getDirectoryProviderStorageIconOcId(metadata.ocId, etag: metadata.etag))
  218. } else {
  219. if metadata.directory {
  220. let image = metadata.e2eEncrypted ? UIImage(named: "folderEncrypted") : UIImage(named: "folder")
  221. headerView.imageView.image = image?.image(color: NCBrandColor.shared.brandElement, size: image?.size.width ?? 0)
  222. headerView.imageView.image = headerView.imageView.image?.colorizeFolder(metadata: metadata)
  223. } else if !metadata.iconName.isEmpty {
  224. headerView.imageView.image = UIImage(named: metadata.iconName)
  225. } else {
  226. headerView.imageView.image = UIImage(named: "file")
  227. }
  228. }
  229. headerView.path.text = NCUtilityFileSystem.shared.getPath(path: metadata.path, user: metadata.user, fileName: metadata.fileName)
  230. headerView.path.textColor = .label
  231. headerView.path.trailingBuffer = headerView.path.frame.width
  232. if metadata.favorite {
  233. headerView.favorite.setImage(NCUtility.shared.loadImage(named: "star.fill", color: NCBrandColor.shared.yellowFavorite, size: 20), for: .normal)
  234. } else {
  235. headerView.favorite.setImage(NCUtility.shared.loadImage(named: "star.fill", color: .systemGray, size: 20), for: .normal)
  236. }
  237. headerView.info.text = CCUtility.transformedSize(metadata.size) + ", " + NSLocalizedString("_modified_", comment: "") + " " + dateFormatter.string(from: metadata.date as Date)
  238. headerView.info.textColor = .systemGray
  239. headerView.creation.text = NSLocalizedString("_creation_", comment: "") + " " + dateFormatter.string(from: metadata.creationDate as Date)
  240. headerView.creation.textColor = .systemGray
  241. headerView.upload.text = NSLocalizedString("_upload_", comment: "") + " " + dateFormatter.string(from: metadata.uploadDate as Date)
  242. headerView.upload.textColor = .systemGray
  243. headerView.details.setTitleColor(.label, for: .normal)
  244. headerView.details.setTitle(NSLocalizedString("_details_", comment: ""), for: .normal)
  245. headerView.details.layer.cornerRadius = 9
  246. headerView.details.layer.masksToBounds = true
  247. headerView.details.layer.backgroundColor = UIColor(red: 152.0 / 255.0, green: 167.0 / 255.0, blue: 181.0 / 255.0, alpha: 0.8).cgColor
  248. for tag in metadata.tags {
  249. headerView.tagListView.addTag(tag)
  250. }
  251. if metadata.tags.isEmpty {
  252. NCSharePagingView.tagHeaderHeight = 0
  253. } else {
  254. NCSharePagingView.tagHeaderHeight = headerView.tagListView.intrinsicContentSize.height + 10
  255. }
  256. addSubview(headerView)
  257. collectionView.translatesAutoresizingMaskIntoConstraints = false
  258. headerView.translatesAutoresizingMaskIntoConstraints = false
  259. pageView.translatesAutoresizingMaskIntoConstraints = false
  260. NSLayoutConstraint.activate([
  261. collectionView.leadingAnchor.constraint(equalTo: leadingAnchor),
  262. collectionView.trailingAnchor.constraint(equalTo: trailingAnchor),
  263. collectionView.heightAnchor.constraint(equalToConstant: options.menuHeight),
  264. collectionView.topAnchor.constraint(equalTo: headerView.bottomAnchor),
  265. headerView.topAnchor.constraint(equalTo: topAnchor),
  266. headerView.leadingAnchor.constraint(equalTo: leadingAnchor),
  267. headerView.trailingAnchor.constraint(equalTo: trailingAnchor),
  268. headerView.heightAnchor.constraint(equalToConstant: NCSharePagingView.headerHeight + NCSharePagingView.tagHeaderHeight),
  269. pageView.leadingAnchor.constraint(equalTo: leadingAnchor),
  270. pageView.trailingAnchor.constraint(equalTo: trailingAnchor),
  271. pageView.bottomAnchor.constraint(equalTo: bottomAnchor),
  272. pageView.topAnchor.constraint(equalTo: topAnchor, constant: 10)
  273. ])
  274. }
  275. }
  276. class NCShareHeaderView: UIView {
  277. @IBOutlet weak var imageView: UIImageView!
  278. @IBOutlet weak var path: MarqueeLabel!
  279. @IBOutlet weak var info: UILabel!
  280. @IBOutlet weak var creation: UILabel!
  281. @IBOutlet weak var upload: UILabel!
  282. @IBOutlet weak var favorite: UIButton!
  283. @IBOutlet weak var details: UIButton!
  284. @IBOutlet weak var tagListView: TagListView!
  285. var ocId = ""
  286. override func awakeFromNib() {
  287. super.awakeFromNib()
  288. let longGesture = UILongPressGestureRecognizer(target: self, action: #selector(self.longTap))
  289. path.addGestureRecognizer(longGesture)
  290. }
  291. @IBAction func touchUpInsideFavorite(_ sender: UIButton) {
  292. guard let metadata = NCManageDatabase.shared.getMetadataFromOcId(ocId) else { return }
  293. NCNetworking.shared.favoriteMetadata(metadata) { error in
  294. if error == .success {
  295. guard let metadata = NCManageDatabase.shared.getMetadataFromOcId(metadata.ocId) else { return }
  296. self.favorite.setImage(NCUtility.shared.loadImage(
  297. named: "star.fill",
  298. color: metadata.favorite ? NCBrandColor.shared.yellowFavorite : .systemGray,
  299. size: 20), for: .normal)
  300. } else {
  301. NCContentPresenter.shared.showError(error: error)
  302. }
  303. }
  304. }
  305. @IBAction func touchUpInsideDetails(_ sender: UIButton) {
  306. creation.isHidden = !creation.isHidden
  307. upload.isHidden = !upload.isHidden
  308. }
  309. @objc func longTap(sender: UIGestureRecognizer) {
  310. UIPasteboard.general.string = path.text
  311. let error = NKError(errorCode: NCGlobal.shared.errorInternalError, errorDescription: "_copied_path_")
  312. NCContentPresenter.shared.showInfo(error: error)
  313. }
  314. }