NCCollectionViewCommon.swift 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778
  1. //
  2. // NCCollectionViewCommon.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 12/09/2020.
  6. // Copyright © 2020 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. class NCCollectionViewCommon: UIViewController, UIGestureRecognizerDelegate, UISearchResultsUpdating, UISearchControllerDelegate, UISearchBarDelegate, NCListCellDelegate, NCGridCellDelegate, NCSectionHeaderMenuDelegate, DZNEmptyDataSetSource, DZNEmptyDataSetDelegate {
  25. @IBOutlet weak var collectionView: UICollectionView!
  26. internal let refreshControl = UIRefreshControl()
  27. internal var searchController: UISearchController?
  28. @objc var serverUrl = ""
  29. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  30. internal var isEditMode = false
  31. internal var selectOcId: [String] = []
  32. internal var metadatasSource: [tableMetadata] = []
  33. internal var metadataFolder: tableMetadata?
  34. internal var metadataPush: tableMetadata?
  35. internal var dataSource: NCDataSource?
  36. internal var layout = ""
  37. internal var groupBy = ""
  38. internal var titleButton = ""
  39. internal var itemForLine = 0
  40. private var autoUploadFileName = ""
  41. private var autoUploadDirectory = ""
  42. private var listLayout: NCListLayout!
  43. private var gridLayout: NCGridLayout!
  44. private let headerMenuHeight: CGFloat = 50
  45. private var headerRichWorkspaceHeight: CGFloat = 0
  46. private let sectionHeaderHeight: CGFloat = 20
  47. private let footerHeight: CGFloat = 50
  48. private var timerInputSearch: Timer?
  49. internal var literalSearch: String?
  50. internal var isSearching: Bool = false
  51. internal var isReloadDataSourceNetworkInProgress: Bool = false
  52. // DECLARE
  53. internal var layoutKey = ""
  54. internal var titleCurrentFolder = ""
  55. internal var enableSearchBar: Bool = false
  56. internal var DZNimage: UIImage?
  57. internal var DZNtitle: String = ""
  58. internal var DZNdescription: String = ""
  59. required init?(coder aDecoder: NSCoder) {
  60. super.init(coder: aDecoder)
  61. }
  62. override func viewDidLoad() {
  63. super.viewDidLoad()
  64. self.navigationController?.navigationBar.prefersLargeTitles = true
  65. if enableSearchBar {
  66. searchController = UISearchController(searchResultsController: nil)
  67. searchController?.searchResultsUpdater = self
  68. self.navigationItem.searchController = searchController
  69. searchController?.dimsBackgroundDuringPresentation = false
  70. searchController?.delegate = self
  71. searchController?.searchBar.delegate = self
  72. }
  73. // Cell
  74. collectionView.register(UINib.init(nibName: "NCListCell", bundle: nil), forCellWithReuseIdentifier: "listCell")
  75. collectionView.register(UINib.init(nibName: "NCGridCell", bundle: nil), forCellWithReuseIdentifier: "gridCell")
  76. // Header
  77. collectionView.register(UINib.init(nibName: "NCSectionHeaderMenu", bundle: nil), forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "sectionHeaderMenu")
  78. collectionView.register(UINib.init(nibName: "NCSectionHeader", bundle: nil), forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "sectionHeader")
  79. // Footer
  80. collectionView.register(UINib.init(nibName: "NCSectionFooter", bundle: nil), forSupplementaryViewOfKind: UICollectionView.elementKindSectionFooter, withReuseIdentifier: "sectionFooter")
  81. collectionView.alwaysBounceVertical = true
  82. listLayout = NCListLayout()
  83. gridLayout = NCGridLayout()
  84. // Refresh Control
  85. collectionView.addSubview(refreshControl)
  86. refreshControl.tintColor = NCBrandColor.sharedInstance.brandText
  87. refreshControl.backgroundColor = NCBrandColor.sharedInstance.brandElement
  88. refreshControl.addTarget(self, action: #selector(reloadDataSourceNetwork), for: .valueChanged)
  89. // empty Data Source
  90. self.collectionView.emptyDataSetDelegate = self
  91. self.collectionView.emptyDataSetSource = self
  92. // 3D Touch peek and pop
  93. if traitCollection.forceTouchCapability == .available {
  94. registerForPreviewing(with: self, sourceView: view)
  95. }
  96. NotificationCenter.default.addObserver(self, selector: #selector(changeTheming), name: NSNotification.Name(rawValue: k_notificationCenter_changeTheming), object: nil)
  97. NotificationCenter.default.addObserver(self, selector: #selector(reloadDataSource), name: NSNotification.Name(rawValue: k_notificationCenter_reloadDataSource), object: nil)
  98. NotificationCenter.default.addObserver(self, selector: #selector(deleteFile(_:)), name: NSNotification.Name(rawValue: k_notificationCenter_deleteFile), object: nil)
  99. NotificationCenter.default.addObserver(self, selector: #selector(moveFile(_:)), name: NSNotification.Name(rawValue: k_notificationCenter_moveFile), object: nil)
  100. NotificationCenter.default.addObserver(self, selector: #selector(copyFile(_:)), name: NSNotification.Name(rawValue: k_notificationCenter_copyFile), object: nil)
  101. NotificationCenter.default.addObserver(self, selector: #selector(renameFile(_:)), name: NSNotification.Name(rawValue: k_notificationCenter_renameFile), object: nil)
  102. NotificationCenter.default.addObserver(self, selector: #selector(createFolder(_:)), name: NSNotification.Name(rawValue: k_notificationCenter_createFolder), object: nil)
  103. NotificationCenter.default.addObserver(self, selector: #selector(favoriteFile(_:)), name: NSNotification.Name(rawValue: k_notificationCenter_favoriteFile), object: nil)
  104. NotificationCenter.default.addObserver(self, selector: #selector(downloadStartFile(_:)), name: NSNotification.Name(rawValue: k_notificationCenter_downloadStartFile), object: nil)
  105. NotificationCenter.default.addObserver(self, selector: #selector(downloadedFile(_:)), name: NSNotification.Name(rawValue: k_notificationCenter_downloadedFile), object: nil)
  106. NotificationCenter.default.addObserver(self, selector: #selector(downloadCancelFile(_:)), name: NSNotification.Name(rawValue: k_notificationCenter_downloadCancelFile), object: nil)
  107. NotificationCenter.default.addObserver(self, selector: #selector(uploadStartFile(_:)), name: NSNotification.Name(rawValue: k_notificationCenter_uploadStartFile), object: nil)
  108. NotificationCenter.default.addObserver(self, selector: #selector(uploadedFile(_:)), name: NSNotification.Name(rawValue: k_notificationCenter_uploadedFile), object: nil)
  109. NotificationCenter.default.addObserver(self, selector: #selector(uploadCancelFile(_:)), name: NSNotification.Name(rawValue: k_notificationCenter_uploadCancelFile), object: nil)
  110. NotificationCenter.default.addObserver(self, selector: #selector(triggerProgressTask(_:)), name: NSNotification.Name(rawValue: k_notificationCenter_progressTask), object:nil)
  111. changeTheming()
  112. }
  113. override func viewWillAppear(_ animated: Bool) {
  114. super.viewWillAppear(animated)
  115. self.navigationItem.title = titleCurrentFolder
  116. // get auto upload folder
  117. autoUploadFileName = NCManageDatabase.sharedInstance.getAccountAutoUploadFileName()
  118. autoUploadDirectory = NCManageDatabase.sharedInstance.getAccountAutoUploadDirectory(urlBase: appDelegate.urlBase, account: appDelegate.account)
  119. (layout, _, _, groupBy, _, titleButton, itemForLine) = NCUtility.shared.getLayoutForView(key: layoutKey)
  120. gridLayout.itemForLine = CGFloat(itemForLine)
  121. if layout == k_layout_list {
  122. collectionView?.collectionViewLayout = listLayout
  123. } else {
  124. collectionView?.collectionViewLayout = gridLayout
  125. }
  126. reloadDataSource()
  127. }
  128. override func viewDidAppear(_ animated: Bool) {
  129. super.viewDidAppear(animated)
  130. reloadDataSourceNetwork()
  131. }
  132. override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
  133. super.viewWillTransition(to: size, with: coordinator)
  134. coordinator.animate(alongsideTransition: nil) { _ in
  135. self.collectionView?.collectionViewLayout.invalidateLayout()
  136. }
  137. }
  138. // MARK: - Utility
  139. @objc func minCharTextFieldDidChange(sender: UITextField) {
  140. guard let alertController = self.presentedViewController as? UIAlertController else { return }
  141. guard let password = alertController.textFields?.first else { return }
  142. guard let ok = alertController.actions.last else { return }
  143. ok.isEnabled = password.text?.count ?? 0 >= 8
  144. }
  145. // MARK: - NotificationCenter
  146. @objc func changeTheming() {
  147. appDelegate.changeTheming(self, tableView: nil, collectionView: collectionView, form: false)
  148. }
  149. @objc func deleteFile(_ notification: NSNotification) {
  150. if self.view?.window == nil { return }
  151. if let userInfo = notification.userInfo as NSDictionary? {
  152. if let metadata = userInfo["metadata"] as? tableMetadata, let onlyLocal = userInfo["onlyLocal"] as? Bool {
  153. if onlyLocal {
  154. if let row = dataSource?.reloadMetadata(ocId: metadata.ocId) {
  155. let indexPath = IndexPath(row: row, section: 0)
  156. collectionView?.reloadItems(at: [indexPath])
  157. }
  158. } else {
  159. if let row = dataSource?.deleteMetadata(ocId: metadata.ocId) {
  160. let indexPath = IndexPath(row: row, section: 0)
  161. collectionView?.performBatchUpdates({
  162. collectionView?.deleteItems(at: [indexPath])
  163. }, completion: { (_) in
  164. DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
  165. self.collectionView?.reloadData()
  166. }
  167. })
  168. }
  169. }
  170. }
  171. }
  172. }
  173. @objc func moveFile(_ notification: NSNotification) {
  174. if self.view?.window == nil { return }
  175. if let userInfo = notification.userInfo as NSDictionary? {
  176. if let metadata = userInfo["metadata"] as? tableMetadata, let _ = userInfo["metadataNew"] as? tableMetadata {
  177. if let row = dataSource?.deleteMetadata(ocId: metadata.ocId) {
  178. let indexPath = IndexPath(row: row, section: 0)
  179. collectionView?.performBatchUpdates({
  180. collectionView?.deleteItems(at: [indexPath])
  181. }, completion: { (_) in
  182. DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
  183. self.collectionView?.reloadData()
  184. }
  185. })
  186. }
  187. }
  188. }
  189. }
  190. @objc func copyFile(_ notification: NSNotification) { }
  191. @objc func renameFile(_ notification: NSNotification) {
  192. if self.view?.window == nil { return }
  193. if let userInfo = notification.userInfo as NSDictionary? {
  194. if let metadata = userInfo["metadata"] as? tableMetadata {
  195. if let row = dataSource?.reloadMetadata(ocId: metadata.ocId) {
  196. let indexPath = IndexPath(row: row, section: 0)
  197. collectionView?.performBatchUpdates({
  198. collectionView?.reloadItems(at: [indexPath])
  199. }, completion: { (_) in
  200. self.collectionView?.reloadData()
  201. })
  202. }
  203. }
  204. }
  205. }
  206. @objc func createFolder(_ notification: NSNotification) {
  207. if self.view?.window == nil { return }
  208. if let userInfo = notification.userInfo as NSDictionary? {
  209. if let metadata = userInfo["metadata"] as? tableMetadata {
  210. if metadata.serverUrl == serverUrl && metadata.account == appDelegate.account {
  211. if let row = dataSource?.addMetadata(metadata) {
  212. let indexPath = IndexPath(row: row, section: 0)
  213. collectionView?.performBatchUpdates({
  214. collectionView?.insertItems(at: [indexPath])
  215. }, completion: { (_) in
  216. self.collectionView?.reloadData()
  217. })
  218. }
  219. }
  220. }
  221. }
  222. }
  223. @objc func favoriteFile(_ notification: NSNotification) {
  224. if self.view?.window == nil { return }
  225. if let userInfo = notification.userInfo as NSDictionary? {
  226. if let metadata = userInfo["metadata"] as? tableMetadata {
  227. if dataSource?.getIndexMetadata(ocId: metadata.ocId) != nil {
  228. self.reloadDataSource()
  229. }
  230. }
  231. }
  232. }
  233. @objc func downloadStartFile(_ notification: NSNotification) {
  234. if self.view?.window == nil { return }
  235. if let userInfo = notification.userInfo as NSDictionary? {
  236. if let metadata = userInfo["metadata"] as? tableMetadata {
  237. if let row = dataSource?.reloadMetadata(ocId: metadata.ocId) {
  238. let indexPath = IndexPath(row: row, section: 0)
  239. collectionView?.reloadItems(at: [indexPath])
  240. }
  241. }
  242. }
  243. }
  244. @objc func downloadedFile(_ notification: NSNotification) {
  245. if self.view?.window == nil { return }
  246. if let userInfo = notification.userInfo as NSDictionary? {
  247. if let metadata = userInfo["metadata"] as? tableMetadata, let _ = userInfo["errorCode"] as? Int {
  248. if let row = dataSource?.reloadMetadata(ocId: metadata.ocId) {
  249. let indexPath = IndexPath(row: row, section: 0)
  250. collectionView?.reloadItems(at: [indexPath])
  251. }
  252. }
  253. }
  254. }
  255. @objc func downloadCancelFile(_ notification: NSNotification) {
  256. if self.view?.window == nil { return }
  257. if let userInfo = notification.userInfo as NSDictionary? {
  258. if let metadata = userInfo["metadata"] as? tableMetadata {
  259. if let row = dataSource?.reloadMetadata(ocId: metadata.ocId) {
  260. let indexPath = IndexPath(row: row, section: 0)
  261. collectionView?.reloadItems(at: [indexPath])
  262. }
  263. }
  264. }
  265. }
  266. @objc func uploadStartFile(_ notification: NSNotification) {
  267. if self.view?.window == nil { return }
  268. if let userInfo = notification.userInfo as NSDictionary? {
  269. if let metadata = userInfo["metadata"] as? tableMetadata {
  270. if metadata.serverUrl == serverUrl && metadata.account == appDelegate.account {
  271. if let row = dataSource?.addMetadata(metadata) {
  272. let indexPath = IndexPath(row: row, section: 0)
  273. collectionView?.performBatchUpdates({
  274. collectionView?.insertItems(at: [indexPath])
  275. }, completion: { (_) in
  276. self.collectionView?.reloadData()
  277. })
  278. }
  279. }
  280. }
  281. }
  282. }
  283. @objc func uploadedFile(_ notification: NSNotification) {
  284. if self.view?.window == nil { return }
  285. if let userInfo = notification.userInfo as NSDictionary? {
  286. if let metadata = userInfo["metadata"] as? tableMetadata, let ocIdTemp = userInfo["ocIdTemp"] as? String, let _ = userInfo["errorCode"] as? Int {
  287. if metadata.serverUrl == serverUrl && metadata.account == appDelegate.account {
  288. dataSource?.reloadMetadata(ocId: metadata.ocId, ocIdTemp: ocIdTemp)
  289. collectionView?.reloadData()
  290. }
  291. }
  292. }
  293. }
  294. @objc func uploadCancelFile(_ notification: NSNotification) {
  295. if self.view?.window == nil { return }
  296. if let userInfo = notification.userInfo as NSDictionary? {
  297. if let metadata = userInfo["metadata"] as? tableMetadata {
  298. if metadata.serverUrl == serverUrl && metadata.account == appDelegate.account {
  299. if let row = dataSource?.deleteMetadata(ocId: metadata.ocId) {
  300. let indexPath = IndexPath(row: row, section: 0)
  301. collectionView?.performBatchUpdates({
  302. collectionView?.deleteItems(at: [indexPath])
  303. }, completion: { (_) in
  304. DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
  305. self.collectionView?.reloadData()
  306. }
  307. })
  308. } else {
  309. self.reloadDataSource()
  310. }
  311. }
  312. }
  313. }
  314. }
  315. @objc func triggerProgressTask(_ notification: NSNotification) {
  316. if self.view?.window == nil { return }
  317. if let userInfo = notification.userInfo as NSDictionary? {
  318. if let ocId = userInfo["ocId"] as? String {
  319. let progressNumber = userInfo["progress"] as? NSNumber ?? 0
  320. let progress = progressNumber.floatValue
  321. if let index = dataSource?.getIndexMetadata(ocId: ocId) {
  322. if let cell = collectionView?.cellForItem(at: IndexPath(row: index, section: 0)) {
  323. if cell is NCListCell {
  324. let cell = cell as! NCListCell
  325. if progress > 0 {
  326. cell.progressView?.isHidden = false
  327. cell.progressView?.progress = progress
  328. cell.setButtonMore(named: "stop")
  329. }
  330. } else if cell is NCGridCell {
  331. let cell = cell as! NCGridCell
  332. if progress > 0 {
  333. cell.progressView.isHidden = false
  334. cell.progressView.progress = progress
  335. cell.setButtonMore(named: "stop")
  336. }
  337. }
  338. }
  339. }
  340. }
  341. }
  342. }
  343. // MARK: DZNEmpty
  344. func backgroundColor(forEmptyDataSet scrollView: UIScrollView) -> UIColor? {
  345. return NCBrandColor.sharedInstance.backgroundView
  346. }
  347. func image(forEmptyDataSet scrollView: UIScrollView) -> UIImage? {
  348. if searchController?.isActive ?? false {
  349. return CCGraphics.changeThemingColorImage(UIImage.init(named: "search"), width: 300, height: 300, color: NCBrandColor.sharedInstance.yellowFavorite)
  350. }
  351. return DZNimage
  352. }
  353. func title(forEmptyDataSet scrollView: UIScrollView) -> NSAttributedString? {
  354. var text = "\n"+NSLocalizedString(DZNtitle, comment: "")
  355. if searchController?.isActive ?? false {
  356. if isReloadDataSourceNetworkInProgress {
  357. text = "\n"+NSLocalizedString("_search_in_progress_", comment: "")
  358. } else {
  359. text = "\n"+NSLocalizedString("_search_no_record_found_", comment: "")
  360. }
  361. }
  362. let attributes = [NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 20), NSAttributedString.Key.foregroundColor: UIColor.lightGray]
  363. return NSAttributedString.init(string: text, attributes: attributes)
  364. }
  365. func description(forEmptyDataSet scrollView: UIScrollView!) -> NSAttributedString! {
  366. var text = "\n"+NSLocalizedString(DZNdescription, comment: "")
  367. if searchController?.isActive ?? false {
  368. text = "\n"+NSLocalizedString("_search_instruction_", comment: "")
  369. }
  370. let attributes = [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 14), NSAttributedString.Key.foregroundColor: UIColor.lightGray]
  371. return NSAttributedString.init(string: text, attributes: attributes)
  372. }
  373. func emptyDataSetShouldAllowScroll(_ scrollView: UIScrollView) -> Bool {
  374. return true
  375. }
  376. // MARK: SEARCH
  377. func updateSearchResults(for searchController: UISearchController) {
  378. timerInputSearch?.invalidate()
  379. timerInputSearch = Timer.scheduledTimer(timeInterval: 1.5, target: self, selector: #selector(reloadDataSourceNetwork), userInfo: nil, repeats: false)
  380. literalSearch = searchController.searchBar.text
  381. collectionView?.reloadData()
  382. }
  383. func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
  384. isSearching = true
  385. metadatasSource.removeAll()
  386. reloadDataSource()
  387. }
  388. func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
  389. isSearching = false
  390. literalSearch = ""
  391. reloadDataSource()
  392. }
  393. // MARK: TAP EVENT
  394. func tapSwitchHeader(sender: Any) {
  395. if collectionView.collectionViewLayout == gridLayout {
  396. // list layout
  397. UIView.animate(withDuration: 0.0, animations: {
  398. self.collectionView.collectionViewLayout.invalidateLayout()
  399. self.collectionView.setCollectionViewLayout(self.listLayout, animated: false, completion: { (_) in
  400. self.collectionView.reloadData()
  401. self.collectionView.setContentOffset(CGPoint(x:0,y:0), animated: false)
  402. })
  403. })
  404. layout = k_layout_list
  405. NCUtility.shared.setLayoutForView(key: layoutKey, layout: layout)
  406. } else {
  407. // grid layout
  408. UIView.animate(withDuration: 0.0, animations: {
  409. self.collectionView.collectionViewLayout.invalidateLayout()
  410. self.collectionView.setCollectionViewLayout(self.gridLayout, animated: false, completion: { (_) in
  411. self.collectionView.reloadData()
  412. self.collectionView.setContentOffset(CGPoint(x:0,y:0), animated: false)
  413. })
  414. })
  415. layout = k_layout_grid
  416. NCUtility.shared.setLayoutForView(key: layoutKey, layout: layout)
  417. }
  418. }
  419. func tapOrderHeader(sender: Any) {
  420. let sortMenu = NCSortMenu()
  421. sortMenu.toggleMenu(viewController: self, key: layoutKey, sortButton: sender as? UIButton, serverUrl: serverUrl)
  422. }
  423. func tapMoreHeader(sender: Any) {
  424. }
  425. func tapMoreListItem(with objectId: String, namedButtonMore: String, sender: Any) {
  426. tapMoreGridItem(with: objectId, namedButtonMore: namedButtonMore, sender: sender)
  427. }
  428. func tapShareListItem(with objectId: String, sender: Any) {
  429. guard let metadata = NCManageDatabase.sharedInstance.getMetadataFromOcId(objectId) else {
  430. return
  431. }
  432. NCMainCommon.shared.openShare(ViewController: self, metadata: metadata, indexPage: 2)
  433. }
  434. func tapMoreGridItem(with objectId: String, namedButtonMore: String, sender: Any) {
  435. guard let metadata = NCManageDatabase.sharedInstance.getMetadataFromOcId(objectId) else { return }
  436. guard let tabBarController = self.tabBarController else { return }
  437. if namedButtonMore == "more" {
  438. toggleMoreMenu(viewController: tabBarController, metadata: metadata, selectOcId: selectOcId)
  439. } else if namedButtonMore == "stop" {
  440. NCMainCommon.shared.cancelTransferMetadata(metadata, uploadStatusForcedStart: false)
  441. }
  442. }
  443. // MARK: SEGUE
  444. @objc func segue(metadata: tableMetadata) {
  445. self.metadataPush = metadata
  446. performSegue(withIdentifier: "segueDetail", sender: self)
  447. }
  448. override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  449. let photoDataSource: NSMutableArray = []
  450. for metadata in (dataSource?.metadatas ?? [tableMetadata]()) {
  451. if metadata.typeFile == k_metadataTypeFile_image || metadata.typeFile == k_metadataTypeFile_video {
  452. photoDataSource.add(metadata)
  453. }
  454. }
  455. if let segueNavigationController = segue.destination as? UINavigationController {
  456. if let segueViewController = segueNavigationController.topViewController as? NCDetailViewController {
  457. segueViewController.metadata = metadataPush
  458. }
  459. }
  460. }
  461. // MARK: - NC API & Algorithm
  462. @objc func reloadDataSource() {
  463. if metadataFolder?.richWorkspace?.count ?? 0 == 0 {
  464. headerRichWorkspaceHeight = 0
  465. } else {
  466. headerRichWorkspaceHeight = UIScreen.main.bounds.size.height / 4
  467. }
  468. }
  469. @objc func reloadDataSourceNetwork() { }
  470. @objc func networkSearch() {
  471. if literalSearch?.count ?? 0 > 1 {
  472. isReloadDataSourceNetworkInProgress = true
  473. collectionView?.reloadData()
  474. NCNetworking.shared.searchFiles(urlBase: appDelegate.urlBase, user: appDelegate.user, literal: literalSearch!) { (account, metadatas, errorCode, errorDescription) in
  475. if self.searchController?.isActive ?? false && errorCode == 0 {
  476. self.metadatasSource = metadatas!
  477. }
  478. self.isReloadDataSourceNetworkInProgress = false
  479. self.reloadDataSource()
  480. }
  481. }
  482. }
  483. }
  484. // MARK: - 3D Touch peek and pop
  485. extension NCCollectionViewCommon: UIViewControllerPreviewingDelegate {
  486. func previewingContext(_ previewingContext: UIViewControllerPreviewing, viewControllerForLocation location: CGPoint) -> UIViewController? {
  487. guard let point = collectionView?.convert(location, from: collectionView?.superview) else { return nil }
  488. guard let indexPath = collectionView?.indexPathForItem(at: point) else { return nil }
  489. guard let metadata = dataSource?.cellForItemAt(indexPath: indexPath) else { return nil }
  490. guard let viewController = UIStoryboard(name: "CCPeekPop", bundle: nil).instantiateViewController(withIdentifier: "PeekPopImagePreview") as? CCPeekPop else { return nil }
  491. viewController.metadata = metadata
  492. if layout == k_layout_grid {
  493. guard let cell = collectionView?.cellForItem(at: indexPath) as? NCGridCell else { return nil }
  494. previewingContext.sourceRect = cell.frame
  495. viewController.imageFile = cell.imageItem.image
  496. } else {
  497. guard let cell = collectionView?.cellForItem(at: indexPath) as? NCListCell else { return nil }
  498. previewingContext.sourceRect = cell.frame
  499. viewController.imageFile = cell.imageItem.image
  500. }
  501. viewController.showOpenIn = true
  502. viewController.showOpenQuickLook = NCUtility.shared.isQuickLookDisplayable(metadata: metadata)
  503. viewController.showShare = false
  504. return viewController
  505. }
  506. func previewingContext(_ previewingContext: UIViewControllerPreviewing, commit viewControllerToCommit: UIViewController) {
  507. guard let indexPath = collectionView?.indexPathForItem(at: previewingContext.sourceRect.origin) else { return }
  508. collectionView(collectionView, didSelectItemAt: indexPath)
  509. }
  510. }
  511. // MARK: - Collection View
  512. extension NCCollectionViewCommon: UICollectionViewDelegate {
  513. func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { }
  514. }
  515. extension NCCollectionViewCommon: UICollectionViewDataSource {
  516. func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
  517. if (indexPath.section == 0) {
  518. if kind == UICollectionView.elementKindSectionHeader {
  519. let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "sectionHeaderMenu", for: indexPath) as! NCSectionHeaderMenu
  520. if collectionView.collectionViewLayout == gridLayout {
  521. header.buttonSwitch.setImage(CCGraphics.changeThemingColorImage(UIImage.init(named: "switchList"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), for: .normal)
  522. } else {
  523. header.buttonSwitch.setImage(CCGraphics.changeThemingColorImage(UIImage.init(named: "switchGrid"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), for: .normal)
  524. }
  525. header.delegate = self
  526. header.backgroundColor = NCBrandColor.sharedInstance.backgroundView
  527. header.separator.backgroundColor = NCBrandColor.sharedInstance.separator
  528. header.setStatusButton(count: dataSource?.metadatas.count ?? 0)
  529. header.setTitleSorted(datasourceTitleButton: titleButton)
  530. header.labelSectionHeightConstraint.constant = headerRichWorkspaceHeight
  531. if groupBy == "none" {
  532. header.labelSection.isHidden = true
  533. header.labelSectionHeightConstraint.constant = 0
  534. } else {
  535. header.labelSection.isHidden = false
  536. header.setTitleLabel(title: "")
  537. header.labelSectionHeightConstraint.constant = sectionHeaderHeight
  538. }
  539. if metadataFolder?.richWorkspace?.count ?? 0 == 0 {
  540. header.labelSection.isHidden = true
  541. } else {
  542. header.labelSection.isHidden = false
  543. }
  544. return header
  545. } else {
  546. let footer = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "sectionFooter", for: indexPath) as! NCSectionFooter
  547. let info = dataSource?.getFilesInformation()
  548. footer.setTitleLabel(directories: info?.directories ?? 0, files: info?.files ?? 0, size: info?.size ?? 0)
  549. return footer
  550. }
  551. } else {
  552. if kind == UICollectionView.elementKindSectionHeader {
  553. let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "sectionHeader", for: indexPath) as! NCSectionHeader
  554. header.setTitleLabel(title: "")
  555. return header
  556. } else {
  557. let footer = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "sectionFooter", for: indexPath) as! NCSectionFooter
  558. let info = dataSource?.getFilesInformation()
  559. footer.setTitleLabel(directories: info?.directories ?? 0, files: info?.files ?? 0, size: info?.size ?? 0)
  560. return footer
  561. }
  562. }
  563. }
  564. func numberOfSections(in collectionView: UICollectionView) -> Int {
  565. return 1
  566. }
  567. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  568. return dataSource?.numberOfItemsInSection(section: section) ?? 1
  569. }
  570. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  571. let cell: UICollectionViewCell
  572. guard let metadata = dataSource?.cellForItemAt(indexPath: indexPath) else {
  573. return collectionView.dequeueReusableCell(withReuseIdentifier: "listCell", for: indexPath) as! NCListCell
  574. }
  575. if layout == k_layout_grid {
  576. cell = collectionView.dequeueReusableCell(withReuseIdentifier: "gridCell", for: indexPath) as! NCGridCell
  577. } else {
  578. cell = collectionView.dequeueReusableCell(withReuseIdentifier: "listCell", for: indexPath) as! NCListCell
  579. }
  580. let shares = NCManageDatabase.sharedInstance.getTableShares(account: metadata.account, serverUrl: metadata.serverUrl, fileName: metadata.fileName)
  581. NCCollectionCommon.shared.cellForItemAt(indexPath: indexPath, collectionView: collectionView, cell: cell, metadata: metadata, metadataFolder: nil, serverUrl: metadata.serverUrl, isEditMode: isEditMode, selectocId: selectOcId, autoUploadFileName: autoUploadFileName, autoUploadDirectory: autoUploadDirectory, hideButtonMore: false, downloadThumbnail: true, shares: shares, source: self, dataSource: dataSource)
  582. return cell
  583. }
  584. }
  585. extension NCCollectionViewCommon: UICollectionViewDelegateFlowLayout {
  586. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
  587. if section == 0 {
  588. return CGSize(width: collectionView.frame.width, height: headerMenuHeight + sectionHeaderHeight + headerRichWorkspaceHeight)
  589. } else {
  590. return CGSize(width: collectionView.frame.width, height: sectionHeaderHeight)
  591. }
  592. }
  593. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
  594. let sections = 1
  595. if (section == sections - 1) {
  596. return CGSize(width: collectionView.frame.width, height: footerHeight)
  597. } else {
  598. return CGSize(width: collectionView.frame.width, height: 0)
  599. }
  600. }
  601. }