iOS組件化

CloverKit

說(shuō)明

該項(xiàng)目為 Target-Action 組件化方案的現(xiàn)代化實(shí)現(xiàn)。

工具

thrift、swift-gen

使用

服務(wù)定義

定義 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)
}

生成代碼

終端執(zhí)行如下命令

./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
        }
    }
}

服務(wù)實(shí)現(xiàn)

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
    }
}

服務(wù)調(diào)用

_ = 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()
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容