NextcloudWidgetView.swift 8.3 KB

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