Skip to content

Commit 9239810

Browse files
carson-katrisupernintendo
authored andcommitted
Update documentation
1 parent 67f062c commit 9239810

File tree

9 files changed

+95
-52
lines changed

9 files changed

+95
-52
lines changed

README.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,15 @@ The LiveViewNative Swift package lets you use Phoenix LiveView to build native i
99
3. Select *Add Package*
1010

1111
## Usage
12-
Create a `LiveSessionCoordinator` and pass in the URL to your Phoenix server. Then use `LiveView` to connect to that session.
12+
Create a `LiveView` to connect to a Phoenix server running on `http://localhost:4000`.
1313

1414
```swift
1515
import SwiftUI
1616
import LiveViewNative
1717

1818
struct ContentView: View {
19-
@StateObject private var session = LiveSessionCoordinator(URL(string: "http://localhost:4000")!)
20-
2119
var body: some View {
22-
LiveView(session: session)
20+
LiveView(.localhost)
2321
}
2422
}
2523
```

Sources/LiveViewNative/LiveViewHost.swift

Lines changed: 53 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,69 @@
88
import Foundation
99

1010
/// The information needed to locate a server hosting a LiveView.
11+
///
12+
/// This protocol gives quick access to common LiveView hosts.
13+
///
14+
/// For example, use ``LocalhostLiveViewHost/localhost`` to quickly connect to a development server on port `4000`.
15+
///
16+
/// - Note: You can use ``LocalhostLiveViewHost/localhost(port:path:)`` to customize the port and base path of your Phoenix server.
17+
///
18+
/// ```swift
19+
/// struct ContentView: View {
20+
/// var body: some View {
21+
/// LiveView(.localhost)
22+
/// }
23+
/// }
24+
/// ```
25+
///
26+
/// To provide a custom `URL`, use ``CustomLiveViewHost/custom(_:)``.
27+
///
28+
/// - Note: In many cases, it is easier to use ``LiveView/init(url:configuration:)`` to provide a custom URL.
29+
///
30+
/// ```swift
31+
/// struct ContentView: View {
32+
/// var body: some View {
33+
/// LiveView(URL(string: "https://example.com")!)
34+
/// }
35+
/// }
36+
/// ```
37+
///
38+
/// ## Switching Between Development/Production
39+
/// In many cases, a different host should be used for development and production environments.
40+
///
41+
/// Use ``AutomaticLiveViewHost/automatic(_:)`` or ``AutomaticLiveViewHost/automatic(development:production:)`` to easily switch hosts based on the `DEBUG` compiler directive.
42+
///
43+
/// - Note: `DEBUG` is setup by default in new Xcode projects for the debug configuration.
44+
///
45+
/// ```swift
46+
/// struct ContentView: View {
47+
/// var body: some View {
48+
/// LiveView(.automatic(URL(string: "https://example.com")!))
49+
/// }
50+
/// }
51+
/// ```
1152
public protocol LiveViewHost {
1253
var url: URL { get }
1354
}
1455

