Files
purchases-ios/Sources/Support/DebugUI/DebugViewController.swift
T
NachoSoto 5ec9d36e22 New DebugViewController: UIKit counterpart for SwiftUI's debugRevenueCatOverlay (#2631)
Built on top of `DebugSwiftUIRootView`. This can be presented either
manually, or using `UIViewController.presentDebugRevenueCatOverlay`.

This looks exactly the same as the `SwiftUI` version:

![image](https://github.com/RevenueCat/purchases-ios/assets/685609/2ef22db4-0030-457e-a721-8daf2dcc1a49)
2023-06-13 09:37:52 -07:00

89 lines
2.5 KiB
Swift

//
// Copyright RevenueCat Inc. All Rights Reserved.
//
// Licensed under the MIT License (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://opensource.org/licenses/MIT
//
// DebugViewController.swift
//
// Created by Nacho Soto on 6/12/23.
import Foundation
#if DEBUG && os(iOS) && swift(>=5.8)
import SwiftUI
import UIKit
/// A view controller which allows debugging the current SDK setup.
///
/// You can present this yourself, or use `UIViewController.presentDebugRevenueCatOverlay`
/// for a default presentation using `UISheetPresentationController`.
///
/// - Seealso: `View.debugRevenueCatOverlay` for `SwiftUI`.
@available(iOS 16.0, *)
@objc(RCDebugViewController)
public final class DebugViewController: UIViewController {
private lazy var hostingController: UIHostingController<DebugSwiftUIRootView> = {
.init(rootView: .init())
}()
// swiftlint:disable:next missing_docs
public init() {
super.init(nibName: nil, bundle: nil)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
public override func loadView() {
super.loadView()
self.addChild(self.hostingController)
self.view.addSubview(self.hostingController.view)
self.hostingController.didMove(toParent: self)
}
public override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
self.hostingController.view.frame = self.view.bounds
}
}
@available(iOS 16.0, *)
extension UIViewController {
/// Presents a bottom sheet overlay on top of the current view controller
/// which allows debugging the current SDK setup.
///
/// - Seealso: `DebugViewController`.
/// - Seealso: `View.debugRevenueCatOverlay` for `SwiftUI`.
@objc(rc_presentDebugRevenueCatOverlayAnimated:)
public func presentDebugRevenueCatOverlay(animated: Bool = true) {
let controller = DebugViewController()
if let sheet = controller.sheetPresentationController {
sheet.detents = [
.custom(resolver: { context in context.maximumDetentValue * 0.2 }),
.medium(),
.large()
]
sheet.largestUndimmedDetentIdentifier = .medium
sheet.preferredCornerRadius = DebugSwiftUIRootView.cornerRadius
sheet.prefersGrabberVisible = true
}
self.present(controller, animated: animated)
}
}
#endif