-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Closes #636 Also permits nil for template key names, allowing for optional templates
- Loading branch information
1 parent
e040f35
commit 3a21949
Showing
4 changed files
with
123 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
Sources/LiveViewNative/Modifiers/Modal Presentations/AlertModifier.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
// | ||
// AlertModifier.swift | ||
// LiveViewNative | ||
// | ||
// Created by Shadowfacts on 4/28/23. | ||
// | ||
|
||
import SwiftUI | ||
|
||
/// Displays a system-style alert when a live binding is active. | ||
/// | ||
/// ```html | ||
/// <Button phx-click="toggle-show" | ||
/// modifiers={ | ||
/// @native | ||
/// |> alert(title: "My Alert", message: :message, actions: :actions, is_presented: :show) | ||
/// } | ||
/// > | ||
/// Present Alert | ||
/// <alert:message> | ||
/// Hello, world! | ||
/// </alert:message> | ||
/// <alert:actions> | ||
/// <Button> | ||
/// OK | ||
/// </Button> | ||
/// </alert:actions> | ||
/// </Button> | ||
/// ``` | ||
/// | ||
/// ```elixir | ||
/// defmodule AppWeb.TestLive do | ||
/// use AppWeb, :live_view | ||
/// use LiveViewNative.LiveView | ||
/// | ||
/// native_binding :show, Atom, false | ||
/// end | ||
/// ``` | ||
/// | ||
/// ## Arguments | ||
/// - ``title`` | ||
/// - ``actions`` | ||
/// - ``message`` | ||
/// - ``isPresented`` | ||
#if swift(>=5.8) | ||
@_documentation(visibility: public) | ||
#endif | ||
struct AlertModifier<R: RootRegistry>: ViewModifier, Decodable { | ||
/// The title of the alert. | ||
#if swift(>=5.8) | ||
@_documentation(visibility: public) | ||
#endif | ||
private let title: String | ||
|
||
/// A reference to a set of ``Button`` views that are used as the actions for the alert. | ||
#if swift(>=5.8) | ||
@_documentation(visibility: public) | ||
#endif | ||
private let actions: String | ||
|
||
/// A reference to an optional ``Text`` view to use as the alert's message. | ||
/// | ||
/// If no reference is provided, the alert will not have a message. | ||
#if swift(>=5.8) | ||
@_documentation(visibility: public) | ||
#endif | ||
private let message: String? | ||
|
||
/// The name of a live binding that controls when the alert is shown. | ||
/// | ||
/// Set the binding to `true` to show the alert. | ||
#if swift(>=5.8) | ||
@_documentation(visibility: public) | ||
#endif | ||
@LiveBinding private var isPresented: Bool | ||
|
||
@ObservedElement private var element | ||
@LiveContext<R> private var context | ||
|
||
init(from decoder: Decoder) throws { | ||
let container = try decoder.container(keyedBy: CodingKeys.self) | ||
|
||
self.title = try container.decode(String.self, forKey: .title) | ||
self.actions = try container.decode(String.self, forKey: .actions) | ||
self.message = try container.decodeIfPresent(String.self, forKey: .message) | ||
self._isPresented = try LiveBinding(decoding: .isPresented, in: container) | ||
} | ||
|
||
func body(content: Content) -> some View { | ||
content.alert(title, isPresented: $isPresented) { | ||
context.buildChildren(of: element, withTagName: self.actions, namespace: "alert") | ||
} message: { | ||
if let message { | ||
context.buildChildren(of: element, withTagName: message, namespace: "alert") | ||
} | ||
} | ||
} | ||
|
||
enum CodingKeys: String, CodingKey { | ||
case title | ||
case actions | ||
case message | ||
case isPresented = "is_presented" | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
lib/live_view_native_swift_ui/modifiers/modal_presentations/alert.ex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
defmodule LiveViewNativeSwiftUi.Modifiers.Alert do | ||
use LiveViewNativePlatform.Modifier | ||
|
||
alias LiveViewNativeSwiftUi.Types.{KeyName, NativeBindingName} | ||
|
||
modifier_schema "alert" do | ||
field(:title, :string) | ||
field(:actions, KeyName) | ||
field(:message, KeyName, default: nil) | ||
field(:is_presented, NativeBindingName) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters