NCShare+Helper.swift 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 isAttributeDownloadEnabled: Bool { get }
  41. }
  42. extension NCTableShareable {
  43. var expDateString: String? {
  44. guard let date = expirationDate else { return nil }
  45. let dateFormatter = DateFormatter()
  46. dateFormatter.dateFormat = "YYYY-MM-dd HH:mm:ss"
  47. return dateFormatter.string(from: date as Date)
  48. }
  49. func hasChanges(comparedTo other: NCTableShareable) -> Bool {
  50. return other.shareType != shareType
  51. || other.permissions != permissions
  52. || other.hideDownload != hideDownload
  53. || other.password != password
  54. || other.label != label
  55. || other.note != note
  56. || other.expirationDate != expirationDate
  57. }
  58. }
  59. class NCTableShareOptions: NCTableShareable {
  60. var shareType: Int
  61. var permissions: Int
  62. var idShare: Int = 0
  63. var shareWith: String = ""
  64. var hideDownload: Bool = false
  65. var password: String = ""
  66. var label: String = ""
  67. var note: String = ""
  68. var expirationDate: NSDate?
  69. var shareWithDisplayname: String = ""
  70. var isAttributeDownloadEnabled: Bool = true
  71. private init(shareType: Int, metadata: tableMetadata, password: String?) {
  72. if metadata.e2eEncrypted {
  73. self.permissions = NCGlobal.shared.permissionCreateShare
  74. } else {
  75. self.permissions = NCGlobal.shared.capabilityFileSharingDefaultPermission & metadata.sharePermissionsCollaborationServices
  76. }
  77. self.shareType = shareType
  78. if let password = password {
  79. self.password = password
  80. }
  81. }
  82. convenience init(sharee: NKSharee, metadata: tableMetadata, password: String?) {
  83. self.init(shareType: sharee.shareType, metadata: metadata, password: password)
  84. self.shareWith = sharee.shareWith
  85. }
  86. static func shareLink(metadata: tableMetadata, password: String?) -> NCTableShareOptions {
  87. return NCTableShareOptions(shareType: NCShareCommon.shared.SHARE_TYPE_LINK, metadata: metadata, password: password)
  88. }
  89. }
  90. protocol NCShareDetail {
  91. var share: NCTableShareable! { get }
  92. }
  93. extension NCShareDetail where Self: UIViewController {
  94. func setNavigationTitle() {
  95. title = NSLocalizedString("_share_", comment: "") + " – "
  96. if share.shareType == NCShareCommon.shared.SHARE_TYPE_LINK {
  97. title! += share.label.isEmpty ? NSLocalizedString("_share_link_", comment: "") : share.label
  98. } else {
  99. title! += share.shareWithDisplayname.isEmpty ? share.shareWith : share.shareWithDisplayname
  100. }
  101. }
  102. }