NCCapabilitiesView.swift 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. import SwiftUI
  9. import NextcloudKit
  10. import PreviewSnapshots
  11. @objc class NCHostingCapabilitiesView: NSObject {
  12. @objc func makeShipDetailsUI() -> UIViewController {
  13. let capabilitiesStatus = NCCapabilitiesViewOO()
  14. let view = NCCapabilitiesView(capabilitiesStatus: capabilitiesStatus)
  15. let vc = UIHostingController(rootView: view)
  16. vc.title = NSLocalizedString("_capabilities_", comment: "")
  17. return vc
  18. }
  19. }
  20. class NCCapabilitiesViewOO: ObservableObject {
  21. struct Capability: Identifiable, Hashable {
  22. let id = UUID()
  23. let text: String
  24. let image: UIImage
  25. let resize: Bool
  26. let available: Bool
  27. }
  28. @Published var capabililies: [Capability] = []
  29. @Published var homeServer = ""
  30. init() {
  31. guard let activeAccount = NCManageDatabase.shared.getActiveAccount() else { return }
  32. var textEditor = false
  33. var onlyofficeEditors = false
  34. if let image = UIImage(named: "share") {
  35. capabililies.append(Capability(text: "File sharing", image: image, resize: true, available: NCGlobal.shared.capabilityFileSharingApiEnabled))
  36. }
  37. if let image = UIImage(systemName: "network") {
  38. capabililies.append(Capability(text: "External site", image: image, resize: false, available: NCGlobal.shared.capabilityExternalSites))
  39. }
  40. if let image = UIImage(systemName: "lock") {
  41. capabililies.append(Capability(text: "End-to-End Encryption", image: image, resize: false, available: NCGlobal.shared.capabilityE2EEEnabled))
  42. }
  43. if let image = UIImage(systemName: "bolt") {
  44. capabililies.append(Capability(text: "Activity", image: image, resize: false, available: !NCGlobal.shared.capabilityActivity.isEmpty))
  45. }
  46. if let image = UIImage(systemName: "bell") {
  47. capabililies.append(Capability(text: "Notification", image: image, resize: false, available: !NCGlobal.shared.capabilityNotification.isEmpty))
  48. }
  49. if let image = UIImage(systemName: "trash") {
  50. capabililies.append(Capability(text: "Deleted files", image: image, resize: false, available: NCGlobal.shared.capabilityFilesUndelete))
  51. }
  52. if let editors = NCManageDatabase.shared.getDirectEditingEditors(account: activeAccount.account) {
  53. for editor in editors {
  54. if editor.editor == NCGlobal.shared.editorText {
  55. textEditor = true
  56. } else if editor.editor == NCGlobal.shared.editorOnlyoffice {
  57. onlyofficeEditors = true
  58. }
  59. }
  60. }
  61. if let image = UIImage(systemName: "doc.text") {
  62. capabililies.append(Capability(text: "Text", image: image, resize: false, available: textEditor))
  63. }
  64. if let image = UIImage(named: "onlyoffice") {
  65. capabililies.append(Capability(text: "ONLYOFFICE", image: image, resize: true, available: onlyofficeEditors))
  66. }
  67. if let image = UIImage(named: "collabora") {
  68. capabililies.append(Capability(text: "Collabora", image: image, resize: true, available: !NCGlobal.shared.capabilityRichdocumentsMimetypes.isEmpty))
  69. }
  70. if let image = UIImage(systemName: "moon") {
  71. capabililies.append(Capability(text: "User Status", image: image, resize: false, available: NCGlobal.shared.capabilityUserStatusEnabled))
  72. }
  73. if let image = UIImage(systemName: "ellipsis.bubble") {
  74. capabililies.append(Capability(text: "Comments", image: image, resize: false, available: NCGlobal.shared.capabilityFilesComments))
  75. }
  76. if let image = UIImage(systemName: "lock") {
  77. capabililies.append(Capability(text: "Lock file", image: image, resize: false, available: !NCGlobal.shared.capabilityFilesLockVersion.isEmpty))
  78. }
  79. if let image = UIImage(systemName: "person.2") {
  80. capabililies.append(Capability(text: "Group folders", image: image, resize: false, available: NCGlobal.shared.capabilityGroupfoldersEnabled))
  81. }
  82. homeServer = NCUtilityFileSystem.shared.getHomeServer(urlBase: activeAccount.urlBase, userId: activeAccount.userId) + "/"
  83. }
  84. }
  85. struct NCCapabilitiesView: View {
  86. @ObservedObject var capabilitiesViewOO: NCCapabilitiesViewOO
  87. init(capabilitiesStatus: NCCapabilitiesViewOO) {
  88. self.capabilitiesViewOO = capabilitiesStatus
  89. }
  90. var body: some View {
  91. VStack {
  92. List {
  93. Section {
  94. ForEach(capabilitiesViewOO.capabililies, id: \.id) { capability in
  95. HStack {
  96. CapabilityName(text: capability.text, image: Image(uiImage: capability.image), resize: capability.resize)
  97. CapabilityStatus(available: capability.available)
  98. }
  99. }
  100. }
  101. Section {
  102. CapabilityName(text: capabilitiesViewOO.homeServer, image: Image(systemName: "house"), resize: false)
  103. }
  104. }
  105. }
  106. .frame(maxWidth: .infinity, alignment: .top)
  107. }
  108. struct CapabilityName: View {
  109. @State var text: String = ""
  110. @State var image: Image
  111. @State var resize: Bool
  112. var body: some View {
  113. Label {
  114. Text(text)
  115. .font(.system(size: 15))
  116. } icon: {
  117. if resize {
  118. image
  119. .renderingMode(.template)
  120. .resizable()
  121. .scaledToFill()
  122. .frame(width: 23.0, height: 23.0)
  123. .foregroundColor(.primary)
  124. } else {
  125. image
  126. .renderingMode(.template)
  127. .foregroundColor(.primary)
  128. }
  129. }
  130. .frame(maxWidth: .infinity, alignment: .leading)
  131. }
  132. }
  133. struct CapabilityStatus: View {
  134. @State var available: Bool
  135. var body: some View {
  136. if available {
  137. Image(systemName: "checkmark.circle.fill")
  138. .foregroundColor(.green)
  139. } else {
  140. Image(systemName: "multiply.circle.fill")
  141. .foregroundColor(.red)
  142. }
  143. }
  144. }
  145. }
  146. struct NCCapabilitiesView_Previews: PreviewProvider {
  147. static var previews: some View {
  148. snapshots.previews.previewLayout(.device)
  149. }
  150. static var snapshots: PreviewSnapshots<String> {
  151. PreviewSnapshots(
  152. configurations: [
  153. .init(name: NCGlobal.shared.defaultSnapshotConfiguration, state: "")
  154. ],
  155. configure: { _ in
  156. NCCapabilitiesView(capabilitiesStatus: getCapabilitiesViewOOForPreview()).padding(.top, 20).frameForPreview()
  157. })
  158. }
  159. }
  160. func getCapabilitiesViewOOForPreview() -> NCCapabilitiesViewOO {
  161. let capabilitiesViewOO = NCCapabilitiesViewOO()
  162. capabilitiesViewOO.capabililies = [
  163. NCCapabilitiesViewOO.Capability(text: "Collabora", image: UIImage(named: "collabora")!, resize: true, available: true),
  164. NCCapabilitiesViewOO.Capability(text: "XXX site", image: UIImage(systemName: "lock.shield")!, resize: false, available: false)
  165. ]
  166. capabilitiesViewOO.homeServer = "https://cloud.nextcloud.com/remote.php.dav/files/marino/"
  167. return capabilitiesViewOO
  168. }