CloverKit 1.1.0

CloverKit 1.1.0

Maintained by Arror.



CloverKit 1.1.0

  • By
  • Arror

CloverKit

说明

该项目为 Target-Action 组件化方案的现代化实现。

工具

thriftswift-gen

使用

服务定义

定义 thrift 文件:InvoiceService.thrift

struct Invoice {
    0: required i32 id
    1: required string name
    2: required string email
}

service InvoiceService {
    Invoice loadInvoice(1: required string userID)
}

生成代码

终端执行如下命令

./thrift -c CK 	-i ./Services/InvoiceService.thrift --client_out ../CKServices
./thrift -s CKS -i ./Services/InvoiceService.thrift --server_out ../CKInvoiceModule
//
// Code generated by thrift & swift-gen.
// Don't edit manually.
//

import Foundation
import CloverKit

public struct CKInvoice: Codable {
    public let id: Int
    public let name: String
    public let email: String
    public init(id: Int, name: String, email: String) {
        self.id = id
        self.name = name
        self.email = email
    }
}

public enum InvoiceService {

    case loadInvoice(userID: String, completion: (Swift.Result<CKInvoice, Error>) -> Void)

    public enum Methods: String, CaseIterable {
        case loadInvoice = "InvoiceService.loadInvoice"
    }

    public func invoke(by session: CloverKit.Session = .shared) -> Bool {
        switch self {
        case .loadInvoice(let userID, let completion):
            struct Parameter: Codable {
                let userID: String
            }
            return session.invoke(
                method: Methods.loadInvoice.rawValue,
                parameter: Parameter(userID: userID),
                completion: completion
            )
        }
    }
}
//
// Code generated by thrift & swift-gen.
// Don't edit manually.
//

import Foundation
import CloverKit

struct CKSInvoice: Codable {
    let id: Int
    let name: String
    let email: String
}

protocol __CKInvoiceServiceProtocol: class {

    func loadInvoice(userID: String, completion: @escaping (Swift.Result<CKSInvoice, Swift.Error>) -> Void) -> Bool
}

@objc(CKInvoiceService)
class CKInvoiceService: NSObject, __CKInvoiceServiceProtocol {

    @objc private func __loadInvoice(handler: CloverKit.Handler) -> Bool {
        struct Parameter: Codable {
            let userID: String
        }
        do {
            let p = try JSONDecoder().decode(Parameter.self, from: handler.parameter)
            return self.loadInvoice(userID: p.userID) { result in
                handler.completion(result.flatMap {
                    do {
                        return .success(try JSONEncoder().encode($0))
                    } catch {
                        return .failure(error)
                    }
                })
            }
        } catch {
            handler.completion(.failure(error))
            return false
        }
    }
}

服务实现

extension CKInvoiceService {
    
    func loadInvoice(userID: String, completion: @escaping (Swift.Result<CKSInvoice, Error>) -> Void) -> Bool {
        let invoiceVC = CKInvoiceViewController.makeViewController(ensureAction: { vc, invoice in
            vc.dismiss(animated: true, completion: {
                completion(.success(invoice))
            })
        }, cancelAction: { vc in
            vc.dismiss(animated: true, completion: {
                completion(.failure(NSError(domain: "", code: 0, userInfo: nil)))
            })
        })
        UIViewController.current.present(UINavigationController(rootViewController: invoiceVC), animated: true, completion: nil)
        return true
    }
}

服务调用

_ = InvoiceService.loadInvoice(userID: "1234567890") { result in
    switch result {
    case .success(let invoice):
        let alert = UIAlertController(title: "Success", message: "\(invoice.name)\n\(invoice.email)", preferredStyle: .alert)
        alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
        UIViewController.current.present(alert, animated: true, completion: nil)
    case .failure(let error):
        let alert = UIAlertController(title: "Failure", message: error.localizedDescription, preferredStyle: .alert)
        alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
        UIViewController.current.present(alert, animated: true, completion: nil)
    }
}.invoke()