NCShareLinkMenuView.swift 19 KB

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