Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add SwiftUI App Lifecycle #195

Merged
merged 16 commits into from
Jul 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions NativeDemo/TokamakDemo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
B56F22E124BC89FD001738DF /* ColorDemo.swift in Sources */ = {isa = PBXBuildFile; fileRef = B56F22DF24BC89FD001738DF /* ColorDemo.swift */; };
B56F22E324BD1C26001738DF /* GridDemo.swift in Sources */ = {isa = PBXBuildFile; fileRef = B56F22E224BD1C26001738DF /* GridDemo.swift */; };
B56F22E424BD1C26001738DF /* GridDemo.swift in Sources */ = {isa = PBXBuildFile; fileRef = B56F22E224BD1C26001738DF /* GridDemo.swift */; };
B5C76E4A24C73ED5003EABB2 /* AppStorageDemo.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5C76E4924C73ED4003EABB2 /* AppStorageDemo.swift */; };
B5C76E4B24C73ED5003EABB2 /* AppStorageDemo.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5C76E4924C73ED4003EABB2 /* AppStorageDemo.swift */; };
D1B4229024B3B9BB00682F74 /* ListDemo.swift in Sources */ = {isa = PBXBuildFile; fileRef = D1B4228E24B3B9BB00682F74 /* ListDemo.swift */; };
D1B4229124B3B9BB00682F74 /* ListDemo.swift in Sources */ = {isa = PBXBuildFile; fileRef = D1B4228E24B3B9BB00682F74 /* ListDemo.swift */; };
D1B4229224B3B9BB00682F74 /* OutlineGroupDemo.swift in Sources */ = {isa = PBXBuildFile; fileRef = D1B4228F24B3B9BB00682F74 /* OutlineGroupDemo.swift */; };
Expand Down Expand Up @@ -93,6 +95,7 @@
B51F214F24B920B400CF2583 /* PathDemo.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PathDemo.swift; sourceTree = "<group>"; };
B56F22DF24BC89FD001738DF /* ColorDemo.swift */ = {isa = PBXFileReference; fileEncoding = 4; indentWidth = 2; lastKnownFileType = sourcecode.swift; path = ColorDemo.swift; sourceTree = "<group>"; tabWidth = 2; };
B56F22E224BD1C26001738DF /* GridDemo.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = GridDemo.swift; sourceTree = "<group>"; };
B5C76E4924C73ED4003EABB2 /* AppStorageDemo.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AppStorageDemo.swift; sourceTree = "<group>"; };
D1B4228E24B3B9BB00682F74 /* ListDemo.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ListDemo.swift; sourceTree = "<group>"; };
D1B4228F24B3B9BB00682F74 /* OutlineGroupDemo.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = OutlineGroupDemo.swift; sourceTree = "<group>"; };
D1E5FDA424C1D54B00E7485E /* libTokamakShim.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libTokamakShim.a; sourceTree = BUILT_PRODUCTS_DIR; };
Expand Down Expand Up @@ -156,6 +159,7 @@
85ED189924AD425E0085DFA0 /* TokamakDemo */ = {
isa = PBXGroup;
children = (
B5C76E4924C73ED4003EABB2 /* AppStorageDemo.swift */,
D1EE7EA624C0DD2100C0D127 /* PickerDemo.swift */,
D1B4228E24B3B9BB00682F74 /* ListDemo.swift */,
D1B4228F24B3B9BB00682F74 /* OutlineGroupDemo.swift */,
Expand Down Expand Up @@ -269,7 +273,7 @@
};
};
};
buildConfigurationList = 85ED184124AD37970085DFA0 /* Build configuration list for PBXProject "TokamakDemo Native" */;
buildConfigurationList = 85ED184124AD37970085DFA0 /* Build configuration list for PBXProject "TokamakDemo" */;
compatibilityVersion = "Xcode 9.3";
developmentRegion = en;
hasScannedForEncodings = 0;
Expand Down Expand Up @@ -323,6 +327,7 @@
D1B4229024B3B9BB00682F74 /* ListDemo.swift in Sources */,
D1EE7EA724C0DD2100C0D127 /* PickerDemo.swift in Sources */,
85ED18A924AD425E0085DFA0 /* TokamakDemo.swift in Sources */,
B5C76E4A24C73ED5003EABB2 /* AppStorageDemo.swift in Sources */,
85ED18AD24AD425E0085DFA0 /* TextFieldDemo.swift in Sources */,
85ED18A724AD425E0085DFA0 /* ForEachDemo.swift in Sources */,
854A1A9124B3E3630027BC32 /* ToggleDemo.swift in Sources */,
Expand All @@ -345,6 +350,7 @@
D1B4229124B3B9BB00682F74 /* ListDemo.swift in Sources */,
D1EE7EA824C0DD2100C0D127 /* PickerDemo.swift in Sources */,
85ED18B624AD42D70085DFA0 /* NSAppDelegate.swift in Sources */,
B5C76E4B24C73ED5003EABB2 /* AppStorageDemo.swift in Sources */,
85ED18AC24AD425E0085DFA0 /* Counter.swift in Sources */,
85ED18A824AD425E0085DFA0 /* ForEachDemo.swift in Sources */,
854A1A9324B3F28F0027BC32 /* ToggleDemo.swift in Sources */,
Expand Down Expand Up @@ -613,7 +619,7 @@
/* End XCBuildConfiguration section */

