NCMainCommon.swift 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  1. //
  2. // NCMainCommon.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 18/07/18.
  6. // Copyright © 2018 Marino Faggiana. All rights reserved.
  7. //
  8. // Author Marino Faggiana <marino.faggiana@nextcloud.com>
  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. import TLPhotoPicker
  25. import ZIPFoundation
  26. import NCCommunication
  27. //MARK: - Main Common
  28. class NCMainCommon: NSObject, NCAudioRecorderViewControllerDelegate, UIDocumentInteractionControllerDelegate {
  29. @objc static let shared: NCMainCommon = {
  30. let instance = NCMainCommon()
  31. instance.createImagesThemingColor()
  32. return instance
  33. }()
  34. var metadataEditPhoto: tableMetadata?
  35. var docController: UIDocumentInteractionController?
  36. //MARK: -
  37. struct NCMainCommonImages {
  38. static var cellSharedImage = UIImage()
  39. static var cellCanShareImage = UIImage()
  40. static var cellShareByLinkImage = UIImage()
  41. static var cellFavouriteImage = UIImage()
  42. static var cellMoreImage = UIImage()
  43. static var cellCommentImage = UIImage()
  44. static var cellLivePhotoImage = UIImage()
  45. static var cellFolderEncryptedImage = UIImage()
  46. static var cellFolderSharedWithMeImage = UIImage()
  47. static var cellFolderPublicImage = UIImage()
  48. static var cellFolderGroupImage = UIImage()
  49. static var cellFolderExternalImage = UIImage()
  50. static var cellFolderAutomaticUploadImage = UIImage()
  51. static var cellFolderImage = UIImage()
  52. static var cellPlayImage = UIImage()
  53. }
  54. @objc func createImagesThemingColor() {
  55. NCMainCommonImages.cellSharedImage = CCGraphics.changeThemingColorImage(UIImage.init(named: "share"), width: 100, height: 100, color: NCBrandColor.sharedInstance.textView)
  56. NCMainCommonImages.cellCanShareImage = CCGraphics.changeThemingColorImage(UIImage.init(named: "share"), width: 100, height: 100, color: NCBrandColor.sharedInstance.optionItem)
  57. NCMainCommonImages.cellShareByLinkImage = CCGraphics.changeThemingColorImage(UIImage.init(named: "sharebylink"), width: 100, height: 100, color: NCBrandColor.sharedInstance.optionItem)
  58. NCMainCommonImages.cellFavouriteImage = CCGraphics.changeThemingColorImage(UIImage.init(named: "favorite"), width: 100, height: 100, color: NCBrandColor.sharedInstance.yellowFavorite)
  59. NCMainCommonImages.cellMoreImage = CCGraphics.changeThemingColorImage(UIImage.init(named: "more"), width: 50, height: 50, color: NCBrandColor.sharedInstance.optionItem)
  60. NCMainCommonImages.cellCommentImage = CCGraphics.changeThemingColorImage(UIImage.init(named: "comment"), width: 30, height: 30, color: NCBrandColor.sharedInstance.graySoft)
  61. NCMainCommonImages.cellLivePhotoImage = CCGraphics.changeThemingColorImage(UIImage.init(named: "livePhoto"), width: 100, height: 100, color: NCBrandColor.sharedInstance.textView)
  62. NCMainCommonImages.cellFolderEncryptedImage = CCGraphics.changeThemingColorImage(UIImage.init(named: "folderEncrypted"), width: 600, height: 600, color: NCBrandColor.sharedInstance.brandElement)
  63. NCMainCommonImages.cellFolderSharedWithMeImage = CCGraphics.changeThemingColorImage(UIImage.init(named: "folder_shared_with_me"), width: 600, height: 600, color: NCBrandColor.sharedInstance.brandElement)
  64. NCMainCommonImages.cellFolderPublicImage = CCGraphics.changeThemingColorImage(UIImage.init(named: "folder_public"), width: 600, height: 600, color: NCBrandColor.sharedInstance.brandElement)
  65. NCMainCommonImages.cellFolderGroupImage = CCGraphics.changeThemingColorImage(UIImage.init(named: "folder_group"), width: 600, height: 600, color: NCBrandColor.sharedInstance.brandElement)
  66. NCMainCommonImages.cellFolderExternalImage = CCGraphics.changeThemingColorImage(UIImage.init(named: "folder_external"), width: 600, height: 600, color: NCBrandColor.sharedInstance.brandElement)
  67. NCMainCommonImages.cellFolderAutomaticUploadImage = CCGraphics.changeThemingColorImage(UIImage.init(named: "folderAutomaticUpload"), width: 600, height: 600, color: NCBrandColor.sharedInstance.brandElement)
  68. NCMainCommonImages.cellFolderImage = CCGraphics.changeThemingColorImage(UIImage.init(named: "folder"), width: 600, height: 600, color: NCBrandColor.sharedInstance.brandElement)
  69. NCMainCommonImages.cellPlayImage = CCGraphics.changeThemingColorImage(UIImage.init(named: "play"), width: 100, height: 100, color: .white)
  70. }
  71. //MARK: -
  72. @objc func triggerProgressTask(_ notification: Notification, sectionDataSourceocIdIndexPath: NSDictionary, tableView: UITableView, viewController: UIViewController, serverUrlViewController: String?) {
  73. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  74. if viewController.viewIfLoaded?.window == nil {
  75. return
  76. }
  77. guard let dic = notification.userInfo else {
  78. return
  79. }
  80. let _ = dic["account"] as? NSString ?? ""
  81. let ocId = dic["ocId"] as? NSString ?? ""
  82. let serverUrl = dic["serverUrl"] as? String ?? ""
  83. let status = dic["status"] as? Int ?? Int(k_metadataStatusNormal)
  84. let progress = dic["progress"] as? CGFloat ?? 0
  85. let totalBytes = dic["totalBytes"] as? Double ?? 0
  86. let totalBytesExpected = dic["totalBytesExpected"] as? Double ?? 0
  87. if serverUrlViewController != nil && serverUrlViewController! != serverUrl {
  88. return
  89. }
  90. appDelegate.listProgressMetadata.setObject([progress as NSNumber, totalBytes as NSNumber, totalBytesExpected as NSNumber], forKey: ocId)
  91. guard let indexPath = sectionDataSourceocIdIndexPath.object(forKey: ocId) else {
  92. return
  93. }
  94. if isValidIndexPath(indexPath as! IndexPath, view: tableView) {
  95. if let cell = tableView.cellForRow(at: indexPath as! IndexPath) as? CCCellMainTransfer {
  96. var image = ""
  97. if status == k_metadataStatusInDownload {
  98. image = "↓"
  99. } else if status == k_metadataStatusInUpload {
  100. image = "↑"
  101. }
  102. cell.labelInfoFile?.text = CCUtility.transformedSize(totalBytesExpected) + " - " + image + CCUtility.transformedSize(totalBytes)
  103. cell.transferButton?.progress = progress
  104. }
  105. }
  106. }
  107. //MARK: -
  108. @objc func cellForRowAtIndexPath(_ indexPath: IndexPath, tableView: UITableView ,metadata: tableMetadata, metadataFolder: tableMetadata?, serverUrl: String, autoUploadFileName: String, autoUploadDirectory: String, tableShare: tableShare?, livePhoto: Bool) -> UITableViewCell {
  109. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  110. // Create File System
  111. if metadata.directory {
  112. CCUtility.getDirectoryProviderStorageOcId(metadata.ocId)
  113. } else {
  114. CCUtility.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView)
  115. }
  116. // CCCell
  117. if metadata.status == k_metadataStatusNormal || metadata.status == k_metadataStatusDownloadError {
  118. // NORMAL
  119. let cell = tableView.dequeueReusableCell(withIdentifier: "CellMain", for: indexPath) as! CCCellMain
  120. cell.labelTitle.text = metadata.fileNameView
  121. cell.filePreviewImageView.image = nil
  122. cell.filePreviewImageView.backgroundColor = UIColor.lightGray
  123. // Download preview
  124. if !(metadataFolder?.e2eEncrypted ?? false) {
  125. NCOperationQueue.shared.downloadThumbnail(metadata: metadata, urlBase: appDelegate.urlBase, view: tableView, indexPath: indexPath)
  126. }
  127. // Share
  128. var isShare = false
  129. var isMounted = false
  130. if metadataFolder != nil {
  131. isShare = metadata.permissions.contains(k_permission_shared) && !metadataFolder!.permissions.contains(k_permission_shared)
  132. isMounted = metadata.permissions.contains(k_permission_mounted) && !metadataFolder!.permissions.contains(k_permission_mounted)
  133. }
  134. if metadata.directory {
  135. // lable Info
  136. cell.labelInfoFile.text = CCUtility.dateDiff(metadata.date as Date)
  137. cell.file.backgroundColor = nil
  138. // File Image & Image Title Segue
  139. if metadata.e2eEncrypted {
  140. cell.file.image = NCMainCommonImages.cellFolderEncryptedImage
  141. } else if isShare {
  142. cell.file.image = NCMainCommonImages.cellFolderSharedWithMeImage
  143. } else if (tableShare != nil && tableShare!.shareType != 3) {
  144. cell.file.image = NCMainCommonImages.cellFolderSharedWithMeImage
  145. } else if (tableShare != nil && tableShare!.shareType == 3) {
  146. cell.file.image = NCMainCommonImages.cellFolderPublicImage
  147. } else if metadata.mountType == "group" {
  148. cell.file.image = NCMainCommonImages.cellFolderGroupImage
  149. } else if isMounted {
  150. cell.file.image = NCMainCommonImages.cellFolderExternalImage
  151. } else if metadata.fileName == autoUploadFileName && serverUrl == autoUploadDirectory {
  152. cell.file.image = NCMainCommonImages.cellFolderAutomaticUploadImage
  153. } else {
  154. cell.file.image = NCMainCommonImages.cellFolderImage
  155. }
  156. let lockServerUrl = CCUtility.stringAppendServerUrl(serverUrl, addFileName: metadata.fileName)!
  157. let tableDirectory = NCManageDatabase.sharedInstance.getTableDirectory(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@", appDelegate.account, lockServerUrl))
  158. // Local image: offline
  159. if tableDirectory != nil && tableDirectory!.offline {
  160. cell.local.image = UIImage.init(named: "offlineFlag")
  161. }
  162. } else {
  163. let iconFileExists = FileManager.default.fileExists(atPath: CCUtility.getDirectoryProviderStorageIconOcId(metadata.ocId, etag: metadata.etag))
  164. // Lable Info
  165. cell.labelInfoFile.text = CCUtility.dateDiff(metadata.date as Date) + " · " + CCUtility.transformedSize(metadata.size)
  166. // File Image
  167. if iconFileExists {
  168. cell.file.backgroundColor = nil
  169. cell.file.image = UIImage(contentsOfFile: CCUtility.getDirectoryProviderStorageIconOcId(metadata.ocId, etag: metadata.etag))
  170. } else if(!metadata.hasPreview){
  171. cell.file.backgroundColor = nil
  172. if metadata.iconName.count > 0 {
  173. cell.file.image = UIImage.init(named: metadata.iconName)
  174. } else {
  175. cell.file.image = UIImage.init(named: "file")
  176. }
  177. }
  178. // Local Image - Offline
  179. let size = CCUtility.fileProviderStorageSize(metadata.ocId, fileNameView: metadata.fileNameView)
  180. if size > 0 {
  181. let tableLocalFile = NCManageDatabase.sharedInstance.getTableLocalFile(predicate: NSPredicate(format: "ocId == %@", metadata.ocId))
  182. if tableLocalFile == nil && size == metadata.size {
  183. NCManageDatabase.sharedInstance.addLocalFile(metadata: metadata)
  184. }
  185. if tableLocalFile != nil && tableLocalFile!.offline { cell.local.image = UIImage.init(named: "offlineFlag") }
  186. else { cell.local.image = UIImage.init(named: "local") }
  187. }
  188. // Status image: encrypted
  189. let tableE2eEncryption = NCManageDatabase.sharedInstance.getE2eEncryption(predicate: NSPredicate(format: "account == %@ AND fileNameIdentifier == %@", appDelegate.account, metadata.fileName))
  190. if tableE2eEncryption != nil && NCUtility.shared.isEncryptedMetadata(metadata) {
  191. cell.status.image = UIImage.init(named: "encrypted")
  192. }
  193. // Live Photo
  194. if metadata.livePhoto && livePhoto {
  195. cell.status.image = NCMainCommonImages.cellLivePhotoImage
  196. }
  197. }
  198. //
  199. // File & Directory
  200. //
  201. // Favorite
  202. if metadata.favorite {
  203. cell.favorite.image = NCMainCommonImages.cellFavouriteImage
  204. }
  205. // Share image
  206. if !(metadataFolder?.e2eEncrypted ?? false) && !metadata.e2eEncrypted {
  207. if (isShare) {
  208. cell.shared.image = NCMainCommonImages.cellSharedImage
  209. } else if (tableShare != nil && tableShare!.shareType == 3) {
  210. cell.shared.image = NCMainCommonImages.cellShareByLinkImage
  211. } else if (tableShare != nil && tableShare!.shareType != 3) {
  212. cell.shared.image = NCMainCommonImages.cellSharedImage
  213. } else {
  214. cell.shared.image = NCMainCommonImages.cellCanShareImage
  215. }
  216. if metadata.ownerId != appDelegate.userID {
  217. // Load avatar
  218. let fileNameSource = CCUtility.getDirectoryUserData() + "/" + CCUtility.getStringUser(appDelegate.user, urlBase: appDelegate.urlBase) + "-" + metadata.ownerId + ".png"
  219. let fileNameSourceAvatar = CCUtility.getDirectoryUserData() + "/" + CCUtility.getStringUser(appDelegate.user, urlBase: appDelegate.urlBase) + "-avatar-" + metadata.ownerId + ".png"
  220. if FileManager.default.fileExists(atPath: fileNameSourceAvatar) {
  221. if let avatar = UIImage(contentsOfFile: fileNameSourceAvatar) {
  222. cell.shared.image = avatar
  223. }
  224. } else if FileManager.default.fileExists(atPath: fileNameSource) {
  225. if let avatar = NCUtility.shared.createAvatar(fileNameSource: fileNameSource, fileNameSourceAvatar: fileNameSourceAvatar) {
  226. cell.shared.image = avatar
  227. }
  228. } else {
  229. NCCommunication.shared.downloadAvatar(userID: metadata.ownerId, fileNameLocalPath: fileNameSource, size: Int(k_avatar_size)) { (account, data, errorCode, errorMessage) in
  230. if errorCode == 0 && account == appDelegate.account {
  231. cell.shared.image = NCUtility.shared.createAvatar(fileNameSource: fileNameSource, fileNameSourceAvatar: fileNameSourceAvatar)
  232. }
  233. }
  234. }
  235. }
  236. }
  237. // Comment
  238. if metadata.commentsUnread {
  239. cell.comment.image = NCMainCommonImages.cellCommentImage
  240. cell.labelTitleTrailingConstraint.constant = 160
  241. } else {
  242. cell.labelTitleTrailingConstraint.constant = 110
  243. }
  244. // More Image
  245. cell.more.image = NCMainCommonImages.cellMoreImage
  246. // downloadFile Error
  247. if metadata.status == k_metadataStatusDownloadError {
  248. cell.status.image = UIImage.init(named: "statuserror")
  249. if metadata.sessionError.count == 0 {
  250. cell.labelInfoFile.text = NSLocalizedString("_error_", comment: "") + ", " + NSLocalizedString("_file_not_downloaded_", comment: "")
  251. } else {
  252. cell.labelInfoFile.text = metadata.sessionError
  253. }
  254. }
  255. return cell
  256. } else {
  257. // TRASNFER
  258. let cell = tableView.dequeueReusableCell(withIdentifier: "CellMainTransfer", for: indexPath) as! CCCellMainTransfer
  259. cell.labelTitle.text = metadata.fileNameView
  260. var progress: CGFloat = 0.0
  261. var totalBytes: Double = 0.0
  262. let progressArray = appDelegate.listProgressMetadata.object(forKey: metadata.ocId) as? NSArray
  263. if progressArray != nil && progressArray?.count == 3 {
  264. progress = progressArray?.object(at: 0) as? CGFloat ?? 0
  265. totalBytes = progressArray?.object(at: 1) as? Double ?? 0
  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.getDirectoryProviderStorageIconOcId(metadata.ocId, etag: metadata.etag))
  296. if iconFileExists {
  297. cell.file.image = UIImage(contentsOfFile: CCUtility.getDirectoryProviderStorageIconOcId(metadata.ocId, etag: metadata.etag))
  298. } else {
  299. if metadata.status == k_metadataStatusWaitUpload || metadata.status == k_metadataStatusInUpload || metadata.status == k_metadataStatusUploading || metadata.status == k_metadataStatusUploadError {
  300. cell.file.image = CCGraphics.changeThemingColorImage(UIImage.init(named: "cloudUpload"), width: 100, height: 100, color: NCBrandColor.sharedInstance.brandElement)
  301. } else {
  302. cell.file.image = CCGraphics.changeThemingColorImage(UIImage.init(named: "cloudDownload"), width: 100, height: 100, color: NCBrandColor.sharedInstance.brandElement)
  303. }
  304. }
  305. // uploadFileError
  306. if metadata.status == k_metadataStatusUploadError {
  307. cell.status.image = UIImage.init(named: "statuserror")
  308. if metadata.sessionError.count == 0 {
  309. cell.labelInfoFile.text = NSLocalizedString("_error_", comment: "") + ", " + NSLocalizedString("_file_not_uploaded_", comment: "")
  310. } else {
  311. cell.labelInfoFile.text = metadata.sessionError
  312. }
  313. }
  314. // Progress
  315. cell.transferButton.progress = progress
  316. // User
  317. if metadata.account != appDelegate.account {
  318. let tableAccount = NCManageDatabase.sharedInstance.getAccount(predicate: NSPredicate(format: "account == %@", metadata.account))
  319. if tableAccount != nil {
  320. let fileNamePath = CCUtility.getDirectoryUserData() + "/" + CCUtility.getStringUser(appDelegate.user, urlBase: appDelegate.urlBase) + "-" + appDelegate.user + ".png"
  321. var avatar = UIImage.init(contentsOfFile: fileNamePath)
  322. if avatar != nil {
  323. let avatarImageView = CCAvatar.init(image: avatar, borderColor: UIColor.black, borderWidth: 0.5)
  324. let imageSize = avatarImageView?.bounds.size
  325. UIGraphicsBeginImageContext(imageSize!)
  326. let context = UIGraphicsGetCurrentContext()
  327. avatarImageView?.layer.render(in: context!)
  328. avatar = UIGraphicsGetImageFromCurrentImageContext()
  329. UIGraphicsEndImageContext()
  330. cell.user.image = avatar
  331. }
  332. }
  333. }
  334. return cell
  335. }
  336. }
  337. @objc func getMetadataFromSectionDataSourceIndexPath(_ indexPath: IndexPath?, sectionDataSource: CCSectionDataSourceMetadata?) -> tableMetadata? {
  338. guard let indexPath = indexPath else {
  339. return nil
  340. }
  341. guard let sectionDataSource = sectionDataSource else {
  342. return nil
  343. }
  344. let section = indexPath.section + 1
  345. let row = indexPath.row + 1
  346. let totSections = sectionDataSource.sections.count
  347. if totSections < section || section > totSections {
  348. return nil
  349. }
  350. let valueSection = sectionDataSource.sections.object(at: indexPath.section)
  351. guard let filesID = sectionDataSource.sectionArrayRow.object(forKey: valueSection) as? NSArray else {
  352. return nil
  353. }
  354. let totRows = filesID.count
  355. if totRows < row || row > totRows {
  356. return nil
  357. }
  358. let ocId = filesID.object(at: indexPath.row)
  359. let metadata = sectionDataSource.allRecordsDataSource.object(forKey: ocId) as? tableMetadata
  360. return metadata
  361. }
  362. @objc func isValidIndexPath(_ indexPath: IndexPath, view: Any) -> Bool {
  363. if view is UICollectionView {
  364. return indexPath.section < (view as! UICollectionView).numberOfSections && indexPath.row < (view as! UICollectionView).numberOfItems(inSection: indexPath.section)
  365. }
  366. if view is UITableView {
  367. return indexPath.section < (view as! UITableView).numberOfSections && indexPath.row < (view as! UITableView).numberOfRows(inSection: indexPath.section)
  368. }
  369. return true
  370. }
  371. //MARK: - download Open Selector
  372. @objc func downloadOpen(metadata: tableMetadata, selector: String) {
  373. if CCUtility.fileProviderStorageExists(metadata.ocId, fileNameView: metadata.fileNameView) {
  374. NotificationCenter.default.postOnMainThread(name: k_notificationCenter_downloadedFile, userInfo: ["metadata": metadata, "selector": selector, "errorCode": 0, "errorDescription": "" ])
  375. } else {
  376. NCNetworking.shared.download(metadata: metadata, selector: selector) { (_) in }
  377. }
  378. }
  379. //MARK: - OpenIn
  380. func openIn(fileURL: URL, selector: String?) {
  381. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  382. docController = UIDocumentInteractionController(url: fileURL)
  383. docController?.delegate = self
  384. if selector == selectorOpenInDetail {
  385. guard let barButtonItem = appDelegate.activeDetail.navigationItem.rightBarButtonItem else { return }
  386. guard let buttonItemView = barButtonItem.value(forKey: "view") as? UIView else { return }
  387. docController?.presentOptionsMenu(from: buttonItemView.frame, in: buttonItemView, animated: true)
  388. } else {
  389. guard let splitViewController = appDelegate.window?.rootViewController as? UISplitViewController, let view = splitViewController.viewControllers.first?.view, let frame = splitViewController.viewControllers.first?.view.frame else {
  390. return }
  391. docController?.presentOptionsMenu(from: frame, in: view, animated: true)
  392. }
  393. }
  394. //MARK: - OpenShare
  395. @objc func openShare(ViewController: UIViewController, metadata: tableMetadata, indexPage: Int) {
  396. let shareNavigationController = UIStoryboard(name: "NCShare", bundle: nil).instantiateInitialViewController() as! UINavigationController
  397. let shareViewController = shareNavigationController.topViewController as! NCSharePaging
  398. shareViewController.metadata = metadata
  399. shareViewController.indexPage = indexPage
  400. shareNavigationController.modalPresentationStyle = .formSheet
  401. ViewController.present(shareNavigationController, animated: true, completion: nil)
  402. }
  403. //MARK: - NCAudioRecorder
  404. func startAudioRecorder() {
  405. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  406. let fileName = CCUtility.createFileNameDate(NSLocalizedString("_voice_memo_filename_", comment: ""), extension: "m4a")!
  407. let viewController = UIStoryboard(name: "NCAudioRecorderViewController", bundle: nil).instantiateInitialViewController() as! NCAudioRecorderViewController
  408. viewController.delegate = self
  409. viewController.createRecorder(fileName: fileName)
  410. viewController.modalTransitionStyle = .crossDissolve
  411. viewController.modalPresentationStyle = UIModalPresentationStyle.overCurrentContext
  412. appDelegate.window.rootViewController?.present(viewController, animated: true, completion: nil)
  413. }
  414. func didFinishRecording(_ viewController: NCAudioRecorderViewController, fileName: String) {
  415. guard let navigationController = UIStoryboard(name: "NCCreateFormUploadVoiceNote", bundle: nil).instantiateInitialViewController() else { return }
  416. navigationController.modalPresentationStyle = UIModalPresentationStyle.formSheet
  417. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  418. let viewController = (navigationController as! UINavigationController).topViewController as! NCCreateFormUploadVoiceNote
  419. viewController.setup(serverUrl: appDelegate.activeServerUrl, fileNamePath: NSTemporaryDirectory() + fileName, fileName: fileName)
  420. appDelegate.window.rootViewController?.present(navigationController, animated: true, completion: nil)
  421. }
  422. func didFinishWithoutRecording(_ viewController: NCAudioRecorderViewController, fileName: String) {
  423. }
  424. }
  425. //MARK: - Main TabBarController
  426. class CCMainTabBarController : UITabBarController, UITabBarControllerDelegate {
  427. override func viewDidLoad() {
  428. super.viewDidLoad()
  429. delegate = self
  430. }
  431. //Delegate methods
  432. func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
  433. let tabViewControllers = tabBarController.viewControllers!
  434. guard let toIndex = tabViewControllers.firstIndex(of: viewController) else {
  435. if let vc = viewController as? UINavigationController {
  436. vc.popToRootViewController(animated: true);
  437. }
  438. return false
  439. }
  440. animateToTab(toIndex: toIndex)
  441. return true
  442. }
  443. func animateToTab(toIndex: Int) {
  444. let tabViewControllers = viewControllers!
  445. let fromView = selectedViewController!.view!
  446. let toView = tabViewControllers[toIndex].view!
  447. let fromIndex = tabViewControllers.firstIndex(of: selectedViewController!)
  448. guard fromIndex != toIndex else {return}
  449. // Add the toView to the tab bar view
  450. fromView.superview?.addSubview(toView)
  451. fromView.superview?.backgroundColor = NCBrandColor.sharedInstance.backgroundView
  452. // Position toView off screen (to the left/right of fromView)
  453. let screenWidth = UIScreen.main.bounds.size.width;
  454. let scrollRight = toIndex > fromIndex!;
  455. let offset = (scrollRight ? screenWidth : -screenWidth)
  456. toView.center = CGPoint(x: (fromView.center.x) + offset, y: (toView.center.y))
  457. // Disable interaction during animation
  458. view.isUserInteractionEnabled = false
  459. UIView.animate(withDuration: 0.3, delay: 0.0, usingSpringWithDamping: 1, initialSpringVelocity: 0, options: UIView.AnimationOptions.curveEaseOut, animations: {
  460. // Slide the views by -offset
  461. fromView.center = CGPoint(x: fromView.center.x - offset, y: fromView.center.y);
  462. toView.center = CGPoint(x: toView.center.x - offset, y: toView.center.y);
  463. }, completion: { finished in
  464. // Remove the old view from the tabbar view.
  465. fromView.removeFromSuperview()
  466. self.selectedIndex = toIndex
  467. self.view.isUserInteractionEnabled = true
  468. })
  469. }
  470. }