Skip to content
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

Add public _ModifierStack type #998

Merged
merged 4 commits into from
Jun 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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