Home > Enterprise >  How to map response from Single tuple
How to map response from Single tuple

Time:10-03

I have a response create with single that return tuples, i want to get if the value $0.0 return false it should get request from API. And if the value is $0.1 return true, it should go to next viewController. I already try this, but i'm stuck because xcode give a yellow warning that the variable in requestEmail is unused. it means i used driver wrong.

this is my code

class LoginVM: ViewModelType {

    struct Input {
        let emailText: Driver<String>
        let validate: Driver<Void>
    }

    struct Output {
        let loading: Driver<Bool>
        let gotoLoginScreen: Driver<Void>
        let errorNoInternetConnection: Driver<Void>
    }

    private let manageSession: ManageSessionProtocol
    private let manageAccountDetail: ManageAccountDetailProtocol

    init(manageSession: ManageSessionProtocol, manageAccountDetail: ManageAccountDetailProtocol) {
        self.manageSession = manageSession
        self.manageAccountDetail = manageAccountDetail
    }

    func transform(input: Input) -> Output {
        let errorRouter = ErrorRouter()
        let checkEmailRequest = input.validate
            .withLatestFrom(input.emailText)
            .filter { $0.isValidEmail }
            .map { emailText in
                self.manageSession.checkEmail(email: emailText)
                    .rerouteError(errorRouter)
                    .asDriver(onErrorJustReturn: (false, false))
            }

        let requestEmail = checkEmailRequest
            .flatMapLatest { $0.asSharedSequence() }
            .map { args in
                if args.0 == false {
                    self.manageSession.requestEmailLoginChallenge()
                        .rerouteError(errorRouter)
                        .asDriver(onErrorJustReturn: .init(workflowId: 0, lastAttempt: false, lastState: 0, nextRequestInSec: 0))
                }
            }

        let openPassword = checkEmailRequest
            .flatMapLatest { $0.asSharedSequence() }
            .map { args in
                if args.1 == true {

                }
            }



        return Output(loading: <#Driver<Void>#>, gotoLoginScreen: <#T##Driver<Void>#>, errorNoInternetConnection: <#Driver<Void>#>)
    }
}

CodePudding user response:

You want to do something like this:

let checkEmailRequest = input.validate
    .withLatestFrom(input.emailText)
    .filter { $0.isValidEmail }
    .flatMapLatest { [manageSession] emailText in
        manageSession.checkEmail(email: emailText)
            .rerouteError(errorRouter)
            .asDriver(onErrorJustReturn: (false, false))
    }

let requestEmail = checkEmailRequest
    .filter { $0.0 == false }
    .flatMapLatest { [manageSession] _ in
        manageSession.requestEmailLoginChallenge()
            .rerouteError(errorRouter)
            .asDriver(onErrorJustReturn: .init(workflowId: 0, lastAttempt: false, lastState: 0, nextRequestInSec: 0))
    }

let openPassword = checkEmailRequest
    .filter { $0.1 == true }
  • Related