NCShare+Helper.swift 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. //
  2. // NCShare+Helper.swift
  3. // Nextcloud
  4. //
  5. // Created by Henrik Storch on 19.03.22.
  6. // Copyright © 2022 Henrik Storch. All rights reserved.
  7. //
  8. // Author Henrik Storch <henrik.storch@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. //
  23. import UIKit
  24. import NextcloudKit
  25. extension tableShare: NCTableShareable { }
  26. extension NKShare: NCTableShareable {
  27. var isAttributeDownloadEnabled: Bool { return true }
  28. }
  29. protocol NCTableShareable: AnyObject {
  30. var shareType: Int { get set }
  31. var permissions: Int { get set }
  32. var idShare: Int { get set }
  33. var shareWith: String { get set }
  34. var hideDownload: Bool { get set }
  35. var password: String { get set }
  36. var label: String { get set }
  37. var note: String { get set }
  38. var expirationDate: NSDate? { get set }
  39. var shareWithDisplayname: String { get set }
  40. var attributes: String? { get set }
  41. var isAttributeDownloadEnabled: Bool { get }
  42. }
  43. extension NCTableShareable {
  44. var expDateString: String? {
  45. guard let date = expirationDate else { return nil }
  46. let dateFormatter = DateFormatter()
  47. dateFormatter.dateFormat = "YYYY-MM-dd HH:mm:ss"
  48. return dateFormatter.string(from: date as Date)
  49. }
  50. func hasChanges(comparedTo other: NCTableShareable) -> Bool {
  51. return other.shareType != shareType
  52. || other.permissions != permissions
  53. || other.hideDownload != hideDownload
  54. || other.password != password
  55. || other.label != label
  56. || other.note != note
  57. || other.expirationDate != expirationDate
  58. }
  59. }
  60. class NCTableShareOptions: NCTableShareable {
  61. var shareType: Int
  62. var permissions: Int
  63. var idShare: Int = 0
  64. var shareWith: String = ""
  65. var hideDownload: Bool = false
  66. var password: String = ""
  67. var label: String = ""
  68. var note: String = ""
  69. var expirationDate: NSDate?
  70. var shareWithDisplayname: String = ""
  71. var attributes: String?
  72. var isAttributeDownloadEnabled: Bool = true
  73. private init(shareType: Int, metadata: tableMetadata, password: String?) {
  74. if metadata.e2eEncrypted {
  75. self.permissions = NCGlobal.shared.permissionCreateShare
  76. } else {
  77. self.permissions = NCGlobal.shared.capabilityFileSharingDefaultPermission & metadata.sharePermissionsCollaborationServices
  78. }
  79. self.shareType = shareType
  80. if let password = password {
  81. self.password = password
  82. }
  83. }
  84. convenience init(sharee: NKSharee, metadata: tableMetadata, password: String?) {
  85. self.init(shareType: sharee.shareType, metadata: metadata, password: password)
  86. self.shareWith = sharee.shareWith
  87. }
  88. static func shareLink(metadata: tableMetadata, password: String?) -> NCTableShareOptions {
  89. return NCTableShareOptions(shareType: NCShareCommon.shared.SHARE_TYPE_LINK, metadata: metadata, password: password)
  90. }
  91. }
  92. protocol NCShareDetail {
  93. var share: NCTableShareable! { get }
  94. }
  95. extension NCShareDetail where Self: UIViewController {
  96. func setNavigationTitle() {
  97. title = NSLocalizedString("_share_", comment: "") + " – "
  98. if share.shareType == NCShareCommon.shared.SHARE_TYPE_LINK {
  99. title! += share.label.isEmpty ? NSLocalizedString("_share_link_", comment: "") : share.label
  100. } else {
  101. title! += share.shareWithDisplayname.isEmpty ? share.shareWith : share.shareWithDisplayname
  102. }
  103. }
  104. }