NCOffline.swift 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734
  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, DropdownMenuDelegate, 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 datasourceSorted = ""
  35. private var datasourceAscending = true
  36. private var datasourceGroupBy = ""
  37. private var datasourceDirectoryOnTop = false
  38. private var autoUploadFileName = ""
  39. private var autoUploadDirectory = ""
  40. private var listLayout: NCListLayout!
  41. private var gridLayout: NCGridLayout!
  42. private let headerMenuHeight: CGFloat = 50
  43. private let sectionHeaderHeight: CGFloat = 20
  44. private let footerHeight: CGFloat = 50
  45. private let refreshControl = UIRefreshControl()
  46. //BKPasscodeViewController
  47. private var failedAttempts: Double = 0
  48. private var lockUntilDate: NSDate?
  49. required init?(coder aDecoder: NSCoder) {
  50. super.init(coder: aDecoder)
  51. appDelegate.activeOffline = self
  52. }
  53. override func viewDidLoad() {
  54. super.viewDidLoad()
  55. // Cell
  56. collectionView.register(UINib.init(nibName: "NCListCell", bundle: nil), forCellWithReuseIdentifier: "listCell")
  57. collectionView.register(UINib.init(nibName: "NCGridCell", bundle: nil), forCellWithReuseIdentifier: "gridCell")
  58. // Header
  59. collectionView.register(UINib.init(nibName: "NCSectionHeaderMenu", bundle: nil), forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "sectionHeaderMenu")
  60. collectionView.register(UINib.init(nibName: "NCSectionHeader", bundle: nil), forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "sectionHeader")
  61. // Footer
  62. collectionView.register(UINib.init(nibName: "NCSectionFooter", bundle: nil), forSupplementaryViewOfKind: UICollectionView.elementKindSectionFooter, withReuseIdentifier: "sectionFooter")
  63. collectionView.alwaysBounceVertical = true
  64. listLayout = NCListLayout()
  65. gridLayout = NCGridLayout()
  66. // Refresh Control
  67. collectionView.addSubview(refreshControl)
  68. // Configure Refresh Control
  69. refreshControl.tintColor = NCBrandColor.sharedInstance.brandText
  70. refreshControl.backgroundColor = NCBrandColor.sharedInstance.brand
  71. refreshControl.addTarget(self, action: #selector(loadDatasource), for: .valueChanged)
  72. // empty Data Source
  73. self.collectionView.emptyDataSetDelegate = self
  74. self.collectionView.emptyDataSetSource = self
  75. // 3D Touch peek and pop
  76. if traitCollection.forceTouchCapability == .available {
  77. registerForPreviewing(with: self, sourceView: view)
  78. }
  79. NotificationCenter.default.addObserver(self, selector: #selector(changeTheming), name: NSNotification.Name(rawValue: k_notificationCenter_changeTheming), object: nil)
  80. NotificationCenter.default.addObserver(self, selector: #selector(deleteFile(_:)), name: NSNotification.Name(rawValue: k_notificationCenter_deleteFile), object: nil)
  81. changeTheming()
  82. }
  83. override func viewWillAppear(_ animated: Bool) {
  84. super.viewWillAppear(animated)
  85. self.navigationItem.title = titleCurrentFolder
  86. (typeLayout, datasourceSorted, datasourceAscending, datasourceGroupBy, datasourceDirectoryOnTop) = NCUtility.sharedInstance.getLayoutForView(key: k_layout_view_offline)
  87. // get auto upload folder
  88. autoUploadFileName = NCManageDatabase.sharedInstance.getAccountAutoUploadFileName()
  89. autoUploadDirectory = NCManageDatabase.sharedInstance.getAccountAutoUploadDirectory(appDelegate.activeUrl)
  90. if typeLayout == k_layout_list {
  91. collectionView.collectionViewLayout = listLayout
  92. } else {
  93. collectionView.collectionViewLayout = gridLayout
  94. }
  95. loadDatasource()
  96. }
  97. override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
  98. super.viewWillTransition(to: size, with: coordinator)
  99. coordinator.animate(alongsideTransition: nil) { _ in
  100. self.collectionView.collectionViewLayout.invalidateLayout()
  101. }
  102. }
  103. //MARK: - NotificationCenter
  104. @objc func deleteFile(_ notification: NSNotification) {
  105. if self.view?.window == nil { return }
  106. if let userInfo = notification.userInfo as NSDictionary? {
  107. if let errorCode = userInfo["errorCode"] as? Int, let errorDescription = userInfo["errorDescription"] as? String {
  108. if errorCode == 0 {
  109. self.loadDatasource()
  110. } else {
  111. NCContentPresenter.shared.messageNotification("_error_", description: errorDescription, delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
  112. }
  113. }
  114. }
  115. }
  116. @objc func changeTheming() {
  117. appDelegate.changeTheming(self, tableView: nil, collectionView: collectionView, form: false)
  118. }
  119. // MARK: DZNEmpty
  120. func backgroundColor(forEmptyDataSet scrollView: UIScrollView) -> UIColor? {
  121. return NCBrandColor.sharedInstance.backgroundView
  122. }
  123. func image(forEmptyDataSet scrollView: UIScrollView) -> UIImage? {
  124. return CCGraphics.changeThemingColorImage(UIImage.init(named: "folder"), width: 300, height: 300, color: NCBrandColor.sharedInstance.brandElement)
  125. }
  126. func title(forEmptyDataSet scrollView: UIScrollView) -> NSAttributedString? {
  127. let text = "\n"+NSLocalizedString("_files_no_files_", comment: "")
  128. let attributes = [NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 20), NSAttributedString.Key.foregroundColor: UIColor.lightGray]
  129. return NSAttributedString.init(string: text, attributes: attributes)
  130. }
  131. func emptyDataSetShouldAllowScroll(_ scrollView: UIScrollView) -> Bool {
  132. return true
  133. }
  134. // MARK: TAP EVENT
  135. func tapSwitchHeader(sender: Any) {
  136. if collectionView.collectionViewLayout == gridLayout {
  137. // list layout
  138. UIView.animate(withDuration: 0.0, animations: {
  139. self.collectionView.collectionViewLayout.invalidateLayout()
  140. self.collectionView.setCollectionViewLayout(self.listLayout, animated: false, completion: { (_) in
  141. self.collectionView.reloadData()
  142. self.collectionView.setContentOffset(CGPoint(x:0,y:0), animated: false)
  143. })
  144. })
  145. typeLayout = k_layout_list
  146. NCUtility.sharedInstance.setLayoutForView(key: k_layout_view_offline, layout: typeLayout, sort: datasourceSorted, ascending: datasourceAscending, groupBy: datasourceGroupBy, directoryOnTop: datasourceDirectoryOnTop)
  147. } else {
  148. // grid layout
  149. UIView.animate(withDuration: 0.0, animations: {
  150. self.collectionView.collectionViewLayout.invalidateLayout()
  151. self.collectionView.setCollectionViewLayout(self.gridLayout, animated: false, completion: { (_) in
  152. self.collectionView.reloadData()
  153. self.collectionView.setContentOffset(CGPoint(x:0,y:0), animated: false)
  154. })
  155. })
  156. typeLayout = k_layout_grid
  157. NCUtility.sharedInstance.setLayoutForView(key: k_layout_view_offline, layout: typeLayout, sort: datasourceSorted, ascending: datasourceAscending, groupBy: datasourceGroupBy, directoryOnTop: datasourceDirectoryOnTop)
  158. }
  159. }
  160. func tapOrderHeader(sender: Any) {
  161. var menuView: DropdownMenu?
  162. var selectedIndexPath = [IndexPath()]
  163. let item1 = DropdownItem(image: CCGraphics.changeThemingColorImage(UIImage.init(named: "sortFileNameAZ"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), title: NSLocalizedString("_order_by_name_a_z_", comment: ""))
  164. let item2 = DropdownItem(image: CCGraphics.changeThemingColorImage(UIImage.init(named: "sortFileNameZA"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), title: NSLocalizedString("_order_by_name_z_a_", comment: ""))
  165. let item3 = DropdownItem(image: CCGraphics.changeThemingColorImage(UIImage.init(named: "sortDateMoreRecent"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), title: NSLocalizedString("_order_by_date_more_recent_", comment: ""))
  166. let item4 = DropdownItem(image: CCGraphics.changeThemingColorImage(UIImage.init(named: "sortDateLessRecent"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), title: NSLocalizedString("_order_by_date_less_recent_", comment: ""))
  167. let item5 = DropdownItem(image: CCGraphics.changeThemingColorImage(UIImage.init(named: "sortSmallest"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), title: NSLocalizedString("_order_by_size_smallest_", comment: ""))
  168. let item6 = DropdownItem(image: CCGraphics.changeThemingColorImage(UIImage.init(named: "sortLargest"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), title: NSLocalizedString("_order_by_size_largest_", comment: ""))
  169. switch datasourceSorted {
  170. case "fileName":
  171. if datasourceAscending == true { item1.style = .highlight; selectedIndexPath.append(IndexPath(row: 0, section: 0)) }
  172. if datasourceAscending == false { item2.style = .highlight; selectedIndexPath.append(IndexPath(row: 1, section: 0)) }
  173. case "date":
  174. if datasourceAscending == false { item3.style = .highlight; selectedIndexPath.append(IndexPath(row: 2, section: 0)) }
  175. if datasourceAscending == true { item4.style = .highlight; selectedIndexPath.append(IndexPath(row: 3, section: 0)) }
  176. case "size":
  177. if datasourceAscending == true { item5.style = .highlight; selectedIndexPath.append(IndexPath(row: 4, section: 0)) }
  178. if datasourceAscending == false { item6.style = .highlight; selectedIndexPath.append(IndexPath(row: 5, section: 0)) }
  179. default:
  180. ()
  181. }
  182. let item7 = DropdownItem(image: CCGraphics.changeThemingColorImage(UIImage.init(named: "MenuGroupByAlphabetic"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), title: NSLocalizedString("_group_alphabetic_no_", comment: ""))
  183. let item8 = DropdownItem(image: CCGraphics.changeThemingColorImage(UIImage.init(named: "MenuGroupByFile"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), title: NSLocalizedString("_group_typefile_no_", comment: ""))
  184. let item9 = DropdownItem(image: CCGraphics.changeThemingColorImage(UIImage.init(named: "MenuGroupByDate"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), title: NSLocalizedString("_group_date_no_", comment: ""))
  185. switch datasourceGroupBy {
  186. case "alphabetic":
  187. item7.style = .highlight; selectedIndexPath.append(IndexPath(row: 0, section: 1))
  188. case "typefile":
  189. item8.style = .highlight; selectedIndexPath.append(IndexPath(row: 1, section: 1))
  190. case "date":
  191. item9.style = .highlight; selectedIndexPath.append(IndexPath(row: 2, section: 1))
  192. default:
  193. ()
  194. }
  195. let item10 = DropdownItem(image: CCGraphics.changeThemingColorImage(UIImage.init(named: "foldersOnTop"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), title: NSLocalizedString("_directory_on_top_no_", comment: ""))
  196. if datasourceDirectoryOnTop {
  197. item10.style = .highlight; selectedIndexPath.append(IndexPath(row: 0, section: 2))
  198. }
  199. let sectionOrder = DropdownSection(sectionIdentifier: "", items: [item1, item2, item3, item4, item5, item6])
  200. let sectionGroupBy = DropdownSection(sectionIdentifier: "", items: [item7, item8, item9])
  201. let sectionFolderOnTop = DropdownSection(sectionIdentifier: "", items: [item10])
  202. menuView = DropdownMenu(navigationController: self.navigationController!, sections: [sectionOrder, sectionGroupBy, sectionFolderOnTop], selectedIndexPath: selectedIndexPath)
  203. menuView?.token = "tapOrderHeaderMenu"
  204. menuView?.delegate = self
  205. menuView?.rowHeight = 45
  206. menuView?.sectionHeaderHeight = 0.3
  207. menuView?.highlightColor = NCBrandColor.sharedInstance.brand
  208. menuView?.tableView.alwaysBounceVertical = false
  209. menuView?.tableViewSeperatorColor = NCBrandColor.sharedInstance.separator
  210. menuView?.tableViewBackgroundColor = NCBrandColor.sharedInstance.backgroundForm
  211. menuView?.cellBackgroundColor = NCBrandColor.sharedInstance.backgroundForm
  212. menuView?.textColor = NCBrandColor.sharedInstance.textView
  213. let header = (sender as? UIButton)?.superview
  214. let headerRect = self.collectionView.convert(header!.bounds, from: self.view)
  215. let menuOffsetY = headerRect.height - headerRect.origin.y - 2
  216. menuView?.topOffsetY = CGFloat(menuOffsetY)
  217. menuView?.showMenu()
  218. }
  219. func tapMoreHeader(sender: Any) {
  220. }
  221. func tapMoreListItem(with objectId: String, sender: Any) {
  222. tapMoreGridItem(with: objectId, sender: sender)
  223. }
  224. func tapShareListItem(with objectId: String, sender: Any) {
  225. guard let metadata = NCManageDatabase.sharedInstance.getMetadata(predicate: NSPredicate(format: "ocId == %@", objectId)) else {
  226. return
  227. }
  228. NCMainCommon.sharedInstance.openShare(ViewController: self, metadata: metadata, indexPage: 2)
  229. }
  230. func tapMoreGridItem(with objectId: String, sender: Any) {
  231. guard let metadata = NCManageDatabase.sharedInstance.getMetadata(predicate: NSPredicate(format: "ocId == %@", objectId)) else {
  232. return
  233. }
  234. if !isEditMode {
  235. let mainMenuViewController = UIStoryboard.init(name: "NCMenu", bundle: nil).instantiateViewController(withIdentifier: "NCMainMenuTableViewController") as! NCMainMenuTableViewController
  236. var actions = [NCMenuAction]()
  237. var iconHeader: UIImage!
  238. if let icon = UIImage(contentsOfFile: CCUtility.getDirectoryProviderStorageIconOcId(metadata.ocId, fileNameView: metadata.fileNameView)) {
  239. iconHeader = icon
  240. } else {
  241. iconHeader = UIImage(named: metadata.iconName)
  242. }
  243. actions.append(
  244. NCMenuAction(
  245. title: metadata.fileNameView,
  246. icon: iconHeader,
  247. action: nil
  248. )
  249. )
  250. actions.append(
  251. NCMenuAction(
  252. title: NSLocalizedString("_remove_available_offline_", comment: ""),
  253. icon: CCGraphics.changeThemingColorImage(UIImage(named: "offline"), width: 50, height: 50, color: NCBrandColor.sharedInstance.icon),
  254. action: { menuAction in
  255. if metadata.directory {
  256. NCManageDatabase.sharedInstance.setDirectory(serverUrl: CCUtility.stringAppendServerUrl(metadata.serverUrl, addFileName: metadata.fileName)!, offline: false, account: self.appDelegate.activeAccount)
  257. } else {
  258. NCManageDatabase.sharedInstance.setLocalFile(ocId: metadata.ocId, offline: false)
  259. }
  260. self.loadDatasource()
  261. }
  262. )
  263. )
  264. actions.append(
  265. NCMenuAction(
  266. title: NSLocalizedString("_details_", comment: ""),
  267. icon: CCGraphics.changeThemingColorImage(UIImage(named: "details"), width: 50, height: 50, color: NCBrandColor.sharedInstance.icon),
  268. action: { menuAction in
  269. NCMainCommon.sharedInstance.openShare(ViewController: self, metadata: metadata, indexPage: 0)
  270. }
  271. )
  272. )
  273. actions.append(
  274. NCMenuAction(
  275. title: NSLocalizedString("_delete_", comment: ""),
  276. icon: CCGraphics.changeThemingColorImage(UIImage(named: "trash"), width: 50, height: 50, color: .red),
  277. action: { menuAction in
  278. NCNetworking.sharedInstance.deleteMetadata(metadata, account: self.appDelegate.activeAccount, user: self.appDelegate.activeUser, userID: self.appDelegate.activeUserID, password: self.appDelegate.activePassword, url: self.appDelegate.activeUrl) { (errorCode, errorDescription) in }
  279. }
  280. )
  281. )
  282. mainMenuViewController.actions = actions
  283. let menuPanelController = NCMenuPanelController()
  284. menuPanelController.parentPresenter = self
  285. menuPanelController.delegate = mainMenuViewController
  286. menuPanelController.set(contentViewController: mainMenuViewController)
  287. menuPanelController.track(scrollView: mainMenuViewController.tableView)
  288. self.present(menuPanelController, animated: true, completion: nil)
  289. } else {
  290. let buttonPosition:CGPoint = (sender as! UIButton).convert(CGPoint.zero, to:collectionView)
  291. let indexPath = collectionView.indexPathForItem(at: buttonPosition)
  292. collectionView(self.collectionView, didSelectItemAt: indexPath!)
  293. }
  294. }
  295. // MARK: DROP-DOWN-MENU
  296. func dropdownMenu(_ dropdownMenu: DropdownMenu, didSelectRowAt indexPath: IndexPath) {
  297. if dropdownMenu.token == "tapOrderHeaderMenu" {
  298. switch indexPath.section {
  299. case 0: switch indexPath.row {
  300. case 0: datasourceSorted = "fileName"; datasourceAscending = true
  301. case 1: datasourceSorted = "fileName"; datasourceAscending = false
  302. case 2: datasourceSorted = "date"; datasourceAscending = false
  303. case 3: datasourceSorted = "date"; datasourceAscending = true
  304. case 4: datasourceSorted = "size"; datasourceAscending = true
  305. case 5: datasourceSorted = "size"; datasourceAscending = false
  306. default: ()
  307. }
  308. case 1: switch indexPath.row {
  309. case 0:
  310. if datasourceGroupBy == "alphabetic" {
  311. datasourceGroupBy = "none"
  312. } else {
  313. datasourceGroupBy = "alphabetic"
  314. }
  315. case 1:
  316. if datasourceGroupBy == "typefile" {
  317. datasourceGroupBy = "none"
  318. } else {
  319. datasourceGroupBy = "typefile"
  320. }
  321. case 2:
  322. if datasourceGroupBy == "date" {
  323. datasourceGroupBy = "none"
  324. } else {
  325. datasourceGroupBy = "date"
  326. }
  327. default: ()
  328. }
  329. case 2:
  330. if datasourceDirectoryOnTop {
  331. datasourceDirectoryOnTop = false
  332. } else {
  333. datasourceDirectoryOnTop = true
  334. }
  335. default: ()
  336. }
  337. NCUtility.sharedInstance.setLayoutForView(key: k_layout_view_offline, layout: typeLayout, sort: datasourceSorted, ascending: datasourceAscending, groupBy: datasourceGroupBy, directoryOnTop: datasourceDirectoryOnTop)
  338. loadDatasource()
  339. }
  340. if dropdownMenu.token == "tapMoreHeaderMenu" {
  341. }
  342. if dropdownMenu.token == "tapMoreHeaderMenuSelect" {
  343. }
  344. }
  345. // MARK: SEGUE
  346. override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  347. let photoDataSource: NSMutableArray = []
  348. for ocId: String in sectionDatasource.allOcId as! [String] {
  349. let metadata = sectionDatasource.allRecordsDataSource.object(forKey: ocId) as! tableMetadata
  350. if metadata.typeFile == k_metadataTypeFile_image {
  351. photoDataSource.add(metadata)
  352. }
  353. }
  354. if let segueNavigationController = segue.destination as? UINavigationController {
  355. if let segueViewController = segueNavigationController.topViewController as? NCDetailViewController {
  356. segueViewController.metadata = metadataPush
  357. segueViewController.offlineFilterImage = true
  358. }
  359. }
  360. }
  361. }
  362. // MARK: - 3D Touch peek and pop
  363. extension NCOffline: UIViewControllerPreviewingDelegate {
  364. func previewingContext(_ previewingContext: UIViewControllerPreviewing, viewControllerForLocation location: CGPoint) -> UIViewController? {
  365. guard let point = collectionView?.convert(location, from: collectionView?.superview) else { return nil }
  366. guard let indexPath = collectionView?.indexPathForItem(at: point) else { return nil }
  367. guard let metadata = NCMainCommon.sharedInstance.getMetadataFromSectionDataSourceIndexPath(indexPath, sectionDataSource: sectionDatasource) else { return nil }
  368. guard let viewController = UIStoryboard(name: "CCPeekPop", bundle: nil).instantiateViewController(withIdentifier: "PeekPopImagePreview") as? CCPeekPop else { return nil }
  369. viewController.metadata = metadata
  370. if typeLayout == k_layout_grid {
  371. guard let cell = collectionView?.cellForItem(at: indexPath) as? NCGridCell else { return nil }
  372. previewingContext.sourceRect = cell.frame
  373. viewController.imageFile = cell.imageItem.image
  374. } else {
  375. guard let cell = collectionView?.cellForItem(at: indexPath) as? NCListCell else { return nil }
  376. previewingContext.sourceRect = cell.frame
  377. viewController.imageFile = cell.imageItem.image
  378. }
  379. viewController.showOpenIn = true
  380. viewController.showOpenQuickLook = NCUtility.sharedInstance.isQuickLookDisplayable(metadata: metadata)
  381. viewController.showShare = false
  382. return viewController
  383. }
  384. func previewingContext(_ previewingContext: UIViewControllerPreviewing, commit viewControllerToCommit: UIViewController) {
  385. guard let indexPath = collectionView?.indexPathForItem(at: previewingContext.sourceRect.origin) else { return }
  386. collectionView(collectionView, didSelectItemAt: indexPath)
  387. }
  388. }
  389. // MARK: - Collection View
  390. extension NCOffline: UICollectionViewDelegate {
  391. func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  392. guard let metadata = NCMainCommon.sharedInstance.getMetadataFromSectionDataSourceIndexPath(indexPath, sectionDataSource: sectionDatasource) else {
  393. return
  394. }
  395. metadataPush = metadata
  396. if isEditMode {
  397. if let index = selectocId.firstIndex(of: metadata.ocId) {
  398. selectocId.remove(at: index)
  399. } else {
  400. selectocId.append(metadata.ocId)
  401. }
  402. collectionView.reloadItems(at: [indexPath])
  403. return
  404. }
  405. if metadata.directory {
  406. guard let serverUrlPush = CCUtility.stringAppendServerUrl(metadataPush!.serverUrl, addFileName: metadataPush!.fileName) else { return }
  407. let ncOffline:NCOffline = UIStoryboard(name: "NCOffline", bundle: nil).instantiateInitialViewController() as! NCOffline
  408. ncOffline.serverUrl = serverUrlPush
  409. ncOffline.titleCurrentFolder = metadataPush!.fileNameView
  410. self.navigationController?.pushViewController(ncOffline, animated: true)
  411. } else {
  412. performSegue(withIdentifier: "segueDetail", sender: self)
  413. }
  414. }
  415. }
  416. extension NCOffline: UICollectionViewDataSource {
  417. func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
  418. if (indexPath.section == 0) {
  419. if kind == UICollectionView.elementKindSectionHeader {
  420. let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "sectionHeaderMenu", for: indexPath) as! NCSectionHeaderMenu
  421. if collectionView.collectionViewLayout == gridLayout {
  422. header.buttonSwitch.setImage(CCGraphics.changeThemingColorImage(UIImage.init(named: "switchList"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), for: .normal)
  423. } else {
  424. header.buttonSwitch.setImage(CCGraphics.changeThemingColorImage(UIImage.init(named: "switchGrid"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), for: .normal)
  425. }
  426. header.delegate = self
  427. header.backgroundColor = NCBrandColor.sharedInstance.backgroundView
  428. header.separator.backgroundColor = NCBrandColor.sharedInstance.separator
  429. header.setStatusButton(count: sectionDatasource.allOcId.count)
  430. header.setTitleOrder(datasourceSorted: datasourceSorted, datasourceAscending: datasourceAscending)
  431. if datasourceGroupBy == "none" {
  432. header.labelSection.isHidden = true
  433. header.labelSectionHeightConstraint.constant = 0
  434. } else {
  435. header.labelSection.isHidden = false
  436. header.setTitleLabel(sectionDatasource: sectionDatasource, section: indexPath.section)
  437. header.labelSectionHeightConstraint.constant = sectionHeaderHeight
  438. }
  439. return header
  440. } else {
  441. let footer = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "sectionFooter", for: indexPath) as! NCSectionFooter
  442. footer.setTitleLabel(sectionDatasource: sectionDatasource)
  443. return footer
  444. }
  445. } else {
  446. if kind == UICollectionView.elementKindSectionHeader {
  447. let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "sectionHeader", for: indexPath) as! NCSectionHeader
  448. header.setTitleLabel(sectionDatasource: sectionDatasource, section: indexPath.section)
  449. return header
  450. } else {
  451. let footer = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "sectionFooter", for: indexPath) as! NCSectionFooter
  452. footer.setTitleLabel(sectionDatasource: sectionDatasource)
  453. return footer
  454. }
  455. }
  456. }
  457. func numberOfSections(in collectionView: UICollectionView) -> Int {
  458. let sections = sectionDatasource.sectionArrayRow.allKeys.count
  459. return sections
  460. }
  461. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  462. let key = sectionDatasource.sections.object(at: section)
  463. let datasource = sectionDatasource.sectionArrayRow.object(forKey: key) as! [tableMetadata]
  464. return datasource.count
  465. }
  466. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  467. let cell: UICollectionViewCell
  468. guard let metadata = NCMainCommon.sharedInstance.getMetadataFromSectionDataSourceIndexPath(indexPath, sectionDataSource: sectionDatasource) else {
  469. return collectionView.dequeueReusableCell(withReuseIdentifier: "listCell", for: indexPath) as! NCListCell
  470. }
  471. if typeLayout == k_layout_grid {
  472. cell = collectionView.dequeueReusableCell(withReuseIdentifier: "gridCell", for: indexPath) as! NCGridCell
  473. } else {
  474. cell = collectionView.dequeueReusableCell(withReuseIdentifier: "listCell", for: indexPath) as! NCListCell
  475. (cell as! NCListCell).separator.backgroundColor = NCBrandColor.sharedInstance.separator
  476. }
  477. let shares = NCManageDatabase.sharedInstance.getTableShares(account: metadata.account, serverUrl: metadata.serverUrl, fileName: metadata.fileName)
  478. 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)
  479. return cell
  480. }
  481. }
  482. extension NCOffline: UICollectionViewDelegateFlowLayout {
  483. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
  484. if section == 0 {
  485. if datasourceGroupBy == "none" {
  486. return CGSize(width: collectionView.frame.width, height: headerMenuHeight)
  487. } else {
  488. return CGSize(width: collectionView.frame.width, height: headerMenuHeight + sectionHeaderHeight)
  489. }
  490. } else {
  491. return CGSize(width: collectionView.frame.width, height: sectionHeaderHeight)
  492. }
  493. }
  494. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
  495. let sections = sectionDatasource.sectionArrayRow.allKeys.count
  496. if (section == sections - 1) {
  497. return CGSize(width: collectionView.frame.width, height: footerHeight)
  498. } else {
  499. return CGSize(width: collectionView.frame.width, height: 0)
  500. }
  501. }
  502. }
  503. // MARK: - NC API & Algorithm
  504. extension NCOffline {
  505. @objc func loadDatasource() {
  506. var ocIds = [String]()
  507. sectionDatasource = CCSectionDataSourceMetadata()
  508. if serverUrl == "" {
  509. if let directories = NCManageDatabase.sharedInstance.getTablesDirectory(predicate: NSPredicate(format: "account == %@ AND offline == true", appDelegate.activeAccount), sorted: "serverUrl", ascending: true) {
  510. for directory: tableDirectory in directories {
  511. ocIds.append(directory.ocId)
  512. }
  513. }
  514. if let files = NCManageDatabase.sharedInstance.getTableLocalFiles(predicate: NSPredicate(format: "account == %@ AND offline == true", appDelegate.activeAccount), sorted: "fileName", ascending: true) {
  515. for file: tableLocalFile in files {
  516. ocIds.append(file.ocId)
  517. }
  518. }
  519. if let metadatas = NCManageDatabase.sharedInstance.getMetadatas(predicate: NSPredicate(format: "account == %@ AND ocId IN %@", appDelegate.activeAccount, ocIds), sorted: nil, ascending: false) {
  520. sectionDatasource = CCSectionMetadata.creataDataSourseSectionMetadata(metadatas, listProgressMetadata: nil, groupByField: datasourceGroupBy, filterTypeFileImage: false, filterTypeFileVideo: false, sorted: datasourceSorted, ascending: datasourceAscending, activeAccount: appDelegate.activeAccount)
  521. }
  522. } else {
  523. if let metadatas = NCManageDatabase.sharedInstance.getMetadatas(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@", appDelegate.activeAccount, serverUrl), sorted: nil, ascending: false) {
  524. sectionDatasource = CCSectionMetadata.creataDataSourseSectionMetadata(metadatas, listProgressMetadata: nil, groupByField: datasourceGroupBy, filterTypeFileImage: false, filterTypeFileVideo: false, sorted: datasourceSorted, ascending: datasourceAscending, activeAccount: appDelegate.activeAccount)
  525. }
  526. }
  527. self.refreshControl.endRefreshing()
  528. collectionView.reloadData()
  529. }
  530. /*
  531. func deleteItem(with metadata: tableMetadata, sender: Any) {
  532. let mainMenuViewController = UIStoryboard.init(name: "NCMenu", bundle: nil).instantiateViewController(withIdentifier: "NCMainMenuTableViewController") as! NCMainMenuTableViewController
  533. var actions = [NCMenuAction]()
  534. actions.append(
  535. NCMenuAction(
  536. title: NSLocalizedString("_delete_", comment: ""),
  537. icon: CCGraphics.changeThemingColorImage(UIImage(named: "trash"), width: 50, height: 50, color: .red),
  538. action: { menuAction in
  539. NCNetworking.sharedInstance.deleteMetadata(metadata, user: self.appDelegate.activeUser, userID: self.appDelegate.activeUserID, password: self.appDelegate.activePassword, url: self.appDelegate.activeUrl) { (errorCode, errorDescription) in }
  540. }
  541. )
  542. )
  543. actions.append(
  544. NCMenuAction(
  545. title: NSLocalizedString("_cancel_", comment: ""),
  546. icon: CCGraphics.changeThemingColorImage(UIImage(named: "cancel"), width: 50, height: 50, color: NCBrandColor.sharedInstance.icon),
  547. action: { menuAction in }
  548. )
  549. )
  550. mainMenuViewController.actions = actions
  551. let menuPanelController = NCMenuPanelController()
  552. menuPanelController.parentPresenter = self
  553. menuPanelController.delegate = mainMenuViewController
  554. menuPanelController.set(contentViewController: mainMenuViewController)
  555. menuPanelController.track(scrollView: mainMenuViewController.tableView)
  556. }
  557. */
  558. }