liveview-native / liveview-client-swiftui

MIT License
372 stars 37 forks source link

Add access to `LiveContext` from `ContentBuilder` #1406

Closed carson-katri closed 1 month ago

carson-katri commented 1 month ago

This fixes instances in addon libraries where a blank (unusable) LiveContext<R>() had to be passed to resolve a stylesheet value.

https://github.com/liveview-native/liveview-native-swiftui-charts/blob/c612c5ac085fc61a33a4c3d097d09b0db9a2be7f/Sources/LiveViewNativeCharts/Modifiers/SymbolModifier.swift#L44

Instead, a valid LiveContext can be accessed from the ContentBuilder context, via context.context.

Addon libraries will need to be updated to make use of this new API.

carson-katri commented 1 month ago

Here's one example of what this fixes:

Without a valid context, a Chart with a modifier that embeds a View (such as this Image rendering for each PointMark) would crash. Now we can render these templates properly:

<Chart style="aspectRatio(1.5, contentMode: .fit); foregroundStyle(.orange); padding();">
  <PointMark
    :for={i <- 1..10}
    x:label="X"
    x:value={i}
    y:label="Y"
    y:value={:rand.uniform()}

    style="symbol(symbol: :symbol);"
  >
    <Image systemName="bolt" template="symbol" style={[
      "font(.title3)",
      "symbolVariant(.circle.fill)",
      "symbolRenderingMode(.hierarchical)"
    ]} />
  </PointMark>
</Chart>
Screenshot 2024-08-08 at 3 15 56 PM