Skip to content
This repository has been archived by the owner on May 10, 2024. It is now read-only.

Commit

Permalink
Fix #5744: Hide recovery phrase when app is backgrounded (#5755)
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
StephenHeaps authored Jul 29, 2022
1 parent 00d5b10 commit 38ecb72
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ struct BackupRecoveryPhraseView: View {
.padding(.vertical)
RecoveryPhraseGrid(data: recoveryWords, id: \.self) { word in
Text(verbatim: "\(word.index + 1). \(word.value)")
.noCapture()
.customPrivacySensitive()
.font(.footnote.bold())
.padding(8)
.frame(maxWidth: .infinity)
Expand Down
4 changes: 2 additions & 2 deletions BraveWallet/Crypto/Onboarding/VerifyRecoveryPhraseView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ struct VerifyRecoveryPhraseView: View {
tappedWord(word)
}) {
Text(verbatim: word.value)
.noCapture()
.customPrivacySensitive()
.font(.footnote.bold())
.foregroundColor(.primary)
.fixedSize(horizontal: false, vertical: true)
Expand Down Expand Up @@ -176,7 +176,7 @@ private struct SelectedWordsBox: View {
case .word(let word, let index, let isCorrect):
Button(action: { tappedWord(atIndex: index) }) {
Text(verbatim: "\(index + 1). \(word)")
.noCapture()
.customPrivacySensitive()
.padding(8)
.frame(maxWidth: .infinity)
.fixedSize(horizontal: false, vertical: true)
Expand Down
52 changes: 52 additions & 0 deletions Sources/BraveUI/SwiftUI/CustomPrivacySensitiveModifier.swift
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())
}
}

0 comments on commit 38ecb72

Please sign in to comment.