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

Commit

Permalink
Fix #7460: Fix cookie consent notice dialog (#7462)
Browse files Browse the repository at this point in the history
  • Loading branch information
cuba authored and iccub committed May 16, 2023
1 parent ab3fe87 commit cd08836
Show file tree
Hide file tree
Showing 13 changed files with 78 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,9 @@ extension BrowserViewController {
guard Preferences.FullScreenCallout.omniboxCalloutCompleted.value else { return }

let popover = PopoverController(
contentController: CookieNotificationBlockingConsentViewController(),
contentController: CookieNotificationBlockingConsentViewController(yesCallback: {
FilterListStorage.shared.enableFilterList(for: FilterList.cookieConsentNoticesComponentID, isEnabled: true)
}),
contentSizeBehavior: .preferredContentSize)
popover.addsConvenientDismissalMargins = false
popover.present(from: topToolbar.locationView.shieldsButton, on: self)
Expand Down
20 changes: 10 additions & 10 deletions Sources/BraveUI/Images/GIFImage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import UIKit
import SwiftUI

public struct GIFImage: UIViewRepresentable {
private let asset: String
private let asset: NSDataAsset
private let animationRepeatCount: Int
private let animate: Bool

public init(asset: String, animationRepeatCount: Int = 1, animate: Bool) {
public init(asset: NSDataAsset, animationRepeatCount: Int = 1, animate: Bool) {
self.asset = asset
self.animationRepeatCount = animationRepeatCount
self.animate = animate
Expand All @@ -27,8 +27,8 @@ public struct GIFImage: UIViewRepresentable {
}

public class GIFImageView: UIView, CAAnimationDelegate {
private var assetName: String = ""
private var animationRepeatCount: Int = 1
private var asset: NSDataAsset
private var animationRepeatCount: Int

private let imageView = UIImageView()
private var firstFrame: UIImage?
Expand All @@ -40,10 +40,11 @@ public class GIFImageView: UIView, CAAnimationDelegate {
fatalError("init(coder:) has not been implemented")
}

public init(asset: String, animationRepeatCount: Int = 1) {
super.init(frame: .zero)
self.assetName = asset
public init(asset: NSDataAsset, animationRepeatCount: Int = 1) {
self.asset = asset
self.animationRepeatCount = animationRepeatCount

super.init(frame: .zero)
initView()
}

Expand All @@ -53,7 +54,7 @@ public class GIFImageView: UIView, CAAnimationDelegate {
}

private func loadAsset() {
let frames = Self.getFramesFrom(asset: assetName)
let frames = Self.getFramesFrom(asset: asset)
firstFrame = frames.first
lastFrame = frames.last
framesCount = frames.count
Expand Down Expand Up @@ -84,8 +85,7 @@ public class GIFImageView: UIView, CAAnimationDelegate {
}

/// Get `gif` image frames (an array of images) from an asset by its name
private class func getFramesFrom(asset: String) -> [UIImage] {
guard let asset = getAsset(name: asset) else { return [] }
private class func getFramesFrom(asset: NSDataAsset) -> [UIImage] {
return getFrames(from: asset.data)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"info" : {
"author" : "xcode",
"version" : 1
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import Growth
import BraveUI

public struct CookieNotificationBlockingConsentView: View {
public typealias YesCallback = () -> Void
public static let contentHeight = 480.0
public static let contentWidth = 344.0
private static let gifHeight = 328.0
Expand All @@ -22,11 +23,9 @@ public struct CookieNotificationBlockingConsentView: View {

@Environment(\.presentationMode) @Binding private var presentationMode
@State private var showAnimation = false

public var onYesButtonPressed: (() -> Void)?
public let onYesButtonPressed: YesCallback

private var yesButton: some View {

Button(Strings.yesBlockCookieConsentNotices,
action: {
withAnimation(animation) {
Expand All @@ -35,7 +34,9 @@ public struct CookieNotificationBlockingConsentView: View {

Task { @MainActor in
recordCookieListPromptP3A(answer: .tappedYes)
onYesButtonPressed?()
onYesButtonPressed()
// Allow the animation to play before dismissing
try await Task.sleep(seconds: 3.5)
self.dismiss()
}
}
Expand All @@ -59,27 +60,16 @@ public struct CookieNotificationBlockingConsentView: View {
public var body: some View {
ScrollView {
VStack {
VStack {
if !showAnimation {
VStack(spacing: Self.textPadding) {
Text(Strings.blockCookieConsentNoticesPopupTitle).font(.title)
Text(Strings.blockCookieConsentNoticesPopupDescription).font(.body)
}
.transition(transition)
.padding(Self.textPadding)
.padding(.top, 80)
.foregroundColor(Color(UIColor.braveLabel))
.multilineTextAlignment(.center)
.fixedSize(horizontal: false, vertical: true)
}
if let asset = cookieConsentAnimation {
content
.background(
GIFImage(asset: asset, animate: showAnimation)
.frame(width: Self.contentWidth, height: Self.gifHeight, alignment: .top),
alignment: .top
)
} else {
content
}
.frame(width: Self.contentWidth)
.frame(minHeight: Self.gifHeight)
.background(
GIFImage(asset: "cookie-consent-animation", animate: showAnimation)
.frame(width: Self.contentWidth, height: Self.gifHeight, alignment: .top),
alignment: .top
)

VStack(spacing: Self.textPadding) {
if !showAnimation {
Expand All @@ -101,6 +91,25 @@ public struct CookieNotificationBlockingConsentView: View {
}
}

private var content: some View {
VStack {
if !showAnimation {
VStack(spacing: Self.textPadding) {
Text(Strings.blockCookieConsentNoticesPopupTitle).font(.title)
Text(Strings.blockCookieConsentNoticesPopupDescription).font(.body)
}
.transition(transition)
.padding(Self.textPadding)
.padding(.top, 80)
.foregroundColor(Color(UIColor.braveLabel))
.multilineTextAlignment(.center)
.fixedSize(horizontal: false, vertical: true)
}
}
.frame(width: Self.contentWidth)
.frame(minHeight: Self.gifHeight)
}

private func dismiss() {
presentationMode.dismiss()
}
Expand All @@ -116,19 +125,42 @@ public struct CookieNotificationBlockingConsentView: View {
// Q68 If you have viewed the cookie consent block prompt, how did you react?
UmaHistogramEnumeration("Brave.Shields.CookieListPrompt", sample: answer)
}

private var cookieConsentAnimation: NSDataAsset? {
guard let asset = getAsset(name: "cookie-consent-animation") else {
assertionFailure()
return nil
}

return asset
}

/// Get `gif` image asset by its name. Will append `-dark` to the name when dark mode is enabled
private func getAsset(name: String) -> NSDataAsset? {
switch UITraitCollection.current.userInterfaceStyle {
case .light, .unspecified:
return NSDataAsset(name: name, bundle: .module)
case .dark:
return NSDataAsset(name: [name, "dark"].joined(separator: "-"), bundle: .module)
@unknown default:
return NSDataAsset(name: name, bundle: .module)
}
}
}



#if DEBUG
struct CookieNotificationBlockingConsentView_Previews: PreviewProvider {
static var previews: some View {
CookieNotificationBlockingConsentView()
CookieNotificationBlockingConsentView(onYesButtonPressed: {})
}
}
#endif

public class CookieNotificationBlockingConsentViewController: UIHostingController<CookieNotificationBlockingConsentView>, PopoverContentComponent {
public init() {
super.init(rootView: CookieNotificationBlockingConsentView())
public init(yesCallback: @escaping CookieNotificationBlockingConsentView.YesCallback) {
super.init(rootView: CookieNotificationBlockingConsentView(onYesButtonPressed: yesCallback))

self.preferredContentSize = CGSize(
width: CookieNotificationBlockingConsentView.contentWidth,
Expand Down

0 comments on commit cd08836

Please sign in to comment.