Skip to content

Commit

Permalink
✨ Add Environment
Browse files Browse the repository at this point in the history
  • Loading branch information
usatie committed Jun 26, 2019
1 parent 3b19e20 commit 3c96efc
Show file tree
Hide file tree
Showing 5 changed files with 192 additions and 1 deletion.
20 changes: 20 additions & 0 deletions Mew.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
29350EB622BF04C0003A86B4 /* TableViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 29350EB222BF04BF003A86B4 /* TableViewCell.swift */; };
29350EB722BF04C0003A86B4 /* TableViewHeaderFooterView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 29350EB322BF04C0003A86B4 /* TableViewHeaderFooterView.swift */; };
29350EB822BF04C0003A86B4 /* CollectionReusableView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 29350EB422BF04C0003A86B4 /* CollectionReusableView.swift */; };
29D7467922C2F85900C9FCF4 /* EnvironmentRequest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 29D7467822C2F85900C9FCF4 /* EnvironmentRequest.swift */; };
29D7468722C2FC2700C9FCF4 /* ViewControllerRequest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 29D7468622C2FC2700C9FCF4 /* ViewControllerRequest.swift */; };
29D7468922C2FC3000C9FCF4 /* ViewControllerResponse.swift in Sources */ = {isa = PBXBuildFile; fileRef = 29D7468822C2FC3000C9FCF4 /* ViewControllerResponse.swift */; };
D2568DE6214BC86800FCA44A /* CollectionViewCellTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D2568DE3214BC86400FCA44A /* CollectionViewCellTests.swift */; };
D2568DE7214BC86800FCA44A /* TableViewCellTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D2568DE2214BC86400FCA44A /* TableViewCellTests.swift */; };
D2568DEF214BCA3B00FCA44A /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = D2568DEE214BCA3B00FCA44A /* AppDelegate.swift */; };
Expand Down Expand Up @@ -63,6 +66,9 @@
29350EB222BF04BF003A86B4 /* TableViewCell.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TableViewCell.swift; sourceTree = "<group>"; };
29350EB322BF04C0003A86B4 /* TableViewHeaderFooterView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TableViewHeaderFooterView.swift; sourceTree = "<group>"; };
29350EB422BF04C0003A86B4 /* CollectionReusableView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CollectionReusableView.swift; sourceTree = "<group>"; };
29D7467822C2F85900C9FCF4 /* EnvironmentRequest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EnvironmentRequest.swift; sourceTree = "<group>"; };
29D7468622C2FC2700C9FCF4 /* ViewControllerRequest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewControllerRequest.swift; sourceTree = "<group>"; };
29D7468822C2FC3000C9FCF4 /* ViewControllerResponse.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewControllerResponse.swift; sourceTree = "<group>"; };
D2568DE2214BC86400FCA44A /* TableViewCellTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TableViewCellTests.swift; sourceTree = "<group>"; };
D2568DE3214BC86400FCA44A /* CollectionViewCellTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CollectionViewCellTests.swift; sourceTree = "<group>"; };
D2568DEC214BCA3B00FCA44A /* MewTestsApp.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = MewTestsApp.app; sourceTree = BUILT_PRODUCTS_DIR; };
Expand Down Expand Up @@ -108,6 +114,16 @@
/* End PBXFrameworksBuildPhase section */

