NCOffline.swift 35 KB

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