DashboardWidgetView.swift 6.9 KB

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