-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathMediaListWidget.swift
226 lines (203 loc) · 7.38 KB
/
MediaListWidget.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
//
// MediaListWidget.swift
// AniHyou WidgetExtension
//
// Created by Axel Lopez on 20/09/2023.
//
import WidgetKit
import SwiftUI
import AniListAPI
@available(iOS 17.0, *)
struct MediaListProvider: AppIntentTimelineProvider {
func placeholder(in context: Context) -> MediaListEntry {
MediaListEntry(
animeList: [],
date: Date(),
placeholderText: "Your anime or manga list appear here",
widgetSize: context.displaySize
)
}
func snapshot(for configuration: SelectMediaTypeIntent, in context: Context) async -> MediaListEntry {
let timeline = await getAnimeTimeline(context: context, mediaType: configuration.mediaType.value)
if let entry = timeline.entries.first {
return entry
} else {
return placeholder(in: context)
}
}
func timeline(for configuration: SelectMediaTypeIntent, in context: Context) async -> Timeline<MediaListEntry> {
await getAnimeTimeline(context: context, mediaType: configuration.mediaType.value)
}
func getAnimeTimeline(context: Context, mediaType: MediaType) async -> Timeline<MediaListEntry> {
let now = Date.now
let userId = UserDefaults(suiteName: ANIHYOU_GROUP)?.integer(forKey: USER_ID_KEY) ?? 0
if userId == 0 {
let entry = MediaListEntry(
animeList: [],
date: now,
placeholderText: "Login to use this widget",
widgetSize: context.displaySize
)
return Timeline(entries: [entry], policy: .never)
}
let maxItems = context.family.maxMediaListItems
if let result = await MediaListRepository.getShouUserMediaList(
userId: userId,
mediaType: mediaType,
status: .current,
sort: [.updatedTimeDesc],
page: 1,
perPage: 12
) {
let tempList = Array(result.data.prefix(maxItems))
let nextUpdateDate = Calendar.current.date(byAdding: .hour, value: 12, to: now)!
let entry = MediaListEntry(
animeList: tempList,
date: nextUpdateDate,
placeholderText: nil,
widgetSize: context.displaySize
)
return Timeline(entries: [entry], policy: .after(nextUpdateDate))
} else {
let nextUpdateDate = Calendar.current.date(byAdding: .hour, value: 1, to: now)!
let entry = MediaListEntry(
animeList: [],
date: nextUpdateDate,
placeholderText: "Error updating",
widgetSize: context.displaySize
)
return Timeline(entries: [entry], policy: .after(nextUpdateDate))
}
}
}
struct MediaListEntry: BaseEntry {
let animeList: [ShouUserMediaList]
var date: Date
var placeholderText: String?
var widgetSize: CGSize
}
@available(iOS 17.0, *)
struct MediaListWidgetEntryView: View {
@Environment(\.widgetFamily) var family: WidgetFamily
@AppStorage(ACCENT_COLOR_KEY, store: UserDefaults(suiteName: ANIHYOU_GROUP)) private var accentColor = ANIHYOU_COLOR
var tintColor: Color {
Color(hex: accentColor) ?? .accentColor
}
let entry: MediaListProvider.Entry
var aligment: Alignment {
if entry.placeholderText != nil || entry.animeList.isEmpty {
return .center
} else {
if entry.animeList.count >= family.maxMediaListItems {
return .center
} else {
return .top
}
}
}
var paddingLenght: CGFloat? {
if aligment == .top {
return nil
} else {
return 0
}
}
var body: some View {
VStack(alignment: .leading) {
content
}//:VStack
.containerBackground(.background, for: .widget)
.padding(.vertical, paddingLenght)
.frame(
width: entry.widgetSize.width,
height: entry.widgetSize.height,
alignment: aligment
)
.tint(tintColor)
}
@ViewBuilder
var content: some View {
if let placeholder = entry.placeholderText {
Text(placeholder)
.multilineTextAlignment(.center)
} else if entry.animeList.isEmpty {
Text("Nothing in your list")
.multilineTextAlignment(.center)
} else {
ForEach(Array(entry.animeList.enumerated()), id: \.element.id) { index, item in
MediaListItemView(item: item, tintColor: tintColor)
if (index + 1) < entry.animeList.count {
Divider()
.padding(.leading)
}
}//:ForEach
}
}
}
@available(iOS 17.0, *)
private struct MediaListItemView: View {
let item: ShouUserMediaList
let tintColor: Color
var body: some View {
Link(destination: URL(string: "anihyou://media/\(item.mediaId)")!) {
HStack(spacing: 0) {
VStack(alignment: .leading, spacing: 0) {
Text(item.media?.title?.userPreferred ?? "")
.font(.system(size: 14))
.lineLimit(1)
.padding(.horizontal)
HStack {
Text("\(item.progress ?? 0)/\(item.maxProgress)")
.foregroundStyle(.secondary)
.frame(width: 40, alignment: .leading)
if let schedule = item.media?.nextAiringEpisode {
AiringText(
episode: schedule.episode,
airingAt: schedule.airingAt,
episodesBehind: (schedule.episode - 1) - (item.progress ?? 0),
airingColor: tintColor
)
}
}
.font(.system(size: 12))
.lineLimit(1)
.padding(.horizontal)
.invalidatableContent()
}//:VStack
Spacer()
Button("+1", intent: SetProgressIntent(
mediaId: item.mediaId,
entryId: item.id,
progress: (item.progress ?? 0) + 1
))
.padding(.horizontal)
}//:HStack
}//:Link
}
}
@available(iOS 17.0, *)
struct MediaListWidget: Widget {
let kind: String = MEDIA_LIST_WIDGET_KIND
var body: some WidgetConfiguration {
AppIntentConfiguration(
kind: kind,
intent: SelectMediaTypeIntent.self,
provider: MediaListProvider()
) { entry in
MediaListWidgetEntryView(entry: entry)
}
.configurationDisplayName("Media List")
.description("Quick access to your anime or manga list.")
.supportedFamilies([.systemMedium, .systemLarge])
}
}
@available(iOS 17.0, *)
#Preview {
MediaListWidgetEntryView(entry: MediaListEntry(
animeList: [],
date: Date(),
placeholderText: "Your anime or manga list here.",
widgetSize: CGSize(width: 291, height: 141)
))
.previewContext(WidgetPreviewContext(family: .systemMedium))
}