NCOffline.swift 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  1. //
  2. // NCOffline.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 24/10/2018.
  6. // Copyright © 2018 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 NCOffline: UIViewController, UIGestureRecognizerDelegate, NCListCellDelegate, NCGridCellDelegate, NCSectionHeaderMenuDelegate, DZNEmptyDataSetSource, DZNEmptyDataSetDelegate {
  25. @IBOutlet weak var collectionView: UICollectionView!
  26. var titleCurrentFolder = NSLocalizedString("_manage_file_offline_", comment: "")
  27. var serverUrl = ""
  28. private let appDelegate = UIApplication.shared.delegate as! AppDelegate
  29. private var metadataPush: tableMetadata?
  30. private var isEditMode = false
  31. private var selectocId: [String] = []
  32. private var dataSource: NCDataSource?
  33. private var layout = ""
  34. private var groupBy = ""
  35. private var titleButton = ""
  36. private var itemForLine = 0
  37. private var autoUploadFileName = ""
  38. private var autoUploadDirectory = ""
  39. private var listLayout: NCListLayout!
  40. private var gridLayout: NCGridLayout!
  41. private let headerMenuHeight: CGFloat = 50
  42. private let sectionHeaderHeight: CGFloat = 20
  43. private let footerHeight: CGFloat = 50
  44. private let refreshControl = UIRefreshControl()
  45. required init?(coder aDecoder: NSCoder) {
  46. super.init(coder: aDecoder)
  47. appDelegate.activeOffline = self
  48. }
  49. override func viewDidLoad() {
  50. super.viewDidLoad()
  51. // Cell
  52. collectionView.register(UINib.init(nibName: "NCListCell", bundle: nil), forCellWithReuseIdentifier: "listCell")
  53. collectionView.register(UINib.init(nibName: "NCGridCell", bundle: nil), forCellWithReuseIdentifier: "gridCell")
  54. // Header
  55. collectionView.register(UINib.init(nibName: "NCSectionHeaderMenu", bundle: nil), forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "sectionHeaderMenu")
  56. collectionView.register(UINib.init(nibName: "NCSectionHeader", bundle: nil), forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "sectionHeader")
  57. // Footer
  58. collectionView.register(UINib.init(nibName: "NCSectionFooter", bundle: nil), forSupplementaryViewOfKind: UICollectionView.elementKindSectionFooter, withReuseIdentifier: "sectionFooter")
  59. collectionView.alwaysBounceVertical = true
  60. listLayout = NCListLayout()
  61. gridLayout = NCGridLayout()
  62. // Refresh Control
  63. collectionView.addSubview(refreshControl)
  64. refreshControl.tintColor = NCBrandColor.sharedInstance.brandText
  65. refreshControl.backgroundColor = NCBrandColor.sharedInstance.brandElement
  66. refreshControl.addTarget(self, action: #selector(reloadDataSourceNetwork), for: .valueChanged)
  67. // empty Data Source
  68. self.collectionView.emptyDataSetDelegate = self
  69. self.collectionView.emptyDataSetSource = self
  70. // 3D Touch peek and pop
  71. if traitCollection.forceTouchCapability == .available {
  72. registerForPreviewing(with: self, sourceView: view)
  73. }
  74. NotificationCenter.default.addObserver(self, selector: #selector(changeTheming), name: NSNotification.Name(rawValue: k_notificationCenter_changeTheming), object: nil)
  75. NotificationCenter.default.addObserver(self, selector: #selector(deleteFile(_:)), name: NSNotification.Name(rawValue: k_notificationCenter_deleteFile), object: nil)
  76. NotificationCenter.default.addObserver(self, selector: #selector(reloadDataSource), name: NSNotification.Name(rawValue: k_notificationCenter_reloadDataSource), object: nil)
  77. NotificationCenter.default.addObserver(self, selector: #selector(downloadedFile(_:)), name: NSNotification.Name(rawValue: k_notificationCenter_downloadedFile), object: nil)
  78. changeTheming()
  79. }
  80. override func viewWillAppear(_ animated: Bool) {
  81. super.viewWillAppear(animated)
  82. self.navigationItem.title = titleCurrentFolder
  83. // get auto upload folder
  84. autoUploadFileName = NCManageDatabase.sharedInstance.getAccountAutoUploadFileName()
  85. autoUploadDirectory = NCManageDatabase.sharedInstance.getAccountAutoUploadDirectory(urlBase: appDelegate.urlBase, account: appDelegate.account)
  86. (layout, _, _, groupBy, _, titleButton, itemForLine) = NCUtility.shared.getLayoutForView(key: k_layout_view_offline)
  87. gridLayout.itemForLine = CGFloat(itemForLine)
  88. if layout == k_layout_list {
  89. collectionView.collectionViewLayout = listLayout
  90. } else {
  91. collectionView.collectionViewLayout = gridLayout
  92. }
  93. reloadDataSource()
  94. }
  95. override func viewDidAppear(_ animated: Bool) {
  96. super.viewDidAppear(animated)
  97. reloadDataSourceNetwork()
  98. }
  99. override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
  100. super.viewWillTransition(to: size, with: coordinator)
  101. coordinator.animate(alongsideTransition: nil) { _ in
  102. self.collectionView?.collectionViewLayout.invalidateLayout()
  103. }
  104. }
  105. //MARK: - NotificationCenter
  106. @objc func deleteFile(_ notification: NSNotification) {
  107. if self.view?.window == nil { return }
  108. if let userInfo = notification.userInfo as NSDictionary? {
  109. if let metadata = userInfo["metadata"] as? tableMetadata, let errorCode = userInfo["errorCode"] as? Int, let errorDescription = userInfo["errorDescription"] as? String {
  110. if errorCode == 0 {
  111. self.dataSource?.deleteMetadata(ocId: metadata.ocId)
  112. collectionView.reloadData()
  113. } else {
  114. NCContentPresenter.shared.messageNotification("_error_", description: errorDescription, delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
  115. }
  116. }
  117. }
  118. }
  119. @objc func downloadedFile(_ notification: NSNotification) {
  120. if self.view?.window == nil { return }
  121. if let userInfo = notification.userInfo as NSDictionary? {
  122. if let metadata = userInfo["metadata"] as? tableMetadata, let errorCode = userInfo["errorCode"] as? Int {
  123. if errorCode == 0 {
  124. self.dataSource?.reloadMetadata(ocId: metadata.ocId)
  125. collectionView.reloadData()
  126. }
  127. }
  128. }
  129. }
  130. @objc func changeTheming() {
  131. appDelegate.changeTheming(self, tableView: nil, collectionView: collectionView, form: false)
  132. }
  133. // MARK: DZNEmpty
  134. func backgroundColor(forEmptyDataSet scrollView: UIScrollView) -> UIColor? {
  135. return NCBrandColor.sharedInstance.backgroundView
  136. }
  137. func image(forEmptyDataSet scrollView: UIScrollView) -> UIImage? {
  138. return CCGraphics.changeThemingColorImage(UIImage.init(named: "folder"), width: 300, height: 300, color: NCBrandColor.sharedInstance.brandElement)
  139. }
  140. func title(forEmptyDataSet scrollView: UIScrollView) -> NSAttributedString? {
  141. let text = "\n"+NSLocalizedString("_files_no_files_", comment: "")
  142. let attributes = [NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 20), NSAttributedString.Key.foregroundColor: UIColor.lightGray]
  143. return NSAttributedString.init(string: text, attributes: attributes)
  144. }
  145. func emptyDataSetShouldAllowScroll(_ scrollView: UIScrollView) -> Bool {
  146. return true
  147. }
  148. // MARK: TAP EVENT
  149. func tapSwitchHeader(sender: Any) {
  150. if collectionView.collectionViewLayout == gridLayout {
  151. // list layout
  152. UIView.animate(withDuration: 0.0, animations: {
  153. self.collectionView.collectionViewLayout.invalidateLayout()
  154. self.collectionView.setCollectionViewLayout(self.listLayout, animated: false, completion: { (_) in
  155. self.collectionView.reloadData()
  156. self.collectionView.setContentOffset(CGPoint(x:0,y:0), animated: false)
  157. })
  158. })
  159. layout = k_layout_list
  160. NCUtility.shared.setLayoutForView(key: k_layout_view_offline, layout: layout)
  161. } else {
  162. // grid layout
  163. UIView.animate(withDuration: 0.0, animations: {
  164. self.collectionView.collectionViewLayout.invalidateLayout()
  165. self.collectionView.setCollectionViewLayout(self.gridLayout, animated: false, completion: { (_) in
  166. self.collectionView.reloadData()
  167. self.collectionView.setContentOffset(CGPoint(x:0,y:0), animated: false)
  168. })
  169. })
  170. layout = k_layout_grid
  171. NCUtility.shared.setLayoutForView(key: k_layout_view_offline, layout: layout)
  172. }
  173. }
  174. func tapOrderHeader(sender: Any) {
  175. let sortMenu = NCSortMenu()
  176. sortMenu.toggleMenu(viewController: self, key: k_layout_view_offline, sortButton: sender as? UIButton, serverUrl: serverUrl)
  177. }
  178. func tapMoreHeader(sender: Any) {
  179. }
  180. func tapMoreListItem(with objectId: String, namedButtonMore: String, sender: Any) {
  181. tapMoreGridItem(with: objectId, namedButtonMore: namedButtonMore, sender: sender)
  182. }
  183. func tapShareListItem(with objectId: String, sender: Any) {
  184. guard let metadata = NCManageDatabase.sharedInstance.getMetadata(predicate: NSPredicate(format: "ocId == %@", objectId)) else {
  185. return
  186. }
  187. NCMainCommon.shared.openShare(ViewController: self, metadata: metadata, indexPage: 2)
  188. }
  189. func tapMoreGridItem(with objectId: String, namedButtonMore: String, sender: Any) {
  190. guard let metadata = NCManageDatabase.sharedInstance.getMetadata(predicate: NSPredicate(format: "ocId == %@", objectId)) else {
  191. return
  192. }
  193. if !isEditMode {
  194. let mainMenuViewController = UIStoryboard.init(name: "NCMenu", bundle: nil).instantiateViewController(withIdentifier: "NCMainMenuTableViewController") as! NCMainMenuTableViewController
  195. var actions: [NCMenuAction] = []
  196. var iconHeader: UIImage!
  197. if let icon = UIImage(contentsOfFile: CCUtility.getDirectoryProviderStorageIconOcId(metadata.ocId, etag: metadata.etag)) {
  198. iconHeader = icon
  199. } else {
  200. iconHeader = UIImage(named: metadata.iconName)
  201. }
  202. actions.append(
  203. NCMenuAction(
  204. title: metadata.fileNameView,
  205. icon: iconHeader,
  206. action: nil
  207. )
  208. )
  209. if self.serverUrl == "" {
  210. actions.append(
  211. NCMenuAction(
  212. title: NSLocalizedString("_remove_available_offline_", comment: ""),
  213. icon: CCGraphics.changeThemingColorImage(UIImage(named: "offline"), width: 50, height: 50, color: NCBrandColor.sharedInstance.icon),
  214. action: { menuAction in
  215. if metadata.directory {
  216. NCManageDatabase.sharedInstance.setDirectory(serverUrl: CCUtility.stringAppendServerUrl(metadata.serverUrl, addFileName: metadata.fileName)!, offline: false, account: self.appDelegate.account)
  217. } else {
  218. NCManageDatabase.sharedInstance.setLocalFile(ocId: metadata.ocId, offline: false)
  219. }
  220. self.reloadDataSource()
  221. }
  222. )
  223. )
  224. }
  225. actions.append(
  226. NCMenuAction(
  227. title: NSLocalizedString("_details_", comment: ""),
  228. icon: CCGraphics.changeThemingColorImage(UIImage(named: "details"), width: 50, height: 50, color: NCBrandColor.sharedInstance.icon),
  229. action: { menuAction in
  230. NCMainCommon.shared.openShare(ViewController: self, metadata: metadata, indexPage: 0)
  231. }
  232. )
  233. )
  234. actions.append(
  235. NCMenuAction(
  236. title: NSLocalizedString("_delete_", comment: ""),
  237. icon: CCGraphics.changeThemingColorImage(UIImage(named: "trash"), width: 50, height: 50, color: .red),
  238. action: { menuAction in
  239. NCNetworking.shared.deleteMetadata(metadata, account: self.appDelegate.account, urlBase: self.appDelegate.urlBase,onlyLocal: true) { (errorCode, errorDescription) in }
  240. }
  241. )
  242. )
  243. mainMenuViewController.actions = actions
  244. let menuPanelController = NCMenuPanelController()
  245. menuPanelController.parentPresenter = self
  246. menuPanelController.delegate = mainMenuViewController
  247. menuPanelController.set(contentViewController: mainMenuViewController)
  248. menuPanelController.track(scrollView: mainMenuViewController.tableView)
  249. self.present(menuPanelController, animated: true, completion: nil)
  250. } else {
  251. let buttonPosition:CGPoint = (sender as! UIButton).convert(CGPoint.zero, to:collectionView)
  252. let indexPath = collectionView.indexPathForItem(at: buttonPosition)
  253. collectionView(self.collectionView, didSelectItemAt: indexPath!)
  254. }
  255. }
  256. // MARK: SEGUE
  257. override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  258. let photoDataSource: NSMutableArray = []
  259. for metadata in (dataSource?.metadatas ?? [tableMetadata]()) {
  260. if metadata.typeFile == k_metadataTypeFile_image || metadata.typeFile == k_metadataTypeFile_video {
  261. photoDataSource.add(metadata)
  262. }
  263. }
  264. if let segueNavigationController = segue.destination as? UINavigationController {
  265. if let segueViewController = segueNavigationController.topViewController as? NCDetailViewController {
  266. segueViewController.metadata = metadataPush
  267. }
  268. }
  269. }
  270. }
  271. // MARK: - 3D Touch peek and pop
  272. extension NCOffline: UIViewControllerPreviewingDelegate {
  273. func previewingContext(_ previewingContext: UIViewControllerPreviewing, viewControllerForLocation location: CGPoint) -> UIViewController? {
  274. guard let point = collectionView?.convert(location, from: collectionView?.superview) else { return nil }
  275. guard let indexPath = collectionView?.indexPathForItem(at: point) else { return nil }
  276. guard let metadata = dataSource?.cellForItemAt(indexPath: indexPath) else { return nil }
  277. guard let viewController = UIStoryboard(name: "CCPeekPop", bundle: nil).instantiateViewController(withIdentifier: "PeekPopImagePreview") as? CCPeekPop else { return nil }
  278. viewController.metadata = metadata
  279. if layout == k_layout_grid {
  280. guard let cell = collectionView?.cellForItem(at: indexPath) as? NCGridCell else { return nil }
  281. previewingContext.sourceRect = cell.frame
  282. viewController.imageFile = cell.imageItem.image
  283. } else {
  284. guard let cell = collectionView?.cellForItem(at: indexPath) as? NCListCell else { return nil }
  285. previewingContext.sourceRect = cell.frame
  286. viewController.imageFile = cell.imageItem.image
  287. }
  288. viewController.showOpenIn = true
  289. viewController.showOpenQuickLook = NCUtility.shared.isQuickLookDisplayable(metadata: metadata)
  290. viewController.showShare = false
  291. return viewController
  292. }
  293. func previewingContext(_ previewingContext: UIViewControllerPreviewing, commit viewControllerToCommit: UIViewController) {
  294. guard let indexPath = collectionView?.indexPathForItem(at: previewingContext.sourceRect.origin) else { return }
  295. collectionView(collectionView, didSelectItemAt: indexPath)
  296. }
  297. }
  298. // MARK: - Collection View
  299. extension NCOffline: UICollectionViewDelegate {
  300. func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  301. guard let metadata = dataSource?.cellForItemAt(indexPath: indexPath) else { return }
  302. metadataPush = metadata
  303. if isEditMode {
  304. if let index = selectocId.firstIndex(of: metadata.ocId) {
  305. selectocId.remove(at: index)
  306. } else {
  307. selectocId.append(metadata.ocId)
  308. }
  309. collectionView.reloadItems(at: [indexPath])
  310. return
  311. }
  312. if metadata.directory {
  313. guard let serverUrlPush = CCUtility.stringAppendServerUrl(metadataPush!.serverUrl, addFileName: metadataPush!.fileName) else { return }
  314. let ncOffline:NCOffline = UIStoryboard(name: "NCOffline", bundle: nil).instantiateInitialViewController() as! NCOffline
  315. ncOffline.serverUrl = serverUrlPush
  316. ncOffline.titleCurrentFolder = metadataPush!.fileNameView
  317. self.navigationController?.pushViewController(ncOffline, animated: true)
  318. } else {
  319. if CCUtility.fileProviderStorageExists(metadataPush?.ocId, fileNameView: metadataPush?.fileNameView) {
  320. performSegue(withIdentifier: "segueDetail", sender: self)
  321. } else {
  322. NCNetworking.shared.download(metadata: metadataPush!, selector: "") { (errorCode) in
  323. if errorCode == 0 {
  324. self.performSegue(withIdentifier: "segueDetail", sender: self)
  325. }
  326. }
  327. }
  328. }
  329. }
  330. }
  331. extension NCOffline: UICollectionViewDataSource {
  332. func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
  333. if (indexPath.section == 0) {
  334. if kind == UICollectionView.elementKindSectionHeader {
  335. let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "sectionHeaderMenu", for: indexPath) as! NCSectionHeaderMenu
  336. if collectionView.collectionViewLayout == gridLayout {
  337. header.buttonSwitch.setImage(CCGraphics.changeThemingColorImage(UIImage.init(named: "switchList"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), for: .normal)
  338. } else {
  339. header.buttonSwitch.setImage(CCGraphics.changeThemingColorImage(UIImage.init(named: "switchGrid"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), for: .normal)
  340. }
  341. header.delegate = self
  342. header.backgroundColor = NCBrandColor.sharedInstance.backgroundView
  343. header.separator.backgroundColor = NCBrandColor.sharedInstance.separator
  344. header.setStatusButton(count: dataSource?.metadatas.count ?? 0)
  345. header.setTitleSorted(datasourceTitleButton: titleButton)
  346. if groupBy == "none" {
  347. header.labelSection.isHidden = true
  348. header.labelSectionHeightConstraint.constant = 0
  349. } else {
  350. header.labelSection.isHidden = false
  351. header.setTitleLabel(title: "")
  352. header.labelSectionHeightConstraint.constant = sectionHeaderHeight
  353. }
  354. return header
  355. } else {
  356. let footer = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "sectionFooter", for: indexPath) as! NCSectionFooter
  357. let info = dataSource?.getFilesInformation()
  358. footer.setTitleLabel(directories: info?.directories ?? 0, files: info?.files ?? 0, size: info?.size ?? 0)
  359. return footer
  360. }
  361. } else {
  362. if kind == UICollectionView.elementKindSectionHeader {
  363. let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "sectionHeader", for: indexPath) as! NCSectionHeader
  364. header.setTitleLabel(title: "")
  365. return header
  366. } else {
  367. let footer = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "sectionFooter", for: indexPath) as! NCSectionFooter
  368. let info = dataSource?.getFilesInformation()
  369. footer.setTitleLabel(directories: info?.directories ?? 0, files: info?.files ?? 0, size: info?.size ?? 0)
  370. return footer
  371. }
  372. }
  373. }
  374. func numberOfSections(in collectionView: UICollectionView) -> Int {
  375. return dataSource?.sections ?? 1
  376. }
  377. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  378. return dataSource?.numberOfItemsInSection(section: section) ?? 1
  379. }
  380. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  381. let cell: UICollectionViewCell
  382. guard let metadata = dataSource?.cellForItemAt(indexPath: indexPath) else {
  383. return collectionView.dequeueReusableCell(withReuseIdentifier: "listCell", for: indexPath) as! NCListCell
  384. }
  385. if layout == k_layout_grid {
  386. cell = collectionView.dequeueReusableCell(withReuseIdentifier: "gridCell", for: indexPath) as! NCGridCell
  387. } else {
  388. cell = collectionView.dequeueReusableCell(withReuseIdentifier: "listCell", for: indexPath) as! NCListCell
  389. (cell as! NCListCell).separator.backgroundColor = NCBrandColor.sharedInstance.separator
  390. }
  391. let shares = NCManageDatabase.sharedInstance.getTableShares(account: metadata.account, serverUrl: metadata.serverUrl, fileName: metadata.fileName)
  392. 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)
  393. return cell
  394. }
  395. }
  396. extension NCOffline: UICollectionViewDelegateFlowLayout {
  397. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
  398. if section == 0 {
  399. if groupBy == "none" {
  400. return CGSize(width: collectionView.frame.width, height: headerMenuHeight)
  401. } else {
  402. return CGSize(width: collectionView.frame.width, height: headerMenuHeight + sectionHeaderHeight)
  403. }
  404. } else {
  405. return CGSize(width: collectionView.frame.width, height: sectionHeaderHeight)
  406. }
  407. }
  408. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
  409. let sections = dataSource?.sections ?? 1
  410. if (section == sections - 1) {
  411. return CGSize(width: collectionView.frame.width, height: footerHeight)
  412. } else {
  413. return CGSize(width: collectionView.frame.width, height: 0)
  414. }
  415. }
  416. }
  417. // MARK: - NC API & Algorithm
  418. extension NCOffline {
  419. @objc func reloadDataSource() {
  420. var ocIds: [String] = []
  421. var sort: String
  422. var ascending: Bool
  423. var directoryOnTop: Bool
  424. (layout, sort, ascending, groupBy, directoryOnTop, titleButton, itemForLine) = NCUtility.shared.getLayoutForView(key: k_layout_view_offline)
  425. if serverUrl == "" {
  426. if let directories = NCManageDatabase.sharedInstance.getTablesDirectory(predicate: NSPredicate(format: "account == %@ AND offline == true", appDelegate.account), sorted: "serverUrl", ascending: true) {
  427. for directory: tableDirectory in directories {
  428. ocIds.append(directory.ocId)
  429. }
  430. }
  431. let files = NCManageDatabase.sharedInstance.getTableLocalFiles(predicate: NSPredicate(format: "account == %@ AND offline == true", appDelegate.account), sorted: "fileName", ascending: true)
  432. for file: tableLocalFile in files {
  433. ocIds.append(file.ocId)
  434. }
  435. let metadatasSource = NCManageDatabase.sharedInstance.getMetadatas(predicate: NSPredicate(format: "account == %@ AND ocId IN %@", appDelegate.account, ocIds))
  436. self.dataSource = NCDataSource.init(metadatasSource: metadatasSource, sort: sort, ascending: ascending, directoryOnTop: directoryOnTop, filterLivePhoto: true)
  437. } else {
  438. let metadatasSource = NCManageDatabase.sharedInstance.getMetadatas(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@", appDelegate.account, serverUrl))
  439. self.dataSource = NCDataSource.init(metadatasSource: metadatasSource, sort: sort, ascending: ascending, directoryOnTop: directoryOnTop, filterLivePhoto: true)
  440. }
  441. refreshControl.endRefreshing()
  442. collectionView.reloadData()
  443. }
  444. @objc func reloadDataSourceNetwork() {
  445. if serverUrl != "" {
  446. NCNetworking.shared.readFolder(serverUrl: serverUrl, account: appDelegate.account) { (account, metadataFolder, metadatas, metadatasUpdate, metadatasLocalUpdate, errorCode, errorDescription) in
  447. if errorCode == 0 {
  448. for metadata in metadatas ?? [] {
  449. if !metadata.directory {
  450. let localFile = NCManageDatabase.sharedInstance.getTableLocalFile(predicate: NSPredicate(format: "ocId == %@", metadata.ocId))
  451. if localFile == nil || localFile?.etag != metadata.etag {
  452. NCOperationQueue.shared.download(metadata: metadata, selector: selectorDownloadFile, setFavorite: false)
  453. }
  454. }
  455. }
  456. }
  457. self.reloadDataSource()
  458. }
  459. }
  460. }
  461. }