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.
 
 
 
 

274 lines
11 KiB

//
// SettingViewController.swift
// GMERemittance
//
// Created by Sujal on 2/18/18.
// Copyright © 2018 Gobal Money Express Co. Ltd. All rights reserved.
//
import UIKit
import Localize_Swift
class SettingViewController: UIViewController {
struct StringConstants {
var changePasswordText = "change_password_text".localized()
var changePINText = "changepin_text".localized()
var settingText = "setting_text".localized()
var languageText = "language_text".localized()
}
var languages: [SendMoneyCountryViewModel] = []
var selectedLanguage: SendMoneyCountryViewModel? {
didSet {
let code = selectedLanguage?.code ?? "en"
if code == "lk" {
Localize.setCurrentLanguage("si")
} else if code == "kh" {
Localize.setCurrentLanguage("km")
} else if code == "kr" {
Localize.setCurrentLanguage("ko")
} else if code == "mm" {
Localize.setCurrentLanguage("my")
} else if code == "bd" {
Localize.setCurrentLanguage("bn")
} else if code == "np" {
Localize.setCurrentLanguage("ne")
} else if code == "ru" {
Localize.setCurrentLanguage("ru-RU")
} else if code == "vn" {
Localize.setCurrentLanguage("vi-VN")
} else if code == "pk" {
Localize.setCurrentLanguage("ur-PK")
}else if code == "jp" {
Localize.setCurrentLanguage("ja")
} else if code == "ja" {
Localize.setCurrentLanguage("ja")
} else {
Localize.setCurrentLanguage(code)
}
print("code: \(code)")
GMEDB.shared.app.set(code, .firstTimeLanguageIsSet)
}
}
var status: Bool!
@IBOutlet weak var settingLabel: UILabel!
@IBOutlet weak var changePasswordButton: UIButton!
@IBOutlet weak var useBiometricAuthLabel: UILabel!
@IBOutlet weak var settingButton: UIButton!
@IBOutlet weak var useBiometricAuthSwitch: UISwitch!
@IBOutlet weak var settingTopBackgroundView: UIView!
@IBOutlet weak var changePinButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(
self,
selector: #selector(setText),
name: NSNotification.Name(LCLLanguageChangeNotification),
object: nil
)
setupLanguages()
configureLanguage()
let userBiometricAuth = (KeyChain.shared.get(key: .biometricAuth) ?? "0" ) == "1" ? true : false
self.useBiometricAuthSwitch.setOn(userBiometricAuth, animated: true)
self.useBiometricAuthSwitch.onTintColor = .themeBlue
self.useBiometricAuthSwitch.addTarget(
self,
action: #selector(self.changeBiometricAuthSwitch(_:)),
for: .valueChanged
)
// settingTopBackgroundView.backgroundColor = .themeRed
}
@objc func changeBiometricAuthSwitch(_ sender: UISwitch) {
// let isOn = sender.isOn ? "1" : "0"
status = sender.isOn ? true : false
let password = KeyChain.shared.get(key: .password) ?? ""
// if status {
guard let viewController = UIStoryboard(name: "PasswordConfirmation", bundle: nil)
.instantiateViewController(withIdentifier: "PasswordConfirmViewController") as? PasswordConfirmViewController else {
return
}
viewController.delegate = self
self.present(viewController, animated: true, completion: nil)
/* } else {
APIRouter.changeBioSwitch(isBio: status, password: password).request(
needsAuthorization: true,
success: { (response: ResponseMessage) in
if (response.errorCode ?? "") == "1" {
let error = NSError(domain: "Network", code: 0, message: response.message ?? "")
print(error.localizedDescription)
self.alertWithOk(type: .error, message: error.localizedDescription, title: "Error", okTitle: "ok_text".localized()) {
self.dismiss(animated: true, completion: nil)
}
} else {
let defaults = UserDefaults.standard
defaults.register(defaults: ["bioAcessCode" : ""])
defaults.set(response.id, forKey: "bioAcessCode")
if sender.isOn {
BiometricAuthenticationWireframe().showBiometricAuthentication { error in
if error != nil {
DispatchQueue.main.async {
self.alert(type: .error, message: error!.message)
self.useBiometricAuthSwitch.setOn(false, animated: true)
}
} else {
KeyChain.shared.save(data: isOn, key: .biometricAuth)
}
}
} else {
KeyChain.shared.save(data: isOn, key: .biometricAuth)
}
}
},
failure: { error in
self.alertWithOk(type: .error, message: error.localizedDescription, title: "Error", okTitle: "ok_text".localized()) {
self.dismiss(animated: true, completion: nil)
}
}
)
}*/
}
@objc func setText() {
self.changePasswordButton.setTitle("change_password_text".localized(), for: .normal)
self.changePinButton.setTitle("changepin_text".localized(), for: .normal)
self.settingButton.setTitle("language_text".localized(), for: .normal)
self.settingLabel.text = "setting_text".localized()
useBiometricAuthLabel.text = "biometric_setup".localized()
}
private func configureLanguage() {
self.changePasswordButton.setTitle("change_password_text".localized(), for: .normal)
self.settingButton.setTitle("language_text".localized(), for: .normal)
self.changePinButton.setTitle("changepin_text".localized(), for: .normal)
self.useBiometricAuthLabel.text = "biometric_setup".localized()
}
func setupLanguages() {
self.languages = Utility.getLanguages()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
setupNormalNavigation(color: .themeRedDark)
// self.navigationItem.title = StringConstants().settingText
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
self.navigationItem.title = ""
}
@IBAction func redirectToChangePassword(_ sender: Any) {
let storyboard = UIStoryboard.init(name: "Profile", bundle: Bundle.main)
if let changePasswordViewController = storyboard.instantiateViewController(
withIdentifier: "changePassword"
) as? ProfileChangePasswordViewController {
self.navigationController!.pushViewController(changePasswordViewController, animated: true)
}
}
@IBAction func redirectToChangePin(_ sender: UIButton) {
let storyboard = UIStoryboard.init(name: "Profile", bundle: Bundle.main)
if let changePinViewController = storyboard.instantiateViewController(
withIdentifier: "changePin"
) as? ProfileChangePinViewController {
self.navigationController!.pushViewController(changePinViewController, animated: true)
}
}
@IBAction func settings(_ sender: Any) {
self.showCountryPickerview()
}
@objc private func showCountryPickerview() {
setupLanguages()
TablePresenterWireframe().openWith(
delegate: self,
model: languages,
source: self
)
}
func countrySelected(models: [SendMoneyCountryViewModel]) {
self.selectedLanguage = models.first
}
}
extension SettingViewController: ConfirmPasswordDelegate {
func confirm(password: String) {
APIRouter.changeBioSwitch(isBio: self.status, password: password).request(
needsAuthorization: true,
success: { (response: ResponseMessage) in
if (response.errorCode ?? "") == "1" {
let error = NSError(domain: "Network", code: 0, message: response.message ?? "")
print(error.localizedDescription)
self.alertWithOk(type: .error, message: error.localizedDescription, title: "Error", okTitle: "ok_text".localized()) {
self.useBiometricAuthSwitch.setOn(!self.status, animated: true)
self.dismiss(animated: true, completion: nil)
}
} else {
let defaults = UserDefaults.standard
defaults.register(defaults: ["bioAcessCode" : ""])
defaults.set(response.id, forKey: "bioAcessCode")
if self.status {
BiometricAuthenticationWireframe().showBiometricAuthentication { error in
if error != nil {
DispatchQueue.main.async {
self.alert(type: .error, message: error!.message)
self.useBiometricAuthSwitch.setOn(!self.status, animated: true)
}
} else {
KeyChain.shared.save(data: self.status ? "1" : "0", key: .biometricAuth)
}
}
} else {
KeyChain.shared.save(data: self.status ? "1" : "0", key: .biometricAuth)
}
}
},
failure: { error in
self.alertWithOk(type: .error, message: error.localizedDescription, title: "Error", okTitle: "ok_text".localized()) {
self.useBiometricAuthSwitch.setOn(!self.status, animated: true)
self.dismiss(animated: true, completion: nil)
}
}
)
}
func cancel() {
self.useBiometricAuthSwitch.setOn(!self.status, animated: true)
}
}
extension SettingViewController: TablePresenterDelegate {
func tablePresenterView(_ viewController: TablePresenterViewController) -> TablePresenterConfiguration {
return TablePresenterConfiguration(
presenterTitle: "select_country_text".localized(),
closeButtonTitle: "cancel_text".localized(),
notFoundTitle: "no_result_found_text".localized(),
searchBarPlaceHolder: "search_country_text".localized()
)
}
func tablePresenterView(
_ viewController: TablePresenterViewController,
didSelectModel model: TablePresenterProtocol?
) {
self.selectedLanguage = (model as? SendMoneyCountryViewModel) ?? self.selectedLanguage
}
}