NCCapabilitiesView.swift 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. //
  2. // NCCapabilitiesView.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 19/05/23.
  6. // Copyright © 2023 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. //
  23. import SwiftUI
  24. import NextcloudKit
  25. @objc class NCHostingCapabilitiesView: NSObject {
  26. @objc func makeShipDetailsUI() -> UIViewController {
  27. let capabilitiesStatus = NCCapabilitiesViewOO()
  28. let view = NCCapabilitiesView(capabilitiesStatus: capabilitiesStatus)
  29. let vc = UIHostingController(rootView: view)
  30. vc.title = NSLocalizedString("_capabilities_", comment: "")
  31. return vc
  32. }
  33. }
  34. class NCCapabilitiesViewOO: ObservableObject {
  35. struct Capability: Identifiable, Hashable {
  36. let id = UUID()
  37. let text: String
  38. let image: UIImage
  39. let resize: Bool
  40. let available: Bool
  41. }
  42. @Published var capabililies: [Capability] = []
  43. @Published var homeServer = ""
  44. let utilityFileSystem = NCUtilityFileSystem()
  45. let utility = NCUtility()
  46. init() {
  47. loadCapabilities()
  48. NotificationCenter.default.addObserver(forName: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterChangeUser), object: nil, queue: nil) { _ in
  49. self.loadCapabilities()
  50. }
  51. }
  52. func loadCapabilities() {
  53. guard let activeAccount = NCManageDatabase.shared.getActiveAccount() else { return }
  54. var textEditor = false
  55. var onlyofficeEditors = false
  56. capabililies.removeAll()
  57. var image = utility.loadImage(named: "person.fill.badge.plus")
  58. capabililies.append(Capability(text: "File sharing", image: image, resize: false, available: NCGlobal.shared.capabilityFileSharingApiEnabled))
  59. image = utility.loadImage(named: "network")
  60. capabililies.append(Capability(text: "External site", image: image, resize: false, available: NCGlobal.shared.capabilityExternalSites))
  61. image = utility.loadImage(named: "lock")
  62. capabililies.append(Capability(text: "End-to-End Encryption", image: image, resize: false, available: NCGlobal.shared.capabilityE2EEEnabled))
  63. image = utility.loadImage(named: "bolt")
  64. capabililies.append(Capability(text: "Activity", image: image, resize: false, available: !NCGlobal.shared.capabilityActivity.isEmpty))
  65. image = utility.loadImage(named: "bell")
  66. capabililies.append(Capability(text: "Notification", image: image, resize: false, available: !NCGlobal.shared.capabilityNotification.isEmpty))
  67. image = utility.loadImage(named: "trash")
  68. capabililies.append(Capability(text: "Deleted files", image: image, resize: false, available: NCGlobal.shared.capabilityFilesUndelete))
  69. if let editors = NCManageDatabase.shared.getDirectEditingEditors(account: activeAccount.account) {
  70. for editor in editors {
  71. if editor.editor == NCGlobal.shared.editorText {
  72. textEditor = true
  73. } else if editor.editor == NCGlobal.shared.editorOnlyoffice {
  74. onlyofficeEditors = true
  75. }
  76. }
  77. }
  78. capabililies.append(Capability(text: "Text", image: utility.loadImage(named: "doc.text"), resize: false, available: textEditor))
  79. capabililies.append(Capability(text: "ONLYOFFICE", image: utility.loadImage(named: "onlyoffice"), resize: true, available: onlyofficeEditors))
  80. capabililies.append(Capability(text: "Collabora", image: utility.loadImage(named: "collabora"), resize: true, available: NCGlobal.shared.capabilityRichDocumentsEnabled))
  81. capabililies.append(Capability(text: "User Status", image: utility.loadImage(named: "moon"), resize: false, available: NCGlobal.shared.capabilityUserStatusEnabled))
  82. capabililies.append(Capability(text: "Comments", image: utility.loadImage(named: "ellipsis.bubble"), resize: false, available: NCGlobal.shared.capabilityFilesComments))
  83. capabililies.append(Capability(text: "Lock file", image: utility.loadImage(named: "lock"), resize: false, available: !NCGlobal.shared.capabilityFilesLockVersion.isEmpty))
  84. capabililies.append(Capability(text: "Group folders", image: utility.loadImage(named: "person.2"), resize: false, available: NCGlobal.shared.capabilityGroupfoldersEnabled))
  85. if NCBrandOptions.shared.brand != "Nextcloud" {
  86. capabililies.append(Capability(text: "Security Guard Diagnostics", image: utility.loadImage(named: "shield"), resize: false, available: NCGlobal.shared.capabilitySecurityGuardDiagnostics))
  87. }
  88. capabililies.append(Capability(text: "Assistant", image: utility.loadImage(named: "sparkles"), resize: false, available: NCGlobal.shared.capabilityAssistantEnabled))
  89. homeServer = utilityFileSystem.getHomeServer(urlBase: activeAccount.urlBase, userId: activeAccount.userId) + "/"
  90. }
  91. }
  92. struct NCCapabilitiesView: View {
  93. @ObservedObject var capabilitiesViewOO: NCCapabilitiesViewOO
  94. init(capabilitiesStatus: NCCapabilitiesViewOO) {
  95. self.capabilitiesViewOO = capabilitiesStatus
  96. }
  97. var body: some View {
  98. VStack {
  99. List {
  100. Section {
  101. ForEach(capabilitiesViewOO.capabililies, id: \.id) { capability in
  102. HStack {
  103. CapabilityName(text: Binding.constant(capability.text), image: Image(uiImage: capability.image), resize: capability.resize)
  104. CapabilityStatus(available: capability.available)
  105. }
  106. }
  107. }
  108. Section {
  109. CapabilityName(text: $capabilitiesViewOO.homeServer, image: Image(uiImage: NCUtility().loadImage(named: "house")), resize: false)
  110. }
  111. }
  112. }
  113. .frame(maxWidth: .infinity, alignment: .top)
  114. }
  115. struct CapabilityName: View {
  116. @Binding var text: String
  117. @State var image: Image
  118. @State var resize: Bool
  119. var body: some View {
  120. Label {
  121. Text(text)
  122. .font(.system(size: 15))
  123. } icon: {
  124. if resize {
  125. image
  126. .renderingMode(.template)
  127. .resizable()
  128. .scaledToFill()
  129. .frame(width: 23.0, height: 23.0)
  130. .foregroundColor(.primary)
  131. } else {
  132. image
  133. .renderingMode(.template)
  134. .foregroundColor(.primary)
  135. }
  136. }
  137. .frame(maxWidth: .infinity, alignment: .leading)
  138. }
  139. }
  140. struct CapabilityStatus: View {
  141. @State var available: Bool
  142. var body: some View {
  143. if available {
  144. Image(systemName: "checkmark.circle.fill")
  145. .foregroundColor(.green)
  146. } else {
  147. Image(systemName: "multiply.circle.fill")
  148. .foregroundColor(Color(NCBrandColor.shared.textColor2))
  149. }
  150. }
  151. }
  152. }
  153. #Preview {
  154. func getCapabilitiesViewOOForPreview() -> NCCapabilitiesViewOO {
  155. let capabilitiesViewOO = NCCapabilitiesViewOO()
  156. capabilitiesViewOO.capabililies = [
  157. NCCapabilitiesViewOO.Capability(text: "Collabora", image: UIImage(named: "collabora")!, resize: true, available: true),
  158. NCCapabilitiesViewOO.Capability(text: "XXX site", image: UIImage(systemName: "lock.shield")!, resize: false, available: false)
  159. ]
  160. capabilitiesViewOO.homeServer = "https://cloud.nextcloud.com/remote.php.dav/files/marino/"
  161. return capabilitiesViewOO
  162. }
  163. return NCCapabilitiesView(capabilitiesStatus: getCapabilitiesViewOOForPreview())
  164. }