DashboardWidgetView.swift 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. .resizable()
  33. .scaledToFill()
  34. .frame(width: 20, height: 20)
  35. .clipped()
  36. .cornerRadius(5)
  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. Image(uiImage: element.icon)
  54. .resizable()
  55. .scaledToFill()
  56. .frame(width: imageSize, height: imageSize)
  57. .clipped()
  58. .cornerRadius(5)
  59. VStack(alignment: .leading, spacing: 2) {
  60. Text(element.title)
  61. .font(.system(size: 12))
  62. .fontWeight(.regular)
  63. Text(element.subTitle)
  64. .font(.system(size: CGFloat(10)))
  65. .foregroundColor(subTitleColor)
  66. }
  67. Spacer()
  68. }
  69. .padding(.leading, 10)
  70. .frame(height: 45)
  71. }
  72. Divider()
  73. .padding(.leading, 54)
  74. }
  75. }
  76. }
  77. .padding(.top, 40)
  78. .redacted(reason: entry.isPlaceholder ? .placeholder : [])
  79. HStack {
  80. let placeholderColor = Color(white: 0.2)
  81. let brandColor = Color(NCBrandColor.shared.brand)
  82. Image(systemName: entry.footerImage)
  83. .resizable()
  84. .scaledToFit()
  85. .frame(width: 15, height: 15)
  86. .foregroundColor(entry.isPlaceholder ? placeholderColor : brandColor)
  87. Text(entry.footerText)
  88. .font(.caption2)
  89. .padding(.trailing, 13.0)
  90. .foregroundColor(entry.isPlaceholder ? placeholderColor : brandColor)
  91. }
  92. .frame(maxWidth: geo.size.width - 5, maxHeight: geo.size.height - 2, alignment: .bottomTrailing)
  93. }
  94. }
  95. }
  96. }
  97. struct DashboardWidget_Previews: PreviewProvider {
  98. static var previews: some View {
  99. let datas = Array(dashboardDatasTest[0...dashboaardItems - 1])
  100. let title = "Dashboard"
  101. let titleImage = UIImage(named: "widget")!
  102. let entry = DashboardDataEntry(date: Date(), datas: datas, isPlaceholder: false, titleImage: titleImage, title: title, footerImage: "checkmark.icloud", footerText: "Nextcloud widget")
  103. DashboardWidgetView(entry: entry).previewContext(WidgetPreviewContext(family: .systemLarge))
  104. }
  105. }