Files
Pulse/Sources/PulseUI/UIKItSupport/MainViewController.swift
2022-08-07 20:52:47 -04:00

56 lines
1.6 KiB
Swift
Raw Permalink 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) 20202022 Alexander Grebenyuk (github.com/kean).
#if os(iOS)
import Foundation
import UIKit
import Pulse
import SwiftUI
public final class MainViewController: UITabBarController {
private let viewModel: MainViewModel
public static var isAutomaticAppearanceOverrideRemovalEnabled = true
public init(store: LoggerStore = .shared, onDismiss: (() -> Void)? = nil) {
self.viewModel = MainViewModel(store: store, onDismiss: onDismiss)
super.init(nibName: nil, bundle: nil)
if MainViewController.isAutomaticAppearanceOverrideRemovalEnabled {
removeAppearanceOverrides()
}
}
required convenience init?(coder: NSCoder) {
self.init()
}
public override func viewDidLoad() {
super.viewDidLoad()
viewControllers = viewModel.items.enumerated().map {
let item = $0.element
let vc = UINavigationController(rootViewController: UIHostingController(rootView: viewModel.makeView(for: item)))
vc.tabBarItem = UITabBarItem(title: item.title, image: UIImage(systemName: item.imageName), tag: $0.offset)
return vc
}
}
}
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