ToolbarWidgetView.swift 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. //
  2. // ToolbarWidgetView.swift
  3. // Widget
  4. //
  5. // Created by Marino Faggiana on 25/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 ToolbarWidgetView: View {
  26. var entry: ToolbarDataEntry
  27. var body: some View {
  28. let parameterLink = "&user=\(entry.userId)&url=\(entry.url)"
  29. let linkNoAction: URL = URL(string: NCGlobal.shared.widgetActionNoAction + parameterLink) != nil ? URL(string: NCGlobal.shared.widgetActionNoAction + parameterLink)! : URL(string: NCGlobal.shared.widgetActionNoAction)!
  30. let linkActionUploadAsset: URL = URL(string: NCGlobal.shared.widgetActionUploadAsset + parameterLink) != nil ? URL(string: NCGlobal.shared.widgetActionUploadAsset + parameterLink)! : URL(string: NCGlobal.shared.widgetActionUploadAsset)!
  31. let linkActionScanDocument: URL = URL(string: NCGlobal.shared.widgetActionScanDocument + parameterLink) != nil ? URL(string: NCGlobal.shared.widgetActionScanDocument + parameterLink)! : URL(string: NCGlobal.shared.widgetActionScanDocument)!
  32. let linkActionTextDocument: URL = URL(string: NCGlobal.shared.widgetActionTextDocument + parameterLink) != nil ? URL(string: NCGlobal.shared.widgetActionTextDocument + parameterLink)! : URL(string: NCGlobal.shared.widgetActionTextDocument)!
  33. let linkActionVoiceMemo: URL = URL(string: NCGlobal.shared.widgetActionVoiceMemo + parameterLink) != nil ? URL(string: NCGlobal.shared.widgetActionVoiceMemo + parameterLink)! : URL(string: NCGlobal.shared.widgetActionVoiceMemo)!
  34. GeometryReader { geo in
  35. ZStack(alignment: .topLeading) {
  36. HStack(spacing: 0) {
  37. let sizeButton: CGFloat = 65
  38. Link(destination: entry.isPlaceholder ? linkNoAction : linkActionUploadAsset, label: {
  39. Image("addImage")
  40. .resizable()
  41. .renderingMode(.template)
  42. .foregroundColor(entry.isPlaceholder ? Color(.systemGray4) : Color(NCBrandColor.shared.brandText))
  43. .padding()
  44. .background(entry.isPlaceholder ? Color(.systemGray4) : Color(NCBrandColor.shared.brand))
  45. .clipShape(Circle())
  46. .scaledToFit()
  47. .frame(width: geo.size.width / 4, height: sizeButton)
  48. })
  49. Link(destination: entry.isPlaceholder ? linkNoAction : linkActionScanDocument, label: {
  50. Image(systemName: "doc.text.viewfinder")
  51. .resizable()
  52. .renderingMode(.template)
  53. .foregroundColor(entry.isPlaceholder ? Color(.systemGray4) : Color(NCBrandColor.shared.brandText))
  54. .padding()
  55. .background(entry.isPlaceholder ? Color(.systemGray4) : Color(NCBrandColor.shared.brand))
  56. .clipShape(Circle())
  57. .scaledToFit()
  58. .frame(width: geo.size.width / 4, height: sizeButton)
  59. })
  60. Link(destination: entry.isPlaceholder ? linkNoAction : linkActionTextDocument, label: {
  61. Image("note.text")
  62. .resizable()
  63. .renderingMode(.template)
  64. .foregroundColor(entry.isPlaceholder ? Color(.systemGray4) : Color(NCBrandColor.shared.brandText))
  65. .padding()
  66. .background(entry.isPlaceholder ? Color(.systemGray4) : Color(NCBrandColor.shared.brand))
  67. .clipShape(Circle())
  68. .scaledToFit()
  69. .frame(width: geo.size.width / 4, height: sizeButton)
  70. })
  71. Link(destination: entry.isPlaceholder ? linkNoAction : linkActionVoiceMemo, label: {
  72. Image("microphone")
  73. .resizable()
  74. .foregroundColor(entry.isPlaceholder ? Color(.systemGray4) : Color(NCBrandColor.shared.brandText))
  75. .padding()
  76. .background(entry.isPlaceholder ? Color(.systemGray4) : Color(NCBrandColor.shared.brand))
  77. .clipShape(Circle())
  78. .scaledToFit()
  79. .frame(width: geo.size.width / 4, height: sizeButton)
  80. })
  81. }
  82. .frame(width: geo.size.width, height: geo.size.height, alignment: .center)
  83. .redacted(reason: entry.isPlaceholder ? .placeholder : [])
  84. HStack {
  85. Image(systemName: entry.footerImage)
  86. .resizable()
  87. .scaledToFit()
  88. .frame(width: 15, height: 15)
  89. .foregroundColor(entry.isPlaceholder ? Color(.systemGray4) : Color(NCBrandColor.shared.brand))
  90. Text(entry.footerText)
  91. .font(.caption2)
  92. .padding(.trailing, 13.0)
  93. .foregroundColor(entry.isPlaceholder ? Color(.systemGray4) : Color(NCBrandColor.shared.brand))
  94. }
  95. .frame(maxWidth: geo.size.width - 5, maxHeight: geo.size.height - 2, alignment: .bottomTrailing)
  96. }
  97. }
  98. .widgetBackground(Color.black.opacity(0.9))
  99. }
  100. }
  101. struct ToolbarWidget_Previews: PreviewProvider {
  102. static var previews: some View {
  103. let entry = ToolbarDataEntry(date: Date(), isPlaceholder: false, userId: "", url: "", footerImage: "checkmark.icloud", footerText: NCBrandOptions.shared.brand + " toolbar")
  104. ToolbarWidgetView(entry: entry).previewContext(WidgetPreviewContext(family: .systemMedium))
  105. }
  106. }