-
Notifications
You must be signed in to change notification settings - Fork 234
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix SwiftUI item view onAppear not getting called #265
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
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 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 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 |
---|---|---|
|
@@ -30,8 +30,10 @@ public final class SwiftUIWrapperView<Content: View>: UIView { | |
|
||
// MARK: Lifecycle | ||
|
||
public init(content: Content) { | ||
self.content = content | ||
public init(contentAndID: ContentAndID) { | ||
self.contentAndID = contentAndID | ||
hostingController = HostingController( | ||
rootView: .init(content: contentAndID.content, id: contentAndID.id)) | ||
|
||
super.init(frame: .zero) | ||
|
||
|
@@ -69,16 +71,16 @@ public final class SwiftUIWrapperView<Content: View>: UIView { | |
|
||
// MARK: Fileprivate | ||
|
||
fileprivate var content: Content { | ||
fileprivate var contentAndID: ContentAndID { | ||
didSet { | ||
hostingController.rootView = content | ||
hostingController.rootView = .init(content: contentAndID.content, id: contentAndID.id) | ||
configureGestureRecognizers() | ||
} | ||
} | ||
|
||
// MARK: Private | ||
|
||
private lazy var hostingController = HostingController<Content>(rootView: content) | ||
private let hostingController: HostingController<IDWrapperView<Content>> | ||
|
||
private weak var hostingControllerView: UIView? | ||
|
||
|
@@ -126,8 +128,8 @@ extension SwiftUIWrapperView: CalendarItemViewRepresentable { | |
|
||
// MARK: Lifecycle | ||
|
||
init(initialContent: Content) { | ||
self.initialContent = initialContent | ||
init(initialContentAndID: ContentAndID) { | ||
self.initialContentAndID = initialContentAndID | ||
} | ||
|
||
// MARK: Public | ||
|
@@ -142,24 +144,29 @@ extension SwiftUIWrapperView: CalendarItemViewRepresentable { | |
|
||
// MARK: Fileprivate | ||
|
||
fileprivate let initialContent: Content | ||
fileprivate let initialContentAndID: ContentAndID | ||
|
||
} | ||
|
||
public struct ContentWrapper: Equatable { | ||
public struct ContentAndID: Equatable, SwiftUIWrapperViewContentIDUpdatable { | ||
|
||
// MARK: Lifecycle | ||
|
||
public init(content: Content) { | ||
public init(content: Content, id: AnyHashable) { | ||
self.content = content | ||
self.id = id | ||
} | ||
|
||
// MARK: Public | ||
|
||
public static func == (_: ContentWrapper, _: ContentWrapper) -> Bool { | ||
public static func == (_: ContentAndID, _: ContentAndID) -> Bool { | ||
false | ||
} | ||
|
||
// MARK: Internal | ||
|
||
var id: AnyHashable | ||
|
||
// MARK: Fileprivate | ||
|
||
fileprivate let content: Content | ||
|
@@ -168,17 +175,26 @@ extension SwiftUIWrapperView: CalendarItemViewRepresentable { | |
|
||
public static func makeView( | ||
withInvariantViewProperties invariantViewProperties: InvariantViewProperties) | ||
-> SwiftUIWrapperView<Content> | ||
-> SwiftUIWrapperView<Content> | ||
{ | ||
SwiftUIWrapperView<Content>(content: invariantViewProperties.initialContent) | ||
SwiftUIWrapperView<Content>(contentAndID: invariantViewProperties.initialContentAndID) | ||
} | ||
|
||
public static func setContent(_ content: ContentWrapper, on view: SwiftUIWrapperView<Content>) { | ||
view.content = content.content | ||
public static func setContent( | ||
_ contentAndID: ContentAndID, | ||
on view: SwiftUIWrapperView<Content>) | ||
{ | ||
view.contentAndID = contentAndID | ||
} | ||
|
||
} | ||
|
||
// MARK: - SwiftUIWrapperViewContentIDUpdatable | ||
|
||
protocol SwiftUIWrapperViewContentIDUpdatable { | ||
var id: AnyHashable { get set } | ||
} | ||
|
||
// MARK: UIResponder Next View Controller Helper | ||
|
||
extension UIResponder { | ||
|
@@ -188,35 +204,48 @@ extension UIResponder { | |
} | ||
} | ||
|
||
// MARK: - SwiftUIWrapperView.HostingController | ||
// MARK: - IDWrapperView | ||
|
||
/// A wrapper view that uses the `id(_:)` modifier on the wrapped view so that each one has its own identity, even if it was reused. | ||
@available(iOS 13.0, *) | ||
extension SwiftUIWrapperView { | ||
private struct IDWrapperView<Content: View>: View { | ||
|
||
/// The `UIHostingController` type used by `SwiftUIWrapperView` to embed SwiftUI views in a UIKit view hierarchy. | ||
public final class HostingController<Content: View>: UIHostingController<Content> { | ||
let content: Content | ||
let id: AnyHashable | ||
|
||
// MARK: Lifecycle | ||
var body: some View { | ||
content | ||
.id(id) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the magic line that adds a stable ID |
||
} | ||
|
||
fileprivate override init(rootView: Content) { | ||
super.init(rootView: rootView) | ||
} | ||
|
||
// This prevents the safe area from affecting layout. | ||
_disableSafeArea = true | ||
} | ||
// MARK: - HostingController | ||
|
||
@MainActor required dynamic init?(coder aDecoder: NSCoder) { | ||
fatalError("init(coder:) has not been implemented") | ||
} | ||
/// The `UIHostingController` type used by `SwiftUIWrapperView` to embed SwiftUI views in a UIKit view hierarchy. This | ||
/// exists to disable safe area insets and set the background color to clear. | ||
@available(iOS 13.0, *) | ||
private final class HostingController<Content: View>: UIHostingController<Content> { | ||
|
||
public override func viewDidLoad() { | ||
super.viewDidLoad() | ||
// MARK: Lifecycle | ||
|
||
// Override the default `.systemBackground` color since `CalendarView` subviews should be | ||
// clear. | ||
view.backgroundColor = .clear | ||
} | ||
override init(rootView: Content) { | ||
super.init(rootView: rootView) | ||
|
||
// This prevents the safe area from affecting layout. | ||
_disableSafeArea = true | ||
} | ||
|
||
@MainActor required dynamic init?(coder aDecoder: NSCoder) { | ||
fatalError("init(coder:) has not been implemented") | ||
} | ||
|
||
override func viewDidLoad() { | ||
super.viewDidLoad() | ||
|
||
// Override the default `.systemBackground` color since `CalendarView` subviews should be | ||
// clear. | ||
view.backgroundColor = .clear | ||
Comment on lines
+225
to
+248
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. unchanged, but I pulled it out of the |
||
} | ||
|
||
} |
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
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.
kind of annoying that we need this but we're working with a type-erased
AnyCalendarItemModel
, so we don't know if the originalCalendarItemModel<CalendarItemViewRepresentable>
type is aSwiftUIWrapperView
or not.