This repository has been archived by the owner on May 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 442
/
TopNewsListWidget.swift
190 lines (180 loc) · 7.05 KB
/
TopNewsListWidget.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
// Copyright 2023 The Brave Authors. All rights reserved.
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
import WidgetKit
import SwiftUI
import CodableHelpers
import Shared
import DesignSystem
import os
import AVFoundation
import BraveWidgetsModels
import UIKit
import BraveShared
struct TopNewsListWidget: Widget {
var body: some WidgetConfiguration {
StaticConfiguration(kind: "TopNewsListWidget", provider: TopNewsListWidgetProvider()) { entry in
TopNewsListView(entry: entry)
}
.supportedFamilies([.systemMedium, .systemLarge])
.configurationDisplayName(Strings.Widgets.newsClusteringWidgetTitle)
.description(Strings.Widgets.newsClusteringWidgetDescription)
#if swift(>=5.9)
.contentMarginsDisabled()
#endif
}
}
private struct TopNewsListEntry: TimelineEntry {
var date: Date = .now
var topics: [NewsTopic]?
var images: [NewsTopic.ID: UIImage] = [:]
}
private struct TopNewsListWidgetProvider: TimelineProvider {
private let model: NewsTopicsModel = .live
private let thumbnailSize: CGSize = .init(width: 192, height: 192)
func getSnapshot(in context: Context, completion: @escaping (TopNewsListEntry) -> Void) {
Task {
let grouping = context.family == .systemMedium ? 2 : 5
// No need to load more than the grouping in the snapshot
let topics = Array(await model.fetchNewsTopics().prefix(grouping))
let images = await model.fetchImageThumbnailsForTopics(topics, thumbnailSize)
completion(.init(date: Date(), topics: topics, images: images))
}
}
func getTimeline(in context: Context, completion: @escaping (Timeline<TopNewsListEntry>) -> Void) {
Task {
let grouping = context.family == .systemMedium ? 2 : 5
let interval = context.family == .systemMedium ? 15 : 20
let allTopics = Array(await model.fetchNewsTopics().prefix(grouping * 3))
let topics = allTopics.splitEvery(grouping)
let images = await model.fetchImageThumbnailsForTopics(allTopics, thumbnailSize)
let entries: [TopNewsListEntry] = zip(topics, topics.indices).map({ topics, index in
.init(date: Date().addingTimeInterval(interval.minutes * TimeInterval(index)), topics: topics, images: images)
})
completion(.init(entries: entries, policy: .after(Date().addingTimeInterval(60.minutes))))
}
}
func placeholder(in context: Context) -> TopNewsListEntry {
.init(date: Date(), topics: nil)
}
}
private struct TopNewsListView: View {
@Environment(\.pixelLength) var pixelLength
@Environment(\.widgetFamily) var widgetFamily
var entry: TopNewsListEntry
private var headerView: some View {
HStack {
HStack(spacing: 4) {
Image("brave-icon-no-bg")
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width: 16, height: 16)
Text(Strings.Widgets.braveNews)
.foregroundColor(Color(.braveLabel))
.font(.system(size: 14, weight: .bold, design: .rounded))
}
Spacer()
Link(destination: URL(string: "\(AppURLScheme.appURLScheme)://shortcut?path=\(WidgetShortcut.braveNews.rawValue)")!) {
Text(Strings.Widgets.newsClusteringReadMoreButtonTitle)
.foregroundColor(Color(.braveBlurpleTint))
.font(.system(size: 13, weight: .semibold, design: .rounded))
}
}
.padding(.horizontal)
.padding(.vertical, widgetFamily == .systemLarge ? 12 : 8)
.unredacted()
}
var body: some View {
Group {
if let topics = entry.topics, !topics.isEmpty {
VStack(alignment: .leading, spacing: widgetFamily == .systemLarge ? 12 : 8) {
headerView
.background(Color(.braveGroupedBackground))
VStack(alignment: .leading, spacing: 8) {
ForEach(topics.prefix(widgetFamily == .systemLarge ? 5 : 2)) { topic in
HStack {
Link(destination: topic.url) {
VStack(alignment: .leading, spacing: 2) {
Text(topic.title)
.lineLimit(widgetFamily == .systemLarge ? 3 : 2)
.font(.system(size: widgetFamily == .systemLarge ? 14 : 13, weight: .semibold, design: .rounded))
.foregroundColor(.primary)
Text(topic.publisherName)
.lineLimit(1)
.font(.system(size: 10, weight: .medium, design: .rounded))
.foregroundColor(.secondary)
}
}
if let image = entry.images[topic.id] {
Spacer()
Color.clear
.aspectRatio(1, contentMode: .fit)
.frame(maxHeight: 50)
.overlay(
Image(uiImage: image)
.resizable()
.aspectRatio(contentMode: .fill)
)
.clipShape(RoundedRectangle(cornerRadius: 8, style: .continuous))
.overlay(RoundedRectangle(cornerRadius: 8, style: .continuous).strokeBorder(Color.primary.opacity(0.3), lineWidth: pixelLength))
}
}
}
}
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .topLeading)
.padding(.horizontal)
}
.padding(.bottom)
} else {
ZStack(alignment: .top) {
Text(Strings.Widgets.newsClusteringErrorLabel)
.font(.system(size: widgetFamily == .systemLarge ? 26 : 18, weight: .semibold, design: .rounded))
.padding()
.frame(maxWidth: .infinity, maxHeight: .infinity)
.background {
LinearGradient(braveGradient: .darkGradient01)
.mask {
Image("brave-today-error")
.renderingMode(.template)
.resizable(resizingMode: .tile)
.opacity(0.1)
.rotationEffect(.degrees(-45))
.scaleEffect(x: 2.1, y: 2.1)
}
}
headerView
.background {
LinearGradient(
colors: [Color(.braveBackground), Color(.braveBackground).opacity(0.0)],
startPoint: .top,
endPoint: .bottom
)
}
}
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .top)
}
}
.widgetBackground { Color(.braveBackground) }
}
}
#if DEBUG
struct TopNewsListView_PreviewProvider: PreviewProvider {
struct PreviewView: View {
let model = NewsTopicsModel.mock
@State private var topics: [NewsTopic] = []
var body: some View {
TopNewsListView(entry: .init(date: .now, topics: topics, images: [:]))
.task {
topics = await model.fetchNewsTopics()
}
}
}
static var previews: some View {
PreviewView()
.previewContext(WidgetPreviewContext(family: .systemLarge))
PreviewView()
.previewContext(WidgetPreviewContext(family: .systemMedium))
}
}
#endif