You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
133 lines
5.7 KiB
133 lines
5.7 KiB
//
|
|
// LoginViewModel.swift
|
|
// GMERemittance
|
|
//
|
|
// Created by Sujal on 12/12/17.
|
|
// Copyright © 2017 Gobal Money Express Co. Ltd. All rights reserved.
|
|
//
|
|
|
|
import Foundation
|
|
|
|
class LoginViewModel: SignUpViewModel {
|
|
|
|
var loggedin: Box<Bool?> = Box(nil)
|
|
var loggedInConnectionTimeOut: Box<Bool?> = Box(nil)
|
|
var userId: String?
|
|
var pass: String?
|
|
var dpUrl: String?
|
|
var userName: String?
|
|
var fillSignUpCode: Bool = false
|
|
}
|
|
|
|
extension LoginViewModel {
|
|
/**
|
|
To add user id and password
|
|
- parameter userId: account userid
|
|
- parameter password: account password
|
|
*/
|
|
func setCredentials(userId: String, password: String) {
|
|
self.userId = userId
|
|
self.pass = password.toBase64()
|
|
}
|
|
|
|
/**
|
|
Validate userid and password
|
|
- parameter userId: account userid
|
|
- parameter password: account password
|
|
*/
|
|
|
|
func checkParams(username: String, password: String) -> isValid {
|
|
if (username.isBlank || password.isBlank) {
|
|
return .InValid("Please fill all the fields")
|
|
} else {
|
|
if isNumber(username: username) {
|
|
if isValidPhone(phone: username) {
|
|
if isValidPasswordLength(password: password) {
|
|
return .Valid
|
|
} else {
|
|
return .InValid("InValid Password Length")
|
|
}
|
|
}
|
|
return .InValid("InValid Phone")
|
|
} else {
|
|
if isValidEmail(email: username) {
|
|
if isValidPasswordLength(password: password) {
|
|
return .Valid
|
|
} else {
|
|
return .InValid("InValid Password Length")
|
|
}
|
|
}
|
|
return .InValid("InValid Email")
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
Api requesr for Login
|
|
*/
|
|
func logIn() {
|
|
if !Reachability.isConnectedToNetwork() {
|
|
self.internetConnection.value = false
|
|
} else {
|
|
let passwordBase64Data = Data(base64Encoded: pass!)!
|
|
let password = String(data: passwordBase64Data, encoding: .utf8)!
|
|
RestApiMananger.sharedInstance.signInUser(userId: self.userId!, password: password) { result in
|
|
switch result {
|
|
case let .success(loggedJSON):
|
|
let accessCodeBase64 = (loggedJSON["accessCode"].stringValue + ":" + RestApiMananger.sharedInstance.getUUID()).toBase64()
|
|
|
|
self.dpUrl = loggedJSON["dpUrl"].stringValue
|
|
|
|
UserDefaults.standard.set(loggedJSON["dpUrl"].stringValue, forKey: "com.gmeremit.dpUrl")
|
|
UserDefaults.standard.set(loggedJSON["walletNumber"].stringValue, forKey: "com.gmeremit.walletNumber")
|
|
UserDefaults.standard.set(loggedJSON["primaryBankName"].stringValue, forKey: "com.gmeremit.bankName")
|
|
|
|
let fullName = loggedJSON["firstName"].stringValue + " " + loggedJSON["middleName"].stringValue + " " + loggedJSON["lastName"].stringValue
|
|
|
|
UserDefaults.standard.set(fullName, forKey: "com.gmeremit.fullName")
|
|
UserDefaults.standard.set(accessCodeBase64, forKey: "com.gmeremit.accessCode")
|
|
UserDefaults.standard.set(loggedJSON["userId"].stringValue, forKey: "com.gmeremit.username")
|
|
UserDefaults.standard.set(loggedJSON["nickName"].stringValue, forKey: "com.gmeremit.nickName")
|
|
UserDefaults.standard.set(loggedJSON["availableBalance"].stringValue, forKey: "com.gmeremit.balance")
|
|
UserDefaults.standard.set(loggedJSON["rewardPoint"].stringValue, forKey: "com.gmeremit.rewardPoint")
|
|
UserDefaults.standard.set(loggedJSON["verified"].boolValue, forKey: "com.gmeremit.isVerified")
|
|
UserDefaults.standard.set(loggedJSON["kyc"].boolValue, forKey: "com.gmeremit.isKYCSubmitted")
|
|
UserDefaults.standard.set(true, forKey: "com.gmeremit.loginStatus")
|
|
UserDefaults.standard.set(loggedJSON["email"].stringValue, forKey: "com.gmeremit.email")
|
|
UserDefaults.standard.set(loggedJSON["mobileNumber"].stringValue, forKey: "com.gmeremit.mobileNumber")
|
|
UserDefaults.standard.set(loggedJSON["sourceId"].stringValue,forKey: "com.gmeremit.sourceId")
|
|
UserDefaults.standard.set(self.pass, forKey: "com.gmeremit.password")
|
|
UserDefaults.standard.set(loggedJSON["isReferred"].boolValue, forKey: "com.gmeremit.isReferred")
|
|
self.loggedin.value = true
|
|
|
|
case let .failure(errorJSON):
|
|
UserDefaults.standard.set(false, forKey: "com.gmeremit.loginStatus")
|
|
let errorMessage = errorJSON["message"].stringValue
|
|
self.setErrorMessage(message: errorMessage)
|
|
|
|
if errorMessage.contains("is already registered, please click") {
|
|
UserDefaults.standard.set(self.userId, forKey: "com.gmeremit.username")
|
|
self.fillSignUpCode = true
|
|
}
|
|
self.loggedin.value = false
|
|
|
|
case .updateAccessCode:
|
|
return
|
|
case .logOutUser():
|
|
return
|
|
case .timeOut:
|
|
self.loggedInConnectionTimeOut.value = false
|
|
return
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
To redirect Code fill up view
|
|
- returns: code status
|
|
*/
|
|
func redirectToCodeFillUp() -> Bool {
|
|
return fillSignUpCode
|
|
}
|
|
}
|