ToolbarWidgetView.swift 6.9 KB

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