NCMainCommon.swift 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666
  1. //
  2. // NCMainCommon.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 18/07/18.
  6. // Copyright © 2018 TWS. 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 NCMainCommon: NSObject {
  25. @objc static let sharedInstance: NCMainCommon = {
  26. let instance = NCMainCommon()
  27. return instance
  28. }()
  29. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  30. //MARK: -
  31. @objc func triggerProgressTask(_ notification: Notification, sectionDataSourceFileIDIndexPath: NSDictionary, tableView: UITableView) {
  32. guard let dic = notification.userInfo else {
  33. return
  34. }
  35. let fileID = dic["fileID"] as! NSString
  36. _ = dic["serverUrl"] as! NSString
  37. let status = dic["status"] as! Int
  38. let progress = dic["progress"] as! CGFloat
  39. let totalBytes = dic["totalBytes"] as! Double
  40. let totalBytesExpected = dic["totalBytesExpected"] as! Double
  41. appDelegate.listProgressMetadata.setObject([progress as NSNumber, totalBytes as NSNumber, totalBytesExpected as NSNumber], forKey: fileID)
  42. guard let indexPath = sectionDataSourceFileIDIndexPath.object(forKey: fileID) else {
  43. return
  44. }
  45. if isValidIndexPath(indexPath as! IndexPath, tableView: tableView) {
  46. if let cell = tableView.cellForRow(at: indexPath as! IndexPath) as? CCCellMainTransfer {
  47. var image = ""
  48. if status == k_metadataStatusInDownload {
  49. image = "↓"
  50. } else if status == k_metadataStatusInUpload {
  51. image = "↑"
  52. }
  53. cell.labelInfoFile.text = CCUtility.transformedSize(totalBytesExpected) + " - " + image + CCUtility.transformedSize(totalBytes)
  54. cell.transferButton.progress = progress
  55. }
  56. }
  57. }
  58. @objc func cancelTransferMetadata(_ metadata: tableMetadata, reloadDatasource: Bool) {
  59. if metadata.session.count == 0 {
  60. return
  61. }
  62. guard let session = CCNetworking.shared().getSessionfromSessionDescription(metadata.session) else {
  63. return
  64. }
  65. // SESSION EXTENSION
  66. if metadata.session == k_download_session_extension || metadata.session == k_upload_session_extension {
  67. if (metadata.session == k_upload_session_extension) {
  68. do {
  69. try FileManager.default.removeItem(atPath: CCUtility.getDirectoryProviderStorageFileID(metadata.fileID))
  70. } catch { }
  71. NCManageDatabase.sharedInstance.deleteMetadata(predicate: NSPredicate(format: "fileID == %@", metadata.fileID), clearDateReadDirectoryID: metadata.directoryID)
  72. } else {
  73. NCManageDatabase.sharedInstance.setMetadataSession("", sessionError: "", sessionSelector: "", sessionTaskIdentifier: Int(k_taskIdentifierDone), status: Int(k_metadataStatusNormal), predicate: NSPredicate(format: "fileID == %@", metadata.fileID))
  74. }
  75. self.reloadDatasource(ServerUrl: nil)
  76. return
  77. }
  78. session.getTasksWithCompletionHandler { (dataTasks, uploadTasks, downloadTasks) in
  79. var cancel = false
  80. // DOWNLOAD
  81. if metadata.session.count > 0 && metadata.session.contains("download") {
  82. for task in downloadTasks {
  83. if task.taskIdentifier == metadata.sessionTaskIdentifier {
  84. task.cancel()
  85. cancel = true
  86. }
  87. }
  88. if cancel == false {
  89. NCManageDatabase.sharedInstance.setMetadataSession("", sessionError: "", sessionSelector: "", sessionTaskIdentifier: Int(k_taskIdentifierDone), status: Int(k_metadataStatusNormal), predicate: NSPredicate(format: "fileID == %@", metadata.fileID))
  90. }
  91. }
  92. // UPLOAD
  93. if metadata.session.count > 0 && metadata.session.contains("upload") {
  94. for task in uploadTasks {
  95. if task.taskIdentifier == metadata.sessionTaskIdentifier {
  96. task.cancel()
  97. cancel = true
  98. }
  99. }
  100. if cancel == false {
  101. do {
  102. try FileManager.default.removeItem(atPath: CCUtility.getDirectoryProviderStorageFileID(metadata.fileID))
  103. }
  104. catch { }
  105. NCManageDatabase.sharedInstance.deleteMetadata(predicate: NSPredicate(format: "fileID == %@", metadata.fileID), clearDateReadDirectoryID: metadata.directoryID)
  106. }
  107. }
  108. if cancel == false {
  109. self.reloadDatasource(ServerUrl: nil)
  110. }
  111. }
  112. }
  113. @objc func cancelAllTransfer() {
  114. // Delete k_metadataStatusWaitUpload OR k_metadataStatusUploadError
  115. NCManageDatabase.sharedInstance.deleteMetadata(predicate: NSPredicate(format: "account == %@ AND (status == %d OR status == %d)", appDelegate.activeAccount, k_metadataStatusWaitUpload, k_metadataStatusUploadError), clearDateReadDirectoryID: nil)
  116. if let metadatas = NCManageDatabase.sharedInstance.getMetadatas(predicate: NSPredicate(format: "account == %@ AND status != %d AND status != %d", appDelegate.activeAccount, k_metadataStatusNormal, k_metadataStatusHide), sorted: "fileName", ascending: true) {
  117. for metadata in metadatas {
  118. // Modify
  119. if (metadata.status == k_metadataStatusWaitDownload || metadata.status == k_metadataStatusDownloadError) {
  120. metadata.session = ""
  121. metadata.sessionSelector = ""
  122. metadata.status = Int(k_metadataStatusNormal)
  123. _ = NCManageDatabase.sharedInstance.addMetadata(metadata)
  124. }
  125. // Cancel Task
  126. if metadata.status == k_metadataStatusDownloading || metadata.status == k_metadataStatusUploading {
  127. cancelTransferMetadata(metadata, reloadDatasource: false)
  128. }
  129. }
  130. }
  131. self.reloadDatasource(ServerUrl: nil)
  132. }
  133. //MARK: -
  134. @objc func cellForRowAtIndexPath(_ indexPath: IndexPath, tableView: UITableView ,metadata: tableMetadata, metadataFolder: tableMetadata?, serverUrl: String, autoUploadFileName: String, autoUploadDirectory: String) -> UITableViewCell {
  135. // Create File System
  136. if metadata.directory {
  137. CCUtility.getDirectoryProviderStorageFileID(metadata.fileID)
  138. } else {
  139. CCUtility.getDirectoryProviderStorageFileID(metadata.fileID, fileName: metadata.fileNameView)
  140. }
  141. // CCCell
  142. if metadata.status == k_metadataStatusNormal {
  143. // NORMAL
  144. let cell = tableView.dequeueReusableCell(withIdentifier: "CellMain", for: indexPath) as! CCCellMain
  145. cell.separatorInset = UIEdgeInsetsMake(0, 60, 0, 0)
  146. cell.accessoryType = UITableViewCellAccessoryType.none
  147. cell.file.image = nil
  148. cell.status.image = nil
  149. cell.favorite.image = nil
  150. cell.shared.image = nil
  151. cell.local.image = nil
  152. cell.imageTitleSegue = nil
  153. cell.shared.isUserInteractionEnabled = false
  154. cell.backgroundColor = NCBrandColor.sharedInstance.backgroundView
  155. // change color selection
  156. let selectionColor = UIView()
  157. selectionColor.backgroundColor = NCBrandColor.sharedInstance.getColorSelectBackgrond()
  158. cell.selectedBackgroundView = selectionColor
  159. cell.tintColor = NCBrandColor.sharedInstance.brandElement
  160. cell.labelTitle.textColor = UIColor.black
  161. cell.labelTitle.text = metadata.fileNameView
  162. // Share
  163. let sharesLink = appDelegate.sharesLink.object(forKey: serverUrl + metadata.fileName)
  164. let sharesUserAndGroup = appDelegate.sharesUserAndGroup.object(forKey: serverUrl + metadata.fileName)
  165. var isShare = false
  166. var isMounted = false
  167. if metadataFolder != nil {
  168. isShare = metadata.permissions.contains(k_permission_shared) && !metadataFolder!.permissions.contains(k_permission_shared)
  169. isMounted = metadata.permissions.contains(k_permission_mounted) && !metadataFolder!.permissions.contains(k_permission_mounted)
  170. }
  171. if metadata.directory {
  172. // lable Info
  173. cell.labelInfoFile.text = CCUtility.dateDiff(metadata.date as Date)
  174. // File Image & Image Title Segue
  175. if metadata.e2eEncrypted {
  176. cell.file.image = CCGraphics.changeThemingColorImage(UIImage.init(named: "folderEncrypted"), multiplier: 3, color: NCBrandColor.sharedInstance.brandElement)
  177. cell.imageTitleSegue = UIImage.init(named: "lock")
  178. } else if metadata.fileName == autoUploadFileName && serverUrl == autoUploadDirectory {
  179. cell.file.image = CCGraphics.changeThemingColorImage(UIImage.init(named: "folderPhotos"), multiplier: 3, color: NCBrandColor.sharedInstance.brandElement)
  180. cell.imageTitleSegue = UIImage.init(named: "photos")
  181. } else if isShare {
  182. cell.file.image = CCGraphics.changeThemingColorImage(UIImage.init(named: "folder_shared_with_me"), multiplier: 3, color: NCBrandColor.sharedInstance.brandElement)
  183. cell.imageTitleSegue = UIImage.init(named: "share")
  184. } else if isMounted {
  185. cell.file.image = CCGraphics.changeThemingColorImage(UIImage.init(named: "folder_external"), multiplier: 3, color: NCBrandColor.sharedInstance.brandElement)
  186. cell.imageTitleSegue = UIImage.init(named: "shareMounted")
  187. } else if (sharesUserAndGroup != nil) {
  188. cell.file.image = CCGraphics.changeThemingColorImage(UIImage.init(named: "folder_shared_with_me"), multiplier: 3, color: NCBrandColor.sharedInstance.brandElement)
  189. cell.imageTitleSegue = UIImage.init(named: "share")
  190. } else if (sharesLink != nil) {
  191. cell.file.image = CCGraphics.changeThemingColorImage(UIImage.init(named: "folder_public"), multiplier: 3, color: NCBrandColor.sharedInstance.brandElement)
  192. cell.imageTitleSegue = UIImage.init(named: "sharebylink")
  193. } else {
  194. cell.file.image = CCGraphics.changeThemingColorImage(UIImage.init(named: "folder"), multiplier: 3, color: NCBrandColor.sharedInstance.brandElement)
  195. }
  196. // Image Status Lock Passcode
  197. let lockServerUrl = CCUtility.stringAppendServerUrl(serverUrl, addFileName: metadata.fileName)!
  198. let tableDirectory = NCManageDatabase.sharedInstance.getTableDirectory(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@", appDelegate.activeAccount, lockServerUrl))
  199. if tableDirectory != nil && tableDirectory!.lock && CCUtility.getBlockCode() != nil {
  200. cell.status.image = UIImage.init(named: "passcode")
  201. }
  202. } else {
  203. let iconFileExists = FileManager.default.fileExists(atPath: CCUtility.getDirectoryProviderStorageIconFileID(metadata.fileID, fileNameView: metadata.fileNameView))
  204. // Lable Info
  205. cell.labelInfoFile.text = CCUtility.dateDiff(metadata.date as Date) + " " + CCUtility.transformedSize(metadata.size)
  206. // File Image
  207. if iconFileExists {
  208. cell.file.image = UIImage.init(contentsOfFile: CCUtility.getDirectoryProviderStorageIconFileID(metadata.fileID, fileNameView: metadata.fileNameView))
  209. } else {
  210. if metadata.iconName.count > 0 {
  211. cell.file.image = UIImage.init(named: metadata.iconName)
  212. } else {
  213. cell.file.image = UIImage.init(named: "file")
  214. }
  215. }
  216. // Local Image
  217. let tableLocalFile = NCManageDatabase.sharedInstance.getTableLocalFile(predicate: NSPredicate(format: "fileID == %@", metadata.fileID))
  218. if tableLocalFile != nil && CCUtility.fileProviderStorageExists(metadata.fileID, fileName: metadata.fileNameView) {
  219. cell.local.image = UIImage.init(named: "local")
  220. }
  221. // Status Image
  222. let tableE2eEncryption = NCManageDatabase.sharedInstance.getE2eEncryption(predicate: NSPredicate(format: "account == %@ AND fileNameIdentifier == %@", appDelegate.activeAccount, metadata.fileName))
  223. if tableE2eEncryption != nil && NCUtility.sharedInstance.isEncryptedMetadata(metadata) {
  224. cell.status.image = UIImage.init(named: "encrypted")
  225. }
  226. // Share
  227. if (isShare) {
  228. cell.shared.image = CCGraphics.changeThemingColorImage(UIImage.init(named: "share"), multiplier: 2, color: NCBrandColor.sharedInstance.icon)
  229. } else if (isMounted) {
  230. cell.shared.image = CCGraphics.changeThemingColorImage(UIImage.init(named: "shareMounted"), multiplier: 2, color: NCBrandColor.sharedInstance.icon)
  231. } else if (sharesLink != nil) {
  232. cell.shared.image = CCGraphics.changeThemingColorImage(UIImage.init(named: "sharebylink"), multiplier: 2, color: NCBrandColor.sharedInstance.icon)
  233. } else if (sharesUserAndGroup != nil) {
  234. cell.shared.image = CCGraphics.changeThemingColorImage(UIImage.init(named: "share"), multiplier: 2, color: NCBrandColor.sharedInstance.icon)
  235. }
  236. }
  237. //
  238. // File & Directory
  239. //
  240. // Favorite
  241. if metadata.favorite {
  242. cell.favorite.image = CCGraphics.changeThemingColorImage(UIImage.init(named: "favorite"), multiplier: 2, color: NCBrandColor.sharedInstance.yellowFavorite)
  243. }
  244. // More Image
  245. cell.more.image = CCGraphics.changeThemingColorImage(UIImage.init(named: "more"), multiplier: 2, color: NCBrandColor.sharedInstance.icon)
  246. return cell
  247. } else {
  248. // TRASNFER
  249. let cell = tableView.dequeueReusableCell(withIdentifier: "CellMainTransfer", for: indexPath) as! CCCellMainTransfer
  250. cell.separatorInset = UIEdgeInsetsMake(0, 60, 0, 0)
  251. cell.accessoryType = UITableViewCellAccessoryType.none
  252. cell.file.image = nil
  253. cell.status.image = nil
  254. cell.backgroundColor = NCBrandColor.sharedInstance.backgroundView
  255. cell.labelTitle.textColor = UIColor.black
  256. cell.labelTitle.text = metadata.fileNameView
  257. cell.transferButton.tintColor = NCBrandColor.sharedInstance.icon
  258. var progress: CGFloat = 0.0
  259. var totalBytes: Double = 0.0
  260. //var totalBytesExpected : Double = 0
  261. let progressArray = appDelegate.listProgressMetadata.object(forKey: metadata.fileID) as? NSArray
  262. if progressArray != nil && progressArray?.count == 3 {
  263. progress = progressArray?.object(at: 0) as! CGFloat
  264. totalBytes = progressArray?.object(at: 1) as! Double
  265. //totalBytesExpected = progressArray?.object(at: 2) as! Double
  266. }
  267. // Write status on Label Info
  268. switch metadata.status {
  269. case Int(k_metadataStatusWaitDownload):
  270. cell.labelInfoFile.text = CCUtility.transformedSize(metadata.size) + " - " + NSLocalizedString("_status_wait_download_", comment: "")
  271. progress = 0.0
  272. break
  273. case Int(k_metadataStatusInDownload):
  274. cell.labelInfoFile.text = CCUtility.transformedSize(metadata.size) + " - " + NSLocalizedString("_status_in_download_", comment: "")
  275. progress = 0.0
  276. break
  277. case Int(k_metadataStatusDownloading):
  278. cell.labelInfoFile.text = CCUtility.transformedSize(metadata.size) + " - ↓" + CCUtility.transformedSize(totalBytes)
  279. break
  280. case Int(k_metadataStatusWaitUpload):
  281. cell.labelInfoFile.text = CCUtility.transformedSize(metadata.size) + " - " + NSLocalizedString("_status_wait_upload_", comment: "")
  282. progress = 0.0
  283. break
  284. case Int(k_metadataStatusInUpload):
  285. cell.labelInfoFile.text = CCUtility.transformedSize(metadata.size) + " - " + NSLocalizedString("_status_in_upload_", comment: "")
  286. progress = 0.0
  287. break
  288. case Int(k_metadataStatusUploading):
  289. cell.labelInfoFile.text = CCUtility.transformedSize(metadata.size) + " - ↑" + CCUtility.transformedSize(totalBytes)
  290. break
  291. default:
  292. cell.labelInfoFile.text = CCUtility.transformedSize(metadata.size)
  293. progress = 0.0
  294. }
  295. let iconFileExists = FileManager.default.fileExists(atPath: CCUtility.getDirectoryProviderStorageIconFileID(metadata.fileID, fileNameView: metadata.fileNameView))
  296. if iconFileExists {
  297. cell.file.image = UIImage.init(contentsOfFile: CCUtility.getDirectoryProviderStorageIconFileID(metadata.fileID, fileNameView: metadata.fileNameView))
  298. } else {
  299. if metadata.iconName.count > 0 {
  300. cell.file.image = UIImage.init(named: metadata.iconName)
  301. } else {
  302. cell.file.image = UIImage.init(named: "file")
  303. }
  304. }
  305. // Session Upload Extension
  306. if metadata.session == k_upload_session_extension && (metadata.status == k_metadataStatusInUpload || metadata.status == k_metadataStatusUploading) {
  307. cell.labelTitle.isEnabled = false
  308. cell.labelInfoFile.isEnabled = false
  309. } else {
  310. cell.labelTitle.isEnabled = true
  311. cell.labelInfoFile.isEnabled = true
  312. }
  313. // downloadFile
  314. if metadata.status == k_metadataStatusWaitDownload || metadata.status == k_metadataStatusInDownload || metadata.status == k_metadataStatusDownloading || metadata.status == k_metadataStatusDownloadError {
  315. //
  316. }
  317. // downloadFile Error
  318. if metadata.status == k_metadataStatusDownloadError {
  319. cell.status.image = UIImage.init(named: "statuserror")
  320. if metadata.sessionError.count == 0 {
  321. cell.labelInfoFile.text = NSLocalizedString("_error_", comment: "") + ", " + NSLocalizedString("_file_not_downloaded_", comment: "")
  322. } else {
  323. cell.labelInfoFile.text = metadata.sessionError
  324. }
  325. }
  326. // uploadFile
  327. if metadata.status == k_metadataStatusWaitUpload || metadata.status == k_metadataStatusInUpload || metadata.status == k_metadataStatusUploading || metadata.status == k_metadataStatusUploadError {
  328. if (!iconFileExists) {
  329. cell.file.image = CCGraphics.changeThemingColorImage(UIImage.init(named: "uploadCloud"), multiplier: 2, color: NCBrandColor.sharedInstance.brandElement)
  330. }
  331. cell.labelTitle.isEnabled = false
  332. }
  333. // uploadFileError
  334. if metadata.status == k_metadataStatusUploadError {
  335. cell.labelTitle.isEnabled = false
  336. cell.status.image = UIImage.init(named: "statuserror")
  337. if !iconFileExists {
  338. cell.file.image = CCGraphics.changeThemingColorImage(UIImage.init(named: "uploadCloud"), multiplier: 2, color: NCBrandColor.sharedInstance.brandElement)
  339. }
  340. if metadata.sessionError.count == 0 {
  341. cell.labelInfoFile.text = NSLocalizedString("_error_", comment: "") + ", " + NSLocalizedString("_file_not_uploaded_", comment: "")
  342. } else {
  343. cell.labelInfoFile.text = metadata.sessionError
  344. }
  345. }
  346. // Progress
  347. cell.transferButton.progress = progress
  348. return cell
  349. }
  350. }
  351. @objc func getMetadataFromSectionDataSourceIndexPath(_ indexPath: IndexPath, sectionDataSource: CCSectionDataSourceMetadata) -> tableMetadata? {
  352. let section = indexPath.section + 1
  353. let row = indexPath.row + 1
  354. let totSections = sectionDataSource.sections.count
  355. if totSections < section || section > totSections {
  356. return nil
  357. }
  358. let valueSection = sectionDataSource.sections.object(at: indexPath.section)
  359. guard let filesID = sectionDataSource.sectionArrayRow.object(forKey: valueSection) as? NSArray else {
  360. return nil
  361. }
  362. let totRows = filesID.count
  363. if totRows < row || row > totRows {
  364. return nil
  365. }
  366. let fileID = filesID.object(at: indexPath.row)
  367. let metadata = sectionDataSource.allRecordsDataSource.object(forKey: fileID) as? tableMetadata
  368. return metadata
  369. }
  370. @objc func reloadDatasource(ServerUrl: String?) {
  371. DispatchQueue.main.async {
  372. if self.appDelegate.activeMain != nil {
  373. if ServerUrl == nil {
  374. self.appDelegate.activeMain.reloadDatasource()
  375. } else {
  376. self.appDelegate.activeMain.reloadDatasource(ServerUrl)
  377. }
  378. }
  379. if self.appDelegate.activeFavorites != nil {
  380. self.appDelegate.activeFavorites.reloadDatasource()
  381. }
  382. if self.appDelegate.activeTransfers != nil {
  383. self.appDelegate.activeTransfers.reloadDatasource()
  384. }
  385. }
  386. }
  387. @objc func isValidIndexPath(_ indexPath: IndexPath, tableView: UITableView) -> Bool {
  388. return indexPath.section < tableView.numberOfSections && indexPath.row < tableView.numberOfRows(inSection: indexPath.section)
  389. }
  390. //MARK: -
  391. @objc func deleteFile(metadatas: NSArray, e2ee: Bool, serverUrl: String, folderFileID: String, completion: @escaping (_ errorCode: Int, _ message: String)->()) {
  392. var copyMetadatas = [tableMetadata]()
  393. for metadata in metadatas {
  394. copyMetadatas.append(tableMetadata.init(value: metadata))
  395. }
  396. if e2ee {
  397. DispatchQueue.global().async {
  398. let error = NCNetworkingEndToEnd.sharedManager().lockFolderEncrypted(onServerUrl: serverUrl, fileID: folderFileID, user: self.appDelegate.activeUser, userID: self.appDelegate.activeUserID, password: self.appDelegate.activePassword, url: self.appDelegate.activeUrl)
  399. DispatchQueue.main.async {
  400. if error == nil {
  401. self.delete(metadatas: copyMetadatas, serverUrl:serverUrl, e2ee: e2ee, completion: completion)
  402. } else {
  403. self.appDelegate.messageNotification("_delete_", description: error?.localizedDescription, visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: Int(k_CCErrorInternalError))
  404. return
  405. }
  406. }
  407. }
  408. } else {
  409. delete(metadatas: copyMetadatas, serverUrl:serverUrl, e2ee: e2ee, completion: completion)
  410. }
  411. }
  412. private func delete(metadatas: [tableMetadata], serverUrl: String, e2ee: Bool, completion: @escaping (_ errorCode: Int, _ message: String)->()) {
  413. var count: Int = 0
  414. var completionErrorCode: Int = 0
  415. var completionMessage = ""
  416. let ocNetworking = OCnetworking.init(delegate: nil, metadataNet: nil, withUser: appDelegate.activeUser, withUserID: appDelegate.activeUserID, withPassword: appDelegate.activePassword, withUrl: appDelegate.activeUrl)
  417. for metadata in metadatas {
  418. guard let serverUrl = NCManageDatabase.sharedInstance.getServerUrl(metadata.directoryID) else {
  419. continue
  420. }
  421. self.appDelegate.filterFileID.add(metadata.fileID)
  422. ocNetworking?.deleteFileOrFolder(metadata.fileName, serverUrl: serverUrl, completion: { (message, errorCode) in
  423. count += 1
  424. if errorCode == 0 || errorCode == 404 {
  425. do {
  426. try FileManager.default.removeItem(atPath: CCUtility.getDirectoryProviderStorageFileID(metadata.fileID))
  427. } catch { }
  428. NCManageDatabase.sharedInstance.deleteMetadata(predicate: NSPredicate(format: "fileID == %@", metadata.fileID), clearDateReadDirectoryID: metadata.directoryID)
  429. NCManageDatabase.sharedInstance.deleteLocalFile(predicate: NSPredicate(format: "fileID == %@", metadata.fileID))
  430. NCManageDatabase.sharedInstance.deletePhotos(fileID: metadata.fileID)
  431. if metadata.directory {
  432. NCManageDatabase.sharedInstance.deleteDirectoryAndSubDirectory(serverUrl: CCUtility.stringAppendServerUrl(serverUrl, addFileName: metadata.fileName))
  433. }
  434. if (e2ee) {
  435. NCManageDatabase.sharedInstance.deleteE2eEncryption(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@ AND fileNameIdentifier == %@", metadata.account, serverUrl, metadata.fileName))
  436. }
  437. } else {
  438. self.appDelegate.filterFileID.remove(metadata.fileID)
  439. completionErrorCode = errorCode
  440. completionMessage = message!
  441. }
  442. if count == metadatas.count {
  443. if e2ee {
  444. DispatchQueue.global().async {
  445. NCNetworkingEndToEnd.sharedManager().rebuildAndSendMetadata(onServerUrl: serverUrl, account: self.appDelegate.activeAccount, user: self.appDelegate.activeUser, userID: self.appDelegate.activeUserID, password: self.appDelegate.activePassword, url: self.appDelegate.activeUrl)
  446. DispatchQueue.main.async {
  447. completion(completionErrorCode, completionMessage)
  448. }
  449. }
  450. } else {
  451. completion(completionErrorCode, completionMessage)
  452. }
  453. }
  454. })
  455. }
  456. // reload for filesID
  457. self.appDelegate.activeMain.reloadDatasource()
  458. self.appDelegate.activePhotos.reloadDatasource()
  459. }
  460. }
  461. //MARK: -
  462. class CCMainTabBarController : UITabBarController, UITabBarControllerDelegate {
  463. override func viewDidLoad() {
  464. super.viewDidLoad()
  465. delegate = self
  466. }
  467. //Delegate methods
  468. func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
  469. let tabViewControllers = tabBarController.viewControllers!
  470. guard let toIndex = tabViewControllers.index(of: viewController) else {
  471. if let vc = viewController as? UINavigationController {
  472. vc.popToRootViewController(animated: true);
  473. }
  474. return false
  475. }
  476. animateToTab(toIndex: toIndex)
  477. return true
  478. }
  479. func animateToTab(toIndex: Int) {
  480. let tabViewControllers = viewControllers!
  481. let fromView = selectedViewController!.view!
  482. let toView = tabViewControllers[toIndex].view!
  483. let fromIndex = tabViewControllers.index(of: selectedViewController!)
  484. guard fromIndex != toIndex else {return}
  485. // Add the toView to the tab bar view
  486. fromView.superview?.addSubview(toView)
  487. fromView.superview?.backgroundColor = UIColor.white
  488. // Position toView off screen (to the left/right of fromView)
  489. let screenWidth = UIScreen.main.bounds.size.width;
  490. let scrollRight = toIndex > fromIndex!;
  491. let offset = (scrollRight ? screenWidth : -screenWidth)
  492. toView.center = CGPoint(x: (fromView.center.x) + offset, y: (toView.center.y))
  493. // Disable interaction during animation
  494. view.isUserInteractionEnabled = false
  495. UIView.animate(withDuration: 0.3, delay: 0.0, usingSpringWithDamping: 1, initialSpringVelocity: 0, options: UIViewAnimationOptions.curveEaseOut, animations: {
  496. // Slide the views by -offset
  497. fromView.center = CGPoint(x: fromView.center.x - offset, y: fromView.center.y);
  498. toView.center = CGPoint(x: toView.center.x - offset, y: toView.center.y);
  499. }, completion: { finished in
  500. // Remove the old view from the tabbar view.
  501. fromView.removeFromSuperview()
  502. self.selectedIndex = toIndex
  503. self.view.isUserInteractionEnabled = true
  504. })
  505. }
  506. }
  507. //
  508. // https://stackoverflow.com/questions/44822558/ios-11-uitabbar-uitabbaritem-positioning-issue/46348796#46348796
  509. //
  510. extension UITabBar {
  511. // Workaround for iOS 11's new UITabBar behavior where on iPad, the UITabBar inside
  512. // the Master view controller shows the UITabBarItem icon next to the text
  513. override open var traitCollection: UITraitCollection {
  514. if UIDevice.current.userInterfaceIdiom == .pad {
  515. return UITraitCollection(horizontalSizeClass: .compact)
  516. }
  517. return super.traitCollection
  518. }
  519. }