Files
comaps/iphone/Maps/Bridging/BridgeControllers.swift
Yannik Bloscheck bf75728e7d WIP: Modes
Signed-off-by: Yannik Bloscheck <git@yannikbloscheck.com>
2026-01-20 22:06:51 +01:00

86 lines
3.3 KiB
Swift

import SwiftUI
import UIKit
/// Class for accesing SwiftUI views from Objective-C code
@objc class BridgeControllers: NSObject {
/// The `ProfileView` for presentation in an alert
@objc static func profileAsAlert() -> UIViewController {
let profileBridgeController = UIHostingController(rootView: ProfileView(isPresentedAsAlert: true))
profileBridgeController.view.backgroundColor = .systemGroupedBackground
return profileBridgeController
}
/// The `RoutingOptionsView` for presentation in an alert
@objc static func routingOptions() -> UIViewController {
let routinOptionsBridgeController = UIHostingController(rootView: RoutingOptionsView())
routinOptionsBridgeController.view.backgroundColor = .systemGroupedBackground
return routinOptionsBridgeController
}
}
final class BridgeCell<Content: View>: UITableViewCell {
private let hostingController = UIHostingController<Content?>(rootView: nil)
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
hostingController.view.backgroundColor = .clear
}
required init?(coder aDecoder: NSCoder) {
fatalError("Has not been implemented")
}
func layout() {
self.hostingController.view.invalidateIntrinsicContentSize()
}
func set(content: Content, parentController: UIViewController) {
self.hostingController.rootView = content
self.hostingController.view.invalidateIntrinsicContentSize()
let requiresControllerMove = hostingController.parent != parentController
if requiresControllerMove {
parentController.addChild(hostingController)
}
if !self.contentView.subviews.contains(hostingController.view) {
self.contentView.addSubview(hostingController.view)
hostingController.view.translatesAutoresizingMaskIntoConstraints = false
hostingController.view.leadingAnchor.constraint(equalTo: self.contentView.leadingAnchor).isActive = true
hostingController.view.trailingAnchor.constraint(equalTo: self.contentView.trailingAnchor).isActive = true
hostingController.view.topAnchor.constraint(equalTo: self.contentView.topAnchor).isActive = true
hostingController.view.bottomAnchor.constraint(equalTo: self.contentView.bottomAnchor).isActive = true
}
if requiresControllerMove {
hostingController.didMove(toParent: parentController)
}
}
}
/// Class for using the SwiftUI `AboutView` in the interface builder
class AboutBridgeController: UIHostingController<AboutView> {
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder, rootView: AboutView())
}
}
/// Class for using the SwiftUI `SettingsView` in the interface builder
class SettingsBridgeController: UIHostingController<SettingsView> {
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder, rootView: SettingsView())
}
}
/// Class for using the SwiftUI `ProfileView` in the interface builder
class ProfileBridgeController: UIHostingController<ProfileView> {
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder, rootView: ProfileView())
self.view.tintColor = .toolbarAccent
}
}