NCFavorite.swift 26 KB

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