From 86da4c7fd46d7225ae838fcd9a3eba8a776fb021 Mon Sep 17 00:00:00 2001 From: Andrew Breckenridge Date: Sat, 3 Oct 2020 22:47:20 -0700 Subject: [PATCH] Create SwiftUI Example --- Example/Example.xcodeproj/project.pbxproj | 8 ++- .../InputBarStyleSelectionController.swift | 7 +- Example/Example/SwiftUIExample.swift | 71 +++++++++++++++++++ 3 files changed, 83 insertions(+), 3 deletions(-) create mode 100644 Example/Example/SwiftUIExample.swift diff --git a/Example/Example.xcodeproj/project.pbxproj b/Example/Example.xcodeproj/project.pbxproj index cda41689..86876a1d 100755 --- a/Example/Example.xcodeproj/project.pbxproj +++ b/Example/Example.xcodeproj/project.pbxproj @@ -30,6 +30,7 @@ 38F0C20820C89D1600FF8DD3 /* InboxCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 38F0C20720C89D1600FF8DD3 /* InboxCell.swift */; }; 38FCF9D6219F797600A47350 /* READMEPreviewViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 38FCF9D5219F797600A47350 /* READMEPreviewViewController.swift */; }; 468A8728251C8AC20018D007 /* ButtonAnimationExample.swift in Sources */ = {isa = PBXBuildFile; fileRef = 468A8727251C8AC20018D007 /* ButtonAnimationExample.swift */; }; + 46E4094E252996D900D238A8 /* SwiftUIExample.swift in Sources */ = {isa = PBXBuildFile; fileRef = 46E4094D252996D900D238A8 /* SwiftUIExample.swift */; }; /* End PBXBuildFile section */ /* Begin PBXCopyFilesBuildPhase section */ @@ -71,6 +72,7 @@ 38F0C20720C89D1600FF8DD3 /* InboxCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InboxCell.swift; sourceTree = ""; }; 38FCF9D5219F797600A47350 /* READMEPreviewViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = READMEPreviewViewController.swift; sourceTree = ""; }; 468A8727251C8AC20018D007 /* ButtonAnimationExample.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ButtonAnimationExample.swift; sourceTree = ""; }; + 46E4094D252996D900D238A8 /* SwiftUIExample.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SwiftUIExample.swift; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -111,6 +113,7 @@ 3821ADE320F5359800DE0D1D /* CommonTableViewController.swift */, 3821ADE020F534DA00DE0D1D /* InputAccessoryExampleViewController.swift */, 3821ADDF20F534DA00DE0D1D /* SubviewExampleViewController.swift */, + 46E4094D252996D900D238A8 /* SwiftUIExample.swift */, 468A8726251C8AAB0018D007 /* Community Examples */, 38F0C1FC20C89C8700FF8DD3 /* InputBarExamples */, 38F0C20920C89D2D00FF8DD3 /* Cells */, @@ -255,6 +258,7 @@ 38C888C6202051B3000F2E2F /* ImageCell.swift in Sources */, 38DAD87D21A685C6001762CD /* NoTextViewInputBar.swift in Sources */, 3821ADE220F534DA00DE0D1D /* InputAccessoryExampleViewController.swift in Sources */, + 46E4094E252996D900D238A8 /* SwiftUIExample.swift in Sources */, 3821ADE620F5360B00DE0D1D /* InputBarStyle.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; @@ -324,7 +328,7 @@ GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; GCC_WARN_UNUSED_FUNCTION = YES; GCC_WARN_UNUSED_VARIABLE = YES; - IPHONEOS_DEPLOYMENT_TARGET = 12.0; + IPHONEOS_DEPLOYMENT_TARGET = 13.0; MTL_ENABLE_DEBUG_INFO = YES; ONLY_ACTIVE_ARCH = YES; SDKROOT = iphoneos; @@ -380,7 +384,7 @@ GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; GCC_WARN_UNUSED_FUNCTION = YES; GCC_WARN_UNUSED_VARIABLE = YES; - IPHONEOS_DEPLOYMENT_TARGET = 12.0; + IPHONEOS_DEPLOYMENT_TARGET = 13.0; MTL_ENABLE_DEBUG_INFO = NO; SDKROOT = iphoneos; SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule"; diff --git a/Example/Example/InputBarStyleSelectionController.swift b/Example/Example/InputBarStyleSelectionController.swift index c7311c43..4cc0d770 100755 --- a/Example/Example/InputBarStyleSelectionController.swift +++ b/Example/Example/InputBarStyleSelectionController.swift @@ -7,6 +7,7 @@ // import UIKit +import SwiftUI class InputBarStyleSelectionController: UITableViewController { @@ -48,7 +49,7 @@ class InputBarStyleSelectionController: UITableViewController { switch section { case 0: return 1 case 1...2: return styles.count - case 3: return 2 + case 3: return 3 default: fatalError("unknown section \(section)") } } @@ -60,6 +61,7 @@ class InputBarStyleSelectionController: UITableViewController { case (1...2, _): cell.textLabel?.text = styles[indexPath.row].rawValue case (3, 0): cell.textLabel?.text = "Tab bar example (Slack style)" case (3, 1): cell.textLabel?.text = "Send button animations" + case (3, 2): cell.textLabel?.text = "SwiftUI example" default: assertionFailure("unrecognized \(indexPath). Are you trying to add an additional example?") } @@ -93,6 +95,9 @@ class InputBarStyleSelectionController: UITableViewController { case 1: let example = ButtonAnimationExample(style: .imessage, conversation: convo) navigationController?.pushViewController(example, animated: true) + case 2: + let example = UIHostingController(rootView: SwiftUIExample.make(style: .imessage, conversation: convo)) + navigationController?.pushViewController(example, animated: true) default: fatalError("Unknown row \(indexPath.row) in Community Examples section. Are you trying to add a new example?") } diff --git a/Example/Example/SwiftUIExample.swift b/Example/Example/SwiftUIExample.swift new file mode 100644 index 00000000..daccae7f --- /dev/null +++ b/Example/Example/SwiftUIExample.swift @@ -0,0 +1,71 @@ +// +// SwiftUIExample.swift +// Example +// +// Created by Andrew Breckenridge on 10/3/20. +// Copyright © 2020 Nathan Tannar. All rights reserved. +// + +import UIKit +import SwiftUI +import InputBarAccessoryView + +var built: CommonTableViewController? + +struct CommonTableViewControllerUI: UIViewControllerRepresentable { + let style: InputBarStyle + let conversation: SampleData.Conversation + + var inputBar: InputBarAccessoryView { + if built == nil { + built = CommonTableViewController(style: style, conversation: conversation) + } + return built!.inputBar + } + + func makeUIViewController(context: Context) -> some UIViewController { + if built == nil { + built = CommonTableViewController(style: style, conversation: conversation) + } + return built! + } + + func updateUIViewController(_ uiViewController: UIViewControllerType, context: Context) { + } +} + +struct InputBarUI: UIViewRepresentable { + let view: InputBarAccessoryView + + func makeUIView(context: Context) -> some UIView { + return view + } + + func updateUIView(_ uiView: UIViewType, context: Context) { + } +} + + +public struct SwiftUIExample: View { + let style: InputBarStyle + let conversation: SampleData.Conversation + + var table: CommonTableViewControllerUI { + CommonTableViewControllerUI(style: style, conversation: conversation) + } + + + public var body: some View { + VStack { + self.table + InputBarUI(view: table.inputBar) + } + } + +} + +extension SwiftUIExample { + static func make(style: InputBarStyle, conversation: SampleData.Conversation) -> Self { + return Self.init(style: style, conversation: conversation) + } +}