Skip to content

Commit

Permalink
ViewControllerRepresentable
Browse files Browse the repository at this point in the history
  • Loading branch information
mattmassicotte committed Mar 26, 2024
1 parent c8153b2 commit 3edc12d
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,15 @@ public protocol NSUIViewRepresentable {
func makeNSUIView(context: Context) -> NSUIViewType
func updateNSUIView(_ view: NSUIViewType, context: Context)
}

public protocol NSUIViewControllerRepresentable: UIViewControllerRepresentable {
associatedtype NSUIViewControllerType

@MainActor
func makeNSUIViewController(context: Context) -> NSUIViewControllerType
@MainActor
func updateNSUIViewController(_ viewController: NSUIViewControllerType, context: Context)
}
```

## Conventions
Expand Down
48 changes: 48 additions & 0 deletions Sources/NSUI/ViewControllerRepresentable.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import SwiftUI

#if canImport(AppKit) && !targetEnvironment(macCatalyst)
public protocol NSUIViewControllerRepresentable: NSViewControllerRepresentable {
associatedtype NSUIViewControllerType: NSUIViewController

@MainActor
func makeNSUIViewController(context: Self.Context) -> Self.NSUIViewControllerType

@MainActor
func updateNSUIViewController(_ viewController: Self.NSUIViewControllerType, context: Self.Context)
}

public extension NSUIViewControllerRepresentable {
@MainActor
func makeNSViewController(context: Self.Context) -> Self.NSUIViewControllerType {
makeNSUIViewController(context: context)
}

@MainActor
func updateNSViewController(_ viewController: Self.NSUIViewControllerType, context: Self.Context) {
updateNSUIViewController(viewController, context: context)
}
}
#elseif canImport(UIKit)
public protocol NSUIViewControllerRepresentable: UIViewControllerRepresentable {
associatedtype NSUIViewControllerType: NSUIViewController

@MainActor
func makeNSUIViewController(context: Self.Context) -> Self.NSUIViewControllerType

@MainActor
func updateNSUIViewController(_ viewController: Self.NSUIViewControllerType, context: Self.Context)
}

public extension NSUIViewControllerRepresentable {
@MainActor
func makeUIViewController(context: Self.Context) -> Self.NSUIViewControllerType {
makeNSUIViewController(context: context)
}

@MainActor
func updateUIViewController(_ viewController: Self.NSUIViewControllerType, context: Self.Context) {
updateNSUIViewController(viewController, context: context)
}
}
#endif

0 comments on commit 3edc12d

Please sign in to comment.