NCOffline.swift 35 KB

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