Skip to content

Commit

Permalink
Wire up snooze to the widget.
Browse files Browse the repository at this point in the history
  • Loading branch information
samsymons committed Jul 21, 2024
1 parent 2d10357 commit 4a4aa1a
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
4 changes: 4 additions & 0 deletions Widgets/UserText.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ struct UserText {
value: "VPN is On",
comment: "Message describing VPN connected status")

static let vpnWidgetSnoozingStatus = NSLocalizedString("widget.vpn.status.snoozing",
value: "VPN is Snoozing",
comment: "Message describing VPN snoozing status")

static let vpnWidgetDisconnectedStatus = NSLocalizedString("widget.vpn.status.disconnected",
value: "VPN is Off",
comment: "Message describing VPN disconnected status")
Expand Down
32 changes: 30 additions & 2 deletions Widgets/VPNWidget.swift
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ struct VPNStatusView: View {
@Environment(\.openURL) private var openURL
var entry: VPNStatusTimelineProvider.Entry

private let snoozeTimingStore = NetworkProtectionSnoozeTimingStore(userDefaults: .networkProtectionGroupDefaults)

@ViewBuilder
var body: some View {
Group {
Expand Down Expand Up @@ -147,7 +149,26 @@ struct VPNStatusView: View {
.opacity(status.isConnected ? 0.8 : 0.6)

switch status {
case .connected, .connecting, .reasserting:
case .connected:
let buttonTitle = snoozeTimingStore.isSnoozing ? "Resume" : UserText.vpnWidgetDisconnectButton
let intent: any AppIntent = snoozeTimingStore.isSnoozing ? CancelSnoozeVPNIntent() : DisableVPNIntent()

Button(buttonTitle, intent: intent)
.font(.system(size: 14, weight: .semibold))
.foregroundStyle(snoozeTimingStore.isSnoozing ?
connectButtonForegroundColor(isDisabled: false) :
disconnectButtonForegroundColor(isDisabled: status != .connected))
.buttonStyle(.borderedProminent)
.buttonBorderShape(.roundedRectangle(radius: 8))
.tint(snoozeTimingStore.isSnoozing ?
Color(designSystemColor: .accent) :
disconnectButtonBackgroundColor(isDisabled: status != .connected)
)
.disabled(status != .connected)
.frame(height: 28)
.padding(.top, 6)
.padding(.bottom, 16)
case .connecting, .reasserting:
Button(UserText.vpnWidgetDisconnectButton, intent: DisableVPNIntent())
.font(.system(size: 14, weight: .semibold))
.foregroundStyle(disconnectButtonForegroundColor(isDisabled: status != .connected))
Expand Down Expand Up @@ -222,7 +243,14 @@ struct VPNStatusView: View {

private func title(with status: NEVPNStatus) -> String {
switch status {
case .connecting, .connected, .reasserting: return UserText.vpnWidgetConnectedStatus
case .connected:
let snoozeTimingStore = NetworkProtectionSnoozeTimingStore(userDefaults: .networkProtectionGroupDefaults)
if snoozeTimingStore.activeTiming != nil {
return UserText.vpnWidgetSnoozingStatus
} else {
return UserText.vpnWidgetConnectedStatus
}
case .connecting, .reasserting: return UserText.vpnWidgetConnectedStatus
case .disconnecting, .disconnected, .invalid: return UserText.vpnWidgetDisconnectedStatus
@unknown default: return "Unknown"
}
Expand Down
3 changes: 3 additions & 0 deletions Widgets/en.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@
/* Message describing VPN disconnected status */
"widget.vpn.status.disconnected" = "VPN is Off";

/* Message describing VPN snoozing status */
"widget.vpn.status.snoozing" = "VPN is Snoozing";

/* Subtitle describing VPN disconnected status */
"widget.vpn.subtitle.disconnected" = "Not connected";

0 comments on commit 4a4aa1a

Please sign in to comment.