NCCapabilitiesView.swift 8.2 KB

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