Files
Pulse/Sources/PulseUI/UIKItSupport/MainViewController.swift
T
2021-11-08 16:45:55 -05:00

112 lines
3.7 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// The MIT License (MIT)
//
// Copyright (c) 20202021 Alexander Grebenyuk (github.com/kean).
#if os(iOS)
import Foundation
import UIKit
import PulseCore
import SwiftUI
public final class MainViewController: UITabBarController {
private let model: Any?
public static var isAutomaticAppearanceOverrideRemovalEnabled = true
public init(store: LoggerStore = .default, onDismiss: (() -> Void)? = nil) {
if #available(iOS 13.0, *) {
self.model = MainViewModel(store: store, onDismiss: onDismiss)
} else {
self.model = nil
}
super.init(nibName: nil, bundle: nil)
if MainViewController.isAutomaticAppearanceOverrideRemovalEnabled {
removeAppearanceOverrides()
}
}
required convenience init?(coder: NSCoder) {
self.init()
}
public override func viewDidLoad() {
super.viewDidLoad()
if #available(iOS 13.0, *) {
addConsoleView()
} else {
addPlaceholderView()
}
}
@available(iOS 13, *)
private func addConsoleView() {
guard let model = model as? MainViewModel else {
return addPlaceholderView()
}
viewControllers = model.items.enumerated().map {
let item = $0.element
let vc = UIHostingController(rootView: model.makeView(for: item))
vc.tabBarItem = UITabBarItem(title: item.title, image: UIImage(systemName: item.imageName), tag: $0.offset)
return vc
}
}
private func addPlaceholderView() {
let stack = UIStackView(arrangedSubviews: [])
stack.alignment = .center
stack.spacing = 12
stack.axis = .vertical
let label = UILabel()
label.text = "Console is only available\non iOS 13 and higher"
label.textColor = UIColor.black
label.textAlignment = .center
label.numberOfLines = 0
stack.addArrangedSubview(label)
if navigationController != nil || presentingViewController != nil {
let buttonClose = UIButton(type: .system)
buttonClose.setTitle("Close", for: .normal)
buttonClose.addTarget(self, action: #selector(buttonCloseTapped), for: .touchUpInside)
stack.addArrangedSubview(buttonClose)
}
view.backgroundColor = UIColor(red: 230.0/255.0, green: 230.0/255.0, blue: 230.0/255.0, alpha: 1.0)
view.addSubview(stack)
stack.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
stack.leadingAnchor.constraint(greaterThanOrEqualTo: view.safeAreaLayoutGuide.leadingAnchor, constant: 1),
stack.trailingAnchor.constraint(lessThanOrEqualToSystemSpacingAfter: view.safeAreaLayoutGuide.trailingAnchor, multiplier: 1),
stack.centerXAnchor.constraint(equalTo: view.centerXAnchor),
stack.centerYAnchor.constraint(equalTo: view.centerYAnchor)
])
}
@objc private func buttonCloseTapped() {
if let navigationController = self.navigationController {
navigationController.popViewController(animated: true)
} else if let presentingViewController = self.presentingViewController {
presentingViewController.dismiss(animated: true, completion: nil)
}
}
}
private var isAppearanceCleanupNeeded = true
private func removeAppearanceOverrides() {
guard isAppearanceCleanupNeeded else { return }
isAppearanceCleanupNeeded = false
let appearance = UINavigationBar.appearance(whenContainedInInstancesOf: [MainViewController.self])
appearance.tintColor = nil
appearance.barTintColor = nil
appearance.titleTextAttributes = nil
appearance.isTranslucent = true
}
#endif