-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathComplexTaskMasterWidget.swift
110 lines (100 loc) · 3.52 KB
/
ComplexTaskMasterWidget.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
//
// ComplexTaskMasterWidget.swift
// TaskMaster
//
// Created by Genki on 9/25/24.
//
import WidgetKit
import SwiftUI
struct ComplexProvider: TimelineProvider {
func placeholder(in context: Context) -> ComplexEntry {
ComplexEntry(date: Date.now, issues: [.example])
}
func getSnapshot(in context: Context, completion: @escaping (ComplexEntry) -> Void) {
let entry = ComplexEntry(date: Date.now, issues: loadIssues())
completion(entry)
}
func getTimeline(in context: Context, completion: @escaping (Timeline<ComplexEntry>) -> Void) {
let entry = ComplexEntry(date: Date.now, issues: loadIssues())
let timeline = Timeline(entries: [entry], policy: .never)
completion(timeline)
}
func loadIssues() -> [Issue] {
let dataController = DataController()
let request = dataController.fetchRequestForTopIssues(count: 7)
return dataController.results(for: request)
}
}
struct ComplexEntry: TimelineEntry {
let date: Date
let issues: [Issue]
}
struct ComplexTaskMasterWidgetEntryView: View {
var entry: ComplexProvider.Entry
@Environment(\.widgetFamily) var widgetFamily
@Environment(\.dynamicTypeSize) var dynamicTypeSize
var issues: ArraySlice<Issue> {
let issueCount: Int
switch widgetFamily {
case .systemSmall:
issueCount = 1
case .systemLarge, .systemExtraLarge:
if dynamicTypeSize < .xxLarge {
issueCount = 6
} else {
issueCount = 5
}
default:
if dynamicTypeSize < .xLarge {
issueCount = 3
} else {
issueCount = 2
}
}
return entry.issues.prefix(issueCount)
}
var body: some View {
VStack(spacing: 10) {
ForEach(issues) { issue in
Link(destination: issue.objectID.uriRepresentation()) {
VStack(alignment: .leading) {
Text(issue.issueTitle)
.font(.headline)
.layoutPriority(1)
if issue.issueTags.isEmpty == false {
Text(issue.issueTagsList)
.foregroundStyle(.secondary)
} else {
Text("タスクはありません!")
}
}
.frame(maxWidth: .infinity, alignment: .leading)
}
}
}
}
}
struct ComplexTaskMasterWidget: Widget {
let kind: String = "ComplexTaskMasterWidget"
var body: some WidgetConfiguration {
StaticConfiguration(kind: kind, provider: ComplexProvider()) { entry in
if #available(iOS 17.0, *) {
ComplexTaskMasterWidgetEntryView(entry: entry)
.containerBackground(.fill.tertiary, for: .widget)
} else {
ComplexTaskMasterWidgetEntryView(entry: entry)
.padding()
.background()
}
}
.configurationDisplayName("次のタスクは...")
.description("あなたにとって優先度の高いタスクです。")
.supportedFamilies([.systemSmall, .systemMedium, .systemLarge, .systemExtraLarge])
}
}
#Preview(as: .systemSmall) {
ComplexTaskMasterWidget()
} timeline: {
ComplexEntry(date: .now, issues: [.example])
ComplexEntry(date: .now, issues: [.example])
}