NCShareLinkMenuView.swift 19 KB

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