-
Notifications
You must be signed in to change notification settings - Fork 299
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d229008
commit f9865e4
Showing
27 changed files
with
1,630 additions
and
5 deletions.
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
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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
## [Point-Free](https://www.pointfree.co) | ||
|
||
> #### This directory contains code from Point-Free Episode: [Cross-Platform Swift: Navigation](https://www.pointfree.co/episodes/ep293-cross-platform-navigation) | ||
> #### This directory contains code from Point-Free Episode: [Cross-Platform Swift: Navigation](https://www.pointfree.co/episodes/ep293-cross-platform-swift-navigation) | ||
> | ||
> We will introduce navigation APIs to our Wasm application, starting simply with an alert before ramping things up with a `dialog` tag that can be fully configurable from a value type that represents its state and actions. |
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,8 @@ | ||
.DS_Store | ||
/.build | ||
/Packages | ||
xcuserdata/ | ||
DerivedData/ | ||
.swiftpm/configuration/registries.json | ||
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata | ||
.netrc |
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 @@ | ||
wasm-DEVELOPMENT-SNAPSHOT-2024-07-16-a |
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,61 @@ | ||
// swift-tools-version: 6.0 | ||
|
||
import PackageDescription | ||
|
||
let package = Package( | ||
name: "Counter", | ||
platforms: [.iOS(.v16), .macOS(.v14)], | ||
products: [ | ||
.library( | ||
name: "Counter", | ||
targets: ["Counter"] | ||
), | ||
.library( | ||
name: "FactClient", | ||
targets: ["FactClient"] | ||
), | ||
], | ||
dependencies: [ | ||
.package(url: "https://github.com/pointfreeco/swift-dependencies", from: "1.0.0"), | ||
.package(url: "https://github.com/pointfreeco/swift-navigation", from: "2.0.0"), | ||
.package(url: "https://github.com/pointfreeco/swift-perception", from: "1.0.0"), | ||
.package(url: "https://github.com/swiftwasm/carton", from: "1.0.0"), | ||
.package(url: "https://github.com/swiftwasm/JavaScriptKit", exact: "0.19.2"), | ||
], | ||
targets: [ | ||
.executableTarget( | ||
name: "WasmApp", | ||
dependencies: [ | ||
"Counter", | ||
"FactClientLive", | ||
.product(name: "SwiftNavigation", package: "swift-navigation"), | ||
.product(name: "JavaScriptEventLoop", package: "JavaScriptKit"), | ||
.product(name: "JavaScriptKit", package: "JavaScriptKit"), | ||
] | ||
), | ||
.target( | ||
name: "Counter", | ||
dependencies: [ | ||
"FactClient", | ||
.product(name: "SwiftNavigation", package: "swift-navigation"), | ||
.product(name: "Perception", package: "swift-perception") | ||
] | ||
), | ||
.target( | ||
name: "FactClient", | ||
dependencies: [ | ||
.product(name: "Dependencies", package: "swift-dependencies"), | ||
.product(name: "DependenciesMacros", package: "swift-dependencies"), | ||
] | ||
), | ||
.target( | ||
name: "FactClientLive", | ||
dependencies: [ | ||
"FactClient", | ||
.product(name: "JavaScriptKit", package: "JavaScriptKit", condition: .when(platforms: [.wasi])), | ||
.product(name: "JavaScriptEventLoop", package: "JavaScriptKit", condition: .when(platforms: [.wasi])), | ||
] | ||
) | ||
], | ||
swiftLanguageVersions: [.v6] | ||
) |
70 changes: 70 additions & 0 deletions
70
0294-cross-platform-pt5/Counter/Sources/Counter/CounterModel.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,70 @@ | ||
import Dependencies | ||
import FactClient | ||
import Foundation | ||
import Perception | ||
import SwiftNavigation | ||
|
||
@MainActor | ||
@Perceptible | ||
public class CounterModel: HashableObject { | ||
@PerceptionIgnored | ||
@Dependency(FactClient.self) var factClient | ||
|
||
public var count = 0 { | ||
didSet { | ||
isTextFocused = !count.isMultiple(of: 3) | ||
} | ||
} | ||
public var alert: AlertState<Never>? | ||
public var factIsLoading = false | ||
public var isTextFocused = false { | ||
didSet { | ||
print("isTextFocused", isTextFocused) | ||
} | ||
} | ||
public var text = "" | ||
|
||
public struct Fact: Identifiable { | ||
public var value: String | ||
public var id: String { value } | ||
} | ||
|
||
public init() {} | ||
|
||
public func incrementButtonTapped() { | ||
count += 1 | ||
alert = nil | ||
} | ||
|
||
public func decrementButtonTapped() { | ||
count -= 1 | ||
alert = nil | ||
} | ||
|
||
public func factButtonTapped() async { | ||
alert = nil | ||
factIsLoading = true | ||
defer { factIsLoading = false } | ||
|
||
do { | ||
try await Task.sleep(for: .seconds(1)) | ||
|
||
var count = count | ||
let fact = try await factClient.fetch(count) | ||
alert = AlertState { | ||
TextState("Fact") | ||
} actions: { | ||
ButtonState { | ||
TextState("OK") | ||
} | ||
ButtonState { | ||
TextState("Save") | ||
} | ||
} message: { | ||
TextState(fact) | ||
} | ||
} catch { | ||
// TODO: error handling | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
0294-cross-platform-pt5/Counter/Sources/FactClient/FactClient.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,11 @@ | ||
import Dependencies | ||
import DependenciesMacros | ||
|
||
@DependencyClient | ||
public struct FactClient: Sendable { | ||
public var fetch: @Sendable (Int) async throws -> String | ||
} | ||
|
||
extension FactClient: TestDependencyKey { | ||
public static let testValue = FactClient() | ||
} |
28 changes: 28 additions & 0 deletions
28
0294-cross-platform-pt5/Counter/Sources/FactClientLive/FactClientLive.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,28 @@ | ||
import FactClient | ||
import Dependencies | ||
|
||
#if canImport(JavaScriptKit) | ||
@preconcurrency import JavaScriptKit | ||
#endif | ||
#if canImport(JavaScriptEventLoop) | ||
import JavaScriptEventLoop | ||
#endif | ||
|
||
extension FactClient: DependencyKey { | ||
public static let liveValue = FactClient { number in | ||
#if canImport(JavaScriptKit) && canImport(JavaScriptEventLoop) | ||
let response = try await JSPromise( | ||
JSObject.global.fetch!("http://www.numberapi.com/\(number)").object! | ||
)!.value | ||
return try await JSPromise(response.text().object!)!.value.string! | ||
#else | ||
return try await String( | ||
decoding: URLSession.shared | ||
.data( | ||
from: URL(string: "http://www.numberapi.com/\(number)")! | ||
).0, | ||
as: UTF8.self | ||
) | ||
#endif | ||
} | ||
} |
121 changes: 121 additions & 0 deletions
121
0294-cross-platform-pt5/Counter/Sources/WasmApp/App.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,121 @@ | ||
import Counter | ||
import IssueReporting | ||
import JavaScriptEventLoop | ||
import JavaScriptKit | ||
import SwiftNavigation | ||
|
||
@main | ||
@MainActor | ||
struct App { | ||
static var tokens: Set<ObservationToken> = [] | ||
|
||
static func main() { | ||
IssueReporters.current = [JavaScriptConsoleWarning()] | ||
JavaScriptEventLoop.installGlobalExecutor() | ||
|
||
@UIBindable var model = CounterModel() | ||
|
||
let document = JSObject.global.document | ||
|
||
// var countLabel = document.createElement("span") | ||
// _ = document.body.appendChild(countLabel) | ||
// | ||
// var decrementButton = document.createElement("button") | ||
// decrementButton.innerText = "-" | ||
// decrementButton.onclick = .object( | ||
// JSClosure { _ in | ||
// model.decrementButtonTapped() | ||
// return .undefined | ||
// } | ||
// ) | ||
// _ = document.body.appendChild(decrementButton) | ||
// | ||
// var incrementButton = document.createElement("button") | ||
// incrementButton.innerText = "+" | ||
// incrementButton.onclick = .object( | ||
// JSClosure { _ in | ||
// model.incrementButtonTapped() | ||
// return .undefined | ||
// } | ||
// ) | ||
// _ = document.body.appendChild(incrementButton) | ||
|
||
var counter = document.createElement("input") | ||
counter.type = "number" | ||
_ = document.body.appendChild(counter) | ||
counter | ||
.bind($model.count.toString, to: \.value, event: \.onchange) | ||
.store(in: &tokens) | ||
|
||
var textField = document.createElement("input") | ||
textField.type = "text" | ||
_ = document.body.appendChild(textField) | ||
textField.bind($model.text, to: \.value, event: \.onkeyup) | ||
.store(in: &tokens) | ||
|
||
textField.bind(focus: $model.isTextFocused) | ||
.store(in: &tokens) | ||
|
||
// enum Focus { | ||
// case counter | ||
// case textField | ||
// } | ||
// var focus: Focus? | ||
// counter.bind(focus: $model.focus, equals: .counter) | ||
// textField.bind(focus: $model.focus, equals: .textField) | ||
|
||
var factButton = document.createElement("button") | ||
factButton.innerText = "Get fact" | ||
factButton.onclick = .object( | ||
JSClosure { _ in | ||
Task { await model.factButtonTapped() } | ||
return .undefined | ||
} | ||
) | ||
_ = document.body.appendChild(factButton) | ||
|
||
var factLabel = document.createElement("div") | ||
_ = document.body.appendChild(factLabel) | ||
|
||
observe { | ||
//countLabel.innerText = .string("Count: \(model.count)") | ||
|
||
if model.factIsLoading { | ||
factLabel.innerText = "Fact is loading..." | ||
} else { | ||
factLabel.innerText = "" | ||
} | ||
} | ||
.store(in: &tokens) | ||
|
||
alertDialog($model.alert) | ||
.store(in: &tokens) | ||
} | ||
} | ||
|
||
extension Int { | ||
fileprivate var toString: String { | ||
get { | ||
String(self) | ||
} | ||
set { | ||
self = Int(newValue) ?? 0 | ||
} | ||
} | ||
} | ||
|
||
struct JavaScriptConsoleWarning: IssueReporter { | ||
func reportIssue( | ||
_ message: @autoclosure () -> String?, | ||
fileID: StaticString, | ||
filePath: StaticString, | ||
line: UInt, | ||
column: UInt | ||
) { | ||
#if DEBUG | ||
_ = JSObject.global.console.warn(""" | ||
\(fileID):\(line) - \(message() ?? "") | ||
""") | ||
#endif | ||
} | ||
} |
Oops, something went wrong.