NCSharePaging.swift 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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. if page.rawValue < pages.count {
  78. pagingViewController.select(index: page.rawValue)
  79. } else {
  80. pagingViewController.select(index: 0)
  81. }
  82. (pagingViewController.view as? NCSharePagingView)?.setupConstraints()
  83. pagingViewController.reloadMenu()
  84. }
  85. override func viewDidAppear(_ animated: Bool) {
  86. super.viewDidAppear(animated)
  87. currentVC = pagingViewController.pageViewController.selectedViewController as? NCSharePagingContent
  88. }
  89. override func viewWillAppear(_ animated: Bool) {
  90. super.viewWillAppear(animated)
  91. if appDelegate?.disableSharesView == true {
  92. self.dismiss(animated: false, completion: nil)
  93. }
  94. pagingViewController.menuItemSize = .fixed(
  95. width: self.view.bounds.width / CGFloat(self.pages.count),
  96. height: 40)
  97. }
  98. override func viewWillDisappear(_ animated: Bool) {
  99. super.viewWillDisappear(animated)
  100. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterReloadDataSource)
  101. }
  102. deinit {
  103. NotificationCenter.default.removeObserver(self, name: UIResponder.keyboardDidShowNotification, object: nil)
  104. NotificationCenter.default.removeObserver(self, name: UIResponder.keyboardWillHideNotification, object: nil)
  105. }
  106. override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
  107. super.viewWillTransition(to: size, with: coordinator)
  108. coordinator.animate(alongsideTransition: nil) { _ in
  109. self.pagingViewController.menuItemSize = .fixed(
  110. width: self.view.bounds.width / CGFloat(self.pages.count),
  111. height: 40)
  112. self.currentVC?.textField?.resignFirstResponder()
  113. }
  114. }
  115. // MARK: - NotificationCenter & Keyboard & TextField
  116. @objc func keyboardWillShow(notification: Notification) {
  117. let frameEndUserInfoKey = UIResponder.keyboardFrameEndUserInfoKey
  118. guard let info = notification.userInfo,
  119. let textField = currentVC?.textField,
  120. let centerObject = textField.superview?.convert(textField.center, to: nil),
  121. let keyboardFrame = info[frameEndUserInfoKey] as? CGRect
  122. else { return }
  123. let diff = keyboardFrame.origin.y - centerObject.y - textField.frame.height
  124. if diff < 0 {
  125. view.frame.origin.y = diff
  126. }
  127. }
  128. @objc func keyboardWillHide(notification: NSNotification) {
  129. view.frame.origin.y = 0
  130. }
  131. @objc func exitTapped() {
  132. self.dismiss(animated: true, completion: nil)
  133. }
  134. @objc func applicationDidEnterBackground(notification: Notification) {
  135. self.dismiss(animated: false, completion: nil)
  136. }
  137. }
  138. // MARK: - PagingViewController Delegate
  139. extension NCSharePaging: PagingViewControllerDelegate {
  140. func pagingViewController(_ pagingViewController: PagingViewController, willScrollToItem pagingItem: PagingItem, startingViewController: UIViewController, destinationViewController: UIViewController) {
  141. currentVC?.textField?.resignFirstResponder()
  142. self.currentVC = destinationViewController as? NCSharePagingContent
  143. }
  144. }
  145. // MARK: - PagingViewController DataSource
  146. extension NCSharePaging: PagingViewControllerDataSource {
  147. func pagingViewController(_: PagingViewController, viewControllerAt index: Int) -> UIViewController {
  148. let height = pagingViewController.options.menuHeight + NCSharePagingView.headerHeight + NCSharePagingView.tagHeaderHeight
  149. if pages[index] == .activity {
  150. guard let viewController = UIStoryboard(name: "NCActivity", bundle: nil).instantiateInitialViewController() as? NCActivity else {
  151. return UIViewController()
  152. }
  153. viewController.height = height
  154. viewController.showComments = true
  155. viewController.didSelectItemEnable = false
  156. viewController.metadata = metadata
  157. viewController.objectType = "files"
  158. return viewController
  159. } else if pages[index] == .sharing {
  160. guard let viewController = UIStoryboard(name: "NCShare", bundle: nil).instantiateViewController(withIdentifier: "sharing") as? NCShare else {
  161. return UIViewController()
  162. }
  163. viewController.metadata = metadata
  164. viewController.height = height
  165. return viewController
  166. } else {
  167. return applicationHandle.pagingViewController(pagingViewController, viewControllerAt: index, metadata: metadata, topHeight: height)
  168. }
  169. }
  170. func pagingViewController(_: PagingViewController, pagingItemAt index: Int) -> PagingItem {
  171. if pages[index] == .activity {
  172. return PagingIndexItem(index: index, title: NSLocalizedString("_activity_", comment: ""))
  173. } else if pages[index] == .sharing {
  174. return PagingIndexItem(index: index, title: NSLocalizedString("_sharing_", comment: ""))
  175. } else {
  176. return applicationHandle.pagingViewController(pagingViewController, pagingItemAt: index)
  177. }
  178. }
  179. func numberOfViewControllers(in pagingViewController: PagingViewController) -> Int {
  180. return self.pages.count
  181. }
  182. }
  183. // MARK: - Header
  184. class NCShareHeaderViewController: PagingViewController {
  185. public var image: UIImage?
  186. public var metadata = tableMetadata()
  187. public var activityEnabled = true
  188. public var commentsEnabled = true
  189. public var sharingEnabled = true
  190. override func loadView() {
  191. view = NCSharePagingView(
  192. options: options,
  193. collectionView: collectionView,
  194. pageView: pageViewController.view,
  195. metadata: metadata
  196. )
  197. }
  198. }
  199. class NCSharePagingView: PagingView {
  200. static let headerHeight: CGFloat = 90
  201. static var tagHeaderHeight: CGFloat = 0
  202. var metadata = tableMetadata()
  203. public var headerHeightConstraint: NSLayoutConstraint?
  204. // MARK: - View Life Cycle
  205. public init(options: Parchment.PagingOptions, collectionView: UICollectionView, pageView: UIView, metadata: tableMetadata) {
  206. super.init(options: options, collectionView: collectionView, pageView: pageView)
  207. self.metadata = metadata
  208. }
  209. required init?(coder: NSCoder) {
  210. fatalError("init(coder:) has not been implemented")
  211. }
  212. override func setupConstraints() {
  213. guard let headerView = Bundle.main.loadNibNamed("NCShareHeaderView", owner: self, options: nil)?.first as? NCShareHeaderView else { return }
  214. headerView.backgroundColor = .systemBackground
  215. headerView.ocId = metadata.ocId
  216. let dateFormatter = DateFormatter()
  217. dateFormatter.dateStyle = .short
  218. dateFormatter.timeStyle = .short
  219. dateFormatter.locale = Locale.current
  220. if FileManager.default.fileExists(atPath: CCUtility.getDirectoryProviderStorageIconOcId(metadata.ocId, etag: metadata.etag)) {
  221. headerView.imageView.image = UIImage(contentsOfFile: CCUtility.getDirectoryProviderStorageIconOcId(metadata.ocId, etag: metadata.etag))
  222. } else {
  223. if metadata.directory {
  224. let image = metadata.e2eEncrypted ? UIImage(named: "folderEncrypted") : UIImage(named: "folder")
  225. headerView.imageView.image = image?.image(color: NCBrandColor.shared.brandElement, size: image?.size.width ?? 0)
  226. headerView.imageView.image = headerView.imageView.image?.colorizeFolder(metadata: metadata)
  227. } else if !metadata.iconName.isEmpty {
  228. headerView.imageView.image = UIImage(named: metadata.iconName)
  229. } else {
  230. headerView.imageView.image = UIImage(named: "file")
  231. }
  232. }
  233. headerView.path.text = NCUtilityFileSystem.shared.getPath(path: metadata.path, user: metadata.user, fileName: metadata.fileName)
  234. headerView.path.textColor = .label
  235. headerView.path.trailingBuffer = headerView.path.frame.width
  236. if metadata.favorite {
  237. headerView.favorite.setImage(NCUtility.shared.loadImage(named: "star.fill", color: NCBrandColor.shared.yellowFavorite, size: 20), for: .normal)
  238. } else {
  239. headerView.favorite.setImage(NCUtility.shared.loadImage(named: "star.fill", color: .systemGray, size: 20), for: .normal)
  240. }
  241. headerView.info.text = CCUtility.transformedSize(metadata.size) + ", " + NSLocalizedString("_modified_", comment: "") + " " + dateFormatter.string(from: metadata.date as Date)
  242. headerView.info.textColor = .systemGray
  243. headerView.creation.text = NSLocalizedString("_creation_", comment: "") + " " + dateFormatter.string(from: metadata.creationDate as Date)
  244. headerView.creation.textColor = .systemGray
  245. headerView.upload.text = NSLocalizedString("_upload_", comment: "") + " " + dateFormatter.string(from: metadata.uploadDate as Date)
  246. headerView.upload.textColor = .systemGray
  247. headerView.details.setTitleColor(.label, for: .normal)
  248. headerView.details.setTitle(NSLocalizedString("_details_", comment: ""), for: .normal)
  249. headerView.details.layer.cornerRadius = 9
  250. headerView.details.layer.masksToBounds = true
  251. headerView.details.layer.backgroundColor = UIColor(red: 152.0 / 255.0, green: 167.0 / 255.0, blue: 181.0 / 255.0, alpha: 0.8).cgColor
  252. for tag in metadata.tags {
  253. headerView.tagListView.addTag(tag)
  254. }
  255. if metadata.tags.isEmpty {
  256. NCSharePagingView.tagHeaderHeight = 0
  257. } else {
  258. NCSharePagingView.tagHeaderHeight = headerView.tagListView.intrinsicContentSize.height + 10
  259. }
  260. addSubview(headerView)
  261. collectionView.translatesAutoresizingMaskIntoConstraints = false
  262. headerView.translatesAutoresizingMaskIntoConstraints = false
  263. pageView.translatesAutoresizingMaskIntoConstraints = false
  264. NSLayoutConstraint.activate([
  265. collectionView.leadingAnchor.constraint(equalTo: leadingAnchor),
  266. collectionView.trailingAnchor.constraint(equalTo: trailingAnchor),
  267. collectionView.heightAnchor.constraint(equalToConstant: options.menuHeight),
  268. collectionView.topAnchor.constraint(equalTo: headerView.bottomAnchor),
  269. headerView.topAnchor.constraint(equalTo: topAnchor),
  270. headerView.leadingAnchor.constraint(equalTo: leadingAnchor),
  271. headerView.trailingAnchor.constraint(equalTo: trailingAnchor),
  272. headerView.heightAnchor.constraint(equalToConstant: NCSharePagingView.headerHeight + NCSharePagingView.tagHeaderHeight),
  273. pageView.leadingAnchor.constraint(equalTo: leadingAnchor),
  274. pageView.trailingAnchor.constraint(equalTo: trailingAnchor),
  275. pageView.bottomAnchor.constraint(equalTo: bottomAnchor),
  276. pageView.topAnchor.constraint(equalTo: topAnchor, constant: 10)
  277. ])
  278. }
  279. }
  280. class NCShareHeaderView: UIView {
  281. @IBOutlet weak var imageView: UIImageView!
  282. @IBOutlet weak var path: MarqueeLabel!
  283. @IBOutlet weak var info: UILabel!
  284. @IBOutlet weak var creation: UILabel!
  285. @IBOutlet weak var upload: UILabel!
  286. @IBOutlet weak var favorite: UIButton!
  287. @IBOutlet weak var details: UIButton!
  288. @IBOutlet weak var tagListView: TagListView!
  289. var ocId = ""
  290. override func awakeFromNib() {
  291. super.awakeFromNib()
  292. let longGesture = UILongPressGestureRecognizer(target: self, action: #selector(self.longTap))
  293. path.addGestureRecognizer(longGesture)
  294. }
  295. @IBAction func touchUpInsideFavorite(_ sender: UIButton) {
  296. guard let metadata = NCManageDatabase.shared.getMetadataFromOcId(ocId) else { return }
  297. NCNetworking.shared.favoriteMetadata(metadata) { error in
  298. if error == .success {
  299. guard let metadata = NCManageDatabase.shared.getMetadataFromOcId(metadata.ocId) else { return }
  300. self.favorite.setImage(NCUtility.shared.loadImage(
  301. named: "star.fill",
  302. color: metadata.favorite ? NCBrandColor.shared.yellowFavorite : .systemGray,
  303. size: 20), for: .normal)
  304. } else {
  305. NCContentPresenter.shared.showError(error: error)
  306. }
  307. }
  308. }
  309. @IBAction func touchUpInsideDetails(_ sender: UIButton) {
  310. creation.isHidden = !creation.isHidden
  311. upload.isHidden = !upload.isHidden
  312. }
  313. @objc func longTap(sender: UIGestureRecognizer) {
  314. UIPasteboard.general.string = path.text
  315. let error = NKError(errorCode: NCGlobal.shared.errorInternalError, errorDescription: "_copied_path_")
  316. NCContentPresenter.shared.showInfo(error: error)
  317. }
  318. }