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 440
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Add new `CustomPrivacySensitiveModifier` SwiftUI view modifier that hides the content when in background or recording using `redacted(reason: .placeholder)`. Not using `.privacySensitive()` / `.redacted(reason: .privacy)` as they not working as expected due to `redactionReasons` not propagating through the environment in modals.
- Loading branch information
1 parent
00d5b10
commit 38ecb72
Showing
3 changed files
with
55 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
Sources/BraveUI/SwiftUI/CustomPrivacySensitiveModifier.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// Copyright 2021 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 SwiftUI | ||
|
||
/// Custom modifier to behave similar to `.privacySensitive()` | ||
/// The `.privacySensitive()` doesn't work in sheets because the | ||
/// `redactedReasons` doesn't get passed through the environment in sheets. | ||
private struct CustomPrivacySensitiveModifier: ViewModifier { | ||
|
||
@State private var isBackgrounded: Bool = false | ||
@State private var isCaptured: Bool = false | ||
|
||
func body(content: Content) -> some View { | ||
Group { | ||
if isBackgrounded || isCaptured { | ||
content | ||
.redacted(reason: .placeholder) | ||
} else { | ||
content | ||
} | ||
} | ||
.onAppear { | ||
isCaptured = UIScreen.main.isCaptured | ||
} | ||
.onReceive( | ||
NotificationCenter.default.publisher(for: UIScreen.capturedDidChangeNotification, object: nil) | ||
) { notification in | ||
isCaptured = (notification.object as? UIScreen)?.isCaptured ?? UIScreen.main.isCaptured | ||
} | ||
.onReceive( | ||
NotificationCenter.default.publisher(for: UIApplication.willResignActiveNotification) | ||
) { _ in | ||
isBackgrounded = true | ||
} | ||
.onReceive( | ||
NotificationCenter.default.publisher(for: UIApplication.didBecomeActiveNotification) | ||
) { _ in | ||
isBackgrounded = false | ||
} | ||
} | ||
} | ||
|
||
extension View { | ||
/// Redacts the given view if the app is backgrounded, or the system is actively | ||
/// recording, mirroring, or using AirPlay to stream the contents of the screen. | ||
public func customPrivacySensitive() -> some View { | ||
modifier(CustomPrivacySensitiveModifier()) | ||
} | ||
} |