NCCapabilitiesView.swift 7.2 KB

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