Widget.swift 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. //
  2. // NextcloudWidget.swift
  3. // NextcloudWidget
  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 WidgetKit
  24. import SwiftUI
  25. struct Provider: TimelineProvider {
  26. typealias Entry = DashboardDataEntry
  27. func placeholder(in context: Context) -> Entry {
  28. return Entry(date: Date(), dashboardDatas: [])
  29. }
  30. func getSnapshot(in context: Context, completion: @escaping (Entry) -> Void) {
  31. readDashboard { dashboardDatas in
  32. completion(Entry(date: Date(), dashboardDatas: dashboardDatas))
  33. }
  34. }
  35. /*
  36. func getTimeline(for configuration: ConfigurationIntent, in context: Context, completion: @escaping (Timeline<Entry>) -> ()) {
  37. var entries = [SimpleEntry]()
  38. let currentDate = Date()
  39. let midnight = Calendar.current.startOfDay(for: currentDate)
  40. let nextMidnight = Calendar.current.date(byAdding: .day, value: 1, to: midnight)!
  41. for offset in 0 ..< 60 * 24 {
  42. let entryDate = Calendar.current.date(byAdding: .minute, value: offset, to: midnight)!
  43. entries.append(SimpleEntry(date: entryDate))
  44. }
  45. let timeline = Timeline(entries: entries, policy: .after(nextMidnight))
  46. completion(timeline)
  47. }
  48. */
  49. func getTimeline(in context: Context, completion: @escaping (Timeline<Entry>) -> Void) {
  50. readDashboard { dashboardDatas in
  51. let timeLine = Timeline(entries: [Entry(date: Date(), dashboardDatas: dashboardDatas)], policy: .atEnd)
  52. completion(timeLine)
  53. }
  54. }
  55. }
  56. @main
  57. struct DashboardWidget: Widget {
  58. let kind: String = "NextcloudWidget"
  59. var body: some WidgetConfiguration {
  60. StaticConfiguration(kind: kind, provider: Provider()) { entry in
  61. DashboardWidgetView(entry: entry)
  62. }
  63. .supportedFamilies([.systemLarge])
  64. .configurationDisplayName("Nextcloud Dashboard")
  65. .description(NSLocalizedString("_subtitle_dashboard_", comment: ""))
  66. }
  67. }
  68. struct DashboardWidget_Previews: PreviewProvider {
  69. static var previews: some View {
  70. let entry = DashboardDataEntry(date: Date(), dashboardDatas: dashboardDatasTest)
  71. DashboardWidgetView(entry: entry).previewContext(WidgetPreviewContext(family: .systemLarge))
  72. }
  73. }