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 441
/
LockScreenFavoriteWidget.swift
161 lines (151 loc) · 11.1 KB
/
LockScreenFavoriteWidget.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
// Copyright 2022 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 Foundation
import WidgetKit
import SwiftUI
import BraveWidgetsModels
import FaviconModels
import Strings
struct LockScreenFavoriteWidget: Widget {
var body: some WidgetConfiguration {
if #available(iOSApplicationExtension 16.0, *) {
return IntentConfiguration(kind: "LockScreenFavoriteWidget", intent: LockScreenFavoriteConfigurationIntent.self, provider: LockScreenFavoriteProvider()) { entry in
LockScreenFavoriteView(entry: entry)
}
.configurationDisplayName(Strings.Widgets.favoritesWidgetTitle)
.description(Strings.Widgets.favoritesWidgetDescription)
.supportedFamilies([.accessoryCircular])
} else {
return EmptyWidgetConfiguration()
}
}
}
private struct LockScreenFavoriteEntry: TimelineEntry {
var date: Date
var favorite: WidgetFavorite?
}
private struct LockScreenFavoriteProvider: IntentTimelineProvider {
typealias Intent = LockScreenFavoriteConfigurationIntent
typealias Entry = LockScreenFavoriteEntry
func placeholder(in context: Context) -> Entry {
Entry(date: Date(), favorite: nil)
}
func widgetFavorite(for url: URL?, completion: @escaping (WidgetFavorite?) -> Void) {
let favorites = FavoritesWidgetData.loadWidgetData() ?? []
var selectedFavorite = favorites.first(where: { $0.url == url }) ?? favorites.first
if let favicon = selectedFavorite?.favicon, let image = favicon.image {
image.prepareThumbnail(of: .init(width: 64, height: 64)) { image in
selectedFavorite?.favicon = .init(
image: image,
isMonogramImage: favicon.isMonogramImage,
backgroundColor: favicon.backgroundColor
)
completion(selectedFavorite)
}
} else {
completion(selectedFavorite)
}
}
func getSnapshot(for configuration: LockScreenFavoriteConfigurationIntent, in context: Context, completion: @escaping (LockScreenFavoriteEntry) -> Void) {
widgetFavorite(for: configuration.favorite?.url) { selectedFavorite in
completion(Entry(date: Date(), favorite: selectedFavorite))
}
}
func getTimeline(for configuration: LockScreenFavoriteConfigurationIntent, in context: Context, completion: @escaping (Timeline<LockScreenFavoriteEntry>) -> Void) {
widgetFavorite(for: configuration.favorite?.url) { selectedFavorite in
completion(Timeline(entries: [Entry(date: Date(), favorite: selectedFavorite)], policy: .never))
}
}
}
@available(iOS 16.0, *)
private struct LockScreenFavoriteView: View {
var entry: LockScreenFavoriteEntry
var body: some View {
ZStack {
AccessoryWidgetBackground()
.widgetBackground { EmptyView() }
if let fav = entry.favorite {
Group {
if let attributes = fav.favicon, let image = attributes.image {
FaviconImage(image: image, contentMode: .scaleAspectFit, includePadding: false) // includePadding forced to false here since we are providing our own padding below
.background(attributes.backgroundColor.cgColor.alpha == 0 ? .white : Color(attributes.backgroundColor))
.clipShape(RoundedRectangle(cornerRadius: 8, style: .continuous))
.padding(12)
} else {
Text(verbatim: fav.url.baseDomain?.first?.uppercased() ?? "")
.frame(maxWidth: .infinity, maxHeight: .infinity)
.font(.system(size: 28))
.clipped()
.padding(4)
.background(
Color.white.aspectRatio(1, contentMode: .fit)
.clipShape(RoundedRectangle(cornerRadius: 8, style: .continuous))
)
.padding(12)
.foregroundColor(Color.black)
}
}
.accessibilityLabel(fav.title ?? fav.url.absoluteString)
.widgetLabel(fav.title ?? "")
.widgetURL(fav.url)
} else {
Image(braveSystemName: "leo.brave.icon-monochrome")
.imageScale(.large)
.font(.system(size: 24))
.foregroundColor(Color.black)
.frame(maxWidth: .infinity, maxHeight: .infinity)
.background(
Color(white: 0.9)
.clipShape(RoundedRectangle(cornerRadius: 8, style: .continuous))
.padding(12)
)
}
}
}
}
#if DEBUG
@available(iOS 16.0, *)
// swiftlint:disable:next swiftui_previews_guard
struct LockScreenFavoriteViewWidget_Previews: PreviewProvider {
static var previews: some View {
Group {
LockScreenFavoriteView(entry: .init(date: .now, favorite: nil))
.previewDisplayName("No Favs")
LockScreenFavoriteView(
entry: .init(
date: .now,
favorite: .init(
url: URL(string: "https://brave.com")!,
title: "Brave",
favicon: .init(
image: nil,
isMonogramImage: false,
backgroundColor: .white
)
)
)
)
.previewDisplayName("Monogram")
LockScreenFavoriteView(
entry: .init(
date: .now,
favorite: .init(
url: URL(string: "https://wikipedia.com")!,
title: "Wikipedia",
favicon: .init(
image: mockImage,
isMonogramImage: false,
backgroundColor: .white
)
)
)
)
.previewDisplayName("Favicon")
}
.previewContext(WidgetPreviewContext(family: .accessoryCircular))
}
}
private let mockImage = UIImage(data: Data(base64Encoded: "iVBORw0KGgoAAAANSUhEUgAAAHgAAAB4CAYAAAA5ZDbSAAAAAXNSR0IArs4c6QAAAFBlWElmTU0AKgAAAAgAAgESAAMAAAABAAEAAIdpAAQAAAABAAAAJgAAAAAAA6ABAAMAAAABAAEAAKACAAQAAAABAAAAeKADAAQAAAABAAAAeAAAAAAaP3vwAAAPj0lEQVR4Ae2aC7Be0xXHi8QjRLxDSIREPFrSRIqOVzziTaqitMZo1btoq4YOKmmro0Mp+ki1ImmlyrRoU0VV05GQUKpeKUFIgkQEIR4JQvv7c49ua/b+vn3Od/bNzc1eM/97zt5n7bXW/u/3/u4nPpElM5AZyAxkBjIDmYHMQGYgM5AZyAxkBjIDmYHMQGYgM5AZyAxkBjIDmYHMQGYgM5AZyAxkBjIDmYHMQGYgM5AZyAxkBjIDmYE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