NextcloudWidgetView.swift 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. Image(uiImage: element.image)
  50. .resizable()
  51. .scaledToFill()
  52. .frame(width: imageSize+addSizeIcon, height: imageSize+addSizeIcon)
  53. .clipped()
  54. .cornerRadius(5)
  55. VStack(alignment: .leading, spacing: spacing) {
  56. Text(element.title)
  57. .font(.system(size: 12))
  58. .fontWeight(.regular)
  59. Text(element.subTitle)
  60. .font(.system(size: CGFloat(10)))
  61. .foregroundColor(Color(white: 0.5))
  62. }
  63. Spacer()
  64. }
  65. .padding(.leading, 10)
  66. .frame(height: heightFrame)
  67. }
  68. Divider()
  69. .padding(.leading, 48 + addSizeIcon)
  70. }
  71. }
  72. }
  73. .padding(.top, 10)
  74. .redacted(reason: entry.isPlaceholder ? .placeholder : [])
  75. HStack(spacing: 0) {
  76. Link(destination: URL(string: "nextcloud://open-action?action=upload-asset")!, label: {
  77. Image("buttonAddImage")
  78. .resizable()
  79. .renderingMode(.template)
  80. .foregroundColor(entry.isPlaceholder ? Color(white: 0.8) : Color(NCBrandColor.shared.brandText))
  81. .padding(10)
  82. .background(entry.isPlaceholder ? Color(white: 0.8) : Color(NCBrandColor.shared.brand))
  83. .clipShape(Circle())
  84. .scaledToFit()
  85. .frame(width: geo.size.width / 4, height: 50)
  86. })
  87. Link(destination: URL(string: "nextcloud://open-action?action=add-scan-document")!, label: {
  88. Image("buttonAddScan")
  89. .resizable()
  90. .renderingMode(.template)
  91. .foregroundColor(entry.isPlaceholder ? Color(white: 0.8) : Color(NCBrandColor.shared.brandText))
  92. .padding(10)
  93. .background(entry.isPlaceholder ? Color(white: 0.8) : Color(NCBrandColor.shared.brand))
  94. .clipShape(Circle())
  95. .scaledToFit()
  96. .frame(width: geo.size.width / 4, height: 50)
  97. })
  98. Link(destination: URL(string: "nextcloud://open-action?action=create-text-document")!, label: {
  99. Image("note.text")
  100. .resizable()
  101. .renderingMode(.template)
  102. .foregroundColor(entry.isPlaceholder ? Color(white: 0.8) : Color(NCBrandColor.shared.brandText))
  103. .padding(10)
  104. .background(entry.isPlaceholder ? Color(white: 0.8) : Color(NCBrandColor.shared.brand))
  105. .clipShape(Circle())
  106. .scaledToFit()
  107. .frame(width: geo.size.width / 4, height: 50)
  108. })
  109. Link(destination: URL(string: "nextcloud://open-action?action=create-voice-memo")!, label: {
  110. Image("microphone")
  111. .resizable()
  112. .renderingMode(.template)
  113. .foregroundColor(entry.isPlaceholder ? Color(white: 0.8) : Color(NCBrandColor.shared.brandText))
  114. .padding(10)
  115. .background(entry.isPlaceholder ? Color(white: 0.8) : Color(NCBrandColor.shared.brand))
  116. .clipShape(Circle())
  117. .scaledToFit()
  118. .frame(width: geo.size.width / 4, height: 50)
  119. })
  120. }
  121. .frame(width: geo.size.width, height: geo.size.height - 35, alignment: .bottomTrailing)
  122. .redacted(reason: entry.isPlaceholder ? .placeholder : [])
  123. HStack {
  124. Image(systemName: entry.footerImage)
  125. .resizable()
  126. .scaledToFit()
  127. .frame(width: 15, height: 15)
  128. .foregroundColor(entry.isPlaceholder ? Color(white: 0.2) : Color(NCBrandColor.shared.brand))
  129. Text(entry.footerText)
  130. .font(.caption2)
  131. .padding(.trailing, 13.0)
  132. }
  133. .frame(maxWidth: geo.size.width - 5, maxHeight: geo.size.height - 2, alignment: .bottomTrailing)
  134. }
  135. }
  136. }
  137. }
  138. struct NextcloudWidget_Previews: PreviewProvider {
  139. static var previews: some View {
  140. let recentDatas = Array(recentDatasTest[0...3])
  141. let entry = NextcloudDataEntry(date: Date(), recentDatas: recentDatas, isPlaceholder: false, footerImage: "checkmark.icloud", footerText: "Nextcloud widget")
  142. NextcloudWidgetView(entry: entry).previewContext(WidgetPreviewContext(family: .systemLarge))
  143. }
  144. }