NCOffline.swift 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705
  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 <m.faggiana@twsweb.it>
  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 ,UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout, UIGestureRecognizerDelegate, NCOfflineListCellDelegate, NCOfflineGridCellDelegate, NCOfflineHeaderDelegate, DropdownMenuDelegate, DZNEmptyDataSetSource, DZNEmptyDataSetDelegate {
  25. @IBOutlet fileprivate weak var collectionView: UICollectionView!
  26. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  27. var titleCurrentFolder = NSLocalizedString("_manage_file_offline_", comment: "")
  28. var directoryID = ""
  29. var sectionDatasource = CCSectionDataSourceMetadata()
  30. var datasourceSorted = ""
  31. var datasourceAscending = true
  32. var datasourceGroupBy = ""
  33. var datasourceDirectoryOnTop = false
  34. var isEditMode = false
  35. var selectFileID = [String]()
  36. var listLayout: NCListLayoutOffline!
  37. var gridLayout: NCGridLayoutOffline!
  38. var actionSheet: ActionSheet?
  39. private let headerMenuHeight: CGFloat = 50
  40. private let sectionHeaderHeight: CGFloat = 20
  41. private let footerHeight: CGFloat = 50
  42. private let refreshControl = UIRefreshControl()
  43. override func viewDidLoad() {
  44. super.viewDidLoad()
  45. // Cell
  46. collectionView.register(UINib.init(nibName: "NCOfflineListCell", bundle: nil), forCellWithReuseIdentifier: "cell-list")
  47. collectionView.register(UINib.init(nibName: "NCOfflineGridCell", bundle: nil), forCellWithReuseIdentifier: "cell-grid")
  48. // Header
  49. collectionView.register(UINib.init(nibName: "NCOfflineSectionHeaderMenu", bundle: nil), forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "sectionHeaderMenu")
  50. collectionView.register(UINib.init(nibName: "NCOfflineSectionHeader", bundle: nil), forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "sectionHeader")
  51. // Footer
  52. collectionView.register(UINib.init(nibName: "NCOfflineFooter", bundle: nil), forSupplementaryViewOfKind: UICollectionView.elementKindSectionFooter, withReuseIdentifier: "footer")
  53. collectionView.alwaysBounceVertical = true
  54. listLayout = NCListLayoutOffline()
  55. gridLayout = NCGridLayoutOffline()
  56. if CCUtility.getLayoutOffline() == "list" {
  57. collectionView.collectionViewLayout = listLayout
  58. } else {
  59. collectionView.collectionViewLayout = gridLayout
  60. }
  61. // Add Refresh Control
  62. if #available(iOS 10.0, *) {
  63. collectionView.refreshControl = refreshControl
  64. } else {
  65. collectionView.addSubview(refreshControl)
  66. }
  67. // Configure Refresh Control
  68. refreshControl.tintColor = NCBrandColor.sharedInstance.brandText
  69. refreshControl.backgroundColor = NCBrandColor.sharedInstance.brand
  70. refreshControl.addTarget(self, action: #selector(loadDatasource), for: .valueChanged)
  71. // empty Data Source
  72. self.collectionView.emptyDataSetDelegate = self;
  73. self.collectionView.emptyDataSetSource = self;
  74. }
  75. override func viewWillAppear(_ animated: Bool) {
  76. super.viewWillAppear(animated)
  77. self.navigationItem.title = titleCurrentFolder
  78. datasourceAscending = CCUtility.getAscendingSettings()
  79. datasourceSorted = CCUtility.getOrderSettings()
  80. datasourceGroupBy = CCUtility.getGroupBySettings()
  81. datasourceDirectoryOnTop = CCUtility.getDirectoryOnTop()
  82. loadDatasource()
  83. }
  84. override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
  85. super.viewWillTransition(to: size, with: coordinator)
  86. coordinator.animate(alongsideTransition: nil) { _ in
  87. self.collectionView.collectionViewLayout.invalidateLayout()
  88. self.actionSheet?.viewDidLayoutSubviews()
  89. }
  90. }
  91. // MARK: DZNEmpty
  92. func backgroundColor(forEmptyDataSet scrollView: UIScrollView) -> UIColor? {
  93. return NCBrandColor.sharedInstance.backgroundView
  94. }
  95. func image(forEmptyDataSet scrollView: UIScrollView) -> UIImage? {
  96. return CCGraphics.changeThemingColorImage(UIImage.init(named: "filesNoFiles"), multiplier: 2, color: NCBrandColor.sharedInstance.brandElement)
  97. }
  98. func title(forEmptyDataSet scrollView: UIScrollView) -> NSAttributedString? {
  99. let text = "\n"+NSLocalizedString("_files_no_files_", comment: "")
  100. let attributes = [NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 20), NSAttributedString.Key.foregroundColor: UIColor.lightGray]
  101. return NSAttributedString.init(string: text, attributes: attributes)
  102. }
  103. func emptyDataSetShouldAllowScroll(_ scrollView: UIScrollView) -> Bool {
  104. return true
  105. }
  106. // MARK: TAP EVENT
  107. func tapSwitchHeader(sender: Any) {
  108. if collectionView.collectionViewLayout == gridLayout {
  109. // list layout
  110. UIView.animate(withDuration: 0.0, animations: {
  111. self.collectionView.collectionViewLayout.invalidateLayout()
  112. self.collectionView.setCollectionViewLayout(self.listLayout, animated: false, completion: { (_) in
  113. self.collectionView.reloadData()
  114. self.collectionView.setContentOffset(CGPoint(x:0,y:0), animated: false)
  115. })
  116. })
  117. CCUtility.setLayoutOffline("list")
  118. } else {
  119. // grid layout
  120. UIView.animate(withDuration: 0.0, animations: {
  121. self.collectionView.collectionViewLayout.invalidateLayout()
  122. self.collectionView.setCollectionViewLayout(self.gridLayout, animated: false, completion: { (_) in
  123. self.collectionView.reloadData()
  124. self.collectionView.setContentOffset(CGPoint(x:0,y:0), animated: false)
  125. })
  126. })
  127. CCUtility.setLayoutOffline("grid")
  128. }
  129. }
  130. func tapOrderHeader(sender: Any) {
  131. var menuView: DropdownMenu?
  132. var selectedIndexPath = [IndexPath()]
  133. let item1 = DropdownItem(image: CCGraphics.changeThemingColorImage(UIImage.init(named: "sortFileNameAZ"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), title: NSLocalizedString("_order_by_name_a_z_", comment: ""))
  134. let item2 = DropdownItem(image: CCGraphics.changeThemingColorImage(UIImage.init(named: "sortFileNameZA"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), title: NSLocalizedString("_order_by_name_z_a_", comment: ""))
  135. let item3 = DropdownItem(image: CCGraphics.changeThemingColorImage(UIImage.init(named: "sortDateMoreRecent"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), title: NSLocalizedString("_order_by_date_more_recent_", comment: ""))
  136. let item4 = DropdownItem(image: CCGraphics.changeThemingColorImage(UIImage.init(named: "sortDateLessRecent"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), title: NSLocalizedString("_order_by_date_less_recent_", comment: ""))
  137. let item5 = DropdownItem(image: CCGraphics.changeThemingColorImage(UIImage.init(named: "sortSmallest"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), title: NSLocalizedString("_order_by_size_smallest_", comment: ""))
  138. let item6 = DropdownItem(image: CCGraphics.changeThemingColorImage(UIImage.init(named: "sortLargest"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), title: NSLocalizedString("_order_by_size_largest_", comment: ""))
  139. switch datasourceSorted {
  140. case "fileName":
  141. if datasourceAscending == true { item1.style = .highlight; selectedIndexPath.append(IndexPath(row: 0, section: 0)) }
  142. if datasourceAscending == false { item2.style = .highlight; selectedIndexPath.append(IndexPath(row: 1, section: 0)) }
  143. case "date":
  144. if datasourceAscending == false { item3.style = .highlight; selectedIndexPath.append(IndexPath(row: 2, section: 0)) }
  145. if datasourceAscending == true { item4.style = .highlight; selectedIndexPath.append(IndexPath(row: 3, section: 0)) }
  146. case "size":
  147. if datasourceAscending == true { item5.style = .highlight; selectedIndexPath.append(IndexPath(row: 4, section: 0)) }
  148. if datasourceAscending == false { item6.style = .highlight; selectedIndexPath.append(IndexPath(row: 5, section: 0)) }
  149. default:
  150. ()
  151. }
  152. let item7 = DropdownItem(image: CCGraphics.changeThemingColorImage(UIImage.init(named: "MenuGroupByAlphabetic"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), title: NSLocalizedString("_group_alphabetic_no_", comment: ""))
  153. let item8 = DropdownItem(image: CCGraphics.changeThemingColorImage(UIImage.init(named: "MenuGroupByFile"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), title: NSLocalizedString("_group_typefile_no_", comment: ""))
  154. let item9 = DropdownItem(image: CCGraphics.changeThemingColorImage(UIImage.init(named: "MenuGroupByDate"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), title: NSLocalizedString("_group_date_no_", comment: ""))
  155. switch datasourceGroupBy {
  156. case "alphabetic":
  157. item7.style = .highlight; selectedIndexPath.append(IndexPath(row: 0, section: 1))
  158. case "typefile":
  159. item8.style = .highlight; selectedIndexPath.append(IndexPath(row: 1, section: 1))
  160. case "date":
  161. item9.style = .highlight; selectedIndexPath.append(IndexPath(row: 2, section: 1))
  162. default:
  163. ()
  164. }
  165. let item10 = DropdownItem(image: CCGraphics.changeThemingColorImage(UIImage.init(named: "foldersOnTop"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), title: NSLocalizedString("_directory_on_top_no_", comment: ""))
  166. if datasourceDirectoryOnTop {
  167. item10.style = .highlight; selectedIndexPath.append(IndexPath(row: 0, section: 2))
  168. }
  169. let sectionOrder = DropdownSection(sectionIdentifier: "", items: [item1, item2, item3, item4, item5, item6])
  170. let sectionGroupBy = DropdownSection(sectionIdentifier: "", items: [item7, item8, item9])
  171. let sectionFolderOnTop = DropdownSection(sectionIdentifier: "", items: [item10])
  172. menuView = DropdownMenu(navigationController: self.navigationController!, sections: [sectionOrder, sectionGroupBy, sectionFolderOnTop], selectedIndexPath: selectedIndexPath)
  173. menuView?.token = "tapOrderHeaderMenu"
  174. menuView?.delegate = self
  175. menuView?.rowHeight = 45
  176. menuView?.sectionHeaderHeight = 8
  177. menuView?.highlightColor = NCBrandColor.sharedInstance.brand
  178. menuView?.tableView.alwaysBounceVertical = false
  179. menuView?.tableViewBackgroundColor = UIColor.white
  180. let header = (sender as? UIButton)?.superview
  181. let headerRect = self.collectionView.convert(header!.bounds, from: self.view)
  182. let menuOffsetY = headerRect.height - headerRect.origin.y - 2
  183. menuView?.topOffsetY = CGFloat(menuOffsetY)
  184. menuView?.showMenu()
  185. }
  186. func tapMoreHeader(sender: Any) {
  187. var menuView: DropdownMenu?
  188. if isEditMode {
  189. let item0 = DropdownItem(image: CCGraphics.changeThemingColorImage(UIImage.init(named: "checkedNo"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), title: NSLocalizedString("_cancel_", comment: ""))
  190. let item1 = DropdownItem(image: CCGraphics.changeThemingColorImage(UIImage.init(named: "restore"), multiplier: 1, color: NCBrandColor.sharedInstance.icon), title: NSLocalizedString("_trash_restore_selected_", comment: ""))
  191. let item2 = DropdownItem(image: CCGraphics.changeThemingColorImage(UIImage.init(named: "trash"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), title: NSLocalizedString("_trash_delete_selected_", comment: ""))
  192. menuView = DropdownMenu(navigationController: self.navigationController!, items: [item0, item1, item2], selectedRow: -1)
  193. menuView?.token = "tapMoreHeaderMenuSelect"
  194. } else {
  195. let item0 = DropdownItem(image: CCGraphics.changeThemingColorImage(UIImage.init(named: "select"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), title: NSLocalizedString("_select_", comment: ""))
  196. let item1 = DropdownItem(image: CCGraphics.changeThemingColorImage(UIImage.init(named: "restore"), multiplier: 1, color: NCBrandColor.sharedInstance.icon), title: NSLocalizedString("_trash_restore_all_", comment: ""))
  197. let item2 = DropdownItem(image: CCGraphics.changeThemingColorImage(UIImage.init(named: "trash"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), title: NSLocalizedString("_trash_delete_all_", comment: ""))
  198. menuView = DropdownMenu(navigationController: self.navigationController!, items: [item0, item1, item2], selectedRow: -1)
  199. menuView?.token = "tapMoreHeaderMenu"
  200. }
  201. menuView?.delegate = self
  202. menuView?.rowHeight = 50
  203. menuView?.tableView.alwaysBounceVertical = false
  204. let header = (sender as? UIButton)?.superview
  205. let headerRect = self.collectionView.convert(header!.bounds, from: self.view)
  206. let menuOffsetY = headerRect.height - headerRect.origin.y - 2
  207. menuView?.topOffsetY = CGFloat(menuOffsetY)
  208. menuView?.showMenu()
  209. }
  210. func tapMoreItem(with fileID: String, sender: Any) {
  211. tapMoreGridItem(with: fileID, sender: sender)
  212. }
  213. func tapMoreGridItem(with fileID: String, sender: Any) {
  214. guard let metadata = NCManageDatabase.sharedInstance.getMetadata(predicate: NSPredicate(format: "fileID == %@", fileID)) else {
  215. return
  216. }
  217. guard let serverUrl = NCManageDatabase.sharedInstance.getServerUrl(metadata.directoryID) else {
  218. return
  219. }
  220. if !isEditMode {
  221. var items = [ActionSheetItem]()
  222. let appearanceDelete = ActionSheetItemAppearance.init()
  223. appearanceDelete.textColor = UIColor.red
  224. if (metadata.directory == false || serverUrl == CCUtility.getHomeServerUrlActiveUrl(appDelegate.activeUrl)) {
  225. items.append(ActionSheetItem(title: NSLocalizedString("_remove_available_offline_", comment: ""), value: 0, image: CCGraphics.changeThemingColorImage(UIImage.init(named: "offline"), multiplier: 2, color: NCBrandColor.sharedInstance.icon)))
  226. }
  227. items.append(ActionSheetItem(title: NSLocalizedString("_share_", comment: ""), value: 1, image: CCGraphics.changeThemingColorImage(UIImage.init(named: "share"), multiplier: 2, color: NCBrandColor.sharedInstance.icon)))
  228. let itemDelete = ActionSheetItem(title: NSLocalizedString("_delete_", comment: ""), value: 2, image: CCGraphics.changeThemingColorImage(UIImage.init(named: "trash"), multiplier: 2, color: UIColor.red))
  229. itemDelete.customAppearance = appearanceDelete
  230. items.append(itemDelete)
  231. items.append(ActionSheetCancelButton(title: NSLocalizedString("_cancel_", comment: "")))
  232. actionSheet = ActionSheet(items: items) { sheet, item in
  233. if item.value as? Int == 0 {
  234. if metadata.directory {
  235. NCManageDatabase.sharedInstance.setDirectory(serverUrl: CCUtility.stringAppendServerUrl(serverUrl, addFileName: metadata.fileName)!, offline: false)
  236. } else {
  237. NCManageDatabase.sharedInstance.setLocalFile(fileID: metadata.fileID, offline: false)
  238. }
  239. self.loadDatasource()
  240. }
  241. if item.value as? Int == 1 { self.appDelegate.activeMain.openWindowShare(metadata) }
  242. if item.value as? Int == 2 { }
  243. if item is ActionSheetCancelButton { print("Cancel buttons has the value `true`") }
  244. }
  245. let headerView = actionSheetHeader(with: metadata)
  246. actionSheet?.headerView = headerView
  247. actionSheet?.headerView?.frame.size.height = 50
  248. actionSheet?.present(in: self, from: sender as! UIButton)
  249. } else {
  250. let buttonPosition:CGPoint = (sender as! UIButton).convert(CGPoint.zero, to:collectionView)
  251. let indexPath = collectionView.indexPathForItem(at: buttonPosition)
  252. collectionView(self.collectionView, didSelectItemAt: indexPath!)
  253. }
  254. }
  255. // MARK: DROP-DOWN-MENU
  256. func dropdownMenu(_ dropdownMenu: DropdownMenu, didSelectRowAt indexPath: IndexPath) {
  257. if dropdownMenu.token == "tapOrderHeaderMenu" {
  258. switch indexPath.section {
  259. case 0: switch indexPath.row {
  260. case 0: CCUtility.setOrderSettings("fileName"); CCUtility.setAscendingSettings(true)
  261. case 1: CCUtility.setOrderSettings("fileName"); CCUtility.setAscendingSettings(false)
  262. case 2: CCUtility.setOrderSettings("date"); CCUtility.setAscendingSettings(false)
  263. case 3: CCUtility.setOrderSettings("date"); CCUtility.setAscendingSettings(true)
  264. case 4: CCUtility.setOrderSettings("size"); CCUtility.setAscendingSettings(true)
  265. case 5: CCUtility.setOrderSettings("size"); CCUtility.setAscendingSettings(false)
  266. default: ()
  267. }
  268. case 1: switch indexPath.row {
  269. case 0:
  270. if datasourceGroupBy == "alphabetic" {
  271. CCUtility.setGroupBySettings("none")
  272. } else {
  273. CCUtility.setGroupBySettings("alphabetic")
  274. }
  275. case 1:
  276. if datasourceGroupBy == "typefile" {
  277. CCUtility.setGroupBySettings("none")
  278. } else {
  279. CCUtility.setGroupBySettings("typefile")
  280. }
  281. case 2:
  282. if datasourceGroupBy == "date" {
  283. CCUtility.setGroupBySettings("none")
  284. } else {
  285. CCUtility.setGroupBySettings("date")
  286. }
  287. default: ()
  288. }
  289. case 2: if datasourceDirectoryOnTop {
  290. CCUtility.setDirectoryOnTop(false)
  291. } else {
  292. CCUtility.setDirectoryOnTop(true)
  293. }
  294. default: ()
  295. }
  296. datasourceAscending = CCUtility.getAscendingSettings()
  297. datasourceSorted = CCUtility.getOrderSettings()
  298. datasourceGroupBy = CCUtility.getGroupBySettings()
  299. datasourceDirectoryOnTop = CCUtility.getDirectoryOnTop()
  300. loadDatasource()
  301. }
  302. if dropdownMenu.token == "tapMoreHeaderMenu" {
  303. }
  304. if dropdownMenu.token == "tapMoreHeaderMenuSelect" {
  305. }
  306. }
  307. // MARK: NC API
  308. func downloadThumbnail(with tableMetadata: tableMetadata, indexPath: IndexPath) {
  309. let ocNetworking = OCnetworking.init(delegate: self, metadataNet: nil, withUser: appDelegate.activeUser, withUserID: appDelegate.activeUserID, withPassword: appDelegate.activePassword, withUrl: appDelegate.activeUrl)
  310. ocNetworking?.downloadPreviewTrash(withFileID: tableMetadata.fileID, fileName: tableMetadata.fileName, completion: { (message, errorCode) in
  311. if errorCode == 0 && CCUtility.fileProviderStorageIconExists(tableMetadata.fileID, fileNameView: tableMetadata.fileName) {
  312. self.collectionView.reloadItems(at: [indexPath])
  313. }
  314. })
  315. }
  316. // MARK: DATASOURCE
  317. @objc func loadDatasource() {
  318. var fileIDs = [String]()
  319. sectionDatasource = CCSectionDataSourceMetadata()
  320. if directoryID == "" {
  321. if let directories = NCManageDatabase.sharedInstance.getTablesDirectory(predicate: NSPredicate(format: "account == %@ AND offline == true", appDelegate.activeAccount), sorted: "serverUrl", ascending: true) {
  322. for directory: tableDirectory in directories {
  323. fileIDs.append(directory.fileID)
  324. }
  325. }
  326. if let files = NCManageDatabase.sharedInstance.getTableLocalFiles(predicate: NSPredicate(format: "account == %@ AND offline == true", appDelegate.activeAccount), sorted: "fileName", ascending: true) {
  327. for file: tableLocalFile in files {
  328. fileIDs.append(file.fileID)
  329. }
  330. }
  331. if let metadatas = NCManageDatabase.sharedInstance.getMetadatas(predicate: NSPredicate(format: "account == %@ AND fileID IN %@", appDelegate.activeAccount, fileIDs), sorted: datasourceSorted, ascending: datasourceAscending) {
  332. sectionDatasource = CCSectionMetadata.creataDataSourseSectionMetadata(metadatas, listProgressMetadata: nil, groupByField: datasourceGroupBy, filterFileID: nil, filterTypeFileImage: false, filterTypeFileVideo: false, activeAccount: appDelegate.activeAccount)
  333. }
  334. } else {
  335. if let metadatas = NCManageDatabase.sharedInstance.getMetadatas(predicate: NSPredicate(format: "account == %@ AND directoryID == %@", appDelegate.activeAccount, directoryID), sorted: datasourceSorted, ascending: datasourceAscending) {
  336. sectionDatasource = CCSectionMetadata.creataDataSourseSectionMetadata(metadatas, listProgressMetadata: nil, groupByField: datasourceGroupBy, filterFileID: nil, filterTypeFileImage: false, filterTypeFileVideo: false, activeAccount: appDelegate.activeAccount)
  337. }
  338. }
  339. self.refreshControl.endRefreshing()
  340. collectionView.reloadData()
  341. }
  342. // MARK: COLLECTIONVIEW METHODS
  343. func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
  344. if (indexPath.section == 0) {
  345. if kind == UICollectionView.elementKindSectionHeader {
  346. let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "sectionHeaderMenu", for: indexPath) as! NCOfflineSectionHeaderMenu
  347. if collectionView.collectionViewLayout == gridLayout {
  348. header.buttonSwitch.setImage(CCGraphics.changeThemingColorImage(UIImage.init(named: "switchList"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), for: .normal)
  349. } else {
  350. header.buttonSwitch.setImage(CCGraphics.changeThemingColorImage(UIImage.init(named: "switchGrid"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), for: .normal)
  351. }
  352. header.delegate = self
  353. header.setStatusButton(count: sectionDatasource.allFileID.count)
  354. header.setTitleOrder(datasourceSorted: datasourceSorted, datasourceAscending: datasourceAscending)
  355. if datasourceGroupBy == "none" {
  356. header.labelSection.isHidden = true
  357. header.labelSectionHeightConstraint.constant = 0
  358. } else {
  359. header.labelSection.isHidden = false
  360. header.setTitleLabel(sectionDatasource: sectionDatasource, section: indexPath.section)
  361. header.labelSectionHeightConstraint.constant = sectionHeaderHeight
  362. }
  363. return header
  364. } else {
  365. let footer = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "footer", for: indexPath) as! NCOfflineFooter
  366. footer.setTitleLabel(sectionDatasource: sectionDatasource)
  367. return footer
  368. }
  369. } else {
  370. if kind == UICollectionView.elementKindSectionHeader {
  371. let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "sectionHeader", for: indexPath) as! NCOfflineSectionHeader
  372. header.setTitleLabel(sectionDatasource: sectionDatasource, section: indexPath.section)
  373. return header
  374. } else {
  375. let footer = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "footer", for: indexPath) as! NCOfflineFooter
  376. footer.setTitleLabel(sectionDatasource: sectionDatasource)
  377. return footer
  378. }
  379. }
  380. }
  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. func numberOfSections(in collectionView: UICollectionView) -> Int {
  401. let sections = sectionDatasource.sectionArrayRow.allKeys.count
  402. return sections
  403. }
  404. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  405. let key = sectionDatasource.sections.object(at: section)
  406. let datasource = sectionDatasource.sectionArrayRow.object(forKey: key) as! [tableMetadata]
  407. return datasource.count
  408. }
  409. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  410. var image: UIImage?
  411. guard let metadata = NCMainCommon.sharedInstance.getMetadataFromSectionDataSourceIndexPath(indexPath, sectionDataSource: sectionDatasource) else {
  412. return collectionView.dequeueReusableCell(withReuseIdentifier: "cell-list", for: indexPath) as! NCOfflineListCell
  413. }
  414. if metadata.iconName.count > 0 {
  415. image = UIImage.init(named: metadata.iconName)
  416. } else {
  417. image = UIImage.init(named: "file")
  418. }
  419. if FileManager().fileExists(atPath: CCUtility.getDirectoryProviderStorageIconFileID(metadata.fileID, fileNameView: metadata.fileName)) {
  420. image = UIImage.init(contentsOfFile: CCUtility.getDirectoryProviderStorageIconFileID(metadata.fileID, fileNameView: metadata.fileName))
  421. } else {
  422. if metadata.thumbnailExists && !CCUtility.fileProviderStorageIconExists(metadata.fileID, fileNameView: metadata.fileName) {
  423. downloadThumbnail(with: metadata, indexPath: indexPath)
  424. }
  425. }
  426. if collectionView.collectionViewLayout == listLayout {
  427. // LIST
  428. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell-list", for: indexPath) as! NCOfflineListCell
  429. cell.delegate = self
  430. cell.fileID = metadata.fileID
  431. cell.indexPath = indexPath
  432. cell.labelTitle.text = metadata.fileNameView
  433. if metadata.directory {
  434. cell.imageItem.image = CCGraphics.changeThemingColorImage(UIImage.init(named: "folder"), multiplier: 3, color: NCBrandColor.sharedInstance.brandElement)
  435. cell.labelInfo.text = CCUtility.dateDiff(metadata.date as Date)
  436. } else {
  437. cell.imageItem.image = image
  438. cell.labelInfo.text = CCUtility.dateDiff(metadata.date as Date) + " " + CCUtility.transformedSize(metadata.size)
  439. }
  440. if isEditMode {
  441. cell.imageItemLeftConstraint.constant = 45
  442. cell.imageSelect.isHidden = false
  443. if selectFileID.contains(metadata.fileID) {
  444. cell.imageSelect.image = CCGraphics.changeThemingColorImage(UIImage.init(named: "checkedYes"), multiplier: 2, color: NCBrandColor.sharedInstance.brand)
  445. cell.backgroundView = cellBlurEffect(with: cell.bounds)
  446. } else {
  447. cell.imageSelect.image = CCGraphics.changeThemingColorImage(UIImage.init(named: "checkedNo"), multiplier: 2, color: NCBrandColor.sharedInstance.optionItem)
  448. cell.backgroundView = nil
  449. }
  450. } else {
  451. cell.imageItemLeftConstraint.constant = 10
  452. cell.imageSelect.isHidden = true
  453. cell.backgroundView = nil
  454. }
  455. // Remove last separator
  456. if collectionView.numberOfItems(inSection: indexPath.section) == indexPath.row + 1 {
  457. cell.separator.isHidden = true
  458. } else {
  459. cell.separator.isHidden = false
  460. }
  461. return cell
  462. } else {
  463. // GRID
  464. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell-grid", for: indexPath) as! NCOfflineGridCell
  465. cell.delegate = self
  466. cell.fileID = metadata.fileID
  467. cell.indexPath = indexPath
  468. cell.labelTitle.text = metadata.fileNameView
  469. if metadata.directory {
  470. cell.imageItem.image = CCGraphics.changeThemingColorImage(UIImage.init(named: "folder"), multiplier: 3, color: NCBrandColor.sharedInstance.brandElement)
  471. } else {
  472. cell.imageItem.image = image
  473. }
  474. if isEditMode {
  475. cell.imageSelect.isHidden = false
  476. if selectFileID.contains(metadata.fileID) {
  477. cell.imageSelect.image = CCGraphics.changeThemingColorImage(UIImage.init(named: "checkedYes"), multiplier: 2, color: UIColor.white)
  478. cell.backgroundView = cellBlurEffect(with: cell.bounds)
  479. } else {
  480. cell.imageSelect.isHidden = true
  481. cell.backgroundView = nil
  482. }
  483. } else {
  484. cell.imageSelect.isHidden = true
  485. cell.backgroundView = nil
  486. }
  487. return cell
  488. }
  489. }
  490. func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  491. guard let metadata = NCMainCommon.sharedInstance.getMetadataFromSectionDataSourceIndexPath(indexPath, sectionDataSource: sectionDatasource) else {
  492. return
  493. }
  494. if isEditMode {
  495. if let index = selectFileID.index(of: metadata.fileID) {
  496. selectFileID.remove(at: index)
  497. } else {
  498. selectFileID.append(metadata.fileID)
  499. }
  500. collectionView.reloadItems(at: [indexPath])
  501. return
  502. }
  503. if metadata.directory {
  504. let ncOffline:NCOffline = UIStoryboard(name: "NCOffline", bundle: nil).instantiateInitialViewController() as! NCOffline
  505. guard let serverUrl = NCManageDatabase.sharedInstance.getServerUrl(metadata.directoryID) else {
  506. return
  507. }
  508. let serverUrlPush = CCUtility.stringAppendServerUrl(serverUrl, addFileName: metadata.fileName)
  509. guard let directoryIDPush = NCManageDatabase.sharedInstance.getDirectoryID(serverUrlPush) else {
  510. return
  511. }
  512. ncOffline.directoryID = directoryIDPush
  513. ncOffline.titleCurrentFolder = metadata.fileNameView
  514. self.navigationController?.pushViewController(ncOffline, animated: true)
  515. }
  516. }
  517. // MARK: UTILITY
  518. private func cellBlurEffect(with frame: CGRect) -> UIView {
  519. let blurEffect = UIBlurEffect(style: .extraLight)
  520. let blurEffectView = UIVisualEffectView(effect: blurEffect)
  521. blurEffectView.frame = frame
  522. blurEffectView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
  523. blurEffectView.backgroundColor = NCBrandColor.sharedInstance.brand.withAlphaComponent(0.2)
  524. return blurEffectView
  525. }
  526. private func actionSheetHeader(with metadata: tableMetadata) -> UIView? {
  527. var image: UIImage?
  528. // Header
  529. if metadata.directory {
  530. image = CCGraphics.changeThemingColorImage(UIImage.init(named: "folder"), multiplier: 3, color: NCBrandColor.sharedInstance.brandElement)
  531. } else if metadata.iconName.count > 0 {
  532. image = UIImage.init(named: metadata.iconName)
  533. } else {
  534. image = UIImage.init(named: "file")
  535. }
  536. if FileManager().fileExists(atPath: CCUtility.getDirectoryProviderStorageIconFileID(metadata.fileID, fileNameView: metadata.fileNameView)) {
  537. image = UIImage.init(contentsOfFile: CCUtility.getDirectoryProviderStorageIconFileID(metadata.fileID, fileNameView: metadata.fileNameView))
  538. }
  539. let headerView = UINib(nibName: "NCActionSheetHeaderView", bundle: nil).instantiate(withOwner: self, options: nil).first as! NCActionSheetHeaderView
  540. headerView.imageItem.image = image
  541. headerView.label.text = metadata.fileNameView
  542. headerView.label.textColor = NCBrandColor.sharedInstance.icon
  543. return headerView
  544. }
  545. }