NCCreateFormUploadAssets.swift 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. //
  2. // NCCreateFormUploadAssets.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 14/11/2018.
  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. @objc protocol createFormUploadAssetsDelegate {
  25. func dismissFormUploadAssets()
  26. }
  27. class NCCreateFormUploadAssets: XLFormViewController, NCSelectDelegate, PhotoEditorDelegate {
  28. var serverUrl: String = ""
  29. var titleServerUrl: String?
  30. var assets = NSMutableArray()
  31. var cryptated: Bool = false
  32. var session: String = ""
  33. weak var delegate: createFormUploadAssetsDelegate?
  34. let requestOptions = PHImageRequestOptions()
  35. var imagePreview: UIImage?
  36. let targetSizeImagePreview = CGSize(width:100, height: 100)
  37. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  38. @objc convenience init(serverUrl : String, assets : NSMutableArray, cryptated : Bool, session : String, delegate: createFormUploadAssetsDelegate) {
  39. self.init()
  40. if serverUrl == CCUtility.getHomeServerUrlActiveUrl(appDelegate.activeUrl) {
  41. titleServerUrl = "/"
  42. } else {
  43. titleServerUrl = (serverUrl as NSString).lastPathComponent
  44. }
  45. self.serverUrl = serverUrl
  46. self.assets = assets
  47. self.cryptated = cryptated
  48. self.session = session
  49. self.delegate = delegate
  50. requestOptions.resizeMode = PHImageRequestOptionsResizeMode.exact
  51. requestOptions.deliveryMode = PHImageRequestOptionsDeliveryMode.highQualityFormat
  52. requestOptions.isSynchronous = true
  53. }
  54. // MARK: - View Life Cycle
  55. override func viewDidLoad() {
  56. super.viewDidLoad()
  57. self.navigationItem.leftBarButtonItem = UIBarButtonItem(title: NSLocalizedString("_cancel_", comment: ""), style: UIBarButtonItem.Style.plain, target: self, action: #selector(cancel))
  58. self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: NSLocalizedString("_save_", comment: ""), style: UIBarButtonItem.Style.plain, target: self, action: #selector(save))
  59. self.tableView.separatorStyle = UITableViewCell.SeparatorStyle.none
  60. if assets.count == 1 && (assets[0] as! PHAsset).mediaType == PHAssetMediaType.image {
  61. PHImageManager.default().requestImage(for: assets[0] as! PHAsset, targetSize: targetSizeImagePreview, contentMode: PHImageContentMode.aspectFill, options: requestOptions, resultHandler: { (image, info) in
  62. self.imagePreview = image
  63. })
  64. }
  65. // Theming view
  66. NotificationCenter.default.addObserver(self, selector: #selector(self.changeTheming), name: NSNotification.Name(rawValue: "changeTheming"), object: nil)
  67. changeTheming()
  68. }
  69. override func viewWillDisappear(_ animated: Bool)
  70. {
  71. super.viewWillDisappear(animated)
  72. self.delegate?.dismissFormUploadAssets()
  73. }
  74. @objc func changeTheming() {
  75. appDelegate.changeTheming(self, tableView: tableView, collectionView: nil, form: true)
  76. initializeForm()
  77. self.reloadForm()
  78. }
  79. //MARK: XLForm
  80. func initializeForm() {
  81. let form : XLFormDescriptor = XLFormDescriptor(title: NSLocalizedString("_upload_photos_videos_", comment: "")) as XLFormDescriptor
  82. form.rowNavigationOptions = XLFormRowNavigationOptions.stopDisableRow
  83. var section : XLFormSectionDescriptor
  84. var row : XLFormRowDescriptor
  85. // Section Photo Editor only for one photo & IMI
  86. if assets.count == 1 && (assets[0] as! PHAsset).mediaType == PHAssetMediaType.image && self.imagePreview != nil {
  87. section = XLFormSectionDescriptor.formSection(withTitle: NSLocalizedString("_modify_photo_", comment: ""))
  88. form.addFormSection(section)
  89. row = XLFormRowDescriptor(tag: "ButtonPhotoEditor", rowType: XLFormRowDescriptorTypeButton, title: NSLocalizedString("_modify_photo_", comment: ""))
  90. row.action.formSelector = #selector(photoEditor(_:))
  91. row.cellConfig["backgroundColor"] = NCBrandColor.sharedInstance.backgroundForm
  92. row.cellConfig["imageView.image"] = self.imagePreview
  93. row.cellConfig["textLabel.textColor"] = NCBrandColor.sharedInstance.textView
  94. row.cellConfig["textLabel.textAlignment"] = NSTextAlignment.right.rawValue
  95. row.cellConfig["textLabel.font"] = UIFont.systemFont(ofSize: 15.0)
  96. section.addFormRow(row)
  97. if NCBrandOptions.sharedInstance.use_imi_viewer {
  98. row = XLFormRowDescriptor(tag: "createIMI", rowType: XLFormRowDescriptorTypeBooleanSwitch, title: NSLocalizedString("_im_create_new_file", tableName: "IMLocalizable", bundle: Bundle.main, value: "", comment: ""))
  99. row.value = 0
  100. row.cellConfig["backgroundColor"] = NCBrandColor.sharedInstance.backgroundForm
  101. row.cellConfig["textLabel.font"] = UIFont.systemFont(ofSize: 15.0)
  102. row.cellConfig["textLabel.textColor"] = NCBrandColor.sharedInstance.textView
  103. section.addFormRow(row)
  104. }
  105. }
  106. // Section: Destination Folder
  107. section = XLFormSectionDescriptor.formSection(withTitle: NSLocalizedString("_save_path_", comment: ""))
  108. form.addFormSection(section)
  109. row = XLFormRowDescriptor(tag: "ButtonDestinationFolder", rowType: XLFormRowDescriptorTypeButton, title: self.titleServerUrl)
  110. row.action.formSelector = #selector(changeDestinationFolder(_:))
  111. row.cellConfig["backgroundColor"] = NCBrandColor.sharedInstance.backgroundForm
  112. row.cellConfig["imageView.image"] = CCGraphics.changeThemingColorImage(UIImage(named: "folder")!, width: 50, height: 50, color: NCBrandColor.sharedInstance.brandElement) as UIImage
  113. row.cellConfig["textLabel.textAlignment"] = NSTextAlignment.right.rawValue
  114. row.cellConfig["textLabel.font"] = UIFont.systemFont(ofSize: 15.0)
  115. row.cellConfig["textLabel.textColor"] = NCBrandColor.sharedInstance.textView
  116. section.addFormRow(row)
  117. // User folder Autoupload
  118. row = XLFormRowDescriptor(tag: "useFolderAutoUpload", rowType: XLFormRowDescriptorTypeBooleanSwitch, title: NSLocalizedString("_use_folder_auto_upload_", comment: ""))
  119. row.value = 0
  120. row.cellConfig["backgroundColor"] = NCBrandColor.sharedInstance.backgroundForm
  121. row.cellConfig["textLabel.font"] = UIFont.systemFont(ofSize: 15.0)
  122. row.cellConfig["textLabel.textColor"] = NCBrandColor.sharedInstance.textView
  123. section.addFormRow(row)
  124. // Use Sub folder
  125. row = XLFormRowDescriptor(tag: "useSubFolder", rowType: XLFormRowDescriptorTypeBooleanSwitch, title: NSLocalizedString("_autoupload_create_subfolder_", comment: ""))
  126. let tableAccount = NCManageDatabase.sharedInstance.getAccountActive()
  127. if tableAccount?.autoUploadCreateSubfolder == true {
  128. row.value = 1
  129. } else {
  130. row.value = 0
  131. }
  132. row.hidden = "$\("useFolderAutoUpload") == 0"
  133. row.cellConfig["textLabel.font"] = UIFont.systemFont(ofSize: 15.0)
  134. row.cellConfig["textLabel.textColor"] = NCBrandColor.sharedInstance.textView
  135. section.addFormRow(row)
  136. // Section Mode filename
  137. section = XLFormSectionDescriptor.formSection(withTitle: NSLocalizedString("_mode_filename_", comment: ""))
  138. form.addFormSection(section)
  139. // Maintain the original fileName
  140. row = XLFormRowDescriptor(tag: "maintainOriginalFileName", rowType: XLFormRowDescriptorTypeBooleanSwitch, title: NSLocalizedString("_maintain_original_filename_", comment: ""))
  141. row.value = CCUtility.getOriginalFileName(k_keyFileNameOriginal)
  142. row.cellConfig["backgroundColor"] = NCBrandColor.sharedInstance.backgroundForm
  143. row.cellConfig["textLabel.font"] = UIFont.systemFont(ofSize: 15.0)
  144. row.cellConfig["textLabel.textColor"] = NCBrandColor.sharedInstance.textView
  145. section.addFormRow(row)
  146. // Add File Name Type
  147. row = XLFormRowDescriptor(tag: "addFileNameType", rowType: XLFormRowDescriptorTypeBooleanSwitch, title: NSLocalizedString("_add_filenametype_", comment: ""))
  148. row.value = CCUtility.getFileNameType(k_keyFileNameType)
  149. row.hidden = "$\("maintainOriginalFileName") == 1"
  150. row.cellConfig["backgroundColor"] = NCBrandColor.sharedInstance.backgroundForm
  151. row.cellConfig["textLabel.font"] = UIFont.systemFont(ofSize: 15.0)
  152. row.cellConfig["textLabel.textColor"] = NCBrandColor.sharedInstance.textView
  153. section.addFormRow(row)
  154. // Section: Rename File Name
  155. section = XLFormSectionDescriptor.formSection(withTitle: NSLocalizedString("_filename_", comment: ""))
  156. form.addFormSection(section)
  157. row = XLFormRowDescriptor(tag: "maskFileName", rowType: XLFormRowDescriptorTypeAccount, title: (NSLocalizedString("_filename_", comment: "")))
  158. let fileNameMask : String = CCUtility.getFileNameMask(k_keyFileNameMask)
  159. if fileNameMask.count > 0 {
  160. row.value = fileNameMask
  161. }
  162. row.hidden = "$\("maintainOriginalFileName") == 1"
  163. row.cellConfig["backgroundColor"] = NCBrandColor.sharedInstance.backgroundForm
  164. row.cellConfig["textLabel.font"] = UIFont.systemFont(ofSize: 15.0)
  165. row.cellConfig["textLabel.textColor"] = NCBrandColor.sharedInstance.textView
  166. row.cellConfig["textField.textAlignment"] = NSTextAlignment.right.rawValue
  167. row.cellConfig["textField.font"] = UIFont.systemFont(ofSize: 15.0)
  168. row.cellConfig["textField.textColor"] = NCBrandColor.sharedInstance.textView
  169. section.addFormRow(row)
  170. // Section: Preview File Name
  171. row = XLFormRowDescriptor(tag: "previewFileName", rowType: XLFormRowDescriptorTypeTextView, title: "")
  172. row.height = 180
  173. row.disabled = true
  174. row.cellConfig["backgroundColor"] = NCBrandColor.sharedInstance.backgroundForm
  175. row.cellConfig["textView.backgroundColor"] = NCBrandColor.sharedInstance.backgroundForm
  176. row.cellConfig["textView.font"] = UIFont.systemFont(ofSize: 14.0)
  177. row.cellConfig["textView.textColor"] = NCBrandColor.sharedInstance.textView
  178. section.addFormRow(row)
  179. self.form = form
  180. }
  181. override func formRowDescriptorValueHasChanged(_ formRow: XLFormRowDescriptor!, oldValue: Any!, newValue: Any!) {
  182. super.formRowDescriptorValueHasChanged(formRow, oldValue: oldValue, newValue: newValue)
  183. if formRow.tag == "createIMI" {
  184. if (formRow.value! as AnyObject).boolValue == true {
  185. } else {
  186. }
  187. }
  188. else if formRow.tag == "useFolderAutoUpload" {
  189. if (formRow.value! as AnyObject).boolValue == true {
  190. let buttonDestinationFolder : XLFormRowDescriptor = self.form.formRow(withTag: "ButtonDestinationFolder")!
  191. buttonDestinationFolder.hidden = true
  192. } else{
  193. let buttonDestinationFolder : XLFormRowDescriptor = self.form.formRow(withTag: "ButtonDestinationFolder")!
  194. buttonDestinationFolder.hidden = false
  195. }
  196. }
  197. else if formRow.tag == "useSubFolder" {
  198. if (formRow.value! as AnyObject).boolValue == true {
  199. } else{
  200. }
  201. }
  202. else if formRow.tag == "maintainOriginalFileName" {
  203. CCUtility.setOriginalFileName((formRow.value! as AnyObject).boolValue, key: k_keyFileNameOriginal)
  204. self.reloadForm()
  205. }
  206. else if formRow.tag == "addFileNameType" {
  207. CCUtility.setFileNameType((formRow.value! as AnyObject).boolValue, key: k_keyFileNameType)
  208. self.reloadForm()
  209. }
  210. else if formRow.tag == "maskFileName" {
  211. let fileName = formRow.value as? String
  212. self.form.delegate = nil
  213. if let fileName = fileName {
  214. formRow.value = CCUtility.removeForbiddenCharactersServer(fileName)
  215. }
  216. self.form.delegate = self
  217. let previewFileName : XLFormRowDescriptor = self.form.formRow(withTag: "previewFileName")!
  218. previewFileName.value = self.previewFileName(valueRename: formRow.value as? String)
  219. // reload cell
  220. if fileName != nil {
  221. if newValue as! String != formRow.value as! String {
  222. self.reloadFormRow(formRow)
  223. appDelegate.messageNotification("_info_", description: "_forbidden_characters_", visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.info, errorCode: 0)
  224. }
  225. }
  226. self.reloadFormRow(previewFileName)
  227. }
  228. }
  229. func reloadForm() {
  230. self.form.delegate = nil
  231. let buttonDestinationFolder : XLFormRowDescriptor = self.form.formRow(withTag: "ButtonDestinationFolder")!
  232. buttonDestinationFolder.title = self.titleServerUrl
  233. let maskFileName : XLFormRowDescriptor = self.form.formRow(withTag: "maskFileName")!
  234. let previewFileName : XLFormRowDescriptor = self.form.formRow(withTag: "previewFileName")!
  235. previewFileName.value = self.previewFileName(valueRename: maskFileName.value as? String)
  236. self.tableView.reloadData()
  237. self.form.delegate = self
  238. }
  239. // MARK: - Action
  240. func dismissSelect(serverUrl: String?, metadata: tableMetadata?, type: String) {
  241. if serverUrl != nil {
  242. self.serverUrl = serverUrl!
  243. if serverUrl == CCUtility.getHomeServerUrlActiveUrl(appDelegate.activeUrl) {
  244. self.titleServerUrl = "/"
  245. } else {
  246. self.titleServerUrl = (serverUrl! as NSString).lastPathComponent
  247. }
  248. // Update
  249. let row : XLFormRowDescriptor = self.form.formRow(withTag: "ButtonDestinationFolder")!
  250. row.title = self.titleServerUrl
  251. self.updateFormRow(row)
  252. }
  253. }
  254. @objc func save() {
  255. self.dismiss(animated: true, completion: {
  256. let useFolderPhotoRow : XLFormRowDescriptor = self.form.formRow(withTag: "useFolderAutoUpload")!
  257. let useSubFolderRow : XLFormRowDescriptor = self.form.formRow(withTag: "useSubFolder")!
  258. var useSubFolder : Bool = false
  259. if (useFolderPhotoRow.value! as AnyObject).boolValue == true {
  260. self.serverUrl = NCManageDatabase.sharedInstance.getAccountAutoUploadPath(self.appDelegate.activeUrl)
  261. useSubFolder = (useSubFolderRow.value! as AnyObject).boolValue
  262. }
  263. self.appDelegate.activeMain.uploadFileAsset(self.assets, serverUrl: self.serverUrl, useSubFolder: useSubFolder, session: self.session)
  264. })
  265. }
  266. @objc func cancel() {
  267. self.dismiss(animated: true, completion: nil)
  268. }
  269. // MARK: - Utility
  270. func previewFileName(valueRename : String?) -> String {
  271. var returnString: String = ""
  272. let asset = assets[0] as! PHAsset
  273. if (CCUtility.getOriginalFileName(k_keyFileNameOriginal)) {
  274. return (NSLocalizedString("_filename_", comment: "") + ": " + (asset.value(forKey: "filename") as! String))
  275. } else if let valueRename = valueRename {
  276. let valueRenameTrimming = valueRename.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)
  277. if valueRenameTrimming.count > 0 {
  278. self.form.delegate = nil
  279. CCUtility.setFileNameMask(valueRename, key: k_keyFileNameMask)
  280. self.form.delegate = self
  281. returnString = CCUtility.createFileName(asset.value(forKey: "filename") as! String?, fileDate: asset.creationDate, fileType: asset.mediaType, keyFileName: k_keyFileNameMask, keyFileNameType: k_keyFileNameType, keyFileNameOriginal: k_keyFileNameOriginal)
  282. } else {
  283. CCUtility.setFileNameMask("", key: k_keyFileNameMask)
  284. returnString = CCUtility.createFileName(asset.value(forKey: "filename") as! String?, fileDate: asset.creationDate, fileType: asset.mediaType, keyFileName: nil, keyFileNameType: k_keyFileNameType, keyFileNameOriginal: k_keyFileNameOriginal)
  285. }
  286. } else {
  287. CCUtility.setFileNameMask("", key: k_keyFileNameMask)
  288. returnString = CCUtility.createFileName(asset.value(forKey: "filename") as! String?, fileDate: asset.creationDate, fileType: asset.mediaType, keyFileName: nil, keyFileNameType: k_keyFileNameType, keyFileNameOriginal: k_keyFileNameOriginal)
  289. }
  290. return String(format: NSLocalizedString("_preview_filename_", comment: ""), "MM,MMM,DD,YY,YYYY and HH,hh,mm,ss,ampm") + ":" + "\n\n" + returnString
  291. }
  292. @objc func changeDestinationFolder(_ sender: XLFormRowDescriptor) {
  293. self.deselectFormRow(sender)
  294. let storyboard = UIStoryboard(name: "NCSelect", bundle: nil)
  295. let navigationController = storyboard.instantiateInitialViewController() as! UINavigationController
  296. let viewController = navigationController.topViewController as! NCSelect
  297. viewController.delegate = self
  298. viewController.hideButtonCreateFolder = false
  299. viewController.includeDirectoryE2EEncryption = true
  300. viewController.includeImages = false
  301. viewController.layoutViewSelect = k_layout_view_move
  302. viewController.selectFile = false
  303. viewController.titleButtonDone = NSLocalizedString("_select_", comment: "")
  304. viewController.type = ""
  305. navigationController.modalPresentationStyle = UIModalPresentationStyle.fullScreen
  306. self.present(navigationController, animated: true, completion: nil)
  307. }
  308. @objc func photoEditor(_ sender: XLFormRowDescriptor) {
  309. self.deselectFormRow(sender)
  310. PHImageManager.default().requestImage(for: assets[0] as! PHAsset, targetSize: PHImageManagerMaximumSize, contentMode: PHImageContentMode.default, options: requestOptions, resultHandler: { (image, info) in
  311. let photoEditor = PhotoEditorViewController(nibName:"PhotoEditorViewController",bundle: Bundle(for: PhotoEditorViewController.self))
  312. photoEditor.image = image
  313. photoEditor.photoEditorDelegate = self
  314. photoEditor.hiddenControls = [.save, .share, .sticker]
  315. photoEditor.cancelButtonImage = CCGraphics.changeThemingColorImage(UIImage(named: "photoEditorCancel")!, multiplier:2, color: .white)
  316. photoEditor.cropButtonImage = CCGraphics.changeThemingColorImage(UIImage(named: "photoEditorCrop")!, multiplier:2, color: .white)
  317. photoEditor.drawButtonImage = CCGraphics.changeThemingColorImage(UIImage(named: "photoEditorDraw")!, multiplier:2, color: .white)
  318. photoEditor.textButtonImage = CCGraphics.changeThemingColorImage(UIImage(named: "photoEditorText")!, multiplier:2, color: .white)
  319. photoEditor.clearButtonImage = CCGraphics.changeThemingColorImage(UIImage(named: "photoEditorClear")!, multiplier:2, color: .white)
  320. photoEditor.continueButtonImage = CCGraphics.changeThemingColorImage(UIImage(named: "photoEditorDone")!, multiplier:2, color: .white)
  321. self.present(photoEditor, animated: true, completion: nil)
  322. })
  323. }
  324. // MARK: - Photo Editor Delegate
  325. func doneEditing(image: UIImage) {
  326. UIImageWriteToSavedPhotosAlbum(image, self, #selector(imageSaved(_:didFinishSavingWithError:contextInfo:)), nil)
  327. }
  328. @objc func imageSaved(_ image: UIImage, didFinishSavingWithError error: Error?, contextInfo: UnsafeRawPointer) {
  329. let fetchOptions = PHFetchOptions()
  330. fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]
  331. let fetchResult = PHAsset.fetchAssets(with: PHAssetMediaType.image, options: fetchOptions)
  332. if let asset = fetchResult.firstObject {
  333. self.assets = NSMutableArray(array: [asset])
  334. }
  335. // Preview
  336. PHImageManager.default().requestImage(for: assets[0] as! PHAsset, targetSize: targetSizeImagePreview, contentMode: PHImageContentMode.aspectFill, options: requestOptions, resultHandler: { (image, info) in
  337. let row : XLFormRowDescriptor = self.form.formRow(withTag: "ButtonPhotoEditor")!
  338. row.cellConfig["imageView.image"] = image
  339. self.updateFormRow(row)
  340. })
  341. }
  342. func canceledEditing() {
  343. print("Canceled")
  344. }
  345. }