/* Begin PBXGroup section */
29D7467722C2F7B700C9FCF4 /* Environment */ = {
isa = PBXGroup;
children = (
29D7467822C2F85900C9FCF4 /* EnvironmentRequest.swift */,
29D7468822C2FC3000C9FCF4 /* ViewControllerResponse.swift */,
29D7468622C2FC2700C9FCF4 /* ViewControllerRequest.swift */,
);
path = Environment;
sourceTree = "<group>";
};
D2568DD7214BC00A00FCA44A /* Cells */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -223,6 +239,7 @@
OBJ_8 /* Mew */ = {
isa = PBXGroup;
children = (
29D7467722C2F7B700C9FCF4 /* Environment */,
D25C01C621620BEC00935A18 /* Utils */,
D279A3042136B31A007B045A /* Protocols */,
D279A3052136B31F007B045A /* ContainerView */,
Expand Down Expand Up @@ -357,12 +374,15 @@
buildActionMask = 0;
files = (
29350EAB22BF0491003A86B4 /* CellProtocol.swift in Sources */,
29D7468922C2FC3000C9FCF4 /* ViewControllerResponse.swift in Sources */,
D25C01C821620C0100935A18 /* BackwardCompatibility.swift in Sources */,
29350EB522BF04C0003A86B4 /* CollectionViewCell.swift in Sources */,
29350EAA22BF0491003A86B4 /* Interactable.swift in Sources */,
29D7468722C2FC2700C9FCF4 /* ViewControllerRequest.swift in Sources */,
29350EA822BF0491003A86B4 /* Instantiatable.swift in Sources */,
29350EB622BF04C0003A86B4 /* TableViewCell.swift in Sources */,
29350EA922BF0491003A86B4 /* Emittable.swift in Sources */,
29D7467922C2F85900C9FCF4 /* EnvironmentRequest.swift in Sources */,
29350EB722BF04C0003A86B4 /* TableViewHeaderFooterView.swift in Sources */,
29350EB022BF04AB003A86B4 /* ContainerView.swift in Sources */,
29350EB822BF04C0003A86B4 /* CollectionReusableView.swift in Sources */,
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ https://www.icloud.com/keynote/0vgTYDXyHQTd0l1FKTiF1jT7g#MicroViewController-en
----|----
| ContainerView ||
| Container<T> ||
| Environment, Testing support | WIP |
| Environment, Testing support | |
| UITableView support ||
| UICollectionView support ||

Expand All @@ -61,5 +61,6 @@ https://www.mercari.com/cla/
## License

Copyright 2018 Mercari, Inc.
Copyright 2019 Yenom, Inc.

Licensed under the MIT License.
13 changes: 13 additions & 0 deletions Sources/Mew/Environment/EnvironmentRequest.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//
// EnvironmentRequest.swift
// Mew
//
// Created by Shun Usami on 2019/06/26.
// Copyright © 2019 Mercari. All rights reserved.
//

import Foundation

public protocol EnvironmentRequest {
associatedtype EnvironmentResponse
}
103 changes: 103 additions & 0 deletions Sources/Mew/Environment/ViewControllerRequest.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
//
// ViewControllerRequest.swift
// Mew
//
// Created by Shun Usami on 2019/06/26.
// Copyright © 2019 Mercari. All rights reserved.
//

import Foundation
import UIKit

// Instantiatable
public protocol ViewControllerRequest: EnvironmentRequest where EnvironmentResponse == ViewControllerResponse {
associatedtype Input
associatedtype EnvironmentResponse = ViewControllerResponse

var inputValue: Input { get }
}

extension ViewControllerRequest {
public func response<V>(for type: V.Type, environment: V.Environment)
-> ViewControllerResponse
where V: UIViewController,
V: Instantiatable,
V.Input == Input {
let viewController = V(with: inputValue, environment: environment)
return ViewControllerResponse(viewController: viewController)
}
}

// Injectable
public protocol InjectableViewControllerRequest: EnvironmentRequest where EnvironmentResponse == InjectableViewControllerResponse<Input> {
associatedtype Input
associatedtype EnvironmentResponse = InjectableViewControllerResponse<Input>

var inputValue: Input { get }
}

extension InjectableViewControllerRequest {
public func response<V>(for type: V.Type, environment: V.Environment)
-> InjectableViewControllerResponse<Input>
where V: UIViewController,
V: Instantiatable,
V: Injectable,
V.Input == Input {
let viewController = V(with: inputValue, environment: environment)
return InjectableViewControllerResponse(
viewController: viewController,
input: viewController.input
)
}
}

// Emittable
public protocol EmittableViewControllerRequest: EnvironmentRequest where EnvironmentResponse == EmittableViewControllerResponse<Output> {
associatedtype Input
associatedtype Output
associatedtype EnvironmentResponse = EmittableViewControllerResponse<Output>

var inputValue: Input { get }
}

extension EmittableViewControllerRequest {
public func response<V>(for type: V.Type, environment: V.Environment)
-> EmittableViewControllerResponse<Output>
where V: UIViewController,
V: Instantiatable,
V: Emittable,
V.Input == Input,
V.Output == Output {
let viewController = V(with: inputValue, environment: environment)
return EmittableViewControllerResponse(
viewController: viewController,
output: viewController.output
)
}
}

// Interactable
public protocol InteractableViewControllerRequest: EnvironmentRequest where EnvironmentResponse == InteractableViewControllerResponse<Input, Output> {
associatedtype Input
associatedtype Output
associatedtype EnvironmentResponse = InteractableViewControllerResponse<Input, Output>

var inputValue: Input { get }
}

extension InteractableViewControllerRequest {
public func response<V>(for type: V.Type, environment: V.Environment)
-> InteractableViewControllerResponse<Input, Output>
where V: UIViewController,
V: Instantiatable,
V: Interactable,
V.Input == Input,
V.Output == Output {
let viewController = V(with: inputValue, environment: environment)
return InteractableViewControllerResponse(
viewController: viewController,
input: viewController.input,
output: viewController.output
)
}
}
54 changes: 54 additions & 0 deletions Sources/Mew/Environment/ViewControllerResponse.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
//
// ViewControllerResponse.swift
// Mew
//
// Created by Shun Usami on 2019/06/26.
// Copyright © 2019 Mercari. All rights reserved.
//

import Foundation
import UIKit

public final class ViewControllerResponse {
public let viewController: UIViewController

public init(viewController: UIViewController) {
self.viewController = viewController
}
}

public final class InjectableViewControllerResponse<Input> {
public let viewController: UIViewController
public let input: (Input) -> Void

public init(viewController: UIViewController,
input: @escaping (Input) -> Void) {
self.viewController = viewController
self.input = input
}
}

public final class EmittableViewControllerResponse<Output> {
public let viewController: UIViewController
public let output: (((Output) -> Void)?) -> Void

public init(viewController: UIViewController,
output: @escaping (((Output) -> Void)?) -> Void) {
self.viewController = viewController
self.output = output
}
}

public final class InteractableViewControllerResponse<Input, Output> {
public let viewController: UIViewController
public let input: (Input) -> Void
public let output: (((Output) -> Void)?) -> Void

public init(viewController: UIViewController,
input: @escaping (Input) -> Void,
output: @escaping (((Output) -> Void)?) -> Void) {
self.viewController = viewController
self.input = input
self.output = output
}
}

0 comments on commit 3c96efc

Please sign in to comment.