NCMainCommon.swift 31 KB

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