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.
147 lines
4.3 KiB
147 lines
4.3 KiB
//
|
|
// SignUpViewModel.swift
|
|
// GMERemittance
|
|
//
|
|
// Created by Sujal on 12/8/17.
|
|
// Copyright © 2017 Gobal Money Express Co. Ltd. All rights reserved.
|
|
//
|
|
|
|
import Foundation
|
|
import SwiftyJSON
|
|
|
|
class SignUpViewModel: ModelExtension {
|
|
|
|
private let minPasswordLength: Int = 1
|
|
|
|
var registered: Box<Bool?> = Box(nil)
|
|
|
|
var signUpConnectionTimeOut: Box<Bool?> = Box(nil)
|
|
|
|
var username: String!
|
|
var password: String!
|
|
var confirmpassword: String!
|
|
|
|
}
|
|
|
|
extension SignUpViewModel {
|
|
|
|
func setUserCredentials(username: String, password: String, confirmpassword: String) {
|
|
self.username = username
|
|
self.password = password
|
|
self.confirmpassword = confirmpassword
|
|
}
|
|
|
|
/**
|
|
Validate empty textfield
|
|
*/
|
|
func validateUser() -> isValid {
|
|
if (username.isBlank || password.isBlank || confirmpassword.isBlank) {
|
|
return .InValid("Please fill all the fields")
|
|
} else {
|
|
if isNumber(username: username) {
|
|
if isValidPhone(phone: username) {
|
|
if isValidPasswordLength(password: password) {
|
|
if passwordsMatch(password: password, confirmpassword: confirmpassword) {
|
|
return .Valid
|
|
}
|
|
return .InValid("Passwords do not match")
|
|
}
|
|
return .InValid("Password length doesn't meet criteria")
|
|
}
|
|
return .InValid("InValid Phone")
|
|
} else {
|
|
if isValidEmail(email: username) {
|
|
if isValidPasswordLength(password: password) {
|
|
if passwordsMatch(password: password, confirmpassword: confirmpassword) {
|
|
return .Valid
|
|
}
|
|
return .InValid("Passwords do not match")
|
|
}
|
|
return .InValid("Password length doesn't meet criteria")
|
|
}
|
|
return .InValid("InValid Email")
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
Validate email format
|
|
*/
|
|
func isValidEmail(email:String) -> Bool {
|
|
let emailRegEx = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,6}"
|
|
return NSPredicate(format:"SELF MATCHES %@", emailRegEx).evaluate(with: email)
|
|
}
|
|
|
|
/**
|
|
Validate phone number
|
|
*/
|
|
func isValidPhone(phone: String) -> Bool {
|
|
return true
|
|
}
|
|
|
|
/**
|
|
Validate empty textfield
|
|
*/
|
|
func isBlank(text: String) -> Bool {
|
|
return text.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty
|
|
}
|
|
|
|
/**
|
|
Validate password length
|
|
*/
|
|
func isValidPasswordLength(password: String) -> Bool {
|
|
guard password.count < minPasswordLength else {
|
|
return true
|
|
}
|
|
return false
|
|
}
|
|
|
|
/**
|
|
Validate password match
|
|
*/
|
|
func passwordsMatch(password: String, confirmpassword: String) -> Bool {
|
|
guard password == confirmpassword else {
|
|
return false
|
|
}
|
|
return true
|
|
}
|
|
|
|
/**
|
|
Validate phone number
|
|
*/
|
|
func isNumber(username: String) -> Bool {
|
|
guard let _ = Int(username) else {
|
|
return false
|
|
}
|
|
return true
|
|
}
|
|
}
|
|
|
|
extension SignUpViewModel {
|
|
/**
|
|
Api request for register the account
|
|
*/
|
|
func register() {
|
|
if !Reachability.isConnectedToNetwork() {
|
|
self.internetConnection.value = false
|
|
} else {
|
|
RestApiMananger.sharedInstance.registerUser(userId: self.username, password: self.password) { result in
|
|
switch result {
|
|
case let .success(json):
|
|
print(json, "code")
|
|
UserDefaults.standard.set(self.username, forKey: "com.gmeremit.username")
|
|
self.registered.value = true
|
|
case let .failure(errorJSON):
|
|
self.setErrorMessage(message: errorJSON["message"].stringValue)
|
|
self.registered.value = false
|
|
case .updateAccessCode:
|
|
return
|
|
case .logOutUser():
|
|
return
|
|
case .timeOut:
|
|
self.signUpConnectionTimeOut.value = false
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|