Skip to content

Commit fd17961

Browse files
author
Michael Long
committed
Update app and navigator to support WidgetControllerType
1 parent 0cc9412 commit fd17961

File tree

4 files changed

+17
-32
lines changed

4 files changed

+17
-32
lines changed

RxSwiftWidgetsDemo/AppDelegate.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,10 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UISplitViewControllerDele
2424
}
2525

2626
let widget = MainMenuWidget()
27-
let vc = UIWidgetHostController(widget)
2827

2928
window = UIWindow(frame: UIScreen.main.bounds)
3029
let navigationController = UINavigationController()
31-
navigationController.addChild(vc)
30+
navigationController.addChild(widget.controller(with: WidgetContext()))
3231
window?.rootViewController = navigationController
3332
window?.makeKeyAndVisible()
3433

Sources/RxSwiftWidgets/Core/WidgetNavigation.swift

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,38 +26,34 @@ public struct WidgetNavigator {
2626
// dismissible functionality
2727

2828
/// Pushes the widget onto the navigation stack in a new UIWidgetHostController.
29-
public func push(_ widget: Widget, animated: Bool = true) {
29+
public func push(_ widget: WidgetControllerType, animated: Bool = true) {
3030
let context = self.context.set(presentation: .pushed)
31-
let viewController = UIWidgetHostController(widget, with: context)
31+
let viewController = widget.controller(with: context)
3232
navigationController?.pushViewController(viewController, animated: animated)
3333
}
3434

3535
/// Pushes the widget onto the navigation stack in a new UIWidgetHostController with a return value handler.
36-
public func push<ReturnType>(_ widget: Widget, animated: Bool = true, onDismiss handler: @escaping WidgetDismissibleReturnHandler<ReturnType>) {
37-
let context = self.context.set(presentation: .pushed)
36+
public func push<ReturnType>(_ widget: WidgetControllerType, animated: Bool = true,
37+
onDismiss handler: @escaping WidgetDismissibleReturnHandler<ReturnType>) {
3838
let dismissible = WidgetDismissibleReturn<ReturnType>(handler)
39-
let viewController = UIWidgetHostController(widget, with: context, dismissible: dismissible)
39+
let context = self.context.set(presentation: .pushed).set(dismissible: dismissible)
40+
let viewController = widget.controller(with: context)
4041
navigationController?.pushViewController(viewController, animated: animated)
4142
}
4243

4344
/// Presents a widget on the navigation stack in a new UIWidgetHostController.
44-
public func present(_ widget: Widget, animated: Bool = true) {
45+
public func present(_ widget: WidgetControllerType, animated: Bool = true) {
4546
let context = self.context.set(presentation: .presented)
46-
let viewController = UIWidgetHostController(widget, with: context)
47+
let viewController = widget.controller(with: context)
4748
navigationController?.present(viewController, animated: animated, completion: nil)
4849
}
4950

5051
/// Presents a widget on the navigation stack in a new UIWidgetHostController with a return value handler.
51-
public func present<ReturnType>(_ widget: Widget, animated: Bool = true, onDismiss handler: @escaping WidgetDismissibleReturnHandler<ReturnType>) {
52-
let context = self.context.set(presentation: .presented)
52+
public func present<ReturnType>(_ widget: WidgetControllerType, animated: Bool = true,
53+
onDismiss handler: @escaping WidgetDismissibleReturnHandler<ReturnType>) {
5354
let dismissible = WidgetDismissibleReturn<ReturnType>(handler)
54-
let viewController = UIWidgetHostController(widget, with: context, dismissible: dismissible)
55-
navigationController?.present(viewController, animated: animated, completion: nil)
56-
}
57-
58-
public func present(_ widget: WidgetController, animated: Bool = true) {
59-
let context = self.context.set(presentation: .alert)
60-
let viewController = widget.build(with: context)
55+
let context = self.context.set(presentation: .presented).set(dismissible: dismissible)
56+
let viewController = widget.controller(with: context)
6157
navigationController?.present(viewController, animated: animated, completion: nil)
6258
}
6359

Sources/RxSwiftWidgets/Extensions/UIWidgetHostController.swift

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,6 @@ open class UIWidgetHostController: UIViewController {
2424
.set(viewController: self)
2525
}
2626

27-
public init(_ widget: Widget, with context: WidgetContext, dismissible: WidgetDismissibleType) {
28-
super.init(nibName: nil, bundle: nil)
29-
self.widget = widget
30-
self.context = context.new()
31-
.set(viewController: self)
32-
.set(dismissible: dismissible)
33-
}
34-
3527
required public init?(coder: NSCoder) {
3628
fatalError("init(coder:) has not been implemented")
3729
}

Sources/RxSwiftWidgets/Widgets/Navigation/AlertWidget.swift

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,9 @@
77

88
import UIKit
99

10-
public protocol WidgetController {
11-
func build(with context: WidgetContext) -> UIViewController
12-
}
13-
14-
public struct AlertWidget: WidgetController {
10+
public struct AlertWidget
11+
: WidgetControllerType
12+
{
1513

1614
internal let title: String?
1715
internal let message: String?
@@ -23,7 +21,7 @@ public struct AlertWidget: WidgetController {
2321
self.message = message
2422
}
2523

26-
public func build(with context: WidgetContext) -> UIViewController {
24+
public func controller(with context: WidgetContext) -> UIViewController {
2725
let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
2826
let context = context.set(viewController: alert)
2927
for action in actions {

0 commit comments

Comments
 (0)