Skip to content

Commit

Permalink
Fix navigation controller nil and update example
Browse files Browse the repository at this point in the history
  • Loading branch information
lumiasaki committed Jul 29, 2021
1 parent e6f19e1 commit 2e7429e
Show file tree
Hide file tree
Showing 11 changed files with 50 additions and 61 deletions.
8 changes: 4 additions & 4 deletions Example/Example.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
CE7094CD269C2AEA00298AD2 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = CE7094CB269C2AEA00298AD2 /* LaunchScreen.storyboard */; };
CE7094D6269C2B8F00298AD2 /* ExampleSceneState.swift in Sources */ = {isa = PBXBuildFile; fileRef = CE7094D5269C2B8F00298AD2 /* ExampleSceneState.swift */; };
CE7094D9269C2BF900298AD2 /* SceneBox in Frameworks */ = {isa = PBXBuildFile; productRef = CE7094D8269C2BF900298AD2 /* SceneBox */; };
CE7094DB269C390A00298AD2 /* SceneStateConfiguration.swift in Sources */ = {isa = PBXBuildFile; fileRef = CE7094DA269C390A00298AD2 /* SceneStateConfiguration.swift */; };
CE7094DB269C390A00298AD2 /* MyConfigureFile.swift in Sources */ = {isa = PBXBuildFile; fileRef = CE7094DA269C390A00298AD2 /* MyConfigureFile.swift */; };
CE7094DD269C393C00298AD2 /* SceneDataSharingKey.swift in Sources */ = {isa = PBXBuildFile; fileRef = CE7094DC269C393C00298AD2 /* SceneDataSharingKey.swift */; };
CE7094E2269C3FDB00298AD2 /* HomeViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = CE7094E1269C3FDB00298AD2 /* HomeViewController.swift */; };
CE7094E4269C3FE400298AD2 /* DetailViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = CE7094E3269C3FE400298AD2 /* DetailViewController.swift */; };
Expand All @@ -30,7 +30,7 @@
CE7094CC269C2AEA00298AD2 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = "<group>"; };
CE7094CE269C2AEA00298AD2 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
CE7094D5269C2B8F00298AD2 /* ExampleSceneState.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ExampleSceneState.swift; sourceTree = "<group>"; };
CE7094DA269C390A00298AD2 /* SceneStateConfiguration.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SceneStateConfiguration.swift; sourceTree = "<group>"; };
CE7094DA269C390A00298AD2 /* MyConfigureFile.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MyConfigureFile.swift; sourceTree = "<group>"; };
CE7094DC269C393C00298AD2 /* SceneDataSharingKey.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SceneDataSharingKey.swift; sourceTree = "<group>"; };
CE7094E1269C3FDB00298AD2 /* HomeViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HomeViewController.swift; sourceTree = "<group>"; };
CE7094E3269C3FE400298AD2 /* DetailViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DetailViewController.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -86,7 +86,7 @@
isa = PBXGroup;
children = (
CE7094D5269C2B8F00298AD2 /* ExampleSceneState.swift */,
CE7094DA269C390A00298AD2 /* SceneStateConfiguration.swift */,
CE7094DA269C390A00298AD2 /* MyConfigureFile.swift */,
CE7094DC269C393C00298AD2 /* SceneDataSharingKey.swift */,
);
path = SceneConfiguration;
Expand Down Expand Up @@ -196,7 +196,7 @@
files = (
CE7094D6269C2B8F00298AD2 /* ExampleSceneState.swift in Sources */,
CE7094DD269C393C00298AD2 /* SceneDataSharingKey.swift in Sources */,
CE7094DB269C390A00298AD2 /* SceneStateConfiguration.swift in Sources */,
CE7094DB269C390A00298AD2 /* MyConfigureFile.swift in Sources */,
CE7094EA269C4D9700298AD2 /* Color.swift in Sources */,
CE7094E6269C406C00298AD2 /* HomeViewModel.swift in Sources */,
CE7094E8269C407400298AD2 /* DetailViewModel.swift in Sources */,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
"repositoryURL": "https://github.com/lumiasaki/SceneBox.git",
"state": {
"branch": null,
"revision": "6586041e5e3f1c12a928d90eb7f46b9ccebf04ae",
"version": "0.3.1"
"revision": "e6f19e17b3e19f271c189fb0c5e95c5d50e46bf4",
"version": "0.4.0"
}
}
]
Expand Down
7 changes: 0 additions & 7 deletions Example/Example/SceneConfiguration/ExampleSceneState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,4 @@ struct ExampleSceneState: RawRepresentable, Hashable, Equatable {

static let home = ExampleSceneState(rawValue: NavigationExtension.entry)
static let detail = ExampleSceneState(rawValue: 1)
static let termination = ExampleSceneState(rawValue: NavigationExtension.termination)
}

extension ExampleSceneState: CaseIterable {

/// Help to register all states.
static var allCases: [ExampleSceneState] { [.home, .detail, .termination] }
}
24 changes: 24 additions & 0 deletions Example/Example/SceneConfiguration/MyConfigureFile.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//
// MyConfigureFile.swift
// Example
//
// Created by Lumia_Saki on 2021/7/12.
// Copyright © 2021年 tianren.zhu. All rights reserved.
//

