NCOffline.swift 27 KB

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