NCOffline.swift 37 KB

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