NCShareLinkMenuView.swift 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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. override func awakeFromNib() {
  60. layer.borderColor = UIColor.lightGray.cgColor
  61. layer.borderWidth = 0.5
  62. layer.cornerRadius = 5
  63. layer.masksToBounds = false
  64. layer.shadowOffset = CGSize(width: 2, height: 2)
  65. layer.shadowOpacity = 0.2
  66. switchAllowEditing?.transform = CGAffineTransform(scaleX: 0.75, y: 0.75)
  67. switchAllowEditing?.onTintColor = NCBrandColor.sharedInstance.brand
  68. switchReadOnly?.transform = CGAffineTransform(scaleX: 0.75, y: 0.75)
  69. switchReadOnly?.onTintColor = NCBrandColor.sharedInstance.brand
  70. switchAllowUploadAndEditing?.transform = CGAffineTransform(scaleX: 0.75, y: 0.75)
  71. switchAllowUploadAndEditing?.onTintColor = NCBrandColor.sharedInstance.brand
  72. switchFileDrop?.transform = CGAffineTransform(scaleX: 0.75, y: 0.75)
  73. switchFileDrop?.onTintColor = NCBrandColor.sharedInstance.brand
  74. switchHideDownload.transform = CGAffineTransform(scaleX: 0.75, y: 0.75)
  75. switchHideDownload.onTintColor = NCBrandColor.sharedInstance.brand
  76. switchPasswordProtect.transform = CGAffineTransform(scaleX: 0.75, y: 0.75)
  77. switchPasswordProtect.onTintColor = NCBrandColor.sharedInstance.brand
  78. switchSetExpirationDate.transform = CGAffineTransform(scaleX: 0.75, y: 0.75)
  79. switchSetExpirationDate.onTintColor = NCBrandColor.sharedInstance.brand
  80. fieldSetExpirationDate.inputView = UIView()
  81. 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))
  82. 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))
  83. 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))
  84. }
  85. override func willMove(toWindow newWindow: UIWindow?) {
  86. super.willMove(toWindow: newWindow)
  87. if newWindow == nil {
  88. // UIView disappear
  89. shareViewController?.reloadData()
  90. } else {
  91. // UIView appear
  92. networking = NCShareNetworking.init(metadata: metadata!, activeUrl: appDelegate.activeUrl, view: self, delegate: self)
  93. }
  94. }
  95. func unLoad() {
  96. viewWindowCalendar?.removeFromSuperview()
  97. viewWindow?.removeFromSuperview()
  98. viewWindowCalendar = nil
  99. viewWindow = nil
  100. }
  101. func reloadData(idRemoteShared: Int) {
  102. guard let metadata = self.metadata else { return }
  103. tableShare = NCManageDatabase.sharedInstance.getTableShare(account: metadata.account, idRemoteShared: idRemoteShared)
  104. guard let tableShare = self.tableShare else { return }
  105. if metadata.directory {
  106. // File Drop
  107. if tableShare.permissions == k_create_share_permission {
  108. switchReadOnly.setOn(false, animated: false)
  109. switchAllowUploadAndEditing.setOn(false, animated: false)
  110. switchFileDrop.setOn(true, animated: false)
  111. } else {
  112. // Read Only
  113. if UtilsFramework.isAnyPermission(toEdit: tableShare.permissions) {
  114. switchReadOnly.setOn(false, animated: false)
  115. switchAllowUploadAndEditing.setOn(true, animated: false)
  116. } else {
  117. switchReadOnly.setOn(true, animated: false)
  118. switchAllowUploadAndEditing.setOn(false, animated: false)
  119. }
  120. switchFileDrop.setOn(false, animated: false)
  121. }
  122. } else {
  123. // Allow editing
  124. if UtilsFramework.isAnyPermission(toEdit: tableShare.permissions) {
  125. switchAllowEditing.setOn(true, animated: false)
  126. } else {
  127. switchAllowEditing.setOn(false, animated: false)
  128. }
  129. }
  130. // Hide download
  131. if tableShare.hideDownload {
  132. switchHideDownload.setOn(true, animated: false)
  133. } else {
  134. switchHideDownload.setOn(false, animated: false)
  135. }
  136. // Password protect
  137. if tableShare.shareWith.count > 0 {
  138. switchPasswordProtect.setOn(true, animated: false)
  139. fieldPasswordProtect.isEnabled = true
  140. fieldPasswordProtect.text = tableShare.shareWith
  141. } else {
  142. switchPasswordProtect.setOn(false, animated: false)
  143. fieldPasswordProtect.isEnabled = false
  144. fieldPasswordProtect.text = ""
  145. }
  146. // Set expiration date
  147. if tableShare.expirationDate != nil {
  148. switchSetExpirationDate.setOn(true, animated: false)
  149. fieldSetExpirationDate.isEnabled = true
  150. let dateFormatter = DateFormatter()
  151. dateFormatter.formatterBehavior = .behavior10_4
  152. dateFormatter.dateStyle = .medium
  153. fieldSetExpirationDate.text = dateFormatter.string(from: tableShare.expirationDate! as Date)
  154. } else {
  155. switchSetExpirationDate.setOn(false, animated: false)
  156. fieldSetExpirationDate.isEnabled = false
  157. fieldSetExpirationDate.text = ""
  158. }
  159. // Note to recipient
  160. fieldNoteToRecipient.text = tableShare.note
  161. }
  162. // MARK: - IBAction
  163. // Allow editing (file)
  164. @IBAction func switchAllowEditingChanged(sender: UISwitch) {
  165. guard let tableShare = self.tableShare else { return }
  166. guard let metadata = self.metadata else { return }
  167. var permission: Int = 0
  168. if sender.isOn {
  169. permission = UtilsFramework.getPermissionsValue(byCanEdit: true, andCanCreate: true, andCanChange: true, andCanDelete: true, andCanShare: false, andIsFolder: metadata.directory)
  170. } else {
  171. permission = UtilsFramework.getPermissionsValue(byCanEdit: false, andCanCreate: false, andCanChange: false, andCanDelete: false, andCanShare: false, andIsFolder: metadata.directory)
  172. }
  173. networking?.updateShare(idRemoteShared: tableShare.idRemoteShared, password: nil, permission: permission, note: nil, expirationTime: nil, hideDownload: tableShare.hideDownload)
  174. }
  175. // Read Only (directory)
  176. @IBAction func switchReadOnly(sender: UISwitch) {
  177. guard let tableShare = self.tableShare else { return }
  178. guard let metadata = self.metadata else { return }
  179. let permission = UtilsFramework.getPermissionsValue(byCanEdit: false, andCanCreate: false, andCanChange: false, andCanDelete: false, andCanShare: false, andIsFolder: metadata.directory)
  180. if sender.isOn && permission != tableShare.permissions {
  181. switchAllowUploadAndEditing.setOn(false, animated: false)
  182. switchFileDrop.setOn(false, animated: false)
  183. networking?.updateShare(idRemoteShared: tableShare.idRemoteShared, password: nil, permission: permission, note: nil, expirationTime: nil, hideDownload: tableShare.hideDownload)
  184. } else {
  185. sender.setOn(true, animated: false)
  186. }
  187. }
  188. // Allow Upload And Editing (directory)
  189. @IBAction func switchAllowUploadAndEditing(sender: UISwitch) {
  190. guard let tableShare = self.tableShare else { return }
  191. guard let metadata = self.metadata else { return }
  192. let permission = UtilsFramework.getPermissionsValue(byCanEdit: true, andCanCreate: true, andCanChange: true, andCanDelete: true, andCanShare: false, andIsFolder: metadata.directory)
  193. if sender.isOn && permission != tableShare.permissions {
  194. switchReadOnly.setOn(false, animated: false)
  195. switchFileDrop.setOn(false, animated: false)
  196. networking?.updateShare(idRemoteShared: tableShare.idRemoteShared, password: nil, permission: permission, note: nil, expirationTime: nil, hideDownload: tableShare.hideDownload)
  197. } else {
  198. sender.setOn(true, animated: false)
  199. }
  200. }
  201. // File Drop (directory)
  202. @IBAction func switchFileDrop(sender: UISwitch) {
  203. guard let tableShare = self.tableShare else { return }
  204. let permission = Int(k_create_share_permission)
  205. if sender.isOn && permission != tableShare.permissions {
  206. switchReadOnly.setOn(false, animated: false)
  207. switchAllowUploadAndEditing.setOn(false, animated: false)
  208. networking?.updateShare(idRemoteShared: tableShare.idRemoteShared, password: nil, permission: permission, note: nil, expirationTime: nil, hideDownload: tableShare.hideDownload)
  209. } else {
  210. sender.setOn(true, animated: false)
  211. }
  212. }
  213. // Hide download
  214. @IBAction func switchHideDownloadChanged(sender: UISwitch) {
  215. guard let tableShare = self.tableShare else { return }
  216. networking?.updateShare(idRemoteShared: tableShare.idRemoteShared, password: nil, permission: 0, note: nil, expirationTime: nil, hideDownload: sender.isOn)
  217. }
  218. // Password protect
  219. @IBAction func switchPasswordProtectChanged(sender: UISwitch) {
  220. guard let tableShare = self.tableShare else { return }
  221. if sender.isOn {
  222. fieldPasswordProtect.isEnabled = true
  223. fieldPasswordProtect.text = ""
  224. fieldPasswordProtect.becomeFirstResponder()
  225. } else {
  226. networking?.updateShare(idRemoteShared: tableShare.idRemoteShared, password: "", permission: 0, note: nil, expirationTime: nil, hideDownload: tableShare.hideDownload)
  227. }
  228. }
  229. @IBAction func fieldPasswordProtectDidEndOnExit(textField: UITextField) {
  230. guard let tableShare = self.tableShare else { return }
  231. networking?.updateShare(idRemoteShared: tableShare.idRemoteShared, password: fieldPasswordProtect.text, permission: 0, note: nil, expirationTime: nil, hideDownload: tableShare.hideDownload)
  232. }
  233. // Set expiration date
  234. @IBAction func switchSetExpirationDate(sender: UISwitch) {
  235. guard let tableShare = self.tableShare else { return }
  236. if sender.isOn {
  237. fieldSetExpirationDate.isEnabled = true
  238. fieldSetExpirationDate(sender: fieldSetExpirationDate)
  239. } else {
  240. networking?.updateShare(idRemoteShared: tableShare.idRemoteShared, password: nil, permission: 0, note: nil, expirationTime: "", hideDownload: tableShare.hideDownload)
  241. }
  242. }
  243. @IBAction func fieldSetExpirationDate(sender: UITextField) {
  244. let calendar = NCShareCommon.sharedInstance.openCalendar(view: self, width: width, height: height)
  245. calendar.calendarView.delegate = self
  246. viewWindowCalendar = calendar.viewWindow
  247. }
  248. // Note to recipient
  249. @IBAction func fieldNoteToRecipientDidEndOnExit(textField: UITextField) {
  250. guard let tableShare = self.tableShare else { return }
  251. if fieldNoteToRecipient.text == nil { return }
  252. networking?.updateShare(idRemoteShared: tableShare.idRemoteShared, password: nil, permission: 0, note: fieldNoteToRecipient.text, expirationTime: nil, hideDownload: tableShare.hideDownload)
  253. }
  254. // Delete share link
  255. @IBAction func buttonDeleteShareLink(sender: UIButton) {
  256. guard let tableShare = self.tableShare else { return }
  257. networking?.unShare(idRemoteShared: tableShare.idRemoteShared)
  258. }
  259. // Add another link
  260. @IBAction func buttonAddAnotherLink(sender: UIButton) {
  261. networking?.share(password: "", permission: 1, hideDownload: false)
  262. }
  263. // MARK: - Delegate networking
  264. func readShareCompleted() {
  265. reloadData(idRemoteShared: tableShare?.idRemoteShared ?? 0)
  266. }
  267. func shareCompleted() {
  268. unLoad()
  269. NotificationCenter.default.post(name: NSNotification.Name(rawValue: "reloadDataNCShare"), object: nil, userInfo: nil)
  270. }
  271. func unShareCompleted() {
  272. unLoad()
  273. NotificationCenter.default.post(name: NSNotification.Name(rawValue: "reloadDataNCShare"), object: nil, userInfo: nil)
  274. }
  275. func updateShareWithError(idRemoteShared: Int) {
  276. reloadData(idRemoteShared: idRemoteShared)
  277. }
  278. func getUserAndGroup(items: [OCShareUser]?) { }
  279. // MARK: - Delegate calendar
  280. func calendar(_ calendar: FSCalendar, didSelect date: Date, at monthPosition: FSCalendarMonthPosition) {
  281. if monthPosition == .previous || monthPosition == .next {
  282. calendar.setCurrentPage(date, animated: true)
  283. } else {
  284. let dateFormatter = DateFormatter()
  285. dateFormatter.formatterBehavior = .behavior10_4
  286. dateFormatter.dateStyle = .medium
  287. fieldSetExpirationDate.text = dateFormatter.string(from:date)
  288. fieldSetExpirationDate.endEditing(true)
  289. viewWindowCalendar?.removeFromSuperview()
  290. guard let tableShare = self.tableShare else { return }
  291. guard let metadata = self.metadata else { return }
  292. dateFormatter.dateFormat = "YYYY-MM-dd"
  293. let expirationTime = dateFormatter.string(from: date)
  294. networking?.updateShare(idRemoteShared: tableShare.idRemoteShared, password: nil, permission: 0, note: nil, expirationTime: expirationTime, hideDownload: tableShare.hideDownload)
  295. }
  296. }
  297. func calendar(_ calendar: FSCalendar, shouldSelect date: Date, at monthPosition: FSCalendarMonthPosition) -> Bool {
  298. return date > Date()
  299. }
  300. func calendar(_ calendar: FSCalendar, appearance: FSCalendarAppearance, titleDefaultColorFor date: Date) -> UIColor? {
  301. if date > Date() {
  302. return UIColor(red: 60/255, green: 60/255, blue: 60/255, alpha: 1)
  303. } else {
  304. return UIColor(red: 190/255, green: 190/255, blue: 190/255, alpha: 1)
  305. }
  306. }
  307. }