FilesWidgetView.swift 11 KB

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