ToolbarWidgetView.swift 6.6 KB

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