DashboardWidgetView.swift 9.7 KB

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