NCOffline.swift 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705
  1. //
  2. // NCOffline.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 24/10/2018.
  6. // Copyright © 2018 Marino Faggiana. All rights reserved.
  7. //
  8. // Author Marino Faggiana <m.faggiana@twsweb.it>
  9. //
  10. // This program is free software: you can redistribute it and/or modify
  11. // it under the terms of the GNU General Public License as published by
  12. // the Free Software Foundation, either version 3 of the License, or
  13. // (at your option) any later version.
  14. //
  15. // This program is distributed in the hope that it will be useful,
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. // GNU General Public License for more details.
  19. //
  20. // You should have received a copy of the GNU General Public License
  21. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. //
  23. import Foundation
  24. class NCOffline: UIViewController ,UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout, UIGestureRecognizerDelegate, NCListCellDelegate, NCGridCellDelegate, NCSectionHeaderMenuDelegate, DropdownMenuDelegate, DZNEmptyDataSetSource, DZNEmptyDataSetDelegate, BKPasscodeViewControllerDelegate {
  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 metadataPush: tableMetadata?
  30. private var isEditMode = false
  31. private var selectFileID = [String]()
  32. private var sectionDatasource = CCSectionDataSourceMetadata()
  33. private var typeLayout = ""
  34. private var datasourceSorted = ""
  35. private var datasourceAscending = true
  36. private var datasourceGroupBy = ""
  37. private var datasourceDirectoryOnTop = false
  38. private var autoUploadFileName = ""
  39. private var autoUploadDirectory = ""
  40. private var listLayout: NCListLayout!
  41. private var gridLayout: NCGridLayout!
  42. private var actionSheet: ActionSheet?
  43. private let headerMenuHeight: CGFloat = 50
  44. private let sectionHeaderHeight: CGFloat = 20
  45. private let footerHeight: CGFloat = 50
  46. private let refreshControl = UIRefreshControl()
  47. //BKPasscodeViewController
  48. private var failedAttempts: Double = 0
  49. private var lockUntilDate: NSDate?
  50. override func viewDidLoad() {
  51. super.viewDidLoad()
  52. // Cell
  53. collectionView.register(UINib.init(nibName: "NCListCell", bundle: nil), forCellWithReuseIdentifier: "listCell")
  54. collectionView.register(UINib.init(nibName: "NCGridCell", bundle: nil), forCellWithReuseIdentifier: "gridCell")
  55. // Header
  56. collectionView.register(UINib.init(nibName: "NCSectionHeaderMenu", bundle: nil), forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "sectionHeaderMenu")
  57. collectionView.register(UINib.init(nibName: "NCSectionHeader", bundle: nil), forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "sectionHeader")
  58. // Footer
  59. collectionView.register(UINib.init(nibName: "NCSectionFooter", bundle: nil), forSupplementaryViewOfKind: UICollectionView.elementKindSectionFooter, withReuseIdentifier: "sectionFooter")
  60. collectionView.alwaysBounceVertical = true
  61. listLayout = NCListLayout()
  62. gridLayout = NCGridLayout()
  63. // Add Refresh Control
  64. if #available(iOS 10.0, *) {
  65. collectionView.refreshControl = refreshControl
  66. } else {
  67. collectionView.addSubview(refreshControl)
  68. }
  69. // Configure Refresh Control
  70. refreshControl.tintColor = NCBrandColor.sharedInstance.brandText
  71. refreshControl.backgroundColor = NCBrandColor.sharedInstance.brand
  72. refreshControl.addTarget(self, action: #selector(loadDatasource), for: .valueChanged)
  73. // empty Data Source
  74. self.collectionView.emptyDataSetDelegate = self;
  75. self.collectionView.emptyDataSetSource = self;
  76. }
  77. override func viewWillAppear(_ animated: Bool) {
  78. super.viewWillAppear(animated)
  79. // Color
  80. appDelegate.aspectNavigationControllerBar(self.navigationController?.navigationBar, online: appDelegate.reachability.isReachable(), hidden: false)
  81. appDelegate.aspectTabBar(self.tabBarController?.tabBar, hidden: false)
  82. self.navigationItem.title = titleCurrentFolder
  83. (typeLayout, datasourceSorted, datasourceAscending, datasourceGroupBy, datasourceDirectoryOnTop) = NCUtility.sharedInstance.getLayoutForView(key: k_layout_view_offline)
  84. // get auto upload folder
  85. autoUploadFileName = NCManageDatabase.sharedInstance.getAccountAutoUploadFileName()
  86. autoUploadDirectory = NCManageDatabase.sharedInstance.getAccountAutoUploadDirectory(appDelegate.activeUrl)
  87. if typeLayout == k_layout_list {
  88. collectionView.collectionViewLayout = listLayout
  89. } else {
  90. collectionView.collectionViewLayout = gridLayout
  91. }
  92. loadDatasource()
  93. }
  94. override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
  95. super.viewWillTransition(to: size, with: coordinator)
  96. coordinator.animate(alongsideTransition: nil) { _ in
  97. self.collectionView.collectionViewLayout.invalidateLayout()
  98. self.actionSheet?.viewDidLayoutSubviews()
  99. }
  100. }
  101. // MARK: DZNEmpty
  102. func backgroundColor(forEmptyDataSet scrollView: UIScrollView) -> UIColor? {
  103. return NCBrandColor.sharedInstance.backgroundView
  104. }
  105. func image(forEmptyDataSet scrollView: UIScrollView) -> UIImage? {
  106. return CCGraphics.changeThemingColorImage(UIImage.init(named: "filesNoFiles"), multiplier: 2, color: NCBrandColor.sharedInstance.brandElement)
  107. }
  108. func title(forEmptyDataSet scrollView: UIScrollView) -> NSAttributedString? {
  109. let text = "\n"+NSLocalizedString("_files_no_files_", comment: "")
  110. let attributes = [NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 20), NSAttributedString.Key.foregroundColor: UIColor.lightGray]
  111. return NSAttributedString.init(string: text, attributes: attributes)
  112. }
  113. func emptyDataSetShouldAllowScroll(_ scrollView: UIScrollView) -> Bool {
  114. return true
  115. }
  116. // MARK: BKPASSCODEVIEWCONTROLLER
  117. func passcodeViewController(_ aViewController: BKPasscodeViewController!, didFinishWithPasscode aPasscode: String!) {
  118. aViewController.dismiss(animated: true, completion: nil)
  119. performSegueDirectoryWithControlPasscode(controlPasscode: false)
  120. }
  121. func passcodeViewController(_ aViewController: BKPasscodeViewController!, authenticatePasscode aPasscode: String!, resultHandler aResultHandler: ((Bool) -> Void)!) {
  122. if aPasscode == CCUtility.getBlockCode() {
  123. failedAttempts = 0
  124. lockUntilDate = nil
  125. aResultHandler(true)
  126. } else {
  127. aResultHandler(false)
  128. }
  129. }
  130. func passcodeViewControllerDidFailAttempt(_ aViewController: BKPasscodeViewController!) {
  131. failedAttempts += 1
  132. if failedAttempts > 5 {
  133. var timeInterval: TimeInterval = 60
  134. if failedAttempts > 6 {
  135. let multiplier: Double = failedAttempts - 6
  136. timeInterval = (5 * 60) * multiplier
  137. if timeInterval > 3600 * 24 {
  138. timeInterval = 3600 * 24
  139. }
  140. }
  141. lockUntilDate = NSDate.init(timeIntervalSinceNow: timeInterval)
  142. }
  143. }
  144. func passcodeViewControllerNumber(ofFailedAttempts aViewController: BKPasscodeViewController!) -> UInt {
  145. return UInt(failedAttempts)
  146. }
  147. func passcodeViewControllerLock(untilDate aViewController: BKPasscodeViewController!) -> Date? {
  148. return lockUntilDate as Date?
  149. }
  150. @objc func passcodeViewCloseButtonPressed(_ sender: Any) {
  151. self.dismiss(animated: true, completion: nil)
  152. }
  153. // MARK: TAP EVENT
  154. func tapSwitchHeader(sender: Any) {
  155. if collectionView.collectionViewLayout == gridLayout {
  156. // list layout
  157. UIView.animate(withDuration: 0.0, animations: {
  158. self.collectionView.collectionViewLayout.invalidateLayout()
  159. self.collectionView.setCollectionViewLayout(self.listLayout, animated: false, completion: { (_) in
  160. self.collectionView.reloadData()
  161. self.collectionView.setContentOffset(CGPoint(x:0,y:0), animated: false)
  162. })
  163. })
  164. typeLayout = k_layout_list
  165. NCUtility.sharedInstance.setLayoutForView(key: k_layout_view_offline, layout: typeLayout, sort: datasourceSorted, ascending: datasourceAscending, groupBy: datasourceGroupBy, directoryOnTop: datasourceDirectoryOnTop)
  166. } else {
  167. // grid layout
  168. UIView.animate(withDuration: 0.0, animations: {
  169. self.collectionView.collectionViewLayout.invalidateLayout()
  170. self.collectionView.setCollectionViewLayout(self.gridLayout, animated: false, completion: { (_) in
  171. self.collectionView.reloadData()
  172. self.collectionView.setContentOffset(CGPoint(x:0,y:0), animated: false)
  173. })
  174. })
  175. typeLayout = k_layout_grid
  176. NCUtility.sharedInstance.setLayoutForView(key: k_layout_view_offline, layout: typeLayout, sort: datasourceSorted, ascending: datasourceAscending, groupBy: datasourceGroupBy, directoryOnTop: datasourceDirectoryOnTop)
  177. }
  178. }
  179. func tapOrderHeader(sender: Any) {
  180. var menuView: DropdownMenu?
  181. var selectedIndexPath = [IndexPath()]
  182. let item1 = DropdownItem(image: CCGraphics.changeThemingColorImage(UIImage.init(named: "sortFileNameAZ"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), title: NSLocalizedString("_order_by_name_a_z_", comment: ""))
  183. let item2 = DropdownItem(image: CCGraphics.changeThemingColorImage(UIImage.init(named: "sortFileNameZA"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), title: NSLocalizedString("_order_by_name_z_a_", comment: ""))
  184. let item3 = DropdownItem(image: CCGraphics.changeThemingColorImage(UIImage.init(named: "sortDateMoreRecent"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), title: NSLocalizedString("_order_by_date_more_recent_", comment: ""))
  185. let item4 = DropdownItem(image: CCGraphics.changeThemingColorImage(UIImage.init(named: "sortDateLessRecent"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), title: NSLocalizedString("_order_by_date_less_recent_", comment: ""))
  186. let item5 = DropdownItem(image: CCGraphics.changeThemingColorImage(UIImage.init(named: "sortSmallest"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), title: NSLocalizedString("_order_by_size_smallest_", comment: ""))
  187. let item6 = DropdownItem(image: CCGraphics.changeThemingColorImage(UIImage.init(named: "sortLargest"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), title: NSLocalizedString("_order_by_size_largest_", comment: ""))
  188. switch datasourceSorted {
  189. case "fileName":
  190. if datasourceAscending == true { item1.style = .highlight; selectedIndexPath.append(IndexPath(row: 0, section: 0)) }
  191. if datasourceAscending == false { item2.style = .highlight; selectedIndexPath.append(IndexPath(row: 1, section: 0)) }
  192. case "date":
  193. if datasourceAscending == false { item3.style = .highlight; selectedIndexPath.append(IndexPath(row: 2, section: 0)) }
  194. if datasourceAscending == true { item4.style = .highlight; selectedIndexPath.append(IndexPath(row: 3, section: 0)) }
  195. case "size":
  196. if datasourceAscending == true { item5.style = .highlight; selectedIndexPath.append(IndexPath(row: 4, section: 0)) }
  197. if datasourceAscending == false { item6.style = .highlight; selectedIndexPath.append(IndexPath(row: 5, section: 0)) }
  198. default:
  199. ()
  200. }
  201. let item7 = DropdownItem(image: CCGraphics.changeThemingColorImage(UIImage.init(named: "MenuGroupByAlphabetic"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), title: NSLocalizedString("_group_alphabetic_no_", comment: ""))
  202. let item8 = DropdownItem(image: CCGraphics.changeThemingColorImage(UIImage.init(named: "MenuGroupByFile"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), title: NSLocalizedString("_group_typefile_no_", comment: ""))
  203. let item9 = DropdownItem(image: CCGraphics.changeThemingColorImage(UIImage.init(named: "MenuGroupByDate"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), title: NSLocalizedString("_group_date_no_", comment: ""))
  204. switch datasourceGroupBy {
  205. case "alphabetic":
  206. item7.style = .highlight; selectedIndexPath.append(IndexPath(row: 0, section: 1))
  207. case "typefile":
  208. item8.style = .highlight; selectedIndexPath.append(IndexPath(row: 1, section: 1))
  209. case "date":
  210. item9.style = .highlight; selectedIndexPath.append(IndexPath(row: 2, section: 1))
  211. default:
  212. ()
  213. }
  214. let item10 = DropdownItem(image: CCGraphics.changeThemingColorImage(UIImage.init(named: "foldersOnTop"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), title: NSLocalizedString("_directory_on_top_no_", comment: ""))
  215. if datasourceDirectoryOnTop {
  216. item10.style = .highlight; selectedIndexPath.append(IndexPath(row: 0, section: 2))
  217. }
  218. let sectionOrder = DropdownSection(sectionIdentifier: "", items: [item1, item2, item3, item4, item5, item6])
  219. let sectionGroupBy = DropdownSection(sectionIdentifier: "", items: [item7, item8, item9])
  220. let sectionFolderOnTop = DropdownSection(sectionIdentifier: "", items: [item10])
  221. menuView = DropdownMenu(navigationController: self.navigationController!, sections: [sectionOrder, sectionGroupBy, sectionFolderOnTop], selectedIndexPath: selectedIndexPath)
  222. menuView?.token = "tapOrderHeaderMenu"
  223. menuView?.delegate = self
  224. menuView?.rowHeight = 45
  225. menuView?.sectionHeaderHeight = 8
  226. menuView?.highlightColor = NCBrandColor.sharedInstance.brand
  227. menuView?.tableView.alwaysBounceVertical = false
  228. menuView?.tableViewBackgroundColor = UIColor.white
  229. let header = (sender as? UIButton)?.superview
  230. let headerRect = self.collectionView.convert(header!.bounds, from: self.view)
  231. let menuOffsetY = headerRect.height - headerRect.origin.y - 2
  232. menuView?.topOffsetY = CGFloat(menuOffsetY)
  233. menuView?.showMenu()
  234. }
  235. func tapMoreHeader(sender: Any) {
  236. }
  237. func tapMoreListItem(with fileID: String, sender: Any) {
  238. tapMoreGridItem(with: fileID, sender: sender)
  239. }
  240. func tapMoreGridItem(with fileID: String, sender: Any) {
  241. guard let metadata = NCManageDatabase.sharedInstance.getMetadata(predicate: NSPredicate(format: "fileID == %@", fileID)) else {
  242. return
  243. }
  244. guard let serverUrl = NCManageDatabase.sharedInstance.getServerUrl(metadata.directoryID) else {
  245. return
  246. }
  247. if !isEditMode {
  248. var items = [ActionSheetItem]()
  249. let appearanceDelete = ActionSheetItemAppearance.init()
  250. appearanceDelete.textColor = UIColor.red
  251. // 0 == CCMore, 1 = first NCOffline ....
  252. if (self == self.navigationController?.viewControllers[1]) {
  253. items.append(ActionSheetItem(title: NSLocalizedString("_remove_available_offline_", comment: ""), value: 0, image: CCGraphics.changeThemingColorImage(UIImage.init(named: "offline"), multiplier: 2, color: NCBrandColor.sharedInstance.icon)))
  254. }
  255. items.append(ActionSheetItem(title: NSLocalizedString("_share_", comment: ""), value: 1, image: CCGraphics.changeThemingColorImage(UIImage.init(named: "share"), multiplier: 2, color: NCBrandColor.sharedInstance.icon)))
  256. let itemDelete = ActionSheetItem(title: NSLocalizedString("_delete_", comment: ""), value: 2, image: CCGraphics.changeThemingColorImage(UIImage.init(named: "trash"), multiplier: 2, color: UIColor.red))
  257. itemDelete.customAppearance = appearanceDelete
  258. items.append(itemDelete)
  259. items.append(ActionSheetCancelButton(title: NSLocalizedString("_cancel_", comment: "")))
  260. actionSheet = ActionSheet(items: items) { sheet, item in
  261. if item.value as? Int == 0 {
  262. if metadata.directory {
  263. NCManageDatabase.sharedInstance.setDirectory(serverUrl: CCUtility.stringAppendServerUrl(serverUrl, addFileName: metadata.fileName)!, offline: false)
  264. } else {
  265. NCManageDatabase.sharedInstance.setLocalFile(fileID: metadata.fileID, offline: false)
  266. }
  267. self.loadDatasource()
  268. }
  269. if item.value as? Int == 1 { self.appDelegate.activeMain.openWindowShare(metadata) }
  270. if item.value as? Int == 2 { self.deleteItem(with: metadata, sender: sender) }
  271. if item is ActionSheetCancelButton { print("Cancel buttons has the value `true`") }
  272. }
  273. let headerView = NCActionSheetHeader.sharedInstance.actionSheetHeader(isDirectory: metadata.directory, iconName: metadata.iconName, fileID: metadata.fileID, fileNameView: metadata.fileNameView, text: metadata.fileNameView)
  274. actionSheet?.headerView = headerView
  275. actionSheet?.headerView?.frame.size.height = 50
  276. actionSheet?.present(in: self, from: sender as! UIButton)
  277. } else {
  278. let buttonPosition:CGPoint = (sender as! UIButton).convert(CGPoint.zero, to:collectionView)
  279. let indexPath = collectionView.indexPathForItem(at: buttonPosition)
  280. collectionView(self.collectionView, didSelectItemAt: indexPath!)
  281. }
  282. }
  283. // MARK: DROP-DOWN-MENU
  284. func dropdownMenu(_ dropdownMenu: DropdownMenu, didSelectRowAt indexPath: IndexPath) {
  285. if dropdownMenu.token == "tapOrderHeaderMenu" {
  286. switch indexPath.section {
  287. case 0: switch indexPath.row {
  288. case 0: datasourceSorted = "fileName"; datasourceAscending = true
  289. case 1: datasourceSorted = "fileName"; datasourceAscending = false
  290. case 2: datasourceSorted = "date"; datasourceAscending = false
  291. case 3: datasourceSorted = "date"; datasourceAscending = true
  292. case 4: datasourceSorted = "size"; datasourceAscending = true
  293. case 5: datasourceSorted = "size"; datasourceAscending = false
  294. default: ()
  295. }
  296. case 1: switch indexPath.row {
  297. case 0:
  298. if datasourceGroupBy == "alphabetic" {
  299. datasourceGroupBy = "none"
  300. } else {
  301. datasourceGroupBy = "alphabetic"
  302. }
  303. case 1:
  304. if datasourceGroupBy == "typefile" {
  305. datasourceGroupBy = "none"
  306. } else {
  307. datasourceGroupBy = "typefile"
  308. }
  309. case 2:
  310. if datasourceGroupBy == "date" {
  311. datasourceGroupBy = "none"
  312. } else {
  313. datasourceGroupBy = "date"
  314. }
  315. default: ()
  316. }
  317. case 2:
  318. if datasourceDirectoryOnTop {
  319. datasourceDirectoryOnTop = false
  320. } else {
  321. datasourceDirectoryOnTop = true
  322. }
  323. default: ()
  324. }
  325. NCUtility.sharedInstance.setLayoutForView(key: k_layout_view_offline, layout: typeLayout, sort: datasourceSorted, ascending: datasourceAscending, groupBy: datasourceGroupBy, directoryOnTop: datasourceDirectoryOnTop)
  326. loadDatasource()
  327. }
  328. if dropdownMenu.token == "tapMoreHeaderMenu" {
  329. }
  330. if dropdownMenu.token == "tapMoreHeaderMenuSelect" {
  331. }
  332. }
  333. // MARK: NC API
  334. func deleteItem(with metadata: tableMetadata, sender: Any) {
  335. var items = [ActionSheetItem]()
  336. guard let serverUrl = NCManageDatabase.sharedInstance.getServerUrl(metadata.directoryID) else {
  337. return
  338. }
  339. guard let tableDirectory = NCManageDatabase.sharedInstance.getTableDirectory(predicate: NSPredicate(format: "account == %@ AND serverUrl == serverUrl", appDelegate.activeAccount, serverUrl)) else {
  340. return
  341. }
  342. items.append(ActionSheetDangerButton(title: NSLocalizedString("_delete_", comment: "")))
  343. items.append(ActionSheetCancelButton(title: NSLocalizedString("_cancel_", comment: "")))
  344. actionSheet = ActionSheet(items: items) { sheet, item in
  345. if item is ActionSheetDangerButton {
  346. NCMainCommon.sharedInstance.deleteFile(metadatas: [metadata], e2ee: tableDirectory.e2eEncrypted, serverUrl: serverUrl, folderFileID: tableDirectory.fileID) { (errorCode, message) in
  347. self.loadDatasource()
  348. }
  349. }
  350. if item is ActionSheetCancelButton { print("Cancel buttons has the value `true`") }
  351. }
  352. let headerView = NCActionSheetHeader.sharedInstance.actionSheetHeader(isDirectory: metadata.directory, iconName: metadata.iconName, fileID: metadata.fileID, fileNameView: metadata.fileNameView, text: metadata.fileNameView)
  353. actionSheet?.headerView = headerView
  354. actionSheet?.headerView?.frame.size.height = 50
  355. actionSheet?.present(in: self, from: sender as! UIButton)
  356. }
  357. // MARK: DATASOURCE
  358. @objc func loadDatasource() {
  359. var fileIDs = [String]()
  360. sectionDatasource = CCSectionDataSourceMetadata()
  361. if directoryID == "" {
  362. if let directories = NCManageDatabase.sharedInstance.getTablesDirectory(predicate: NSPredicate(format: "account == %@ AND offline == true", appDelegate.activeAccount), sorted: "serverUrl", ascending: true) {
  363. for directory: tableDirectory in directories {
  364. fileIDs.append(directory.fileID)
  365. }
  366. }
  367. if let files = NCManageDatabase.sharedInstance.getTableLocalFiles(predicate: NSPredicate(format: "account == %@ AND offline == true", appDelegate.activeAccount), sorted: "fileName", ascending: true) {
  368. for file: tableLocalFile in files {
  369. fileIDs.append(file.fileID)
  370. }
  371. }
  372. if let metadatas = NCManageDatabase.sharedInstance.getMetadatas(predicate: NSPredicate(format: "account == %@ AND fileID IN %@", appDelegate.activeAccount, fileIDs), sorted: datasourceSorted, ascending: datasourceAscending) {
  373. sectionDatasource = CCSectionMetadata.creataDataSourseSectionMetadata(metadatas, listProgressMetadata: nil, groupByField: datasourceGroupBy, filterFileID: nil, filterTypeFileImage: false, filterTypeFileVideo: false, activeAccount: appDelegate.activeAccount)
  374. }
  375. } else {
  376. if let metadatas = NCManageDatabase.sharedInstance.getMetadatas(predicate: NSPredicate(format: "account == %@ AND directoryID == %@", appDelegate.activeAccount, directoryID), sorted: datasourceSorted, ascending: datasourceAscending) {
  377. sectionDatasource = CCSectionMetadata.creataDataSourseSectionMetadata(metadatas, listProgressMetadata: nil, groupByField: datasourceGroupBy, filterFileID: nil, filterTypeFileImage: false, filterTypeFileVideo: false, activeAccount: appDelegate.activeAccount)
  378. }
  379. }
  380. self.refreshControl.endRefreshing()
  381. collectionView.reloadData()
  382. }
  383. // MARK: COLLECTIONVIEW METHODS
  384. func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
  385. if (indexPath.section == 0) {
  386. if kind == UICollectionView.elementKindSectionHeader {
  387. let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "sectionHeaderMenu", for: indexPath) as! NCSectionHeaderMenu
  388. if collectionView.collectionViewLayout == gridLayout {
  389. header.buttonSwitch.setImage(CCGraphics.changeThemingColorImage(UIImage.init(named: "switchList"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), for: .normal)
  390. } else {
  391. header.buttonSwitch.setImage(CCGraphics.changeThemingColorImage(UIImage.init(named: "switchGrid"), multiplier: 2, color: NCBrandColor.sharedInstance.icon), for: .normal)
  392. }
  393. header.delegate = self
  394. header.setStatusButton(count: sectionDatasource.allFileID.count)
  395. header.setTitleOrder(datasourceSorted: datasourceSorted, datasourceAscending: datasourceAscending)
  396. if datasourceGroupBy == "none" {
  397. header.labelSection.isHidden = true
  398. header.labelSectionHeightConstraint.constant = 0
  399. } else {
  400. header.labelSection.isHidden = false
  401. header.setTitleLabel(sectionDatasource: sectionDatasource, section: indexPath.section)
  402. header.labelSectionHeightConstraint.constant = sectionHeaderHeight
  403. }
  404. return header
  405. } else {
  406. let footer = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "sectionFooter", for: indexPath) as! NCSectionFooter
  407. footer.setTitleLabel(sectionDatasource: sectionDatasource)
  408. return footer
  409. }
  410. } else {
  411. if kind == UICollectionView.elementKindSectionHeader {
  412. let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "sectionHeader", for: indexPath) as! NCSectionHeader
  413. header.setTitleLabel(sectionDatasource: sectionDatasource, section: indexPath.section)
  414. return header
  415. } else {
  416. let footer = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "sectionFooter", for: indexPath) as! NCSectionFooter
  417. footer.setTitleLabel(sectionDatasource: sectionDatasource)
  418. return footer
  419. }
  420. }
  421. }
  422. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
  423. if section == 0 {
  424. if datasourceGroupBy == "none" {
  425. return CGSize(width: collectionView.frame.width, height: headerMenuHeight)
  426. } else {
  427. return CGSize(width: collectionView.frame.width, height: headerMenuHeight + sectionHeaderHeight)
  428. }
  429. } else {
  430. return CGSize(width: collectionView.frame.width, height: sectionHeaderHeight)
  431. }
  432. }
  433. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
  434. let sections = sectionDatasource.sectionArrayRow.allKeys.count
  435. if (section == sections - 1) {
  436. return CGSize(width: collectionView.frame.width, height: footerHeight)
  437. } else {
  438. return CGSize(width: collectionView.frame.width, height: 0)
  439. }
  440. }
  441. func numberOfSections(in collectionView: UICollectionView) -> Int {
  442. let sections = sectionDatasource.sectionArrayRow.allKeys.count
  443. return sections
  444. }
  445. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  446. let key = sectionDatasource.sections.object(at: section)
  447. let datasource = sectionDatasource.sectionArrayRow.object(forKey: key) as! [tableMetadata]
  448. return datasource.count
  449. }
  450. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  451. guard let metadata = NCMainCommon.sharedInstance.getMetadataFromSectionDataSourceIndexPath(indexPath, sectionDataSource: sectionDatasource) else {
  452. return collectionView.dequeueReusableCell(withReuseIdentifier: "listCell", for: indexPath) as! NCListCell
  453. }
  454. guard let serverUrl = NCManageDatabase.sharedInstance.getServerUrl(metadata.directoryID) else {
  455. return collectionView.dequeueReusableCell(withReuseIdentifier: "listCell", for: indexPath) as! NCListCell
  456. }
  457. let cell = NCMainCommon.sharedInstance.collectionViewCellForItemAt(indexPath, collectionView: collectionView, typeLayout: typeLayout, metadata: metadata, metadataFolder: nil, serverUrl: serverUrl, isEditMode: isEditMode, selectFileID: selectFileID, autoUploadFileName: autoUploadFileName, autoUploadDirectory: autoUploadDirectory, hideButtonMore: false, source: self)
  458. return cell
  459. }
  460. func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  461. guard let metadata = NCMainCommon.sharedInstance.getMetadataFromSectionDataSourceIndexPath(indexPath, sectionDataSource: sectionDatasource) else {
  462. return
  463. }
  464. metadataPush = metadata
  465. if isEditMode {
  466. if let index = selectFileID.index(of: metadata.fileID) {
  467. selectFileID.remove(at: index)
  468. } else {
  469. selectFileID.append(metadata.fileID)
  470. }
  471. collectionView.reloadItems(at: [indexPath])
  472. return
  473. }
  474. if metadata.directory {
  475. performSegueDirectoryWithControlPasscode(controlPasscode: true)
  476. } else {
  477. performSegue(withIdentifier: "segueDetail", sender: self)
  478. }
  479. }
  480. // MARK: SEGUE
  481. override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  482. let photoDataSource: NSMutableArray = []
  483. for fileID: String in sectionDatasource.allFileID as! [String] {
  484. let metadata = sectionDatasource.allRecordsDataSource.object(forKey: fileID) as! tableMetadata
  485. if metadata.typeFile == k_metadataTypeFile_image {
  486. photoDataSource.add(metadata)
  487. }
  488. }
  489. if let segueNavigationController = segue.destination as? UINavigationController {
  490. if let segueViewController = segueNavigationController.topViewController as? CCDetail {
  491. segueViewController.metadataDetail = metadataPush
  492. segueViewController.dateFilterQuery = nil
  493. segueViewController.photoDataSource = photoDataSource
  494. segueViewController.title = metadataPush!.fileNameView
  495. }
  496. }
  497. }
  498. // MARK: NAVIGATION
  499. private func performSegueDirectoryWithControlPasscode(controlPasscode: Bool) {
  500. guard let serverUrl = NCManageDatabase.sharedInstance.getServerUrl(metadataPush!.directoryID) else {
  501. return
  502. }
  503. let serverUrlPush = CCUtility.stringAppendServerUrl(serverUrl, addFileName: metadataPush!.fileName)
  504. guard let directoryIDPush = NCManageDatabase.sharedInstance.getDirectoryID(serverUrlPush) else {
  505. return
  506. }
  507. guard let directoryPush = NCManageDatabase.sharedInstance.getTableDirectory(predicate: NSPredicate(format: "directoryID == %@", directoryIDPush)) else {
  508. return
  509. }
  510. if directoryPush.lock == true && CCUtility.getBlockCode() != nil && (CCUtility.getBlockCode()?.count)! > 0 && controlPasscode {
  511. let viewController = CCBKPasscode.init(nibName: nil, bundle: nil)
  512. guard let touchIDManager = BKTouchIDManager.init(keychainServiceName: k_serviceShareKeyChain) else {
  513. return
  514. }
  515. touchIDManager.promptText = NSLocalizedString("_scan_fingerprint_", comment: "")
  516. viewController.delegate = self
  517. viewController.type = BKPasscodeViewControllerCheckPasscodeType
  518. viewController.inputViewTitlePassword = true
  519. if CCUtility.getSimplyBlockCode() {
  520. viewController.passcodeStyle = BKPasscodeInputViewNumericPasscodeStyle
  521. viewController.passcodeInputView.maximumLength = 6
  522. } else {
  523. viewController.passcodeStyle = BKPasscodeInputViewNormalPasscodeStyle
  524. viewController.passcodeInputView.maximumLength = 64
  525. }
  526. viewController.touchIDManager = touchIDManager
  527. viewController.title = NSLocalizedString("_folder_blocked_", comment: "")
  528. viewController.navigationItem.leftBarButtonItem = UIBarButtonItem.init(barButtonSystemItem: UIBarButtonItem.SystemItem.cancel, target: self, action: #selector(passcodeViewCloseButtonPressed(_:)))
  529. viewController.navigationItem.leftBarButtonItem?.tintColor = UIColor.black
  530. let navigationController = UINavigationController.init(rootViewController: viewController)
  531. self.present(navigationController, animated: true, completion: nil)
  532. return
  533. }
  534. let ncOffline:NCOffline = UIStoryboard(name: "NCOffline", bundle: nil).instantiateInitialViewController() as! NCOffline
  535. ncOffline.directoryID = directoryIDPush
  536. ncOffline.titleCurrentFolder = metadataPush!.fileNameView
  537. self.navigationController?.pushViewController(ncOffline, animated: true)
  538. }
  539. }