NextcloudWidgetView.swift 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. //
  2. // NextcloudWidgetView.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 NextcloudWidgetView: View {
  26. var entry: NextcloudDataEntry
  27. var body: some View {
  28. GeometryReader { geo in
  29. ZStack(alignment: .topLeading) {
  30. HStack() {
  31. Text(entry.tile)
  32. .font(.system(size: 12))
  33. .fontWeight(.bold)
  34. .multilineTextAlignment(.center)
  35. .textCase(.uppercase)
  36. .lineLimit(1)
  37. }
  38. .frame(width: geo.size.width - 20)
  39. .padding([.top, .leading, .trailing], 10)
  40. VStack(alignment: .leading) {
  41. VStack(spacing: 0) {
  42. let datasCount = CGFloat(entry.datas.count)
  43. let heightFrame = (geo.size.height - 120) / datasCount
  44. let addSizeIcon = heightFrame / datasCount
  45. let spacing = (addSizeIcon - datasCount) / 2
  46. ForEach(entry.datas, id: \.id) { element in
  47. Link(destination: element.url) {
  48. HStack {
  49. let subTitleColor = Color(white: 0.5)
  50. let imageSize:CGFloat = 30
  51. Image(uiImage: element.image)
  52. .resizable()
  53. .scaledToFill()
  54. .frame(width: imageSize+addSizeIcon, height: imageSize+addSizeIcon)
  55. .clipped()
  56. .cornerRadius(5)
  57. VStack(alignment: .leading, spacing: spacing) {
  58. Text(element.title)
  59. .font(.system(size: 12))
  60. .fontWeight(.regular)
  61. Text(element.subTitle)
  62. .font(.system(size: CGFloat(10)))
  63. .foregroundColor(subTitleColor)
  64. }
  65. Spacer()
  66. }
  67. .padding(.leading, 10)
  68. .frame(height: heightFrame)
  69. }
  70. Divider()
  71. .padding(.leading, 48 + addSizeIcon)
  72. }
  73. }
  74. }
  75. .padding(.top, 30)
  76. .redacted(reason: entry.isPlaceholder ? .placeholder : [])
  77. HStack(spacing: 0) {
  78. let sizeButton: CGFloat = 45
  79. let placeholderColor = Color(white: 0.8)
  80. let brandColor = Color(NCBrandColor.shared.brand)
  81. let brandTextColor = Color(NCBrandColor.shared.brandText)
  82. Link(destination: entry.isPlaceholder ? NCGlobal.shared.widgetActionNoAction : NCGlobal.shared.widgetActionUploadAsset, label: {
  83. Image("buttonAddImage")
  84. .resizable()
  85. .renderingMode(.template)
  86. .foregroundColor(entry.isPlaceholder ? placeholderColor : brandTextColor)
  87. .padding(10)
  88. .background(entry.isPlaceholder ? placeholderColor : brandColor)
  89. .clipShape(Circle())
  90. .scaledToFit()
  91. .frame(width: geo.size.width / 4, height: sizeButton)
  92. })
  93. Link(destination: entry.isPlaceholder ? NCGlobal.shared.widgetActionNoAction : NCGlobal.shared.widgetActionScanDocument, label: {
  94. Image("buttonAddScan")
  95. .resizable()
  96. .renderingMode(.template)
  97. .foregroundColor(entry.isPlaceholder ? placeholderColor : brandTextColor)
  98. .padding(10)
  99. .background(entry.isPlaceholder ? placeholderColor : brandColor)
  100. .clipShape(Circle())
  101. .scaledToFit()
  102. .frame(width: geo.size.width / 4, height: sizeButton)
  103. })
  104. Link(destination: entry.isPlaceholder ? NCGlobal.shared.widgetActionNoAction : NCGlobal.shared.widgetActionTextDocument, label: {
  105. Image("note.text")
  106. .resizable()
  107. .renderingMode(.template)
  108. .foregroundColor(entry.isPlaceholder ? placeholderColor : brandTextColor)
  109. .padding(10)
  110. .background(entry.isPlaceholder ? placeholderColor : brandColor)
  111. .clipShape(Circle())
  112. .scaledToFit()
  113. .frame(width: geo.size.width / 4, height: sizeButton)
  114. })
  115. Link(destination: entry.isPlaceholder ? NCGlobal.shared.widgetActionNoAction : NCGlobal.shared.widgetActionVoiceMemo, label: {
  116. Image("microphone")
  117. .resizable()
  118. .renderingMode(.template)
  119. .foregroundColor(entry.isPlaceholder ? placeholderColor : brandTextColor)
  120. .padding(10)
  121. .background(entry.isPlaceholder ? placeholderColor : brandColor)
  122. .clipShape(Circle())
  123. .scaledToFit()
  124. .frame(width: geo.size.width / 4, height: sizeButton)
  125. })
  126. }
  127. .frame(width: geo.size.width, height: geo.size.height - 25, alignment: .bottomTrailing)
  128. .redacted(reason: entry.isPlaceholder ? .placeholder : [])
  129. HStack {
  130. let placeholderColor = Color(white: 0.2)
  131. let brandColor = Color(NCBrandColor.shared.brand)
  132. Image(systemName: entry.footerImage)
  133. .resizable()
  134. .scaledToFit()
  135. .frame(width: 15, height: 15)
  136. .foregroundColor(entry.isPlaceholder ? placeholderColor : brandColor)
  137. Text(entry.footerText)
  138. .font(.caption2)
  139. .padding(.trailing, 13.0)
  140. }
  141. .frame(maxWidth: geo.size.width - 5, maxHeight: geo.size.height - 2, alignment: .bottomTrailing)
  142. }
  143. }
  144. }
  145. }
  146. struct NextcloudWidget_Previews: PreviewProvider {
  147. static var previews: some View {
  148. let datas = Array(recentDatasTest[0...3])
  149. let entry = NextcloudDataEntry(date: Date(), datas: datas, isPlaceholder: false, tile: "Good afternoon, Marino Faggiana", footerImage: "checkmark.icloud", footerText: "Nextcloud widget")
  150. NextcloudWidgetView(entry: entry).previewContext(WidgetPreviewContext(family: .systemLarge))
  151. }
  152. }