Skip to content
This repository has been archived by the owner on Jan 2, 2024. It is now read-only.

[HOLD] Components system example #5

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 4 additions & 14 deletions Demo/Demo.xcodeproj/project.pbxproj
Expand Up @@ -7,22 +7,22 @@
objects = {

/* Begin PBXBuildFile section */
00DA595E29AEA4A800C84FE7 /* RootWindow.swift in Sources */ = {isa = PBXBuildFile; fileRef = 00DA595D29AEA4A800C84FE7 /* RootWindow.swift */; };
67359891299ED732002EE8D1 /* Components in Frameworks */ = {isa = PBXBuildFile; productRef = 67359890299ED732002EE8D1 /* Components */; };
6771E7AE299C00050012002D /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6771E7AD299C00050012002D /* AppDelegate.swift */; };
6771E7B0299C00050012002D /* SceneDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6771E7AF299C00050012002D /* SceneDelegate.swift */; };
6771E7B2299C00050012002D /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6771E7B1299C00050012002D /* ViewController.swift */; };
6771E7B5299C00050012002D /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 6771E7B3299C00050012002D /* Main.storyboard */; };
6771E7B7299C00060012002D /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 6771E7B6299C00060012002D /* Assets.xcassets */; };
6771E7BA299C00060012002D /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 6771E7B8299C00060012002D /* LaunchScreen.storyboard */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
00DA595D29AEA4A800C84FE7 /* RootWindow.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RootWindow.swift; sourceTree = "<group>"; };
6735988F299ED71F002EE8D1 /* wikipedia-ios-components */ = {isa = PBXFileReference; lastKnownFileType = wrapper; name = "wikipedia-ios-components"; path = ..; sourceTree = "<group>"; };
6771E7AA299C00050012002D /* Demo.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Demo.app; sourceTree = BUILT_PRODUCTS_DIR; };
6771E7AD299C00050012002D /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
6771E7AF299C00050012002D /* SceneDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SceneDelegate.swift; sourceTree = "<group>"; };
6771E7B1299C00050012002D /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = "<group>"; };
6771E7B4299C00050012002D /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = "<group>"; };
6771E7B6299C00060012002D /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
6771E7B9299C00060012002D /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = "<group>"; };
6771E7BB299C00060012002D /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
Expand Down Expand Up @@ -64,7 +64,7 @@
6771E7AD299C00050012002D /* AppDelegate.swift */,
6771E7AF299C00050012002D /* SceneDelegate.swift */,
6771E7B1299C00050012002D /* ViewController.swift */,
6771E7B3299C00050012002D /* Main.storyboard */,
00DA595D29AEA4A800C84FE7 /* RootWindow.swift */,
6771E7B6299C00060012002D /* Assets.xcassets */,
6771E7B8299C00060012002D /* LaunchScreen.storyboard */,
6771E7BB299C00060012002D /* Info.plist */,
Expand Down Expand Up @@ -142,7 +142,6 @@
files = (
6771E7BA299C00060012002D /* LaunchScreen.storyboard in Resources */,
6771E7B7299C00060012002D /* Assets.xcassets in Resources */,
6771E7B5299C00050012002D /* Main.storyboard in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand All @@ -156,20 +155,13 @@
6771E7B2299C00050012002D /* ViewController.swift in Sources */,
6771E7AE299C00050012002D /* AppDelegate.swift in Sources */,
6771E7B0299C00050012002D /* SceneDelegate.swift in Sources */,
00DA595E29AEA4A800C84FE7 /* RootWindow.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
/* End PBXSourcesBuildPhase section */

/* Begin PBXVariantGroup section */
6771E7B3299C00050012002D /* Main.storyboard */ = {
isa = PBXVariantGroup;
children = (
6771E7B4299C00050012002D /* Base */,
);
name = Main.storyboard;
sourceTree = "<group>";
};
6771E7B8299C00060012002D /* LaunchScreen.storyboard */ = {
isa = PBXVariantGroup;
children = (
Expand Down Expand Up @@ -307,7 +299,6 @@
INFOPLIST_FILE = Demo/Info.plist;
INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents = YES;
INFOPLIST_KEY_UILaunchStoryboardName = LaunchScreen;
INFOPLIST_KEY_UIMainStoryboardFile = Main;
INFOPLIST_KEY_UISupportedInterfaceOrientations_iPad = "UIInterfaceOrientationPortrait UIInterfaceOrientationPortraitUpsideDown UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight";
INFOPLIST_KEY_UISupportedInterfaceOrientations_iPhone = "UIInterfaceOrientationPortrait UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight";
IPHONEOS_DEPLOYMENT_TARGET = 14.0;
Expand Down Expand Up @@ -339,7 +330,6 @@
INFOPLIST_FILE = Demo/Info.plist;
INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents = YES;
INFOPLIST_KEY_UILaunchStoryboardName = LaunchScreen;
INFOPLIST_KEY_UIMainStoryboardFile = Main;
INFOPLIST_KEY_UISupportedInterfaceOrientations_iPad = "UIInterfaceOrientationPortrait UIInterfaceOrientationPortraitUpsideDown UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight";
INFOPLIST_KEY_UISupportedInterfaceOrientations_iPhone = "UIInterfaceOrientationPortrait UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight";
IPHONEOS_DEPLOYMENT_TARGET = 14.0;
Expand Down
13 changes: 0 additions & 13 deletions Demo/Demo/AppDelegate.swift
Expand Up @@ -3,27 +3,14 @@ import UIKit
@main
class AppDelegate: UIResponder, UIApplicationDelegate {



func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
return true
}

// MARK: UISceneSession Lifecycle

func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
// Called when a new scene session is being created.
// Use this method to select a configuration to create the new scene with.
return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
}

func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
// Called when the user discards a scene session.
// If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
// Use this method to release any resources that were specific to the discarded scenes, as they will not return.
}


}

24 changes: 0 additions & 24 deletions Demo/Demo/Base.lproj/Main.storyboard

This file was deleted.

2 changes: 0 additions & 2 deletions Demo/Demo/Info.plist
Expand Up @@ -15,8 +15,6 @@
<string>Default Configuration</string>
<key>UISceneDelegateClassName</key>
<string>$(PRODUCT_MODULE_NAME).SceneDelegate</string>
<key>UISceneStoryboardFile</key>
<string>Main</string>
</dict>
</array>
</dict>
Expand Down
13 changes: 13 additions & 0 deletions Demo/Demo/RootWindow.swift
@@ -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)
}

}
41 changes: 7 additions & 34 deletions Demo/Demo/SceneDelegate.swift
@@ -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.
}


}


73 changes: 68 additions & 5 deletions Demo/Demo/ViewController.swift
@@ -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)
Copy link
Collaborator

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:

let featureNavigationController = WKComponentHostingController(rootView: WKFeatureNavigationView(delegate: self))
addComponent(featureNavigationController, pinToEdges: true, respectSafeArea: true)

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)
}

}
6 changes: 0 additions & 6 deletions Sources/Components/Components.swift

This file was deleted.

@@ -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
}

}