/* Begin XCConfigurationList section */
85ED184124AD37970085DFA0 /* Build configuration list for PBXProject "TokamakDemo Native" */ = {
85ED184124AD37970085DFA0 /* Build configuration list for PBXProject "TokamakDemo" */ = {
isa = XCConfigurationList;
buildConfigurations = (
85ED185C24AD379B0085DFA0 /* Debug */,
Expand Down
4 changes: 2 additions & 2 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
"package": "JavaScriptKit",
"repositoryURL": "https://github.com/kateinoigakukun/JavaScriptKit.git",
"state": {
"branch": "47f2bb1",
"revision": "47f2bb16576ba386a2c7c69c97b0fd44f470126f",
"branch": "c90e82f",
"revision": "c90e82fe1d576a2ccd1aae798380bf80be7885fb",
"version": null
}
},
Expand Down
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ let package = Package(
dependencies: [
// Dependencies declare other packages that this package depends on.
// .package(url: /* package url */, from: "1.0.0"),
.package(url: "https://github.com/kateinoigakukun/JavaScriptKit.git", .revision("47f2bb1")),
.package(url: "https://github.com/kateinoigakukun/JavaScriptKit.git", .revision("c90e82f")),
.package(url: "https://github.com/MaxDesiatov/Runtime.git", .branch("wasi-build")),
.package(url: "https://github.com/MaxDesiatov/OpenCombine.git", .branch("observable-object")),
],
Expand Down
47 changes: 47 additions & 0 deletions Sources/TokamakCore/App/App.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright 2020 Tokamak contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Created by Carson Katri on 7/16/20.
//

import OpenCombine
import Runtime

/// Provides the ability to set the title of the Scene.
public protocol _TitledApp {
static func _setTitle(_ title: String)
}

/// The renderer is responsible for implementing certain functionality.
public protocol App: _TitledApp {
associatedtype Body: Scene
var body: Body { get }

/// Implemented by the renderer to mount the `App`
static func _launch(_ app: Self,
_ rootEnvironment: EnvironmentValues)
/// Implemented by the renderer to update the `App` on `ScenePhase` changes
var _phasePublisher: CurrentValueSubject<ScenePhase, Never> { get }

static func main()

init()
}

extension App {
public static func main() {
let app = Self()
_launch(app, EnvironmentValues())
}
j-f1 marked this conversation as resolved.
Show resolved Hide resolved
}
192 changes: 192 additions & 0 deletions Sources/TokamakCore/App/AppStorage.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
// Copyright 2020 Tokamak contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Created by Carson Katri on 7/16/20.
//

import OpenCombine

@propertyWrapper public struct AppStorage<Value>: ObservedProperty {
let provider: _StorageProvider?
@Environment(\._defaultAppStorage) var defaultProvider: _StorageProvider?
var unwrappedProvider: _StorageProvider {
provider ?? defaultProvider!
}

let key: String
let defaultValue: Value
let store: (_StorageProvider, String, Value) -> ()
let read: (_StorageProvider, String) -> Value?

var objectWillChange: AnyPublisher<(), Never> {
unwrappedProvider.publisher.eraseToAnyPublisher()
}

public var wrappedValue: Value {
get {
read(unwrappedProvider, key) ?? defaultValue
}
nonmutating set {
store(unwrappedProvider, key, newValue)
}
}

public var projectedValue: Binding<Value> {
.init {
wrappedValue
} set: {
wrappedValue = $0
}
}
}

extension AppStorage: DynamicProperty {}

extension AppStorage {
public init(wrappedValue: Value,
_ key: String,
store: _StorageProvider? = nil) where Value == Bool {
defaultValue = wrappedValue
self.key = key
provider = store
self.store = { $0.store(key: $1, value: $2) }
read = { $0.read(key: $1) }
}

public init(wrappedValue: Value,
_ key: String,
store: _StorageProvider? = nil) where Value == Int {
defaultValue = wrappedValue
self.key = key
provider = store
self.store = { $0.store(key: $1, value: $2) }
read = { $0.read(key: $1) }
}

public init(wrappedValue: Value,
_ key: String,
store: _StorageProvider? = nil) where Value == Double {
defaultValue = wrappedValue
self.key = key
provider = store
self.store = { $0.store(key: $1, value: $2) }
read = { $0.read(key: $1) }
}

public init(wrappedValue: Value,
_ key: String,
store: _StorageProvider? = nil) where Value == String {
defaultValue = wrappedValue
self.key = key
provider = store
self.store = { $0.store(key: $1, value: $2) }
read = { $0.read(key: $1) }
}

public init(wrappedValue: Value,
_ key: String,
store: _StorageProvider? = nil)
where Value: RawRepresentable, Value.RawValue == Int {
defaultValue = wrappedValue
self.key = key
provider = store
self.store = { $0.store(key: $1, value: $2.rawValue) }
read = {
guard let rawValue = $0.read(key: $1) as Int? else {
return nil
}
return Value(rawValue: rawValue)
}
}

public init(wrappedValue: Value,
_ key: String,
store: _StorageProvider? = nil)
where Value: RawRepresentable, Value.RawValue == String {
defaultValue = wrappedValue
self.key = key
provider = store
self.store = { $0.store(key: $1, value: $2.rawValue) }
read = {
guard let rawValue = $0.read(key: $1) as String? else {
return nil
}
return Value(rawValue: rawValue)
}
}
}

extension AppStorage where Value: ExpressibleByNilLiteral {
public init(wrappedValue: Value,
_ key: String,
store: _StorageProvider? = nil) where Value == Bool? {
defaultValue = wrappedValue
self.key = key
provider = store
self.store = { $0.store(key: $1, value: $2) }
read = { $0.read(key: $1) }
}

public init(wrappedValue: Value,
_ key: String,
store: _StorageProvider? = nil) where Value == Int? {
defaultValue = wrappedValue
self.key = key
provider = store
self.store = { $0.store(key: $1, value: $2) }
read = { $0.read(key: $1) }
}

public init(wrappedValue: Value,
_ key: String,
store: _StorageProvider? = nil) where Value == Double? {
defaultValue = wrappedValue
self.key = key
provider = store
self.store = { $0.store(key: $1, value: $2) }
read = { $0.read(key: $1) }
}

public init(wrappedValue: Value,
_ key: String,
store: _StorageProvider? = nil) where Value == String? {
defaultValue = wrappedValue
self.key = key
provider = store
self.store = { $0.store(key: $1, value: $2) }
read = { $0.read(key: $1) }
}
}

/// The renderer is responsible for making sure a default is set at the root of the App.
struct DefaultAppStorageEnvironmentKey: EnvironmentKey {
static let defaultValue: _StorageProvider? = nil
}

extension EnvironmentValues {
public var _defaultAppStorage: _StorageProvider? {
get {
self[DefaultAppStorageEnvironmentKey.self]
}
set {
self[DefaultAppStorageEnvironmentKey.self] = newValue
}
}
}

extension View {
public func defaultAppStorage(_ store: _StorageProvider) -> some View {
environment(\._defaultAppStorage, store)
}
}
49 changes: 49 additions & 0 deletions Sources/TokamakCore/App/Scenes/Scene.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright 2020 Tokamak contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Created by Carson Katri on 7/16/20.
//

public protocol Scene {
associatedtype Body: Scene

// FIXME: If I put `@SceneBuilder` in front of this
// it fails to build with no useful error message.
var body: Self.Body { get }
}

protocol TitledScene {
var title: Text? { get }
}

protocol ViewContainingScene {
var anyContent: AnyView { get }
}

protocol ParentScene {
var children: [_AnyScene] { get }
}

protocol GroupScene: ParentScene {}

public protocol SceneDeferredToRenderer {
var deferredBody: AnyView { get }
}

extension Never: Scene {}

/// Calls `fatalError` with an explanation that a given `type` is a primitive `Scene`
public func neverScene(_ type: String) -> Never {
fatalError("\(type) is a primitive `Scene`, you're not supposed to access its `body`.")
}
Loading