1-
1+ import SwiftUI
2+ import SwiftUIHosting
23import UIKit
34
4- /**
5- A context object that provides a concrete view to display
6- */
5+ /// A context object that provides a concrete view to display
76open class FloatingDisplayContext : Hashable {
87
9- public static func == ( lhs: FloatingDisplayContext , rhs: FloatingDisplayContext ) -> Bool {
8+ public static func == ( lhs: FloatingDisplayContext , rhs: FloatingDisplayContext ) -> Bool {
109 lhs === rhs
1110 }
1211
@@ -16,24 +15,9 @@ open class FloatingDisplayContext: Hashable {
1615
1716 private let factory : ( ) -> FloatingDisplayViewType
1817
19- public var transition : FloatingDisplayTransitionType
20-
21- public var position : FloatingDisplayController . DisplayPosition
22-
23- /// A view that currently displaying.
24- public private( set) weak var view : FloatingDisplayViewType ?
18+ public let transition : FloatingDisplayTransitionType
2519
26- /// A Boolean value that indicates deliverly was cancelled.
27- open var wasCancelled : Bool = false
28-
29- @available ( * , deprecated, message: " Use the new initializer " )
30- public init (
31- factory: @escaping ( ) -> FloatingDisplayViewType
32- ) {
33- self . factory = factory
34- self . transition = FloatingDisplaySlideInTrantision ( )
35- self . position = . top
36- }
20+ public let position : FloatingDisplayController . DisplayPosition
3721
3822 public init (
3923 viewBuilder: @escaping ( ) -> FloatingDisplayViewType ,
@@ -48,8 +32,43 @@ open class FloatingDisplayContext: Hashable {
4832 func makeView( ) -> FloatingDisplayViewType {
4933 factory ( )
5034 }
35+ }
36+
37+ extension FloatingDisplayContext {
38+
39+ public convenience init < Content: View > (
40+ position: FloatingDisplayController . DisplayPosition ,
41+ transition: FloatingDisplayTransitionType ,
42+ @ViewBuilder content: @escaping ( ) -> Content
43+ ) {
44+
45+ self . init (
46+ viewBuilder: {
47+ _HostingWrapperView ( hostingView: SwiftUIHostingView ( content: content) )
48+ } ,
49+ position: position,
50+ transition: transition
51+ )
52+
53+ }
5154
52- func setView( _ view: FloatingDisplayViewType ) {
53- self . view = view
55+ }
56+
57+ private final class _HostingWrapperView : SnackbarDraggableBase {
58+
59+ private let hostingView : SwiftUIHostingView
60+
61+ init ( hostingView: SwiftUIHostingView ) {
62+ self . hostingView = hostingView
63+ super. init ( topMargin: . zero)
64+ contentView. addSubview ( hostingView)
65+ hostingView. translatesAutoresizingMaskIntoConstraints = false
66+ NSLayoutConstraint . activate ( [
67+ hostingView. topAnchor. constraint ( equalTo: contentView. topAnchor) ,
68+ hostingView. bottomAnchor. constraint ( equalTo: contentView. bottomAnchor) ,
69+ hostingView. leadingAnchor. constraint ( equalTo: contentView. leadingAnchor) ,
70+ hostingView. trailingAnchor. constraint ( equalTo: contentView. trailingAnchor)
71+ ] )
5472 }
73+
5574}
0 commit comments