This repository has been archived by the owner on Jan 2, 2024. It is now read-only.
[HOLD] Components system example #5
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import UIKit | ||
import Components | ||
|
||
/// The root window for the app. Trait collection changes are fed into the Components system here. | ||
final class RootWindow: UIWindow { | ||
|
||
override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) { | ||
super.traitCollectionDidChange(previousTraitCollection) | ||
|
||
WKAppEnvironment.current.set(theme: traitCollection.userInterfaceStyle == .light ? WKTheme.light : WKTheme.dark, traitCollection: traitCollection) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,18 @@ | ||
import UIKit | ||
import Components | ||
|
||
class SceneDelegate: UIResponder, UIWindowSceneDelegate { | ||
|
||
var window: UIWindow? | ||
|
||
var window: RootWindow? | ||
|
||
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) { | ||
// Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`. | ||
// If using a storyboard, the `window` property will automatically be initialized and attached to the scene. | ||
// This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead). | ||
guard let _ = (scene as? UIWindowScene) else { return } | ||
} | ||
|
||
func sceneDidDisconnect(_ scene: UIScene) { | ||
// Called as the scene is being released by the system. | ||
// This occurs shortly after the scene enters the background, or when its session is discarded. | ||
// Release any resources associated with this scene that can be re-created the next time the scene connects. | ||
// The scene may re-connect later, as its session was not necessarily discarded (see `application:didDiscardSceneSessions` instead). | ||
} | ||
|
||
func sceneDidBecomeActive(_ scene: UIScene) { | ||
// Called when the scene has moved from an inactive state to an active state. | ||
// Use this method to restart any tasks that were paused (or not yet started) when the scene was inactive. | ||
} | ||
guard let windowScene = (scene as? UIWindowScene) else { return } | ||
|
||
func sceneWillResignActive(_ scene: UIScene) { | ||
// Called when the scene will move from an active state to an inactive state. | ||
// This may occur due to temporary interruptions (ex. an incoming phone call). | ||
window = RootWindow(windowScene: windowScene) | ||
window?.rootViewController = FeatureNavigationController() | ||
window?.makeKeyAndVisible() | ||
} | ||
|
||
func sceneWillEnterForeground(_ scene: UIScene) { | ||
// Called as the scene transitions from the background to the foreground. | ||
// Use this method to undo the changes made on entering the background. | ||
} | ||
|
||
func sceneDidEnterBackground(_ scene: UIScene) { | ||
// Called as the scene transitions from the foreground to the background. | ||
// Use this method to save data, release shared resources, and store enough scene-specific state information | ||
// to restore the scene back to its current state. | ||
} | ||
|
||
|
||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,76 @@ | ||
import UIKit | ||
import SwiftUI | ||
import Components | ||
|
||
class ViewController: UIViewController { | ||
class FeatureNavigationController: WKCanvasViewController, WKFeatureNavigationHostingControllerDelegate { | ||
|
||
override func viewDidLoad() { | ||
super.viewDidLoad() | ||
print(Components().text) | ||
} | ||
override func viewDidLoad() { | ||
super.viewDidLoad() | ||
|
||
let featureNavigationController = WKFeatureNavigationHostingController(delegate: self) | ||
addComponent(featureNavigationController, pinToEdges: true, respectSafeArea: true) | ||
} | ||
|
||
func featureNavigationUserDidTap(_ feature: WKFeature) { | ||
switch feature { | ||
case .buttons: | ||
present(ButtonViewController(nibName: nil, bundle: nil), animated: true) | ||
case .topRead: | ||
present(TopReadController(), animated: true) | ||
case .randomPhoto: | ||
present(RandomPhotoController(), animated: true) | ||
case .sayHello: | ||
present(WKUICommunicationViewController(), animated: true) | ||
} | ||
} | ||
|
||
} | ||
|
||
/// Manually laying out `UIView`-based components on something that isn't a `WKCanvas`. The components react to environment changes but the base `UIView` does not. | ||
class ButtonViewController: UIViewController { | ||
|
||
override func loadView() { | ||
self.view = UIView() | ||
|
||
view.backgroundColor = .systemOrange | ||
|
||
let largeButton = WKButton(configuration: .large(text: "I'm a large button")) | ||
let smallButton = WKButton(configuration: .small(text: "I'm a small button")) | ||
|
||
view.addSubview(largeButton) | ||
view.addSubview(smallButton) | ||
|
||
NSLayoutConstraint.activate([ | ||
largeButton.topAnchor.constraint(equalTo: view.topAnchor, constant: 55), | ||
largeButton.leadingAnchor.constraint(equalTo: view.readableContentGuide.leadingAnchor), | ||
largeButton.trailingAnchor.constraint(lessThanOrEqualTo: view.readableContentGuide.trailingAnchor), | ||
smallButton.topAnchor.constraint(equalTo: largeButton.bottomAnchor, constant: 25), | ||
smallButton.leadingAnchor.constraint(equalTo: largeButton.leadingAnchor), | ||
smallButton.trailingAnchor.constraint(lessThanOrEqualTo: view.readableContentGuide.trailingAnchor) | ||
]) | ||
|
||
} | ||
|
||
} | ||
|
||
class TopReadController: WKCanvasViewController { | ||
|
||
override func viewDidLoad() { | ||
super.viewDidLoad() | ||
|
||
let topReadController = WKTopReadHostingController(viewModel: WKTopReadViewModel(items: ["these", "are", "some", "top", "read", "items"])) | ||
addComponent(topReadController, pinToEdges: true) | ||
} | ||
|
||
} | ||
|
||
class RandomPhotoController: WKCanvasViewController { | ||
|
||
override func viewDidLoad() { | ||
super.viewDidLoad() | ||
|
||
let randomPhotoController = WKRandomPhotoViewController(viewModel: WKRandomPhotoViewModel(configuration: .cats)) | ||
addComponent(randomPhotoController, pinToEdges: true, respectSafeArea: true) | ||
} | ||
|
||
} |
This file was deleted.
Oops, something went wrong.
52 changes: 52 additions & 0 deletions
52
Sources/Components/Components/Base/Component Base Classes/WKComponentHostingController.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import SwiftUI | ||
import Combine | ||
|
||
/// SwiftUI `View` via `UIHostingController` based Component | ||
open class WKComponentHostingController<HostedView: View>: UIHostingController<HostedView> { | ||
|
||
// MARK: - Private Properties | ||
|
||
private var cancellables = Set<AnyCancellable>() | ||
|
||
// MARK: - Public Properties | ||
|
||
var appEnvironment: WKAppEnvironment { | ||
return WKAppEnvironment.current | ||
} | ||
|
||
var theme: WKTheme { | ||
return WKAppEnvironment.current.theme | ||
} | ||
|
||
// MARK: - Public | ||
|
||
public override init(rootView: HostedView) { | ||
super.init(rootView: rootView) | ||
subscribeToAppEnvironmentChanges() | ||
} | ||
|
||
public override init?(coder aDecoder: NSCoder, rootView: HostedView) { | ||
super.init(coder: aDecoder, rootView: rootView) | ||
subscribeToAppEnvironmentChanges() | ||
} | ||
|
||
public required init?(coder aDecoder: NSCoder) { | ||
super.init(coder: aDecoder) | ||
subscribeToAppEnvironmentChanges() | ||
} | ||
|
||
// MARK: - WKAppEnvironment Subscription | ||
|
||
private func subscribeToAppEnvironmentChanges() { | ||
WKAppEnvironment.publisher | ||
.sink(receiveValue: { _ in self.appEnvironmentDidChange() }) | ||
.store(in: &cancellables) | ||
} | ||
|
||
// MARK: - Subclass Overrides | ||
|
||
public func appEnvironmentDidChange() { | ||
// Subclasses should implement | ||
} | ||
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I may be missing a clear benefit, but for this simple example I would avoid the additional feature-specific view controller layer with
WKFeatureNavigationHostingController
and instantiate it like: