From 89d755368fcc63fe10133037d22ee327781a1c93 Mon Sep 17 00:00:00 2001 From: Carson Katri Date: Thu, 29 Jun 2023 14:24:38 -0400 Subject: [PATCH 1/6] Add `chart_plot_style` modifier --- .../LiveViewNativeCharts/ChartsRegistry.swift | 3 ++ .../Modifiers/ChartPlotStyleModifier.swift | 53 +++++++++++++++++++ .../modifiers/chart_plot_style.ex | 12 +++++ .../types/modifier_stack.ex | 7 +++ 4 files changed, 75 insertions(+) create mode 100644 Sources/LiveViewNativeCharts/Modifiers/ChartPlotStyleModifier.swift create mode 100644 lib/live_view_native_swift_ui_charts/modifiers/chart_plot_style.ex create mode 100644 lib/live_view_native_swift_ui_charts/types/modifier_stack.ex diff --git a/Sources/LiveViewNativeCharts/ChartsRegistry.swift b/Sources/LiveViewNativeCharts/ChartsRegistry.swift index 5585bde..b25f18c 100644 --- a/Sources/LiveViewNativeCharts/ChartsRegistry.swift +++ b/Sources/LiveViewNativeCharts/ChartsRegistry.swift @@ -25,12 +25,15 @@ public struct ChartsRegistry: CustomRegistry { } public enum ModifierType: String { + case chartPlotStyle = "chart_plot_style" case chartXAxis = "chart_x_axis" case chartYAxis = "chart_y_axis" } public static func decodeModifier(_ type: ModifierType, from decoder: Decoder) throws -> some ViewModifier { switch type { + case .chartPlotStyle: + try ChartPlotStyleModifier(from: decoder) case .chartXAxis: try ChartXAxisModifier(from: decoder) case .chartYAxis: diff --git a/Sources/LiveViewNativeCharts/Modifiers/ChartPlotStyleModifier.swift b/Sources/LiveViewNativeCharts/Modifiers/ChartPlotStyleModifier.swift new file mode 100644 index 0000000..e7619d5 --- /dev/null +++ b/Sources/LiveViewNativeCharts/Modifiers/ChartPlotStyleModifier.swift @@ -0,0 +1,53 @@ +// +// ChartPlotStyleModifier.swift +// +// +// Created by Carson Katri on 6/29/23. +// + +import Charts +import SwiftUI +import LiveViewNative + +/// Style the chart's plot with modifiers. +/// +/// Pass any modifiers that should be applied to the plot to the ``modifiers`` argument. +/// +/// ```html +/// frame(width: 200))}> +/// ... +/// +/// ``` +/// +/// ## Arguments +/// * ``modifiers`` +#if swift(>=5.8) +@_documentation(visibility: public) +#endif +struct ChartPlotStyleModifier: ViewModifier, Decodable { + /// The modifier stack to apply to the plot. + #if swift(>=5.8) + @_documentation(visibility: public) + #endif + private let modifiers: [ModifierContainer] + + @ObservedElement private var element + @LiveContext private var context + + init(from decoder: Decoder) throws { + let container = try decoder.container(keyedBy: CodingKeys.self) + self.modifiers = try container.decode([ModifierContainer].self, forKey: .modifiers) + } + + func body(content: Content) -> some View { + content + .chartPlotStyle { content in + content + ._applyModifiers(modifiers[...], element: element, context: context) + } + } + + enum CodingKeys: CodingKey { + case modifiers + } +} diff --git a/lib/live_view_native_swift_ui_charts/modifiers/chart_plot_style.ex b/lib/live_view_native_swift_ui_charts/modifiers/chart_plot_style.ex new file mode 100644 index 0000000..138c219 --- /dev/null +++ b/lib/live_view_native_swift_ui_charts/modifiers/chart_plot_style.ex @@ -0,0 +1,12 @@ +defmodule LiveViewNativeSwiftUiCharts.Modifiers.ChartPlotStyle do + use LiveViewNativePlatform.Modifier + + alias LiveViewNativeSwiftUiCharts.Types.ModifierStack + + modifier_schema "chart_plot_style" do + field :modifiers, ModifierStack + end + + def params(%LiveViewNativeSwiftUi.Modifiers{} = modifiers), do: [modifiers: modifiers] + def params(params), do: params +end diff --git a/lib/live_view_native_swift_ui_charts/types/modifier_stack.ex b/lib/live_view_native_swift_ui_charts/types/modifier_stack.ex new file mode 100644 index 0000000..ff251d6 --- /dev/null +++ b/lib/live_view_native_swift_ui_charts/types/modifier_stack.ex @@ -0,0 +1,7 @@ +defmodule LiveViewNativeSwiftUiCharts.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 From 449a5a62846edd978842fd7fdedc560c154f0af5 Mon Sep 17 00:00:00 2001 From: Carson Katri Date: Thu, 29 Jun 2023 14:35:35 -0400 Subject: [PATCH 2/6] Use new _ModifierStack type --- .../Modifiers/ChartPlotStyleModifier.swift | 13 ++----------- .../modifiers/chart_plot_style.ex | 2 +- .../types/modifier_stack.ex | 7 ------- 3 files changed, 3 insertions(+), 19 deletions(-) delete mode 100644 lib/live_view_native_swift_ui_charts/types/modifier_stack.ex diff --git a/Sources/LiveViewNativeCharts/Modifiers/ChartPlotStyleModifier.swift b/Sources/LiveViewNativeCharts/Modifiers/ChartPlotStyleModifier.swift index e7619d5..017177d 100644 --- a/Sources/LiveViewNativeCharts/Modifiers/ChartPlotStyleModifier.swift +++ b/Sources/LiveViewNativeCharts/Modifiers/ChartPlotStyleModifier.swift @@ -29,21 +29,12 @@ struct ChartPlotStyleModifier: ViewModifier, Decodable { #if swift(>=5.8) @_documentation(visibility: public) #endif - private let modifiers: [ModifierContainer] - - @ObservedElement private var element - @LiveContext private var context - - init(from decoder: Decoder) throws { - let container = try decoder.container(keyedBy: CodingKeys.self) - self.modifiers = try container.decode([ModifierContainer].self, forKey: .modifiers) - } + private let modifiers: _ModifierStack func body(content: Content) -> some View { content .chartPlotStyle { content in - content - ._applyModifiers(modifiers[...], element: element, context: context) + modifiers.apply(to: content) } } diff --git a/lib/live_view_native_swift_ui_charts/modifiers/chart_plot_style.ex b/lib/live_view_native_swift_ui_charts/modifiers/chart_plot_style.ex index 138c219..e9d7c26 100644 --- a/lib/live_view_native_swift_ui_charts/modifiers/chart_plot_style.ex +++ b/lib/live_view_native_swift_ui_charts/modifiers/chart_plot_style.ex @@ -1,7 +1,7 @@ defmodule LiveViewNativeSwiftUiCharts.Modifiers.ChartPlotStyle do use LiveViewNativePlatform.Modifier - alias LiveViewNativeSwiftUiCharts.Types.ModifierStack + alias LiveViewNativeSwiftUi.Types.ModifierStack modifier_schema "chart_plot_style" do field :modifiers, ModifierStack diff --git a/lib/live_view_native_swift_ui_charts/types/modifier_stack.ex b/lib/live_view_native_swift_ui_charts/types/modifier_stack.ex deleted file mode 100644 index ff251d6..0000000 --- a/lib/live_view_native_swift_ui_charts/types/modifier_stack.ex +++ /dev/null @@ -1,7 +0,0 @@ -defmodule LiveViewNativeSwiftUiCharts.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 From 70eaed2fd191b70c507b3f4978d278af85c269ab Mon Sep 17 00:00:00 2001 From: Carson Katri Date: Thu, 29 Jun 2023 14:36:01 -0400 Subject: [PATCH 3/6] Use `public-builtin-registry` branch --- Package.resolved | 13 +++++++++++-- Package.swift | 2 +- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/Package.resolved b/Package.resolved index 1629353..093e99c 100644 --- a/Package.resolved +++ b/Package.resolved @@ -5,8 +5,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/liveview-native/liveview-client-swiftui", "state" : { - "branch" : "content-builder", - "revision" : "b80ea68b9d2ccf9e0d695b1f89e41278f003e740" + "branch" : "public-builtin-registry", + "revision" : "679c4d6f62dbc0b1987be8b04757b49b9ac69449" } }, { @@ -27,6 +27,15 @@ "version" : "1.2.2" } }, + { + "identity" : "swift-syntax", + "kind" : "remoteSourceControl", + "location" : "https://github.com/apple/swift-syntax.git", + "state" : { + "revision" : "f1e9245226002bb134884345d4809b9543da3666", + "version" : "509.0.0-swift-DEVELOPMENT-SNAPSHOT-2023-06-17-a" + } + }, { "identity" : "swiftphoenixclient", "kind" : "remoteSourceControl", diff --git a/Package.swift b/Package.swift index da8d218..f7f6396 100644 --- a/Package.swift +++ b/Package.swift @@ -13,7 +13,7 @@ let package = Package( targets: ["LiveViewNativeCharts"]), ], dependencies: [ - .package(url: "https://github.com/liveview-native/liveview-client-swiftui", branch: "content-builder") + .package(url: "https://github.com/liveview-native/liveview-client-swiftui", branch: "public-builtin-registry") ], targets: [ // Targets are the basic building blocks of a package, defining a module or a test suite. From db1d22b35ed631d2e2192006783fa6c701cca3b4 Mon Sep 17 00:00:00 2001 From: Carson Katri Date: Thu, 29 Jun 2023 14:37:29 -0400 Subject: [PATCH 4/6] Add documentation extensions --- .../LiveViewNativeCharts.docc/Extensions/Chart.md | 2 ++ .../Extensions/ChartPlotStyleModifier.md | 6 ++++++ 2 files changed, 8 insertions(+) create mode 100644 Sources/LiveViewNativeCharts/LiveViewNativeCharts.docc/Extensions/ChartPlotStyleModifier.md diff --git a/Sources/LiveViewNativeCharts/LiveViewNativeCharts.docc/Extensions/Chart.md b/Sources/LiveViewNativeCharts/LiveViewNativeCharts.docc/Extensions/Chart.md index 973c340..32d7e09 100644 --- a/Sources/LiveViewNativeCharts/LiveViewNativeCharts.docc/Extensions/Chart.md +++ b/Sources/LiveViewNativeCharts/LiveViewNativeCharts.docc/Extensions/Chart.md @@ -9,3 +9,5 @@ ### Axis Modifiers - ``ChartXAxisModifier`` - ``ChartYAxisModifier`` +### Style Modifiers +- ``ChartPlotStyleModifier`` diff --git a/Sources/LiveViewNativeCharts/LiveViewNativeCharts.docc/Extensions/ChartPlotStyleModifier.md b/Sources/LiveViewNativeCharts/LiveViewNativeCharts.docc/Extensions/ChartPlotStyleModifier.md new file mode 100644 index 0000000..a8b2a5b --- /dev/null +++ b/Sources/LiveViewNativeCharts/LiveViewNativeCharts.docc/Extensions/ChartPlotStyleModifier.md @@ -0,0 +1,6 @@ +# ``LiveViewNativeCharts/ChartPlotStyleModifier`` + +@Metadata { + @DocumentationExtension(mergeBehavior: append) + @DisplayName("chart_plot_style", style: symbol) +} From a5024d4da6fc530d7b8bae86ec06db68fbbd10af Mon Sep 17 00:00:00 2001 From: Carson Katri Date: Thu, 29 Jun 2023 14:45:40 -0400 Subject: [PATCH 5/6] Use ModifierStack as a ViewModifier --- .../LiveViewNativeCharts/Modifiers/ChartPlotStyleModifier.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/LiveViewNativeCharts/Modifiers/ChartPlotStyleModifier.swift b/Sources/LiveViewNativeCharts/Modifiers/ChartPlotStyleModifier.swift index 017177d..efd5c14 100644 --- a/Sources/LiveViewNativeCharts/Modifiers/ChartPlotStyleModifier.swift +++ b/Sources/LiveViewNativeCharts/Modifiers/ChartPlotStyleModifier.swift @@ -34,7 +34,7 @@ struct ChartPlotStyleModifier: ViewModifier, Decodable { func body(content: Content) -> some View { content .chartPlotStyle { content in - modifiers.apply(to: content) + content.modifier(modifiers) } } From 67b33dd23261a7e02173c43e5fa8318f7e23e959 Mon Sep 17 00:00:00 2001 From: Carson Katri Date: Thu, 29 Jun 2023 16:57:37 -0400 Subject: [PATCH 6/6] Switch to `main` branch --- Package.resolved | 4 ++-- Package.swift | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Package.resolved b/Package.resolved index 093e99c..706ef74 100644 --- a/Package.resolved +++ b/Package.resolved @@ -5,8 +5,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/liveview-native/liveview-client-swiftui", "state" : { - "branch" : "public-builtin-registry", - "revision" : "679c4d6f62dbc0b1987be8b04757b49b9ac69449" + "branch" : "main", + "revision" : "307d322ece3e658875f695183963721849efdaa4" } }, { diff --git a/Package.swift b/Package.swift index f7f6396..f8959e5 100644 --- a/Package.swift +++ b/Package.swift @@ -13,7 +13,7 @@ let package = Package( targets: ["LiveViewNativeCharts"]), ], dependencies: [ - .package(url: "https://github.com/liveview-native/liveview-client-swiftui", branch: "public-builtin-registry") + .package(url: "https://github.com/liveview-native/liveview-client-swiftui", branch: "main") ], targets: [ // Targets are the basic building blocks of a package, defining a module or a test suite.