NCOffline.swift 36 KB

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