Skip to content

Commit

Permalink
修改Result的类型为调用方自定义
Browse files Browse the repository at this point in the history
  • Loading branch information
张鹏 committed Jul 3, 2020
1 parent 406d09c commit 4039dad
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 68 deletions.
2 changes: 1 addition & 1 deletion DPFlowCoordinator.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

Pod::Spec.new do |s|
s.name = 'DPFlowCoordinator'
s.version = '2.1'
s.version = '2.2'
s.license = 'MIT'
s.summary = '流程协调器,用于分离业务流程代码'
s.homepage = 'http://www.dpdev.cn'
Expand Down
42 changes: 7 additions & 35 deletions DPFlowCoordinator/Classes/FlowCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import UIKit
/// 流程协调器,用于处理业务流程
///
/// 此为基类,不应直接调用此类,而应该子类化,在子类处理相关业务逻辑
open class FlowCoordinator<UserInfo, Error: Swift.Error> {
open class FlowCoordinator<Result> {

/// 流程完成的处理
private var completionHandler: CompletionHandler?
Expand All @@ -36,30 +36,12 @@ open class FlowCoordinator<UserInfo, Error: Swift.Error> {
FlowCoordinatorManager.shared.addFlowCoordinator(self, for: identifier)
}

/// 取消流程
/// 如果子类重写此方法,应当调用 `super.cancel()`
///
/// - Parameter error: 取消的错误信息,默认为nil
open func cancel() {
completionHandler?(.cancel)
clearUp()
}

/// 使流程失败
///
/// 如果子类重写此方法,应当调用 `super.fail(_:)`
/// - Parameter error: 对应的错误信息
open func fail(_ error: Error) {
completionHandler?(.failure(error))
clearUp()
}

/// 完成流程
/// 如果子类重写此方法,应当调用 `super.finish(_:)`
///
/// - Parameter userInfo: 完成时需要携带的用户信息
open func finish(_ userInfo: UserInfo) {
completionHandler?(.finish(userInfo))
/// 如果子类重新此方法,应当调用`super.complete(_:)`
/// - Parameter result: 结果
open func complete(_ result: Result) {
completionHandler?(result)
clearUp()
}

Expand All @@ -71,7 +53,7 @@ open class FlowCoordinator<UserInfo, Error: Swift.Error> {
}

// MARK: - Hashable
extension FlowCoordinator: Hashable where UserInfo: Any {
extension FlowCoordinator: Hashable where Result: Any {

public static func == (lhs: FlowCoordinator, rhs: FlowCoordinator) -> Bool {
return lhs.hashValue == rhs.hashValue
Expand Down Expand Up @@ -107,15 +89,5 @@ public extension FlowCoordinator {
public extension FlowCoordinator {

/// 流程协调器完成处理
typealias CompletionHandler = (Result) -> (Void)

/// 结果
enum Result {
/// 取消
case cancel
/// 失败
case failure(Error)
/// 完成
case finish(UserInfo)
}
typealias CompletionHandler = (Result) -> Void
}
24 changes: 16 additions & 8 deletions DPFlowCoordinatorExample/LoginFlowCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import DPFlowCoordinator


/// 登录流程
class LoginFlowCoordinator: FlowCoordinator<String, Swift.Error> {
class LoginFlowCoordinator: FlowCoordinator<LoginFlowCoordinator.Result> {

override func start(in viewController: UIViewController?, completion: CompletionHandler?) {
super.start(in: viewController, completion: completion)
Expand All @@ -36,26 +36,34 @@ class LoginFlowCoordinator: FlowCoordinator<String, Swift.Error> {
extension LoginFlowCoordinator: LoginViewControllerDelegate {

func loginViewController(_ viewController: LoginViewController, didClickCancelButton button: UIButton) {
cancel()
complete(.skip)
viewController.dismiss(animated: true, completion: nil)
}

func loginViewController(_ viewController: LoginViewController, didClickFinishButton button: UIButton) {
finish("张三")
// finish("张三")
complete(.success("张三"))
viewController.dismiss(animated: true, completion: nil)
}

func loginViewController(_ viewController: LoginViewController, didClickStartRegisterFlowButton button: UIButton) {

RegisterFlowCoordinator().start(in: viewController) { (result) -> (Void) in
switch result {
case .cancel:
print("取消注册")
case let .success(info):
print("注册成功\(info)")
case let .failure(error):
print("注册错误: \(error)")
case let .finish(userInfo):
print("注册成功\(userInfo)")
print("注册失败\(error)")
}
}
}
}

extension LoginFlowCoordinator {

enum Result {
case skip
case failure
case success(String)
}
}
11 changes: 8 additions & 3 deletions DPFlowCoordinatorExample/RegisterFlowCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import Foundation
import DPFlowCoordinator

/// 注册流程
class RegisterFlowCoordinator: FlowCoordinator<[String: Any], RegisterFlowCoordinator.Error> {
class RegisterFlowCoordinator: FlowCoordinator<RegisterFlowCoordinator.Result> {

override func start(in viewController: UIViewController?, completion: CompletionHandler?) {
super.start(in: viewController, completion: completion)
Expand All @@ -25,10 +25,10 @@ class RegisterFlowCoordinator: FlowCoordinator<[String: Any], RegisterFlowCoordi
registerPage.completionHandler = { [weak self] (text) in
if text == nil {
self?.viewController?.dismiss(animated: true, completion: nil)
self?.fail(.wrongInput)
self?.complete(.failure(.wrongInput))
} else {
self?.viewController?.dismiss(animated: true, completion: nil)
self?.finish(["1234": 123])
self?.complete(.success(["1234": 123]))
}
}

Expand All @@ -45,5 +45,10 @@ extension RegisterFlowCoordinator {
enum Error: Swift.Error {
case wrongInput
}

enum Result {
case success([String: Any])
case failure(Error)
}
}

20 changes: 9 additions & 11 deletions DPFlowCoordinatorExample/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,23 @@ class ViewController: UIViewController {
let loginFlow = LoginFlowCoordinator()
loginFlow.start(in: self) { (result) -> (Void) in
switch result {
case .cancel:
print("取消登录")
case let .finish(userInfo):
print("登录成功:\(String(describing: userInfo))")
case let .failure(error):
print("登录失败\(String(describing: error))")
case .skip:
print("跳过")
case let .success(user):
print("登录成功:\(user)")
case .failure:
print("登录失败")
}
}
}

@IBAction func registerFlowButtonDidClick(_ sender: Any) {
RegisterFlowCoordinator().start(in: self) { (result) -> (Void) in
switch result {
case let .finish(userInfo):
print("注册成功\(String(describing: userInfo))")
case let .success(info):
print("注册成功\(info)")
case let .failure(error):
print("注册失败\(String(describing: error))")
case .cancel:
print("取消注册")
print("注册失败\(error)")
}
}
}
Expand Down
33 changes: 23 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@
## 安装
使用`CocoaPods`
```
pod 'DPFlowCoordinator', '~> 2.1.0'
pod 'DPFlowCoordinator', '~> 2.2.0'
```

## 使用
一般的使用方式是建立`FlowCoordinator`子类来处理业务流程,子类需要指定流程成功返回的信息,通过父类的范型来指定(下面例子中的`<User>`)
一般的使用方式是建立`FlowCoordinator`子类来处理业务流程,子类需要指定流程成功返回的结果,通过父类的范型来指定(下面例子中的`Result`)

```swift
import DPFlowCoordinator

class LoginFlowCoordinator: FlowCoordinator<User, Error> {
class LoginFlowCoordinator: FlowCoordinator<Result> {

override func start(in viewController: UIViewController?, completion: CompletionHandler?) {
super.start(in: viewController, completion: completion)
Expand All @@ -21,18 +22,30 @@ class LoginFlowCoordinator: FlowCoordinator<User, Error> {
}

func doSomething() {
// ⚠️️️️️️⚠️⚠️ 在业务流程完成后,需要调用`cancel()`或`finished()`方法,来结束流程,否则`FlowCoordinator`生命周期不会完结,并且内存不会被释放
// ⚠️️️️️️⚠️⚠️ 在业务流程完成后,需要调用`complete()`方法,来结束流程,否则`FlowCoordinator`生命周期不会完结,并且内存不会被释放
//
complete(.skip)
}
}
```

// `finish()`方法可以返回必要的信息
// finish(user)
定义`Result`每个流程可以定义自己的`Result`

// `cancel()`方法可以返回必要的错误信息
// cancel(error)
```swift
extension LoginFlowCoordinator {

enum Result {
case skip
case failure(Error)
case success(User)
}
}
```



实现好了业务流程之后,在`ViewController`中调用业务流程

```swift
class MainViewController: UIViewController {

Expand All @@ -45,9 +58,9 @@ class MainViewController: UIViewController {

LoginFlowCoordinator().start(in: self) { (result) -> (Void) in
switch result {
case .cancel:
case .skip:
print("取消登录")
case let .finish(userInfo):
case let .success(userInfo):
print("登录成功:\(String(describing: userInfo))")
case let .failure(error):
print("登录失败\(String(describing: error))")
Expand Down

0 comments on commit 4039dad

Please sign in to comment.