DashboardWidgetView.swift 9.4 KB

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