FilesWidgetView.swift 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. //
  2. // FilesWidgetView.swift
  3. // Widget
  4. //
  5. // Created by Marino Faggiana on 25/08/22.
  6. // Copyright © 2022 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 WidgetKit
  25. struct FilesWidgetView: View {
  26. var entry: FilesDataEntry
  27. var body: some View {
  28. let parameterLink = "&user=\(entry.userId)&url=\(entry.url)"
  29. let linkNoAction: URL = URL(string: NCGlobal.shared.widgetActionNoAction + parameterLink) != nil ? URL(string: NCGlobal.shared.widgetActionNoAction + parameterLink)! : URL(string: NCGlobal.shared.widgetActionNoAction)!
  30. let linkActionUploadAsset: URL = URL(string: NCGlobal.shared.widgetActionUploadAsset + parameterLink) != nil ? URL(string: NCGlobal.shared.widgetActionUploadAsset + parameterLink)! : URL(string: NCGlobal.shared.widgetActionUploadAsset)!
  31. let linkActionScanDocument: URL = URL(string: NCGlobal.shared.widgetActionScanDocument + parameterLink) != nil ? URL(string: NCGlobal.shared.widgetActionScanDocument + parameterLink)! : URL(string: NCGlobal.shared.widgetActionScanDocument)!
  32. let linkActionTextDocument: URL = URL(string: NCGlobal.shared.widgetActionTextDocument + parameterLink) != nil ? URL(string: NCGlobal.shared.widgetActionTextDocument + parameterLink)! : URL(string: NCGlobal.shared.widgetActionTextDocument)!
  33. let linkActionVoiceMemo: URL = URL(string: NCGlobal.shared.widgetActionVoiceMemo + parameterLink) != nil ? URL(string: NCGlobal.shared.widgetActionVoiceMemo + parameterLink)! : URL(string: NCGlobal.shared.widgetActionVoiceMemo)!
  34. GeometryReader { geo in
  35. if entry.isEmpty {
  36. VStack(alignment: .center) {
  37. Image(systemName: "checkmark")
  38. .resizable()
  39. .scaledToFit()
  40. .frame(width: 50, height: 50)
  41. Text(NSLocalizedString("_no_items_", comment: ""))
  42. .font(.system(size: 25))
  43. .padding()
  44. Text(NSLocalizedString("_check_back_later_", comment: ""))
  45. .font(.system(size: 15))
  46. }
  47. .frame(width: geo.size.width, height: geo.size.height)
  48. }
  49. ZStack(alignment: .topLeading) {
  50. HStack {
  51. Text(entry.tile)
  52. .font(.system(size: 12))
  53. .fontWeight(.bold)
  54. .multilineTextAlignment(.center)
  55. .textCase(.uppercase)
  56. .lineLimit(1)
  57. }
  58. .frame(width: geo.size.width - 20)
  59. .padding([.top, .leading, .trailing], 10)
  60. if !entry.isEmpty {
  61. VStack(alignment: .leading) {
  62. VStack(spacing: 0) {
  63. ForEach(entry.datas, id: \.id) { element in
  64. Link(destination: element.url) {
  65. HStack {
  66. Image(uiImage: element.image)
  67. .resizable()
  68. .scaledToFill()
  69. .frame(width: 35, height: 35)
  70. .clipped()
  71. .cornerRadius(5)
  72. VStack(alignment: .leading, spacing: 2) {
  73. Text(element.title)
  74. .font(.system(size: 12))
  75. .fontWeight(.regular)
  76. Text(element.subTitle)
  77. .font(.system(size: CGFloat(10)))
  78. .foregroundColor(Color(.systemGray))
  79. }
  80. Spacer()
  81. }
  82. .padding(.leading, 10)
  83. .frame(height: 50)
  84. }
  85. Divider()
  86. .padding(.leading, 54)
  87. }
  88. }
  89. }
  90. .padding(.top, 30)
  91. .redacted(reason: entry.isPlaceholder ? .placeholder : [])
  92. }
  93. HStack(spacing: 0) {
  94. let sizeButton: CGFloat = 40
  95. Link(destination: entry.isPlaceholder ? linkNoAction : linkActionUploadAsset, label: {
  96. Image("addImage")
  97. .resizable()
  98. .renderingMode(.template)
  99. .foregroundColor(entry.isPlaceholder ? Color(.systemGray4) : Color(NCBrandColor.shared.brandText))
  100. .padding(11)
  101. .background(entry.isPlaceholder ? Color(.systemGray4) : Color(NCBrandColor.shared.brand))
  102. .clipShape(Circle())
  103. .scaledToFit()
  104. .frame(width: geo.size.width / 4, height: sizeButton)
  105. })
  106. Link(destination: entry.isPlaceholder ? linkNoAction : linkActionScanDocument, label: {
  107. Image(systemName: "doc.text.viewfinder")
  108. .resizable()
  109. .renderingMode(.template)
  110. .foregroundColor(entry.isPlaceholder ? Color(.systemGray4) : Color(NCBrandColor.shared.brandText))
  111. .padding(11)
  112. .background(entry.isPlaceholder ? Color(.systemGray4) : Color(NCBrandColor.shared.brand))
  113. .clipShape(Circle())
  114. .scaledToFit()
  115. .frame(width: geo.size.width / 4, height: sizeButton)
  116. })
  117. Link(destination: entry.isPlaceholder ? linkNoAction : linkActionTextDocument, label: {
  118. Image("note.text")
  119. .resizable()
  120. .renderingMode(.template)
  121. .foregroundColor(entry.isPlaceholder ? Color(.systemGray4) : Color(NCBrandColor.shared.brandText))
  122. .padding(11)
  123. .background(entry.isPlaceholder ? Color(.systemGray4) : Color(NCBrandColor.shared.brand))
  124. .clipShape(Circle())
  125. .scaledToFit()
  126. .frame(width: geo.size.width / 4, height: sizeButton)
  127. })
  128. Link(destination: entry.isPlaceholder ? linkNoAction : linkActionVoiceMemo, label: {
  129. Image("microphone")
  130. .resizable()
  131. .renderingMode(.template)
  132. .foregroundColor(entry.isPlaceholder ? Color(.systemGray4) : Color(NCBrandColor.shared.brandText))
  133. .padding(11)
  134. .background(entry.isPlaceholder ? Color(.systemGray4) : Color(NCBrandColor.shared.brand))
  135. .clipShape(Circle())
  136. .scaledToFit()
  137. .frame(width: geo.size.width / 4, height: sizeButton)
  138. })
  139. }
  140. .frame(width: geo.size.width, height: geo.size.height - 25, alignment: .bottomTrailing)
  141. .redacted(reason: entry.isPlaceholder ? .placeholder : [])
  142. HStack {
  143. Image(systemName: entry.footerImage)
  144. .resizable()
  145. .scaledToFit()
  146. .frame(width: 15, height: 15)
  147. .foregroundColor(entry.isPlaceholder ? Color(.systemGray4) : Color(NCBrandColor.shared.brand))
  148. Text(entry.footerText)
  149. .font(.caption2)
  150. .lineLimit(1)
  151. .foregroundColor(entry.isPlaceholder ? Color(.systemGray4) : Color(NCBrandColor.shared.brand))
  152. }
  153. .padding(.horizontal, 15.0)
  154. .frame(maxWidth: geo.size.width, maxHeight: geo.size.height - 2, alignment: .bottomTrailing)
  155. }
  156. }
  157. }
  158. }
  159. struct FilesWidget_Previews: PreviewProvider {
  160. static var previews: some View {
  161. let datas = Array(filesDatasTest[0...4])
  162. let entry = FilesDataEntry(date: Date(), datas: datas, isPlaceholder: false, isEmpty: true, userId: "", url: "", tile: "Good afternoon, Marino Faggiana", footerImage: "checkmark.icloud", footerText: "Nextcloud files")
  163. FilesWidgetView(entry: entry).previewContext(WidgetPreviewContext(family: .systemLarge))
  164. }
  165. }