1556
public struct LocalhostLiveViewHost: LiveViewHost {
1657
public let url: URL
1758

18-
init(port: Int) {
19-
self.url = .init(string: "http://localhost:\(port)")!
59+
init(port: Int = 4000, path: String? = nil) {
60+
let base = URL(string: "http://localhost:\(port)")!
61+
if let path {
62+
self.url = base.appending(path: path)
63+
} else {
64+
self.url = base
65+
}
2066
}
2167
}
2268

2369
public extension LiveViewHost where Self == LocalhostLiveViewHost {
2470
/// A server at `http://localhost:4000`.
25-
static var localhost: Self { .init(port: 4000) }
26-
/// A server at `localhost` on the given port.
27-
static func localhost(port: Int) -> Self { .init(port: port) }
71+
static var localhost: Self { .init() }
72+
/// A server at `localhost` on the given port with a custom base path.
73+
static func localhost(port: Int = 4000, path: String? = nil) -> Self { .init(port: port, path: path) }
2874
}
2975

3076
public struct CustomLiveViewHost: LiveViewHost {
@@ -62,6 +108,8 @@ public extension LiveViewHost where Self == AutomaticLiveViewHost {
62108
}
63109

64110
/// Automatically select a host based on the `DEBUG` compiler directive.
111+
///
112+
/// Provide the `URL` of the production host. The development host defaults to ``LocalhostLiveViewHost/localhost``.
65113
static func automatic(_ url: URL) -> Self {
66114
.init(development: .localhost, production: .custom(url))
67115
}

Sources/LiveViewNative/LiveViewNative.docc/GettingStarted.md

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,22 @@ See how to quickly get up and running displaying a LiveView in your app.
44

55
## First Steps
66

7-
Getting started with LiveViewNative is easy: simply create a ``LiveSessionCoordinator`` and pass it into a ``LiveView`` that's part of your view tree.
7+
Getting started with LiveViewNative is easy: simply use a ``LiveView`` as part of your view tree, and pass a ``LiveViewHost`` to launch.
88

9-
The coordinator object is responsible for connecting to the Phoenix LiveView backend, managing the network connection, and sending and responding to events.
10-
11-
Only one coordinator may be used for a view, so use `@State` to store it on the containing view.
12-
13-
The only information you are required to provide is the URL of the live view to connect to.
14-
15-
The LiveView is then created by passing in the coordinator, no other setup necessary.
9+
This can be ``LocalhostLiveViewHost/localhost``, some other ``AutomaticLiveViewHost/automatic(development:production:)``, or any `URL` with ``LiveView/init(url:configuration:)``.
1610

1711
```swift
1812
@MainActor
1913
struct ContentView: View {
20-
@State private var session = LiveSessionCoordinator(URL(string: "http://localhost:4000/")!)
21-
2214
var body: some View {
23-
LiveView(session: session)
15+
LiveView(.localhost)
2416
}
2517
}
2618
```
2719

28-
## Configuring the Coordinator
20+
## Configuring the LiveView
2921

30-
The coordinator can be configured with a number of different options. To customize these, create a ``LiveSessionConfiguration`` with the values you want, and then pass it to coordinator's initializer.
22+
The ``LiveView`` can be configured with a number of different options. To customize these, create a ``LiveSessionConfiguration`` with the values you want, and then pass it to view's initializer.
3123

3224
```swift
3325
@MainActor
@@ -39,22 +31,41 @@ struct ContentView: View {
3931
}()
4032

4133
var body: some View {
42-
LiveView(session: session)
34+
LiveView(
35+
.localhost,
36+
configuration: LiveSessionConfiguration(navigationMode: .enabled)
37+
)
4338
}
4439
}
4540
```
4641

4742
## Supporting Custom Elements
4843

49-
You can enable support for your own custom HTML elements and attributes by implementing the ``CustomRegistry`` protocol and providing it to the coordinator. The protocol uses only static methods, so rather than constructing an instance of your registry, you provide it as a generic type to the coordinator when constructing it:
44+
You can enable support for your own custom HTML elements and attributes by implementing the ``CustomRegistry`` protocol and providing it to the coordinator. The protocol uses only static methods, so rather than constructing an instance of your registry, you provide it as a generic type to the ``LiveView`` when constructing it:
5045

5146
```swift
5247
@MainActor
5348
struct ContentView: View {
54-
@State private var session = LiveSessionCoordinator<MyCustomRegistry>(URL(string: "http://localhost:4000/")!)
49+
var body: some View {
50+
LiveView<MyCustomRegistry>(.localhost)
51+
}
52+
}
53+
```
54+
55+
## Accessing the ``LiveSessionCoordinator``
56+
57+
``LiveSessionCoordinator`` handles everything needed to connect and run a ``LiveView``. When you pass a ``LiveViewHost`` or `URL` in the initializer, this coordinator is created for you.
58+
59+
However, in some cases you may want access to this coordinator to send or receive custom events. In these cases, create the ``LiveSessionCoordinator`` yourself, and pass it to ``LiveView/init(session:)``.
60+
61+
```swift
62+
struct ContentView: View {
63+
@StateObject private var session = LiveSessionCoordinator<MyCustomRegistry>(.localhost)
5564

5665
var body: some View {
5766
LiveView(session: session)
5867
}
5968
}
6069
```
70+
71+
Now you have full access to the ``LiveSessionCoordinator`` and all of its properties and methods.

Sources/LiveViewNative/LiveViewNative.docc/LiveViewNative.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,15 @@ The LiveViewNative Swift package lets you use Phoenix LiveView to build native i
1313
3. Select *Add Package*
1414

1515
## Usage
16-
Create a `LiveSessionCoordinator` and pass in the URL to your Phoenix server. Then use `LiveView` to connect to that session.
16+
Create a `LiveView` and pass in a ``LiveViewHost`` or the `URL` to your Phoenix server.
1717

1818
```swift
1919
import SwiftUI
2020
import LiveViewNative
2121

2222
struct ContentView: View {
23-
@StateObject private var session = LiveSessionCoordinator(URL(string: "http://localhost:4000")!)
24-
2523
var body: some View {
26-
LiveView(session: session)
24+
LiveView(.localhost)
2725
}
2826
}
2927
```

Sources/LiveViewNative/LiveViewNative.docc/Resources/01-02-06-session.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ import LiveViewNative
33

44
@MainActor
55
struct ContentView: View {
6-
@StateObject private var session = LiveSessionCoordinator(URL(string: "http://localhost:4000/cats")!)
7-
86
var body: some View {
97
VStack {
108
Image(systemName: "globe")

Sources/LiveViewNative/LiveViewNative.docc/Resources/01-02-07-liveview.swift

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@ import LiveViewNative
33

44
@MainActor
55
struct ContentView: View {
6-
@StateObject private var session = LiveSessionCoordinator(URL(string: "http://localhost:4000/cats")!)
7-
86
var body: some View {
9-
LiveView(session: session)
7+
LiveView(.localhost(path: "cats"))
108
}
119
}

Sources/LiveViewNative/LiveViewNative.docc/Resources/03-02-01-navigationmode.swift

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,10 @@ import LiveViewNative
33

44
@MainActor
55
struct ContentView: View {
6-
@StateObject private var session: LiveSessionCoordinator<EmptyRegistry> = {
7-
var config = LiveSessionConfiguration()
8-
config.navigationMode = .enabled
9-
return LiveSessionCoordinator(URL(string: "http://localhost:4000/cats")!, config: config)
10-
}()
11-
126
var body: some View {
13-
LiveView(session: session)
7+
LiveView(
8+
.localhost(path: "cats"),
9+
configuration: LiveSessionConfiguration(navigationMode: .enabled)
10+
)
1411
}
1512
}

Sources/LiveViewNative/LiveViewNative.docc/Resources/04-01-04-session.swift

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,10 @@ import LiveViewNative
33

44
@MainActor
55
struct ContentView: View {
6-
@StateObject private var session: LiveSessionCoordinator<MyRegistry> = {
7-
var config = LiveSessionConfiguration()
8-
config.navigationMode = .enabled
9-
return LiveSessionCoordinator(URL(string: "http://localhost:4000/cats")!, config: config)
10-
}()
11-
126
var body: some View {
13-
LiveView(session: session)
7+
LiveView<MyRegistry>(
8+
.localhost(path: "cats"),
9+
configuration: LiveSessionConfiguration(navigationMode: .enabled)
10+
)
1411
}
1512
}

Sources/LiveViewNative/LiveViewNative.docc/Tutorials/01 Initial List.tutorial

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -165,9 +165,7 @@
165165
}
166166

167167
@Step {
168-
Then, import the LiveViewNative package and add the session as a `@StateObject` variable, using the URL of your Phoenix project.
169-
170-
The coordinator handles all networking for the LiveView, and so only one instance of it should exist. We use a `@State` variable for this so that the coordinator isn't re-created if SwiftUI re-creates the containing view.
168+
Then, import the LiveViewNative package at the top of the file.
171169

172170
@Comment {
173171
project = "app"
@@ -177,7 +175,7 @@
177175
}
178176

179177
@Step {
180-
Lastly, add the `LiveView` to your view's body, passing in the coordinator.
178+
Lastly, add the `LiveView` to your view's body, passing in ``LocalhostLiveViewHost/localhost(port:path:)`` as the host with a custom base path.
181179

182180
Then, you can run the app in the Simulator and see it connecting to your Phoenix app. Make sure you've started the Phoenix server with `mix phx.server`!
183181

0 commit comments

Comments
 (0)