NCShareUserMenuView.swift 16 KB

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