Skip to content

Commit

Permalink
🚀 Taking off
Browse files Browse the repository at this point in the history
  • Loading branch information
MihaelIsaev committed Mar 4, 2021
0 parents commit 3bf956b
Show file tree
Hide file tree
Showing 12 changed files with 252 additions and 0 deletions.
11 changes: 11 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
.DS_Store
/.build
/Packages
/*.xcodeproj
xcuserdata/
/.webber/dev
/.webber/release
/.webber/entrypoint/dev/.ssl
/.webber/node_modules
/.webber/package*
/.swiftpm
25 changes: 25 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// swift-tools-version:5.3

import PackageDescription

let package = Package(
name: "spa-template",
platforms: [
.macOS(.v10_15)
],
products: [
.executable(name: "App", targets: ["App"])
],
dependencies: [
.package(url: "https://github.com/swifweb/web", from: "1.0.0-beta.1.0.0")
],
targets: [
.target(name: "App", dependencies: [
.product(name: "Web", package: "web")
], resources: [
.copy("images/favicon.ico"),
.copy("images")
]),
.testTarget(name: "AppTests", dependencies: ["App"])
]
)
66 changes: 66 additions & 0 deletions Sources/App/App.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import Web

public class App: WebApp {
@State var color = Color.cyan

enum Theme {
case happy, sad
}

@State var theme: Theme = .happy

@AppBuilder public override var body: AppBuilder.Content {
Lifecycle.didFinishLaunching {
print("Lifecycle.didFinishLaunching")
}.willTerminate {
print("Lifecycle.willTerminate")
}.willResignActive {
print("Lifecycle.willResignActive")
}.didBecomeActive {
print("Lifecycle.didBecomeActive")
}.didEnterBackground {
print("Lifecycle.didEnterBackground")
}.willEnterForeground {
print("Lifecycle.willEnterForeground")
}
Routes {
Page { WelcomeViewController() }
Page("hello") { HelloViewController() }
Page("**") { NotFoundViewController() }
}
HappyStyle().id(.happyStyle).disabled($theme.map { $0 != .happy })
SadStyle().id("sadStyle").disabled($theme.map { $0 != .sad })
}
}

class HappyStyle: Stylesheet {
@Rules
override var rules: Rules.Content {
Rule(H1.pointer).color(App.current.$color)
Rule(Pointer.any)
.margin(all: 0)
.padding(all: 0)
Rule(H1.class(.hello).after, H2.class(.hello).after) {
AlignContent(.baseline)
Color(.red)
}
.property(.alignContent, .auto)
.alignContent(.auto)
.color(.red)
}
}

class SadStyle: Stylesheet {
@Rules
override var rules: Rules.Content {
Rule(H1.pointer).color(.deepPink)
}
}

extension Id {
static var happyStyle: Id { "happyStyle" }
}

extension Class {
static var hello: Class { "hello" }
}
15 changes: 15 additions & 0 deletions Sources/App/Extensions/Fonts.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import Web

extension FontFamilyType {
static var sanFrancisco: Self { .init("San Francisco") }

static var roboto: Self { .init("Roboto") }

static var segoeUI: Self { .init("Segoe UI") }

static var helveticaNeue: Self { .init("Helvetica Neue") }

static var lucidaGrande: Self { .init("Lucida Grande") }

static var app: Self { .combined(.system, .appleSystem, .sanFrancisco, .roboto, .segoeUI, .helveticaNeue, .lucidaGrande, .sansSerif) }
}
11 changes: 11 additions & 0 deletions Sources/App/ViewControllers/HelloViewController.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import Web

class HelloViewController: ViewController {
@DOM override var body: DOM.Content {
P("Hello page")
.textAlign(.center)
.body {
Button("go back").display(.block)
}
}
}
11 changes: 11 additions & 0 deletions Sources/App/ViewControllers/NotFoundViewController.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import Web

class NotFoundViewController: ViewController {
@DOM override var body: DOM.Content {
P("this is catchall aka 404 NOT FOUND page")
.textAlign(.center)
.body {
Button("go back").display(.block)
}
}
}
49 changes: 49 additions & 0 deletions Sources/App/ViewControllers/WelcomeViewController.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import Web

class WelcomeViewController: ViewController {
@State var clickCount = 0
@State var clicked = false

@DOM override var body: DOM.Content {
Header {
Div {
H1(self.$clickCount.map { "Hello world SPA app, clicks: \($0)" })
.color(self.$clicked.map { $0 ? .red : .white })
.textAlign(.center)
.fontFamily(.app)
Br()
H4("Now you can run the world using Swift")
.color(.white)
.textAlign(.center)
.fontFamily(.system, .appleSystem, .sanFrancisco, .roboto, .segoeUI, .helveticaNeue, .lucidaGrande, .sansSerif)
Button("click").onClick {
self.clicked = !self.clicked
self.clickCount += 1
}
Button("go to hello").onClick {
History.pushState(path: "hello")
}
Button("toggle style").onClick {
App.current.theme = App.current.theme == .happy ? .sad : .happy
}
}
.position(.absolute)
.display(.block)
.top(50.percent)
.left(50.percent)
.transform(.translate(-50.percent, -50.percent))
.whiteSpace(.nowrap)
.overflow(.hidden)
}
.position(.fixed)
.width(100.percent)
.height(100.percent)
.background(.linearGradient(180, .black/70, .gray))
}

override func buildUI() {
super.buildUI()
title = "SPA Hello world"
metaDescription = "An awesome Swift in heart of your website"
}
}
Binary file added Sources/App/images/favicon.ico
Binary file not shown.
1 change: 1 addition & 0 deletions Sources/App/main.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
let app = App.start()
47 changes: 47 additions & 0 deletions Tests/AppTests/AppTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import XCTest
import class Foundation.Bundle

final class SwiftWasmAppTests: XCTestCase {
func testExample() throws {
// This is an example of a functional test case.
// Use XCTAssert and related functions to verify your tests produce the correct
// results.

// Some of the APIs that we use below are available in macOS 10.13 and above.
guard #available(macOS 10.13, *) else {
return
}

let fooBinary = productsDirectory.appendingPathComponent("SwiftWasmApp")

let process = Process()
process.executableURL = fooBinary

let pipe = Pipe()
process.standardOutput = pipe

try process.run()
process.waitUntilExit()

let data = pipe.fileHandleForReading.readDataToEndOfFile()
let output = String(data: data, encoding: .utf8)

XCTAssertEqual(output, "Hello, world!\n")
}

/// Returns path to the built products directory.
var productsDirectory: URL {
#if os(macOS)
for bundle in Bundle.allBundles where bundle.bundlePath.hasSuffix(".xctest") {
return bundle.bundleURL.deletingLastPathComponent()
}
fatalError("couldn't find the products directory")
#else
return Bundle.main.bundleURL
#endif
}

static var allTests = [
("testExample", testExample),
]
}
9 changes: 9 additions & 0 deletions Tests/AppTests/XCTestManifests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import XCTest

#if !canImport(ObjectiveC)
public func allTests() -> [XCTestCaseEntry] {
return [
testCase(SwiftWasmAppTests.allTests),
]
}
#endif
7 changes: 7 additions & 0 deletions Tests/LinuxMain.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import XCTest

import SwiftWasmAppTests

var tests = [XCTestCaseEntry]()
tests += SwiftWasmAppTests.allTests()
XCTMain(tests)

0 comments on commit 3bf956b

Please sign in to comment.