Skip to content

Commit

Permalink
Fix an issue with compliance popover not dismissing for self-hosted s…
Browse files Browse the repository at this point in the history
…ite (#23932)
  • Loading branch information
kean authored Dec 31, 2024
2 parents fef54d1 + f9012c3 commit a0a74e9
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 115 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@ public extension CGFloat {
public static let max: CGFloat = 48
}

public enum Hitbox {
public static let minTappableLength: CGFloat = 44
}

public enum Radius {
public static let small: CGFloat = 5
public static let medium: CGFloat = 10
Expand Down
2 changes: 1 addition & 1 deletion Modules/Sources/DesignSystem/Gallery/LengthGallery.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ struct LengthGallery: View {
ZStack {
RoundedRectangle(cornerRadius: .DS.Radius.small)
.fill(.background)
.frame(height: .DS.Hitbox.minTappableLength)
.frame(height: 44)
HStack {
Text(name)
.offset(x: .DS.Padding.double)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,28 @@
import SwiftUI
import JetpackStatsWidgetsCore
import DesignSystem

struct CompliancePopover: View {
@StateObject
var viewModel: CompliancePopoverViewModel

var body: some View {
VStack(alignment: .leading, spacing: .DS.Padding.double) {
titleText
subtitleText
analyticsToggle
footnote
buttonsHStack
ScrollView(.vertical) {
VStack(alignment: .leading, spacing: 8) {
titleText.padding(.top, 16)
subtitleText
analyticsToggle.padding(.top, 8)
footnote
}
.padding(20)
}
.safeAreaInset(edge: .bottom) {
HStack(spacing: 8) {
settingsButton
saveButton
}
.padding(20)
.background(Color(.systemBackground))
}
.padding(.DS.Padding.medium)
.fixedSize(horizontal: false, vertical: true)
}

private var titleText: some View {
Expand All @@ -33,7 +40,7 @@ struct CompliancePopover: View {
Toggle(Strings.toggleTitle, isOn: $viewModel.isAnalyticsEnabled)
.foregroundStyle(Color(.label))
.toggleStyle(UIAppColor.switchStyle)
.padding(.vertical, .DS.Padding.single)
.padding(.vertical, 8)
}

private var footnote: some View {
Expand All @@ -42,26 +49,19 @@ struct CompliancePopover: View {
.foregroundColor(.secondary)
}

private var buttonsHStack: some View {
HStack(spacing: .DS.Padding.single) {
settingsButton
saveButton
}.padding(.top, .DS.Padding.medium)
}

private var settingsButton: some View {
Button(action: {
self.viewModel.didTapSettings()
}) {
ZStack {
RoundedRectangle(cornerRadius: .DS.Padding.single)
.stroke(.gray, lineWidth: .DS.Border.thin)
RoundedRectangle(cornerRadius: 8)
.stroke(.gray, lineWidth: 0.5)
Text(Strings.settingsButtonTitle)
.font(.body)
}
}
.foregroundColor(AppColor.brand)
.frame(height: .DS.Hitbox.minTappableLength)
.frame(height: 44)
}

private var saveButton: some View {
Expand All @@ -76,7 +76,7 @@ struct CompliancePopover: View {
}
}
.foregroundColor(.white)
.frame(height: .DS.Hitbox.minTappableLength)
.frame(height: 44)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,10 @@ final class CompliancePopoverCoordinator: CompliancePopoverCoordinatorProtocol {
contextManager: ContextManager.shared
)
complianceViewModel.coordinator = self
let complianceViewController = CompliancePopoverViewController(viewModel: complianceViewModel)
let bottomSheetViewController = BottomSheetViewController(childViewController: complianceViewController, customHeaderSpacing: 0)

bottomSheetViewController.show(from: presentingViewController)
let complianceVC = CompliancePopoverViewController(viewModel: complianceViewModel)
complianceVC.sheetPresentationController?.detents = [.medium(), .large()]
complianceVC.isModalInPresentation = true
presentingViewController.present(complianceVC, animated: true)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,12 @@ import UIKit
import SwiftUI
import WordPressUI

final class CompliancePopoverViewController: UIViewController {

// MARK: - Dependencies

final class CompliancePopoverViewController: UIHostingController<CompliancePopover> {
private let viewModel: CompliancePopoverViewModel

// MARK: - Views

private let scrollView: UIScrollView = {
let view = UIScrollView()
view.showsVerticalScrollIndicator = false
view.translatesAutoresizingMaskIntoConstraints = false
return view
}()

private let hostingController: UIHostingController<CompliancePopover>

private var contentView: UIView {
return hostingController.view
}

// MARK: - Init

init(viewModel: CompliancePopoverViewModel) {
self.viewModel = viewModel
let content = CompliancePopover(viewModel: viewModel)
self.hostingController = UIHostingController(rootView: content)
super.init(nibName: nil, bundle: nil)
super.init(rootView: CompliancePopover(viewModel: viewModel))
}

required dynamic init?(coder aDecoder: NSCoder) {
Expand All @@ -40,60 +18,7 @@ final class CompliancePopoverViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()
self.addContentView()
self.viewModel.didDisplayPopover()
}

override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
// Calculate the size needed for the view to fit its content
let targetSize = CGSize(width: view.bounds.width, height: 0)
self.contentView.frame = CGRect(origin: .zero, size: targetSize)
let contentViewSize = contentView.systemLayoutSizeFitting(targetSize)
self.contentView.frame.size = contentViewSize

// Set the scrollView's content size to match the contentView's size
//
// Scroll is enabled / disabled automatically depending on whether the `contentSize` is bigger than the its size.
self.scrollView.contentSize = contentViewSize

// Set the preferred content size for the view controller to match the contentView's size
//
// This property should be updated when `DrawerPresentable.collapsedHeight` is `intrinsicHeight`.
// Because under the hood the `BottomSheetViewController` reads this property to layout its subviews.
self.preferredContentSize = contentViewSize
}

private func addContentView() {
self.view.addSubview(scrollView)
self.view.pinSubviewToAllEdges(scrollView)
self.hostingController.willMove(toParent: self)
self.addChild(hostingController)
self.contentView.translatesAutoresizingMaskIntoConstraints = true
self.scrollView.addSubview(contentView)
self.hostingController.didMove(toParent: self)
}
}

// MARK: - DrawerPresentable

extension CompliancePopoverViewController: DrawerPresentable {
var collapsedHeight: DrawerHeight {
if traitCollection.verticalSizeClass == .compact {
return .maxHeight
}
return .intrinsicHeight
}

var allowsUserTransition: Bool {
return false
}

var allowsDragToDismiss: Bool {
false
}

var allowsTapToDismiss: Bool {
return false
self.viewModel.didDisplayPopover()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Foundation
import UIKit
import WordPressUI

class CompliancePopoverViewModel: ObservableObject {
final class CompliancePopoverViewModel: ObservableObject {

@Published
var isAnalyticsEnabled: Bool = !WPAppAnalytics.userHasOptedOut()
Expand Down Expand Up @@ -45,13 +45,10 @@ class CompliancePopoverViewModel: ObservableObject {
let account = try? WPAccount.lookupDefaultWordPressComAccount(in: context)
return (account?.userID, account?.wordPressComRestApi)
}

guard let accountID, let restAPI else {
return
if let accountID, let restAPI {
let change = AccountSettingsChange.tracksOptOut(!isAnalyticsEnabled)
AccountSettingsService(userID: accountID.intValue, api: restAPI).saveChange(change)
}

let change = AccountSettingsChange.tracksOptOut(!isAnalyticsEnabled)
AccountSettingsService(userID: accountID.intValue, api: restAPI).saveChange(change)
coordinator?.dismiss()
defaults.didShowCompliancePopup = true
analyticsTracker.trackPrivacyChoicesBannerSaveButtonTapped(analyticsEnabled: isAnalyticsEnabled)
Expand Down

0 comments on commit a0a74e9

Please sign in to comment.