Skip to content

Commit

Permalink
Fix for conform with both reference size and fitting size.
Browse files Browse the repository at this point in the history
  • Loading branch information
ra1028 committed Oct 24, 2019
1 parent b8f0122 commit b6da679
Showing 1 changed file with 41 additions and 19 deletions.
60 changes: 41 additions & 19 deletions Sources/SwiftUISupport/ComponentSwiftUISupport.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,10 @@ private struct ComponentView<C: Component>: View {
}

var body: some View {
let size: CGSize? = bounds.flatMap { bounds in
if let referenceSize = component.referenceSize(in: bounds) {
return referenceSize
}

return proxy.uiView?.systemLayoutSizeFitting(
bounds.size,
withHorizontalFittingPriority: .required,
verticalFittingPriority: .fittingSizeLevel
)
}

let size = preferredSize()
return ComponentRepresenting(component: component, proxy: proxy)
.frame(height: size?.height)
.frame(idealWidth: size?.width)
.frame(idealWidth: size.idealWidth)
.frame(width: size.fixedWidth, height: size.fixedHeight)
.onAppear { self.proxy.uiView?.contentWillDisplay() }
.onDisappear { self.proxy.uiView?.contentDidEndDisplay() }
.background(GeometryReader { geometry in
Expand All @@ -46,6 +35,39 @@ private struct ComponentView<C: Component>: View {
}
}

@available(iOS 13.0, *)
extension ComponentView {
struct Size {
var idealWidth: CGFloat?
var fixedWidth: CGFloat?
var fixedHeight: CGFloat?
}

func preferredSize() -> Size {
if let bounds = bounds, let referenceSize = component.referenceSize(in: bounds) {
return Size(
fixedWidth: referenceSize.width,
fixedHeight: referenceSize.height
)
}
else if let bounds = bounds, let uiView = proxy.uiView {
let fittingSize = uiView.systemLayoutSizeFitting(
bounds.size,
withHorizontalFittingPriority: .required,
verticalFittingPriority: .fittingSizeLevel
)

return Size(
idealWidth: fittingSize.width,
fixedHeight: fittingSize.height
)
}
else {
return Size()
}
}
}

private struct BoundsPreferenceKey: PreferenceKey {
static func reduce(value: inout CGRect?, nextValue: () -> CGRect?) {}
}
Expand All @@ -65,16 +87,16 @@ private struct ComponentRepresenting<C: Component>: UIViewRepresentable {
}

private final class UIComponentView: UIView, ComponentRenderable {
@available(*, unavailable)
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

override init(frame: CGRect) {
super.init(frame: frame)

backgroundColor = .clear
}

@available(*, unavailable)
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}

private final class ComponentViewProxy {
Expand Down

0 comments on commit b6da679

Please sign in to comment.