NCCapabilitiesView.swift 8.2 KB

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