NCSharePaging.swift 17 KB

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