import Foundation
import SceneBox

/// Configuration file for this example project.
struct MyConfigureFile: ConfigurationFile {

static var sceneStates: Set<Int> = Set([
ExampleSceneState.home.rawValue,
ExampleSceneState.detail.rawValue
])

static var extensions: [Extension] = [
NavigationExtension(),
SharedStateExtension(stateValue: SceneData())
]
}
34 changes: 0 additions & 34 deletions Example/Example/SceneConfiguration/SceneStateConfiguration.swift

This file was deleted.

14 changes: 5 additions & 9 deletions Example/Example/SceneDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,13 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate {
window?.makeKeyAndVisible()

// configure SceneBox with some configurations
let sceneStateConfiguration = SceneStateConfiguration(sceneStates: ExampleSceneState.allCases)

let sceneBoxConfiguration = Configuration(stateSceneIdentifierTable: sceneStateConfiguration.currentSceneStateMap).withBuiltInNavigationExtension().withBuiltInSharedStateExtension(stateValue: SceneData())

sceneBoxConfiguration.navigationController = navigationController
let sceneBoxConfiguration = try! Configuration(configurationFile: MyConfigureFile.self)

let sceneBox = SceneBox(configuration: sceneBoxConfiguration) { scene, sceneBox in
self.navigationController.pushViewController(scene, animated: false)
} exit: { _ in }

setUpSceneStateIdentifierTable(for: sceneBox, sceneStateConfiguration: sceneStateConfiguration)
setUpSceneStateIdentifierTable(for: sceneBox)

try? Executor.shared.execute(box: sceneBox)
}
Expand Down Expand Up @@ -79,10 +75,10 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate {
extension SceneDelegate {

// The real set up place for view controllers and view models, it's a great tool for decoupling
private func setUpSceneStateIdentifierTable(for box: SceneBox, sceneStateConfiguration: SceneStateConfiguration) {
private func setUpSceneStateIdentifierTable(for box: SceneBox) {
// home
do {
box.lazyAdd(identifier: sceneStateConfiguration.identifier(with: .home)) {
box.lazyAdd(sceneState: ExampleSceneState.home.rawValue) {
let viewModel = HomeViewModel()
let viewController = HomeViewController(viewModel: viewModel)
viewModel.scene = viewController
Expand All @@ -93,7 +89,7 @@ extension SceneDelegate {

// detail
do {
box.lazyAdd(identifier: sceneStateConfiguration.identifier(with: .detail)) {
box.lazyAdd(sceneState: ExampleSceneState.detail.rawValue) {
let viewModel = DetailViewModel()
let viewController = DetailViewController(viewModel: viewModel)
viewModel.scene = viewController
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Based on these two pain points, I conceived the framework to enable us to develo

To integrate using Apple's SPM, add following as a dependency to your Target.

`.package(url: "https://github.com/lumiasaki/SceneBox.git", .upToNextMajor(from: "0.4.0"))`
`.package(url: "https://github.com/lumiasaki/SceneBox.git", .upToNextMajor(from: "0.4.1"))`

## How to use

Expand Down
8 changes: 5 additions & 3 deletions Sources/SceneBox/Extension/Core/NavigationExtension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,16 @@ public final class NavigationExtension: NSObject, Extension {
// insert entry into state trace
stateTrace.append(Self.entry)

previousNavigationControllerDelegate = sceneBox?.navigationController?.delegate
sceneBox?.navigationController?.delegate = self

sceneBox?.watch(on: EventBus.EventName.getStatesRequest, messageType: Void.self, next: { [unowned self] _ in
self.sceneBox?.dispatch(event: EventBus.EventName.getStatesResponse, message: NavigationGetScenesMessage(scenes: stateTrace))
})
}

public func sceneBoxReady() {
previousNavigationControllerDelegate = sceneBox?.navigationController?.delegate
sceneBox?.navigationController?.delegate = self
}

// MARK: - Private

fileprivate func transit(to state: Int) {
Expand Down
2 changes: 1 addition & 1 deletion Sources/SceneBox/LifeCycle/ExtensionLifeCycle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ public protocol ExtensionLifeCycle {

public extension ExtensionLifeCycle {

func extensionDidMount() { }
func extensionDidMount() { }
}
4 changes: 4 additions & 0 deletions Sources/SceneBox/LifeCycle/SceneBoxLifeCycle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,15 @@ import Foundation
/// Life cycle of the a scene box.
public protocol SceneBoxLifeCycle {

/// Called at last step in `SceneBox.execute`.
func sceneBoxReady()

/// Called when transit to the `terminate` state, the scene box will be terminated.
func sceneBoxWillTerminate()
}

public extension SceneBoxLifeCycle {

func sceneBoxReady() { }
func sceneBoxWillTerminate() { }
}
4 changes: 4 additions & 0 deletions Sources/SceneBox/SceneBox.swift
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ public final class SceneBox {
}

self.navigationController = navigationController

// notify ready event
scenesIdentifierMap.values.forEach { $0.sceneBoxReady() }
extensions.values.forEach { $0.sceneBoxReady() }
}

// MARK: - Private Methods
Expand Down

0 comments on commit 2e7429e

Please sign in to comment.