123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 |
- //
- // ChangePasswordViewController.swift
- // NexilisLite
- //
- // Created by Qindi on 23/03/22.
- //
- import UIKit
- import NotificationBannerSwift
- class ChangePasswordViewController: UIViewController {
- @IBOutlet weak var oldPassField: PasswordTextField!
- @IBOutlet weak var newPassField: PasswordTextField!
- @IBOutlet weak var showOldPassButton: UIButton!
- @IBOutlet weak var showNewPassButton: UIButton!
- @IBOutlet weak var labelDesc: UILabel!
-
- override func viewDidLoad() {
- super.viewDidLoad()
-
- self.title = "Change Password".localized()
- navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Save".localized(), style: .plain, target: self, action: #selector(didTapNext(sender:)))
-
- oldPassField.addPadding(.right(40))
- oldPassField.isSecureTextEntry = false
-
- newPassField.addPadding(.right(40))
- newPassField.isSecureTextEntry = false
-
- oldPassField.placeholder = "Old Password".localized()
- newPassField.placeholder = "New Password".localized()
-
- showOldPassButton.addTarget(self, action: #selector(showOldPassword), for: .touchUpInside)
- showNewPassButton.addTarget(self, action: #selector(showNewPassword), for: .touchUpInside)
-
- labelDesc.text = "Change password to keep your account secure".localized()
-
- let tapGesture = UITapGestureRecognizer(target: self, action: #selector(dismissKeyboard))
- tapGesture.cancelsTouchesInView = false
- view.addGestureRecognizer(tapGesture)
- }
-
- @objc func dismissKeyboard() {
- //Causes the view (or one of its embedded text fields) to resign the first responder status.
- view.endEditing(true)
- }
-
- @objc func didTapNext(sender: Any) {
- guard let odlPassword = oldPassField.text, !odlPassword.isEmpty else {
- let imageView = UIImageView(image: UIImage(systemName: "xmark.circle.fill"))
- imageView.tintColor = .white
- let banner = FloatingNotificationBanner(title: "Old password can't be empty".localized(), subtitle: nil, titleFont: UIFont.systemFont(ofSize: 16), titleColor: nil, titleTextAlign: .left, subtitleFont: nil, subtitleColor: nil, subtitleTextAlign: nil, leftView: imageView, rightView: nil, style: .danger, colors: nil, iconPosition: .center)
- banner.show()
- return
- }
- if odlPassword.count < 6 {
- let imageView = UIImageView(image: UIImage(systemName: "xmark.circle.fill"))
- imageView.tintColor = .white
- let banner = FloatingNotificationBanner(title: "Old password min 6 character".localized(), subtitle: nil, titleFont: UIFont.systemFont(ofSize: 16), titleColor: nil, titleTextAlign: .left, subtitleFont: nil, subtitleColor: nil, subtitleTextAlign: nil, leftView: imageView, rightView: nil, style: .danger, colors: nil, iconPosition: .center)
- banner.show()
- return
- }
- // if odlPassword != UserDefaults.standard.string(forKey: "pwd"){
- // let imageView = UIImageView(image: UIImage(systemName: "xmark.circle.fill"))
- // imageView.tintColor = .white
- // let banner = FloatingNotificationBanner(title: "Incorrect old password".localized(), subtitle: nil, titleFont: UIFont.systemFont(ofSize: 16), titleColor: nil, titleTextAlign: .left, subtitleFont: nil, subtitleColor: nil, subtitleTextAlign: nil, leftView: imageView, rightView: nil, style: .danger, colors: nil, iconPosition: .center)
- // banner.show()
- // return
- // }
- guard let newPassword = newPassField.text, !newPassword.isEmpty else {
- let imageView = UIImageView(image: UIImage(systemName: "xmark.circle.fill"))
- imageView.tintColor = .white
- let banner = FloatingNotificationBanner(title: "New password can't be empty".localized(), subtitle: nil, titleFont: UIFont.systemFont(ofSize: 16), titleColor: nil, titleTextAlign: .left, subtitleFont: nil, subtitleColor: nil, subtitleTextAlign: nil, leftView: imageView, rightView: nil, style: .danger, colors: nil, iconPosition: .center)
- banner.show()
- return
- }
- if newPassword.count < 6 {
- let imageView = UIImageView(image: UIImage(systemName: "xmark.circle.fill"))
- imageView.tintColor = .white
- let banner = FloatingNotificationBanner(title: "New password min 6 character".localized(), subtitle: nil, titleFont: UIFont.systemFont(ofSize: 16), titleColor: nil, titleTextAlign: .left, subtitleFont: nil, subtitleColor: nil, subtitleTextAlign: nil, leftView: imageView, rightView: nil, style: .danger, colors: nil, iconPosition: .center)
- banner.show()
- return
- }
- let idMe = UserDefaults.standard.string(forKey: "me")!
- DispatchQueue.global().async {
- let tMessage = CoreMessage_TMessageBank.getChangePersonInfo_New(p_f_pin: idMe)
- let md5HexOld = Utils.getMD5(string: odlPassword).map { String(format: "%02hhx", $0) }.joined()
- let md5HexNew = Utils.getMD5(string: newPassword).map { String(format: "%02hhx", $0) }.joined()
- tMessage.mBodies[CoreMessage_TMessageKey.PASSWORD] = md5HexNew
- tMessage.mBodies[CoreMessage_TMessageKey.PASSWORD_OLD] = md5HexOld
- if let resp = Nexilis.writeAndWait(message: tMessage){
- if resp.isOk() {
- DispatchQueue.main.async {
- let imageView = UIImageView(image: UIImage(systemName: "checkmark.circle.fill"))
- imageView.tintColor = .white
- let banner = FloatingNotificationBanner(title: "Successfully changed password".localized(), subtitle: nil, titleFont: UIFont.systemFont(ofSize: 16), titleColor: nil, titleTextAlign: .left, subtitleFont: nil, subtitleColor: nil, subtitleTextAlign: nil, leftView: imageView, rightView: nil, style: .success, colors: nil, iconPosition: .center)
- banner.show()
- self.navigationController?.popViewController(animated: true)
- }
- } else {
- DispatchQueue.main.async {
- let imageView = UIImageView(image: UIImage(systemName: "xmark.circle.fill"))
- imageView.tintColor = .white
- let banner = FloatingNotificationBanner(title: resp.getBody(key: CoreMessage_TMessageKey.MESSAGE_TEXT), subtitle: nil, titleFont: UIFont.systemFont(ofSize: 16), titleColor: nil, titleTextAlign: .left, subtitleFont: nil, subtitleColor: nil, subtitleTextAlign: nil, leftView: imageView, rightView: nil, style: .danger, colors: nil, iconPosition: .center)
- banner.show()
- }
- }
- } else {
- DispatchQueue.main.async {
- let imageView = UIImageView(image: UIImage(systemName: "xmark.circle.fill"))
- imageView.tintColor = .white
- let banner = FloatingNotificationBanner(title: "Unable to access servers. Try again later".localized(), subtitle: nil, titleFont: UIFont.systemFont(ofSize: 16), titleColor: nil, titleTextAlign: .left, subtitleFont: nil, subtitleColor: nil, subtitleTextAlign: nil, leftView: imageView, rightView: nil, style: .danger, colors: nil, iconPosition: .center)
- banner.show()
- }
- }
- }
- }
-
- @objc func showOldPassword() {
- if oldPassField.isSecureTextEntry {
- oldPassField.isSecureTextEntry = false
- showOldPassButton.setImage(UIImage(systemName: "eye.fill"), for: .normal)
- } else {
- oldPassField.isSecureTextEntry = true
- showOldPassButton.setImage(UIImage(systemName: "eye.slash.fill"), for: .normal)
- }
- }
-
- @objc func showNewPassword() {
- if newPassField.isSecureTextEntry {
- newPassField.isSecureTextEntry = false
- showNewPassButton.setImage(UIImage(systemName: "eye.fill"), for: .normal)
- } else {
- newPassField.isSecureTextEntry = true
- showNewPassButton.setImage(UIImage(systemName: "eye.slash.fill"), for: .normal)
- }
- }
- }
|