NCCapabilitiesView.swift 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. @objc class NCHostingCapabilitiesView: NSObject {
  10. @objc func makeShipDetailsUI() -> UIViewController {
  11. let capabilitiesStatus = NCCapabilitiesStatus()
  12. let view = NCCapabilitiesView(capabilitiesStatus: capabilitiesStatus)
  13. let vc = UIHostingController(rootView: view)
  14. vc.title = NSLocalizedString("_capabilities_", comment: "")
  15. return vc
  16. }
  17. }
  18. class NCCapabilitiesStatus: ObservableObject {
  19. struct Capability: Identifiable, Hashable {
  20. let id = UUID()
  21. let text: String
  22. let image: UIImage
  23. let available: Bool
  24. }
  25. @Published var capabililies: [Capability] = []
  26. init(preview: Bool = false) {
  27. // if preview {
  28. capabililies = [Capability(text: "File sharing", image: UIImage(named: "share")!, available: true),
  29. Capability(text: "Externa site", image: UIImage(systemName: "network")!, available: false)
  30. ]
  31. // }
  32. }
  33. }
  34. struct NCCapabilitiesView: View {
  35. @ObservedObject var capabilitiesStatus: NCCapabilitiesStatus
  36. init(capabilitiesStatus: NCCapabilitiesStatus) {
  37. self.capabilitiesStatus = capabilitiesStatus
  38. }
  39. var body: some View {
  40. VStack {
  41. List {
  42. ForEach(capabilitiesStatus.capabililies, id: \.id) { capability in
  43. HStack {
  44. Capability(text: capability.text, image: Image(uiImage: capability.image))
  45. CapabilityAvailable(available: capability.available)
  46. }
  47. .complexModifier { view in
  48. if #available(iOS 15, *) {
  49. view.listRowSeparator(.hidden)
  50. }
  51. }
  52. }
  53. }
  54. }
  55. .frame(maxWidth: .infinity, alignment: .top)
  56. }
  57. }
  58. struct Capability: View {
  59. @State var text: String = ""
  60. @State var image: Image
  61. var body: some View {
  62. Label {
  63. Text(text)
  64. .font(.system(size: 15))
  65. .foregroundColor(Color(UIColor.systemGray))
  66. } icon: {
  67. image
  68. .renderingMode(.template)
  69. .resizable()
  70. .frame(width: 25.0, height: 25.0)
  71. .foregroundColor(Color(UIColor.systemGray))
  72. }
  73. .frame(maxWidth: .infinity, alignment: .leading)
  74. }
  75. }
  76. struct CapabilityAvailable: View {
  77. @State private var text: String
  78. init(available: Bool) {
  79. if available {
  80. _text = State(initialValue: NSLocalizedString("_available_", comment: ""))
  81. } else {
  82. _text = State(initialValue: NSLocalizedString("_not_available_", comment: ""))
  83. }
  84. }
  85. var body: some View {
  86. Text(text)
  87. .frame(width: 100)
  88. .font(.system(size: 12))
  89. .padding(EdgeInsets(top: 7, leading: 12, bottom: 7, trailing: 12))
  90. .foregroundColor(.primary)
  91. .background(
  92. RoundedRectangle(cornerRadius: 20)
  93. .stroke(Color(UIColor.systemGray), lineWidth: 0.5)
  94. .background(
  95. RoundedRectangle(cornerRadius: 20)
  96. .fill(Color(UIColor.secondarySystemBackground))
  97. )
  98. )
  99. .frame(maxWidth: .infinity, alignment: .trailing)
  100. }
  101. }
  102. struct NCCapabilitiesView_Previews: PreviewProvider {
  103. static var previews: some View {
  104. NCCapabilitiesView(capabilitiesStatus: NCCapabilitiesStatus(preview: true))
  105. }
  106. }