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
/
StatsWidget.swift
132 lines (119 loc) · 4.38 KB
/
StatsWidget.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
// Copyright 2020 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 Shared
import BraveWidgetsModels
struct StatsWidget: Widget {
var body: some WidgetConfiguration {
StaticConfiguration(kind: "StatsWidget", provider: StatsProvider()) { entry in
StatsView(entry: entry)
}
.supportedFamilies([.systemMedium])
.configurationDisplayName(Strings.Widgets.shieldStatsTitle)
.description(Strings.Widgets.shieldStatsDescription)
#if swift(>=5.9)
.contentMarginsDisabled()
#endif
}
}
private struct StatsEntry: TimelineEntry {
var date: Date
var statData: [StatData]
}
private struct StatsProvider: TimelineProvider {
typealias Entry = StatsEntry
var stats: [StatData] {
let kinds: [StatKind] = [.adsBlocked, .dataSaved, .timeSaved]
return kinds.map { StatData(name: $0.name, value: $0.displayString, color: $0.valueColor) }
}
func placeholder(in context: Context) -> Entry {
Entry(date: Date(), statData: [])
}
func getSnapshot(in context: Context, completion: @escaping (Entry) -> Void) {
let entry = Entry(date: Date(), statData: stats)
completion(entry)
}
func getTimeline(in context: Context, completion: @escaping (Timeline<Entry>) -> Void) {
let entry = Entry(date: Date(), statData: stats)
let timeline = Timeline(entries: [entry], policy: .never)
completion(timeline)
}
}
private struct StatsView: View {
var entry: StatsEntry
@Environment(\.redactionReasons) var redactionReasons
private var placeholderOrPrivacyRedaction: Bool {
redactionReasons.contains(.placeholder) || redactionReasons.contains(.privacy)
}
var body: some View {
VStack {
HStack {
Image("brave.shields.done")
Text(Strings.Widgets.shieldStatsWidgetTitle)
.foregroundColor(Color(UIColor.braveLabel))
.font(.system(size: 13, weight: .semibold))
Spacer()
Image("brave-icon-no-bg")
.resizable()
.aspectRatio(contentMode: .fit)
.frame(height: 24)
}
.unredacted()
Spacer()
HStack(alignment: .top, spacing: 8) {
ForEach(entry.statData, id: \.name) { data in
Color.clear
.overlay(
VStack(spacing: 4) {
Text(verbatim: placeholderOrPrivacyRedaction ? "-" : data.value)
.font(.system(size: 32.0))
.foregroundColor(Color(data.color))
.minimumScaleFactor(0.5)
.lineLimit(1)
.unredacted()
Text(verbatim: data.name)
.font(.system(size: 10, weight: .semibold))
.multilineTextAlignment(.center)
.unredacted()
}
)
.frame(maxWidth: .infinity, maxHeight: .infinity)
.padding(4)
}
}
Spacer()
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
.padding(.vertical, 12)
.padding(.horizontal, 16)
.widgetBackground { Color(UIColor.secondaryBraveBackground) }
.foregroundColor(Color(UIColor.braveLabel))
}
}
// MARK: - Previews
#if DEBUG
struct StatsWidget_Previews: PreviewProvider {
static var stats: [StatData] {
let kinds: [StatKind] = [.adsBlocked, .dataSaved, .timeSaved]
return kinds.map { StatData(name: $0.name, value: $0.displayString, color: $0.valueColor) }
}
static var fullStats: [StatData] {
let ads = StatData(name: StatKind.adsBlocked.name, value: "113K", color: StatKind.adsBlocked.valueColor)
let data = StatData(name: StatKind.dataSaved.name, value: "3,47GB", color: StatKind.dataSaved.valueColor)
let time = StatData(name: StatKind.timeSaved.name, value: "1h", color: StatKind.timeSaved.valueColor)
return [ads, data, time]
}
static var previews: some View {
StatsView(entry: StatsEntry(date: Date(), statData: fullStats))
.previewContext(WidgetPreviewContext(family: .systemMedium))
StatsView(entry: StatsEntry(date: Date(), statData: stats))
.previewContext(WidgetPreviewContext(family: .systemMedium))
StatsView(entry: StatsEntry(date: Date(), statData: stats))
.redacted(reason: .placeholder)
.previewContext(WidgetPreviewContext(family: .systemMedium))
}
}
#endif