NCShareLinkMenuView.swift 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. //
  2. // NCShareLinkMenuView.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 25/07/2019.
  6. // Copyright © 2019 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. import UIKit
  23. import FSCalendar
  24. import NCCommunication
  25. class NCShareLinkMenuView: UIView, UIGestureRecognizerDelegate, UITextFieldDelegate, NCShareNetworkingDelegate, FSCalendarDelegate, FSCalendarDelegateAppearance {
  26. @IBOutlet weak var fieldLabel: UITextField!
  27. @IBOutlet weak var switchAllowEditing: UISwitch!
  28. @IBOutlet weak var labelAllowEditing: UILabel!
  29. @IBOutlet weak var switchReadOnly: UISwitch!
  30. @IBOutlet weak var labelReadOnly: UILabel!
  31. @IBOutlet weak var switchAllowUploadAndEditing: UISwitch!
  32. @IBOutlet weak var labelAllowUploadAndEditing: UILabel!
  33. @IBOutlet weak var switchFileDrop: UISwitch!
  34. @IBOutlet weak var labelFileDrop: UILabel!
  35. @IBOutlet weak var switchHideDownload: UISwitch!
  36. @IBOutlet weak var labelHideDownload: UILabel!
  37. @IBOutlet weak var switchPasswordProtect: UISwitch!
  38. @IBOutlet weak var labelPasswordProtect: UILabel!
  39. @IBOutlet weak var fieldPasswordProtect: UITextField!
  40. @IBOutlet weak var switchSetExpirationDate: UISwitch!
  41. @IBOutlet weak var labelSetExpirationDate: UILabel!
  42. @IBOutlet weak var fieldSetExpirationDate: UITextField!
  43. @IBOutlet weak var imageNoteToRecipient: UIImageView!
  44. @IBOutlet weak var labelNoteToRecipient: UILabel!
  45. @IBOutlet weak var fieldNoteToRecipient: UITextField!
  46. @IBOutlet weak var buttonDeleteShareLink: UIButton!
  47. @IBOutlet weak var labelDeleteShareLink: UILabel!
  48. @IBOutlet weak var imageDeleteShareLink: UIImageView!
  49. @IBOutlet weak var buttonAddAnotherLink: UIButton!
  50. @IBOutlet weak var labelAddAnotherLink: UILabel!
  51. @IBOutlet weak var imageAddAnotherLink: UIImageView!
  52. private let appDelegate = UIApplication.shared.delegate as! AppDelegate
  53. var width: CGFloat = 0
  54. var height: CGFloat = 0
  55. private var tableShare: tableShare?
  56. var metadata: tableMetadata?
  57. var shareViewController: NCShare?
  58. private var networking: NCShareNetworking?
  59. var viewWindow: UIView?
  60. var viewWindowCalendar: UIView?
  61. private var calendar: FSCalendar?
  62. private var activeTextfieldDiff: CGFloat = 0
  63. private var activeTextField = UITextField()
  64. override func awakeFromNib() {
  65. layer.borderColor = UIColor.lightGray.cgColor
  66. layer.borderWidth = 0.5
  67. layer.cornerRadius = 5
  68. layer.masksToBounds = false
  69. layer.shadowOffset = CGSize(width: 2, height: 2)
  70. layer.shadowOpacity = 0.2
  71. fieldLabel?.placeholder = NSLocalizedString("_Link_name_", comment: "")
  72. switchAllowEditing?.transform = CGAffineTransform(scaleX: 0.75, y: 0.75)
  73. switchAllowEditing?.onTintColor = NCBrandColor.shared.brandElement
  74. switchReadOnly?.transform = CGAffineTransform(scaleX: 0.75, y: 0.75)
  75. switchReadOnly?.onTintColor = NCBrandColor.shared.brandElement
  76. switchAllowUploadAndEditing?.transform = CGAffineTransform(scaleX: 0.75, y: 0.75)
  77. switchAllowUploadAndEditing?.onTintColor = NCBrandColor.shared.brandElement
  78. switchFileDrop?.transform = CGAffineTransform(scaleX: 0.75, y: 0.75)
  79. switchFileDrop?.onTintColor = NCBrandColor.shared.brandElement
  80. switchHideDownload.transform = CGAffineTransform(scaleX: 0.75, y: 0.75)
  81. switchHideDownload.onTintColor = NCBrandColor.shared.brandElement
  82. switchPasswordProtect.transform = CGAffineTransform(scaleX: 0.75, y: 0.75)
  83. switchPasswordProtect.onTintColor = NCBrandColor.shared.brandElement
  84. switchSetExpirationDate.transform = CGAffineTransform(scaleX: 0.75, y: 0.75)
  85. switchSetExpirationDate.onTintColor = NCBrandColor.shared.brandElement
  86. labelAllowEditing?.text = NSLocalizedString("_share_allow_editing_", comment: "")
  87. labelAllowEditing?.textColor = NCBrandColor.shared.label
  88. labelReadOnly?.text = NSLocalizedString("_share_read_only_", comment: "")
  89. labelReadOnly?.textColor = NCBrandColor.shared.label
  90. labelAllowUploadAndEditing?.text = NSLocalizedString("_share_allow_upload_", comment: "")
  91. labelAllowUploadAndEditing?.textColor = NCBrandColor.shared.label
  92. labelFileDrop?.text = NSLocalizedString("_share_file_drop_", comment: "")
  93. labelFileDrop?.textColor = NCBrandColor.shared.label
  94. labelHideDownload?.text = NSLocalizedString("_share_hide_download_", comment: "")
  95. labelHideDownload?.textColor = NCBrandColor.shared.label
  96. labelPasswordProtect?.text = NSLocalizedString("_share_password_protect_", comment: "")
  97. labelPasswordProtect?.textColor = NCBrandColor.shared.label
  98. labelSetExpirationDate?.text = NSLocalizedString("_share_expiration_date_", comment: "")
  99. labelSetExpirationDate?.textColor = NCBrandColor.shared.label
  100. labelNoteToRecipient?.text = NSLocalizedString("_share_note_recipient_", comment: "")
  101. labelNoteToRecipient?.textColor = NCBrandColor.shared.label
  102. labelDeleteShareLink?.text = NSLocalizedString("_share_delete_sharelink_", comment: "")
  103. labelDeleteShareLink?.textColor = NCBrandColor.shared.label
  104. labelAddAnotherLink?.text = NSLocalizedString("_share_add_sharelink_", comment: "")
  105. labelAddAnotherLink?.textColor = NCBrandColor.shared.label
  106. fieldSetExpirationDate.inputView = UIView()
  107. fieldLabel.delegate = self
  108. fieldPasswordProtect.delegate = self
  109. fieldNoteToRecipient.delegate = self
  110. imageNoteToRecipient.image = UIImage.init(named: "file_txt")!.image(color: UIColor(red: 76/255, green: 76/255, blue: 76/255, alpha: 1), size: 50)
  111. imageDeleteShareLink.image = NCUtility.shared.loadImage(named: "trash", color: UIColor(red: 76/255, green: 76/255, blue: 76/255, alpha: 1), size: 50)
  112. imageAddAnotherLink.image = NCUtility.shared.loadImage(named: "plus", color: UIColor(red: 76/255, green: 76/255, blue: 76/255, alpha: 1), size: 50)
  113. NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(_:)), name: UIResponder.keyboardWillShowNotification, object: nil)
  114. NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(_:)), name: UIResponder.keyboardWillHideNotification, object: nil)
  115. }
  116. override func willMove(toWindow newWindow: UIWindow?) {
  117. super.willMove(toWindow: newWindow)
  118. if newWindow == nil {
  119. // UIView disappear
  120. shareViewController?.reloadData()
  121. } else {
  122. // UIView appear
  123. networking = NCShareNetworking.init(metadata: metadata!, urlBase: appDelegate.urlBase, view: self, delegate: self)
  124. }
  125. }
  126. func unLoad() {
  127. viewWindowCalendar?.removeFromSuperview()
  128. viewWindow?.removeFromSuperview()
  129. viewWindowCalendar = nil
  130. viewWindow = nil
  131. }
  132. func reloadData(idShare: Int) {
  133. guard let metadata = self.metadata else { return }
  134. tableShare = NCManageDatabase.shared.getTableShare(account: metadata.account, idShare: idShare)
  135. guard let tableShare = self.tableShare else { return }
  136. // Label
  137. fieldLabel.text = tableShare.label
  138. if metadata.directory {
  139. // File Drop
  140. if tableShare.permissions == NCGlobal.shared.permissionCreateShare {
  141. switchReadOnly.setOn(false, animated: false)
  142. switchAllowUploadAndEditing.setOn(false, animated: false)
  143. switchFileDrop.setOn(true, animated: false)
  144. } else {
  145. // Read Only
  146. if CCUtility.isAnyPermission(toEdit: tableShare.permissions) {
  147. switchReadOnly.setOn(false, animated: false)
  148. switchAllowUploadAndEditing.setOn(true, animated: false)
  149. } else {
  150. switchReadOnly.setOn(true, animated: false)
  151. switchAllowUploadAndEditing.setOn(false, animated: false)
  152. }
  153. switchFileDrop.setOn(false, animated: false)
  154. }
  155. } else {
  156. // Allow editing
  157. if CCUtility.isAnyPermission(toEdit: tableShare.permissions) {
  158. switchAllowEditing.setOn(true, animated: false)
  159. } else {
  160. switchAllowEditing.setOn(false, animated: false)
  161. }
  162. }
  163. // Hide download
  164. if tableShare.hideDownload {
  165. switchHideDownload.setOn(true, animated: false)
  166. } else {
  167. switchHideDownload.setOn(false, animated: false)
  168. }
  169. // Password protect
  170. if tableShare.shareWith.count > 0 {
  171. switchPasswordProtect.setOn(true, animated: false)
  172. fieldPasswordProtect.isEnabled = true
  173. fieldPasswordProtect.text = tableShare.shareWith
  174. } else {
  175. switchPasswordProtect.setOn(false, animated: false)
  176. fieldPasswordProtect.isEnabled = false
  177. fieldPasswordProtect.text = ""
  178. }
  179. // Set expiration date
  180. if tableShare.expirationDate != nil {
  181. switchSetExpirationDate.setOn(true, animated: false)
  182. fieldSetExpirationDate.isEnabled = true
  183. let dateFormatter = DateFormatter()
  184. dateFormatter.formatterBehavior = .behavior10_4
  185. dateFormatter.dateStyle = .medium
  186. fieldSetExpirationDate.text = dateFormatter.string(from: tableShare.expirationDate! as Date)
  187. } else {
  188. switchSetExpirationDate.setOn(false, animated: false)
  189. fieldSetExpirationDate.isEnabled = false
  190. fieldSetExpirationDate.text = ""
  191. }
  192. // Note to recipient
  193. fieldNoteToRecipient.text = tableShare.note
  194. }
  195. func textFieldDidBeginEditing(_ textField: UITextField) {
  196. self.activeTextField = textField
  197. }
  198. // MARK: - Keyboard notification
  199. @objc internal func keyboardWillShow(_ notification : Notification?) {
  200. activeTextfieldDiff = 0
  201. if let info = notification?.userInfo, let centerObject = self.activeTextField.superview?.convert(self.activeTextField.center, to: nil) {
  202. let frameEndUserInfoKey = UIResponder.keyboardFrameEndUserInfoKey
  203. if let keyboardFrame = info[frameEndUserInfoKey] as? CGRect {
  204. let diff = keyboardFrame.origin.y - centerObject.y - self.activeTextField.frame.height
  205. if diff < 0 {
  206. activeTextfieldDiff = diff
  207. self.frame.origin.y += diff
  208. }
  209. }
  210. }
  211. }
  212. @objc func keyboardWillHide(_ notification: Notification) {
  213. self.frame.origin.y -= activeTextfieldDiff
  214. }
  215. // MARK: - Tap viewWindowCalendar
  216. @objc func tapViewWindowCalendar(gesture: UITapGestureRecognizer) {
  217. calendar?.removeFromSuperview()
  218. viewWindowCalendar?.removeFromSuperview()
  219. calendar = nil
  220. viewWindowCalendar = nil
  221. reloadData(idShare: tableShare?.idShare ?? 0)
  222. }
  223. func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
  224. return gestureRecognizer.view == touch.view
  225. }
  226. // MARK: - IBAction
  227. // Allow editing (file)
  228. @IBAction func switchAllowEditingChanged(sender: UISwitch) {
  229. guard let tableShare = self.tableShare else { return }
  230. guard let metadata = self.metadata else { return }
  231. var permissions: Int = 0
  232. if sender.isOn {
  233. permissions = CCUtility.getPermissionsValue(byCanEdit: true, andCanCreate: true, andCanChange: true, andCanDelete: true, andCanShare: false, andIsFolder: metadata.directory)
  234. } else {
  235. permissions = CCUtility.getPermissionsValue(byCanEdit: false, andCanCreate: false, andCanChange: false, andCanDelete: false, andCanShare: false, andIsFolder: metadata.directory)
  236. }
  237. networking?.updateShare(idShare: tableShare.idShare, password: nil, permissions: permissions, note: nil, label: nil, expirationDate: nil, hideDownload: tableShare.hideDownload)
  238. }
  239. // Read Only (directory)
  240. @IBAction func switchReadOnly(sender: UISwitch) {
  241. guard let tableShare = self.tableShare else { return }
  242. guard let metadata = self.metadata else { return }
  243. let permissions = CCUtility.getPermissionsValue(byCanEdit: false, andCanCreate: false, andCanChange: false, andCanDelete: false, andCanShare: false, andIsFolder: metadata.directory)
  244. if sender.isOn && permissions != tableShare.permissions {
  245. switchAllowUploadAndEditing.setOn(false, animated: false)
  246. switchFileDrop.setOn(false, animated: false)
  247. networking?.updateShare(idShare: tableShare.idShare, password: nil, permissions: permissions, note: nil, label: nil, expirationDate: nil, hideDownload: tableShare.hideDownload)
  248. } else {
  249. sender.setOn(true, animated: false)
  250. }
  251. }
  252. // Allow Upload And Editing (directory)
  253. @IBAction func switchAllowUploadAndEditing(sender: UISwitch) {
  254. guard let tableShare = self.tableShare else { return }
  255. guard let metadata = self.metadata else { return }
  256. let permissions = CCUtility.getPermissionsValue(byCanEdit: true, andCanCreate: true, andCanChange: true, andCanDelete: true, andCanShare: false, andIsFolder: metadata.directory)
  257. if sender.isOn && permissions != tableShare.permissions {
  258. switchReadOnly.setOn(false, animated: false)
  259. switchFileDrop.setOn(false, animated: false)
  260. networking?.updateShare(idShare: tableShare.idShare, password: nil, permissions: permissions, note: nil, label: nil, expirationDate: nil, hideDownload: tableShare.hideDownload)
  261. } else {
  262. sender.setOn(true, animated: false)
  263. }
  264. }
  265. // File Drop (directory)
  266. @IBAction func switchFileDrop(sender: UISwitch) {
  267. guard let tableShare = self.tableShare else { return }
  268. let permissions = NCGlobal.shared.permissionCreateShare
  269. if sender.isOn && permissions != tableShare.permissions {
  270. switchReadOnly.setOn(false, animated: false)
  271. switchAllowUploadAndEditing.setOn(false, animated: false)
  272. networking?.updateShare(idShare: tableShare.idShare, password: nil, permissions: permissions, note: nil, label: nil, expirationDate: nil, hideDownload: tableShare.hideDownload)
  273. } else {
  274. sender.setOn(true, animated: false)
  275. }
  276. }
  277. // Hide download
  278. @IBAction func switchHideDownloadChanged(sender: UISwitch) {
  279. guard let tableShare = self.tableShare else { return }
  280. networking?.updateShare(idShare: tableShare.idShare, password: nil, permissions: tableShare.permissions, note: nil, label: nil, expirationDate: nil, hideDownload: sender.isOn)
  281. }
  282. // Password protect
  283. @IBAction func switchPasswordProtectChanged(sender: UISwitch) {
  284. guard let tableShare = self.tableShare else { return }
  285. if sender.isOn {
  286. fieldPasswordProtect.isEnabled = true
  287. fieldPasswordProtect.text = ""
  288. fieldPasswordProtect.becomeFirstResponder()
  289. } else {
  290. networking?.updateShare(idShare: tableShare.idShare, password: "", permissions: tableShare.permissions, note: nil, label: nil, expirationDate: nil, hideDownload: tableShare.hideDownload)
  291. }
  292. }
  293. @IBAction func fieldPasswordProtectDidEndOnExit(textField: UITextField) {
  294. guard let tableShare = self.tableShare else { return }
  295. networking?.updateShare(idShare: tableShare.idShare, password: fieldPasswordProtect.text, permissions: tableShare.permissions, note: nil, label: nil, expirationDate: nil, hideDownload: tableShare.hideDownload)
  296. }
  297. // Set expiration date
  298. @IBAction func switchSetExpirationDate(sender: UISwitch) {
  299. guard let tableShare = self.tableShare else { return }
  300. if sender.isOn {
  301. fieldSetExpirationDate.isEnabled = true
  302. fieldSetExpirationDate(sender: fieldSetExpirationDate)
  303. } else {
  304. networking?.updateShare(idShare: tableShare.idShare, password: nil, permissions: tableShare.permissions, note: nil, label: nil, expirationDate: "", hideDownload: tableShare.hideDownload)
  305. }
  306. }
  307. @IBAction func fieldSetExpirationDate(sender: UITextField) {
  308. let calendar = NCShareCommon.shared.openCalendar(view: self, width: width, height: height)
  309. calendar.calendarView.delegate = self
  310. self.calendar = calendar.calendarView
  311. viewWindowCalendar = calendar.viewWindow
  312. let tap = UITapGestureRecognizer(target: self, action: #selector(tapViewWindowCalendar))
  313. tap.delegate = self
  314. viewWindowCalendar?.addGestureRecognizer(tap)
  315. }
  316. // Note to recipient
  317. @IBAction func fieldNoteToRecipientDidEndOnExit(textField: UITextField) {
  318. guard let tableShare = self.tableShare else { return }
  319. if fieldNoteToRecipient.text == nil { return }
  320. networking?.updateShare(idShare: tableShare.idShare, password: nil, permissions: tableShare.permissions, note: fieldNoteToRecipient.text, label: nil, expirationDate: nil, hideDownload: tableShare.hideDownload)
  321. }
  322. // Label
  323. @IBAction func fielLabelDidEndOnExit(textField: UITextField) {
  324. guard let tableShare = self.tableShare else { return }
  325. if fieldLabel.text == nil { return }
  326. networking?.updateShare(idShare: tableShare.idShare, password: nil, permissions: tableShare.permissions, note: nil, label: fieldLabel.text, expirationDate: nil, hideDownload: tableShare.hideDownload)
  327. }
  328. // Delete share link
  329. @IBAction func buttonDeleteShareLink(sender: UIButton) {
  330. guard let tableShare = self.tableShare else { return }
  331. networking?.unShare(idShare: tableShare.idShare)
  332. }
  333. // Add another link
  334. @IBAction func buttonAddAnotherLink(sender: UIButton) {
  335. networking?.createShareLink(password: "")
  336. }
  337. // MARK: - Delegate networking
  338. func readShareCompleted() {
  339. reloadData(idShare: tableShare?.idShare ?? 0)
  340. }
  341. func shareCompleted() {
  342. unLoad()
  343. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterReloadDataNCShare)
  344. }
  345. func unShareCompleted() {
  346. unLoad()
  347. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterReloadDataNCShare)
  348. }
  349. func updateShareWithError(idShare: Int) {
  350. reloadData(idShare: idShare)
  351. }
  352. func getSharees(sharees: [NCCommunicationSharee]?) { }
  353. // MARK: - Delegate calendar
  354. func calendar(_ calendar: FSCalendar, didSelect date: Date, at monthPosition: FSCalendarMonthPosition) {
  355. if monthPosition == .previous || monthPosition == .next {
  356. calendar.setCurrentPage(date, animated: true)
  357. } else {
  358. let dateFormatter = DateFormatter()
  359. dateFormatter.formatterBehavior = .behavior10_4
  360. dateFormatter.dateStyle = .medium
  361. fieldSetExpirationDate.text = dateFormatter.string(from:date)
  362. fieldSetExpirationDate.endEditing(true)
  363. viewWindowCalendar?.removeFromSuperview()
  364. guard let tableShare = self.tableShare else { return }
  365. dateFormatter.dateFormat = "YYYY-MM-dd HH:mm:ss"
  366. let expirationDate = dateFormatter.string(from: date)
  367. networking?.updateShare(idShare: tableShare.idShare, password: nil, permissions: tableShare.permissions, note: nil, label: nil, expirationDate: expirationDate, hideDownload: tableShare.hideDownload)
  368. }
  369. }
  370. func calendar(_ calendar: FSCalendar, shouldSelect date: Date, at monthPosition: FSCalendarMonthPosition) -> Bool {
  371. return date > Date()
  372. }
  373. func calendar(_ calendar: FSCalendar, appearance: FSCalendarAppearance, titleDefaultColorFor date: Date) -> UIColor? {
  374. if date > Date() {
  375. return UIColor(red: 60/255, green: 60/255, blue: 60/255, alpha: 1)
  376. } else {
  377. return UIColor(red: 190/255, green: 190/255, blue: 190/255, alpha: 1)
  378. }
  379. }
  380. }