DashboardWidgetView.swift 8.2 KB

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