NCShareUserMenuView.swift 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. //
  2. // NCShareUserMenuView.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 NCShareUserMenuView: UIView, UIGestureRecognizerDelegate, NCShareNetworkingDelegate, FSCalendarDelegate, FSCalendarDelegateAppearance {
  26. @IBOutlet weak var switchCanReshare: UISwitch!
  27. @IBOutlet weak var labelCanReshare: UILabel!
  28. @IBOutlet weak var switchCanCreate: UISwitch!
  29. @IBOutlet weak var labelCanCreate: UILabel!
  30. @IBOutlet weak var switchCanChange: UISwitch!
  31. @IBOutlet weak var labelCanChange: UILabel!
  32. @IBOutlet weak var switchCanDelete: UISwitch!
  33. @IBOutlet weak var labelCanDelete: UILabel!
  34. @IBOutlet weak var switchSetExpirationDate: UISwitch!
  35. @IBOutlet weak var labelSetExpirationDate: UILabel!
  36. @IBOutlet weak var fieldSetExpirationDate: UITextField!
  37. @IBOutlet weak var imageNoteToRecipient: UIImageView!
  38. @IBOutlet weak var labelNoteToRecipient: UILabel!
  39. @IBOutlet weak var fieldNoteToRecipient: UITextField!
  40. @IBOutlet weak var buttonUnshare: UIButton!
  41. @IBOutlet weak var labelUnshare: UILabel!
  42. @IBOutlet weak var imageUnshare: UIImageView!
  43. private let appDelegate = UIApplication.shared.delegate as! AppDelegate
  44. var width: CGFloat = 0
  45. var height: CGFloat = 0
  46. private var tableShare: tableShare?
  47. var metadata: tableMetadata?
  48. var shareViewController: NCShare?
  49. private var networking: NCShareNetworking?
  50. var viewWindow: UIView?
  51. var viewWindowCalendar: UIView?
  52. private var calendar: FSCalendar?
  53. private var selfFrameOriginYDiff: CGFloat = 0
  54. override func awakeFromNib() {
  55. layer.borderColor = UIColor.lightGray.cgColor
  56. layer.borderWidth = 0.5
  57. layer.cornerRadius = 5
  58. layer.masksToBounds = false
  59. layer.shadowOffset = CGSize(width: 2, height: 2)
  60. layer.shadowOpacity = 0.2
  61. switchCanReshare.transform = CGAffineTransform(scaleX: 0.75, y: 0.75)
  62. switchCanReshare.onTintColor = NCBrandColor.shared.brandElement
  63. switchCanCreate?.transform = CGAffineTransform(scaleX: 0.75, y: 0.75)
  64. switchCanCreate?.onTintColor = NCBrandColor.shared.brandElement
  65. switchCanChange?.transform = CGAffineTransform(scaleX: 0.75, y: 0.75)
  66. switchCanChange?.onTintColor = NCBrandColor.shared.brandElement
  67. switchCanDelete?.transform = CGAffineTransform(scaleX: 0.75, y: 0.75)
  68. switchCanDelete?.onTintColor = NCBrandColor.shared.brandElement
  69. switchSetExpirationDate.transform = CGAffineTransform(scaleX: 0.75, y: 0.75)
  70. switchSetExpirationDate.onTintColor = NCBrandColor.shared.brandElement
  71. labelCanReshare?.text = NSLocalizedString("_share_can_reshare_", comment: "")
  72. labelCanReshare?.textColor = NCBrandColor.shared.label
  73. labelCanCreate?.text = NSLocalizedString("_share_can_create_", comment: "")
  74. labelCanCreate?.textColor = NCBrandColor.shared.label
  75. labelCanChange?.text = NSLocalizedString("_share_can_change_", comment: "")
  76. labelCanChange?.textColor = NCBrandColor.shared.label
  77. labelCanDelete?.text = NSLocalizedString("_share_can_delete_", comment: "")
  78. labelCanDelete?.textColor = NCBrandColor.shared.label
  79. labelSetExpirationDate?.text = NSLocalizedString("_share_expiration_date_", comment: "")
  80. labelSetExpirationDate?.textColor = NCBrandColor.shared.label
  81. labelNoteToRecipient?.text = NSLocalizedString("_share_note_recipient_", comment: "")
  82. labelNoteToRecipient?.textColor = NCBrandColor.shared.label
  83. labelUnshare?.text = NSLocalizedString("_share_unshare_", comment: "")
  84. labelUnshare?.textColor = NCBrandColor.shared.label
  85. fieldSetExpirationDate.inputView = UIView()
  86. imageNoteToRecipient.image = UIImage.init(named: "file_txt")!.image(color: UIColor(red: 76/255, green: 76/255, blue: 76/255, alpha: 1), size: 50)
  87. imageUnshare.image = NCUtility.shared.loadImage(named: "trash", color: UIColor(red: 76/255, green: 76/255, blue: 76/255, alpha: 1), size: 50)
  88. NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(_:)), name: UIResponder.keyboardWillShowNotification, object: nil)
  89. NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(_:)), name: UIResponder.keyboardWillHideNotification, object: nil)
  90. }
  91. override func willMove(toWindow newWindow: UIWindow?) {
  92. super.willMove(toWindow: newWindow)
  93. if newWindow == nil {
  94. // UIView disappear
  95. shareViewController?.reloadData()
  96. } else {
  97. // UIView appear
  98. networking = NCShareNetworking.init(metadata: metadata!, urlBase: appDelegate.urlBase, view: self, delegate: self)
  99. }
  100. }
  101. func unLoad() {
  102. viewWindowCalendar?.removeFromSuperview()
  103. viewWindow?.removeFromSuperview()
  104. viewWindowCalendar = nil
  105. viewWindow = nil
  106. }
  107. func reloadData(idShare: Int) {
  108. guard let metadata = self.metadata else { return }
  109. tableShare = NCManageDatabase.shared.getTableShare(account: metadata.account, idShare: idShare)
  110. guard let tableShare = self.tableShare else { return }
  111. // Can reshare (file)
  112. let canReshare = CCUtility.isPermission(toCanShare: tableShare.permissions)
  113. switchCanReshare.setOn(canReshare, animated: false)
  114. if metadata.directory {
  115. // Can create (folder)
  116. let canCreate = CCUtility.isPermission(toCanCreate: tableShare.permissions)
  117. switchCanCreate.setOn(canCreate, animated: false)
  118. // Can change (folder)
  119. let canChange = CCUtility.isPermission(toCanChange: tableShare.permissions)
  120. switchCanChange.setOn(canChange, animated: false)
  121. // Can delete (folder)
  122. let canDelete = CCUtility.isPermission(toCanDelete: tableShare.permissions)
  123. switchCanDelete.setOn(canDelete, animated: false)
  124. }
  125. // Set expiration date
  126. if tableShare.expirationDate != nil {
  127. switchSetExpirationDate.setOn(true, animated: false)
  128. fieldSetExpirationDate.isEnabled = true
  129. let dateFormatter = DateFormatter()
  130. dateFormatter.formatterBehavior = .behavior10_4
  131. dateFormatter.dateStyle = .medium
  132. fieldSetExpirationDate.text = dateFormatter.string(from: tableShare.expirationDate! as Date)
  133. } else {
  134. switchSetExpirationDate.setOn(false, animated: false)
  135. fieldSetExpirationDate.isEnabled = false
  136. fieldSetExpirationDate.text = ""
  137. }
  138. // Note to recipient
  139. fieldNoteToRecipient.text = tableShare.note
  140. }
  141. // MARK: - Keyboard notification
  142. @objc internal func keyboardWillShow(_ notification : Notification?) {
  143. selfFrameOriginYDiff = 0
  144. if let info = notification?.userInfo, let centerObject = fieldNoteToRecipient.superview?.convert(fieldNoteToRecipient.center, to: nil) {
  145. let frameEndUserInfoKey = UIResponder.keyboardFrameEndUserInfoKey
  146. if let keyboardFrame = info[frameEndUserInfoKey] as? CGRect {
  147. let diff = keyboardFrame.origin.y - centerObject.y - (fieldNoteToRecipient.frame.height / 2)
  148. if diff < 0 {
  149. selfFrameOriginYDiff = diff
  150. self.frame.origin.y += selfFrameOriginYDiff
  151. }
  152. }
  153. }
  154. }
  155. @objc func keyboardWillHide(_ notification: Notification) {
  156. self.frame.origin.y -= selfFrameOriginYDiff
  157. }
  158. // MARK: - Tap viewWindowCalendar
  159. @objc func tapViewWindowCalendar(gesture: UITapGestureRecognizer) {
  160. calendar?.removeFromSuperview()
  161. viewWindowCalendar?.removeFromSuperview()
  162. calendar = nil
  163. viewWindowCalendar = nil
  164. reloadData(idShare: tableShare?.idShare ?? 0)
  165. }
  166. func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
  167. return gestureRecognizer.view == touch.view
  168. }
  169. // MARK: - IBAction
  170. // Can reshare
  171. @IBAction func switchCanReshareChanged(sender: UISwitch) {
  172. guard let tableShare = self.tableShare else { return }
  173. guard let metadata = self.metadata else { return }
  174. let canEdit = CCUtility.isAnyPermission(toEdit: tableShare.permissions)
  175. let canCreate = CCUtility.isPermission(toCanCreate: tableShare.permissions)
  176. let canChange = CCUtility.isPermission(toCanChange: tableShare.permissions)
  177. let canDelete = CCUtility.isPermission(toCanDelete: tableShare.permissions)
  178. var permissions: Int = 0
  179. if metadata.directory {
  180. permissions = CCUtility.getPermissionsValue(byCanEdit: canEdit, andCanCreate: canCreate, andCanChange: canChange, andCanDelete: canDelete, andCanShare: sender.isOn, andIsFolder: metadata.directory)
  181. } else {
  182. if sender.isOn {
  183. if canEdit {
  184. permissions = CCUtility.getPermissionsValue(byCanEdit: true, andCanCreate: true, andCanChange: true, andCanDelete: true, andCanShare: sender.isOn, andIsFolder: metadata.directory)
  185. } else {
  186. permissions = CCUtility.getPermissionsValue(byCanEdit: false, andCanCreate: false, andCanChange: false, andCanDelete: false, andCanShare: sender.isOn, andIsFolder: metadata.directory)
  187. }
  188. } else {
  189. if canEdit {
  190. permissions = CCUtility.getPermissionsValue(byCanEdit: true, andCanCreate: true, andCanChange: true, andCanDelete: true, andCanShare: sender.isOn, andIsFolder: metadata.directory)
  191. } else {
  192. permissions = CCUtility.getPermissionsValue(byCanEdit: false, andCanCreate: false, andCanChange: false, andCanDelete: false, andCanShare: sender.isOn, andIsFolder: metadata.directory)
  193. }
  194. }
  195. }
  196. networking?.updateShare(idShare: tableShare.idShare, password: nil, permissions: permissions, note: nil, label: nil, expirationDate: nil, hideDownload: tableShare.hideDownload)
  197. }
  198. @IBAction func switchCanCreate(sender: UISwitch) {
  199. guard let tableShare = self.tableShare else { return }
  200. guard let metadata = self.metadata else { return }
  201. let canEdit = CCUtility.isAnyPermission(toEdit: tableShare.permissions)
  202. let canChange = CCUtility.isPermission(toCanChange: tableShare.permissions)
  203. let canDelete = CCUtility.isPermission(toCanDelete: tableShare.permissions)
  204. let canShare = CCUtility.isPermission(toCanShare: tableShare.permissions)
  205. let permissions = CCUtility.getPermissionsValue(byCanEdit: canEdit, andCanCreate: sender.isOn, andCanChange: canChange, andCanDelete: canDelete, andCanShare: canShare, andIsFolder: metadata.directory)
  206. networking?.updateShare(idShare: tableShare.idShare, password: nil, permissions: permissions, note: nil, label: nil, expirationDate: nil, hideDownload: tableShare.hideDownload)
  207. }
  208. @IBAction func switchCanChange(sender: UISwitch) {
  209. guard let tableShare = self.tableShare else { return }
  210. guard let metadata = self.metadata else { return }
  211. let canEdit = CCUtility.isAnyPermission(toEdit: tableShare.permissions)
  212. let canCreate = CCUtility.isPermission(toCanCreate: tableShare.permissions)
  213. let canDelete = CCUtility.isPermission(toCanDelete: tableShare.permissions)
  214. let canShare = CCUtility.isPermission(toCanShare: tableShare.permissions)
  215. let permissions = CCUtility.getPermissionsValue(byCanEdit: canEdit, andCanCreate: canCreate, andCanChange: sender.isOn, andCanDelete: canDelete, andCanShare: canShare, andIsFolder: metadata.directory)
  216. networking?.updateShare(idShare: tableShare.idShare, password: nil, permissions: permissions, note: nil, label: nil, expirationDate: nil, hideDownload: tableShare.hideDownload)
  217. }
  218. @IBAction func switchCanDelete(sender: UISwitch) {
  219. guard let tableShare = self.tableShare else { return }
  220. guard let metadata = self.metadata else { return }
  221. let canEdit = CCUtility.isAnyPermission(toEdit: tableShare.permissions)
  222. let canCreate = CCUtility.isPermission(toCanCreate: tableShare.permissions)
  223. let canChange = CCUtility.isPermission(toCanChange: tableShare.permissions)
  224. let canShare = CCUtility.isPermission(toCanShare: tableShare.permissions)
  225. let permissions = CCUtility.getPermissionsValue(byCanEdit: canEdit, andCanCreate: canCreate, andCanChange: canChange, andCanDelete: sender.isOn, andCanShare: canShare, andIsFolder: metadata.directory)
  226. networking?.updateShare(idShare: tableShare.idShare, password: nil, permissions: permissions, note: nil, label: nil, expirationDate: nil, hideDownload: tableShare.hideDownload)
  227. }
  228. // Set expiration date
  229. @IBAction func switchSetExpirationDate(sender: UISwitch) {
  230. guard let tableShare = self.tableShare else { return }
  231. if sender.isOn {
  232. fieldSetExpirationDate.isEnabled = true
  233. fieldSetExpirationDate(sender: fieldSetExpirationDate)
  234. } else {
  235. networking?.updateShare(idShare: tableShare.idShare, password: nil, permissions: tableShare.permissions, note: nil, label: nil, expirationDate: "", hideDownload: tableShare.hideDownload)
  236. }
  237. }
  238. @IBAction func fieldSetExpirationDate(sender: UITextField) {
  239. let calendar = NCShareCommon.shared.openCalendar(view: self, width: width, height: height)
  240. calendar.calendarView.delegate = self
  241. self.calendar = calendar.calendarView
  242. viewWindowCalendar = calendar.viewWindow
  243. let tap = UITapGestureRecognizer(target: self, action: #selector(tapViewWindowCalendar))
  244. tap.delegate = self
  245. viewWindowCalendar?.addGestureRecognizer(tap)
  246. }
  247. // Note to recipient
  248. @IBAction func fieldNoteToRecipientDidEndOnExit(textField: UITextField) {
  249. guard let tableShare = self.tableShare else { return }
  250. if fieldNoteToRecipient.text == nil { return }
  251. networking?.updateShare(idShare: tableShare.idShare, password: nil, permissions: tableShare.permissions, note: fieldNoteToRecipient.text, label: nil, expirationDate: nil, hideDownload: tableShare.hideDownload)
  252. }
  253. // Unshare
  254. @IBAction func buttonUnshare(sender: UIButton) {
  255. guard let tableShare = self.tableShare else { return }
  256. networking?.unShare(idShare: tableShare.idShare)
  257. }
  258. // MARK: - Delegate networking
  259. func readShareCompleted() {
  260. reloadData(idShare: tableShare?.idShare ?? 0)
  261. }
  262. func shareCompleted() {
  263. unLoad()
  264. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterReloadDataNCShare)
  265. }
  266. func unShareCompleted() {
  267. unLoad()
  268. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterReloadDataNCShare)
  269. }
  270. func updateShareWithError(idShare: Int) {
  271. reloadData(idShare: idShare)
  272. }
  273. func getSharees(sharees: [NCCommunicationSharee]?) { }
  274. // MARK: - Delegate calendar
  275. func calendar(_ calendar: FSCalendar, didSelect date: Date, at monthPosition: FSCalendarMonthPosition) {
  276. if monthPosition == .previous || monthPosition == .next {
  277. calendar.setCurrentPage(date, animated: true)
  278. } else {
  279. let dateFormatter = DateFormatter()
  280. dateFormatter.formatterBehavior = .behavior10_4
  281. dateFormatter.dateStyle = .medium
  282. fieldSetExpirationDate.text = dateFormatter.string(from:date)
  283. fieldSetExpirationDate.endEditing(true)
  284. viewWindowCalendar?.removeFromSuperview()
  285. guard let tableShare = self.tableShare else { return }
  286. dateFormatter.dateFormat = "YYYY-MM-dd HH:mm:ss"
  287. let expirationDate = dateFormatter.string(from: date)
  288. networking?.updateShare(idShare: tableShare.idShare, password: nil, permissions: tableShare.permissions, note: nil, label: nil, expirationDate: expirationDate, hideDownload: tableShare.hideDownload)
  289. }
  290. }
  291. func calendar(_ calendar: FSCalendar, shouldSelect date: Date, at monthPosition: FSCalendarMonthPosition) -> Bool {
  292. return date > Date()
  293. }
  294. func calendar(_ calendar: FSCalendar, appearance: FSCalendarAppearance, titleDefaultColorFor date: Date) -> UIColor? {
  295. if date > Date() {
  296. return UIColor(red: 60/255, green: 60/255, blue: 60/255, alpha: 1)
  297. } else {
  298. return UIColor(red: 190/255, green: 190/255, blue: 190/255, alpha: 1)
  299. }
  300. }
  301. }