DashboardWidgetView.swift 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. //
  2. // DashboardWidgetView.swift
  3. // Widget
  4. //
  5. // Created by Marino Faggiana on 20/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 DashboardWidgetView: View {
  26. var entry: DashboardDataEntry
  27. var body: some View {
  28. GeometryReader { geo in
  29. ZStack(alignment: .topLeading) {
  30. HStack() {
  31. Image(uiImage: entry.titleImage)
  32. .renderingMode(.template)
  33. .resizable()
  34. .scaledToFill()
  35. .frame(width: 20, height: 20)
  36. Text(entry.title)
  37. .font(.system(size: 15))
  38. .fontWeight(.bold)
  39. .multilineTextAlignment(.center)
  40. .textCase(.uppercase)
  41. .lineLimit(1)
  42. }
  43. .frame(width: geo.size.width - 20)
  44. .padding([.top, .leading, .trailing], 10)
  45. VStack(alignment: .leading) {
  46. VStack(spacing: 0) {
  47. ForEach(entry.datas, id: \.id) { element in
  48. Link(destination: element.link) {
  49. HStack {
  50. let subTitleColor = Color(white: 0.5)
  51. if entry.isPlaceholder {
  52. Circle()
  53. .fill(Color(.systemGray4))
  54. .frame(width: 35, height: 35)
  55. } else if let color = element.imageColor {
  56. Image(uiImage: element.icon)
  57. .renderingMode(.template)
  58. .resizable()
  59. .frame(width: 20, height: 20)
  60. .foregroundColor(Color(color))
  61. } else if element.template {
  62. if entry.dashboard?.itemIconsRound ?? false {
  63. Image(uiImage: element.icon)
  64. .renderingMode(.template)
  65. .resizable()
  66. .scaledToFill()
  67. .frame(width: 20, height: 20)
  68. .foregroundColor(.white)
  69. .padding(8)
  70. .background(Color(.systemGray4))
  71. .clipShape(Circle())
  72. } else {
  73. Image(uiImage: element.icon)
  74. .renderingMode(.template)
  75. .resizable()
  76. .scaledToFill()
  77. .frame(width: 25, height: 25)
  78. .clipped()
  79. .cornerRadius(5)
  80. }
  81. } else {
  82. if entry.dashboard?.itemIconsRound ?? false || element.avatar {
  83. Image(uiImage: element.icon)
  84. .resizable()
  85. .scaledToFill()
  86. .frame(width: 35, height: 35)
  87. .clipShape(Circle())
  88. } else {
  89. Image(uiImage: element.icon)
  90. .resizable()
  91. .scaledToFill()
  92. .frame(width: 35, height: 35)
  93. .clipped()
  94. .cornerRadius(5)
  95. }
  96. }
  97. VStack(alignment: .leading, spacing: 2) {
  98. Text(element.title)
  99. .font(.system(size: 12))
  100. .fontWeight(.regular)
  101. Text(element.subTitle)
  102. .font(.system(size: CGFloat(10)))
  103. .foregroundColor(subTitleColor)
  104. }
  105. Spacer()
  106. }
  107. .padding(.leading, 10)
  108. .frame(height: 50)
  109. }
  110. Divider()
  111. .padding(.leading, 54)
  112. }
  113. }
  114. }
  115. .padding(.top, 35)
  116. .redacted(reason: entry.isPlaceholder ? .placeholder : [])
  117. if let buttons = entry.buttons, !buttons.isEmpty, !entry.isPlaceholder {
  118. HStack(spacing: 10) {
  119. let brandColor = Color(NCBrandColor.shared.brand)
  120. let brandTextColor = Color(NCBrandColor.shared.brandText)
  121. ForEach(buttons, id: \.index) { element in
  122. Link(destination: URL(string: element.link)! , label: {
  123. Text(element.text)
  124. .font(.system(size: 15))
  125. .padding(7)
  126. .background(brandColor)
  127. .foregroundColor(brandTextColor)
  128. .border(brandColor, width: 1)
  129. .cornerRadius(16)
  130. })
  131. }
  132. }
  133. .frame(width: geo.size.width - 10, height: geo.size.height - 25, alignment: .bottomTrailing)
  134. }
  135. HStack {
  136. Image(systemName: entry.footerImage)
  137. .resizable()
  138. .scaledToFit()
  139. .frame(width: 15, height: 15)
  140. .foregroundColor(entry.isPlaceholder ? Color(.systemGray4) : Color(NCBrandColor.shared.brand))
  141. Text(entry.footerText)
  142. .font(.caption2)
  143. .padding(.trailing, 13.0)
  144. .foregroundColor(entry.isPlaceholder ? Color(.systemGray4) : Color(NCBrandColor.shared.brand))
  145. }
  146. .frame(maxWidth: geo.size.width - 5, maxHeight: geo.size.height - 2, alignment: .bottomTrailing)
  147. }
  148. }
  149. }
  150. }
  151. struct DashboardWidget_Previews: PreviewProvider {
  152. static var previews: some View {
  153. let datas = Array(dashboardDatasTest[0...4])
  154. let title = "Dashboard"
  155. let titleImage = UIImage(named: "widget")!
  156. let entry = DashboardDataEntry(date: Date(), datas: datas, dashboard: nil, buttons: nil, isPlaceholder: false, titleImage: titleImage, title: title, footerImage: "checkmark.icloud", footerText: "Nextcloud widget")
  157. DashboardWidgetView(entry: entry).previewContext(WidgetPreviewContext(family: .systemLarge))
  158. }
  159. }