Skip to content

Commit

Permalink
Add public _ModifierStack type (#998)
Browse files Browse the repository at this point in the history
* Make BuiltinRegistry and ModifierContainer public

* Revert "Make BuiltinRegistry and ModifierContainer public"

This reverts commit 4b68667.

* Add public `_ModifierStack` type

* Convert to ViewModifier
  • Loading branch information
carson-katri authored Jun 29, 2023
1 parent 962a487 commit 307d322
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
33 changes: 33 additions & 0 deletions Sources/LiveViewNative/ModifierStack.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//
// ModifierStack.swift
//
//
// Created by Carson Katri on 6/29/23.
//

import SwiftUI

/// A container that decodes a modifier stack.
///
/// Use this as a modifier:
///
/// ```swift
/// content
/// .modifier(modifierStack)
/// ```
public struct _ModifierStack<R: RootRegistry>: Decodable, ViewModifier {
private let stack: [ModifierContainer<R>]

@ObservedElement private var element
@LiveContext<R> private var context

public init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
self.stack = try container.decode([ModifierContainer<R>].self)
}

public func body(content: Content) -> some View {
content
.applyModifiers(stack[...], element: element, context: context.storage)
}
}
7 changes: 7 additions & 0 deletions lib/live_view_native_swift_ui/types/modifier_stack.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
defmodule LiveViewNativeSwiftUi.Types.ModifierStack do
use LiveViewNativePlatform.Modifier.Type
def type, do: :map

def cast(%LiveViewNativeSwiftUi.Modifiers{} = value), do: {:ok, Jason.decode!(Jason.encode!(value))}
def cast(_), do: :error
end

0 comments on commit 307d322

Please sign in to comment.