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.
 
 
 
 

435 lines
17 KiB

//
// ProfileEditViewController.swift
// GMERemittance
//
// Created by Fm-user on 1/9/18.
// Copyright © 2018 Gobal Money Express Co. Ltd. All rights reserved.
//
import UIKit
class ProfileGeneralViewController: UIViewController {
var userinfoviewmodel: UserInfoViewModel?
var alertController: UIAlertController!
private var activityIndicator: UIActivityIndicatorView = UIActivityIndicatorView()
@IBOutlet weak var labelFirstName: UILabel!
@IBOutlet weak var labelMiddleName: UILabel!
@IBOutlet weak var labelLastName: UILabel!
@IBOutlet weak var labelNickName: UILabel!
@IBOutlet weak var labelGender: UILabel!
@IBOutlet weak var labelDOB: UILabel!
@IBOutlet weak var labelMobile: UILabel!
@IBOutlet weak var labelEmail: UILabel!
@IBOutlet weak var labelCountry: UILabel!
@IBOutlet weak var labelCountryNative: UILabel!
@IBOutlet weak var labelProvince: UILabel!
@IBOutlet weak var labelOccupation: UILabel!
private var indexTag: Int = 0
private var selectedOccupationIndex: Int = 0
private var selectedProvinceIndex: Int = 0
private var updatedString: String = ""
private var selectedOccupation: String!
private var selectedProvince: String!
let pickerView = UIPickerView()
var pickerData: [String] = [String]()
var editTextField: UITextField!
public static var profileGeneralConnectionTimeOutCheck = 0
@IBAction func goBack(_ sender: Any) {
navigationController?.popViewController(animated: true)
}
override func viewDidLoad() {
super.viewDidLoad()
setUpNavBar(id: 201, title: "Profile")
guard userinfoviewmodel != nil else {
self.popUpMessageInfo(value: 16, title: "No Data Available", message: "Data not received")
return
}
setUpAnotherLoginListener(genericviewmodel: userinfoviewmodel!)
userinfoviewmodel!.userDataAvailable.value = nil
userinfoviewmodel!.userOccupationProvinceAvailable.value = nil
userinfoviewmodel?.internetConnection.value = nil
userinfoviewmodel?.userConnectionTimeOut.value = nil
/**
connection timeout
*/
userinfoviewmodel?.userConnectionTimeOut.bind { [unowned self] in
guard $0 != nil else {
return
}
self.enableUserInteractions()
self.dismissActivityIndicator(activityIndicator: self.activityIndicator)
if ProfileGeneralViewController.profileGeneralConnectionTimeOutCheck == 0{
ProfileGeneralViewController.profileGeneralConnectionTimeOutCheck = ProfileGeneralViewController.profileGeneralConnectionTimeOutCheck+1
self.popUpMessage(value: 34)
}
}
/**
Internet check
*/
userinfoviewmodel?.internetConnection.bind { [unowned self] in
guard $0 != nil else {
return
}
self.enableUserInteractions()
self.dismissActivityIndicator(activityIndicator: self.activityIndicator)
self.popUpMessage(value: 15)
}
/**
Update the view for Occupation and Province
*/
userinfoviewmodel!.userOccupationProvinceAvailable.bind { [unowned self] in
guard $0 != nil else {
return
}
if $0 == 0 {
self.popUpMessageError(value: 10, message: (self.userinfoviewmodel?.getErrorMessage())!)
} else {
if $0 == 3 {
self.setUpTextField(category: 3)
} else if $0 == 5 {
self.setUpTextField(category: 5)
} else if $0 == -1 {
if self.alertController != nil {
self.alertController.dismiss(animated: true, completion: nil)
}
}
}
}
/**
Update the view for User data
*/
userinfoviewmodel!.userDataAvailable.bind { [unowned self] in
guard $0 != nil else {
return
}
self.dismissActivityIndicator(activityIndicator: self.activityIndicator)
self.enableUserInteractions()
guard $0! else {
//self.popUpMessageError(value: 10, message: (self.userinfoviewmodel?.getErrorMessage())!)
return
}
switch self.indexTag {
case 1:
self.labelNickName.attributedText = self.getAttributedLabel(inputLabel: self.updatedString)
UserDefaults.standard.set(self.updatedString, forKey: "com.gmeremit.nickName") //Updating UserDefaults so that when the user goes back to HomeScreen, the changes are reflected
case 3:
self.labelProvince.attributedText = self.getAttributedLabel(inputLabel: self.pickerData[self.selectedProvinceIndex])
case 5:
self.labelOccupation.attributedText = self.getAttributedLabel(inputLabel: self.pickerData[self.selectedOccupationIndex])
default:
return
}
}
if let firstName = userinfoviewmodel!.getInfo().firstName {
labelFirstName.text = firstName
}
if let lastName = userinfoviewmodel!.getInfo().lastName {
labelLastName.text = lastName
}
if let middleName = userinfoviewmodel!.getInfo().middleName {
labelMiddleName.text = middleName
} else {
labelMiddleName.text = "-"
}
if let gender = userinfoviewmodel!.getInfo().gender{
labelGender.text = gender
}
if let dob = userinfoviewmodel!.getInfo().dateOfBirth {
labelDOB.text = unixTimeStampToDate(unixTimeStamp:dob )
}
if let mobileNumber = userinfoviewmodel!.getInfo().mobileNumber {
labelMobile.text = mobileNumber
}
if let email = userinfoviewmodel!.getInfo().email{
labelEmail.text = email
}
if let nativeCountry = userinfoviewmodel!.getInfo().nativeCountry{
labelCountryNative.text = nativeCountry
}
if let country = userinfoviewmodel!.getInfo().country{
labelCountry.text = country
}
if let nickName = userinfoviewmodel!.getInfo().nickName {
labelNickName.attributedText = getAttributedLabel(inputLabel: nickName)
} else {
labelNickName.attributedText = getAttributedLabel(inputLabel: "")
}
if let occupation = userinfoviewmodel!.getInfo().occupation{
selectedOccupation = occupation
labelOccupation.attributedText = getAttributedLabel(inputLabel: selectedOccupation)
}
if let province = userinfoviewmodel!.getInfo().province{
selectedProvince = province
labelProvince.attributedText = getAttributedLabel(inputLabel: selectedProvince)
}
labelNickName.isUserInteractionEnabled = true
labelProvince.isUserInteractionEnabled = true
labelOccupation.isUserInteractionEnabled = true
labelNickName.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(editDetail(tapGestureRecognizer:))))
labelProvince.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(editDetail(tapGestureRecognizer:))))
labelOccupation.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(editDetail(tapGestureRecognizer:))))
}
/**
Picker view data list
- parameter Integer:
- parameter: 5 for Occupation list
- parameter: 3 for Province list
*/
func setUpTextField(category: Int) {
if category == 5 {
if let userinfoModelCount = self.userinfoviewmodel?.getOccupationList().count{
if userinfoModelCount > 0{
self.pickerData = (self.userinfoviewmodel?.getOccupationList())!
if let selectedOccupationIndex = self.pickerData.index(of: selectedOccupation) {
self.editTextField.text = self.pickerData[selectedOccupationIndex]
self.pickerView.dataSource = self
self.pickerView.delegate = self
pickerView.selectRow(selectedOccupationIndex, inComponent: 0, animated: true)
}
}
}
} else if category == 3 {
if let provinceCount = self.userinfoviewmodel?.getProvinceList().count {
if provinceCount > 0 {
self.pickerData = self.userinfoviewmodel!.getProvinceList()
if let selectedProvinceIndex = self.pickerData.index(of: selectedProvince) {
self.editTextField.text = self.pickerData[selectedProvinceIndex]
self.pickerView.dataSource = self
self.pickerView.delegate = self
pickerView.selectRow(selectedProvinceIndex, inComponent: 0, animated: true)
}
}
}
}
self.editTextField.becomeFirstResponder()
}
/**
To dispaly image and Text in single label
- parameter String : Occupation or Province
- returns: NSMutableAttributedString
*/
func getAttributedLabel(inputLabel: String) -> NSMutableAttributedString {
let fullString: NSMutableAttributedString = NSMutableAttributedString(string: "")
// create our NSTextAttachment
let imageAttachment = NSTextAttachment()
imageAttachment.image = UIImage(named: "ic_profile_change_edit")
// wrap the attachment in its own attributed string so we can append it
let imageString = NSAttributedString(attachment: imageAttachment)
// add the NSTextAttachment wrapper to our full string, then add some more text.
fullString.append(imageString)
fullString.append(NSAttributedString(string: " "))
fullString.append(NSAttributedString(string: inputLabel))
return fullString
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
/**
To edit user nick name, 0ccupation and province
*/
@objc func editDetail(tapGestureRecognizer: UITapGestureRecognizer) {
let tappedLabel:UILabel = (tapGestureRecognizer.view as! UILabel) // Type cast it with the class for which you have added gesture
var title: String = ""
switch tappedLabel.tag {
case 1:
indexTag = 1
title = "Nick Name"
case 3:
indexTag = 3
title = "Province"
if self.userinfoviewmodel!.getProvinceList().count > 0 {
self.pickerData = self.userinfoviewmodel!.getProvinceList()
} else {
pickerData = self.userinfoviewmodel!.getEmptyData()
self.userinfoviewmodel!.fetchCDDList(cddName: cddCode.Province.rawValue, param: ["countryId": "118"])
}
case 5:
indexTag = 5
title = "Occupation"
if Reachability.isConnectedToNetwork() == true{
if self.userinfoviewmodel!.getOccupationList().count > 0 {
self.pickerData = self.userinfoviewmodel!.getOccupationList()
} else {
pickerData = self.userinfoviewmodel!.getEmptyData()
self.userinfoviewmodel!.fetchCDDList(cddName: cddCode.Occupation.rawValue, param: nil)
}
}
default:
title = ""
}
alertController = UIAlertController(title: "Edit " + title, message: nil, preferredStyle: .alert)
alertController.addTextField { (textField : UITextField!) -> Void in
textField.placeholder = "New value"
if self.indexTag == 5 {
if Reachability.isConnectedToNetwork() == true{
textField.inputView = self.pickerView
self.editTextField = textField
self.editTextField.delegate = self
if self.userinfoviewmodel!.getOccupationList().count > 0 {
self.setUpTextField(category: self.indexTag)
}
}
} else if self.indexTag == 3 {
textField.inputView = self.pickerView
self.editTextField = textField
self.editTextField.delegate = self
if self.userinfoviewmodel!.getProvinceList().count > 0 {
self.setUpTextField(category: self.indexTag)
}
} else {
textField.tag = 51 //unique identifier set
textField.delegate = self
}
}
let confirmAction = UIAlertAction(title: "Confirm", style: .default, handler: {
alert -> Void in
let valueTextField = self.alertController.textFields![0] as UITextField
let trimmedString = valueTextField.text?.removeSpacesTrailingPreceding()
if trimmedString != "" {
self.updatedString = trimmedString!
self.showActivityIndicator(activityIndicator: self.activityIndicator)
self.disableUserInteractions()
switch tappedLabel.tag {
case 1:
self.userinfoviewmodel!.updateUserInfo(param: ["nickName": trimmedString!])
case 3:
self.userinfoviewmodel!.updateUserInfo(param: ["provinceId": self.userinfoviewmodel!.getProvinceID(index: self.selectedProvinceIndex)])
case 5:
self.userinfoviewmodel!.updateUserInfo(param: ["occupation": self.userinfoviewmodel!.getOccupationID(index: self.selectedOccupationIndex)])
default:
return
}
} else {
self.popUpMessageError(value: 10, message: "No data provided")
//self.present(self.prepareAlert(title: "No updates done", message: "No data supplied"), animated: true, completion: nil )
}
})
let cancelAction = UIAlertAction(title: "Cancel", style: .destructive, handler: {
(action : UIAlertAction!) -> Void in
})
cancelAction.setValue(UIColor.black, forKey: "titleTextColor")
confirmAction.setValue(UIColor(hex:0xEC1C24), forKey: "titleTextColor")
alertController.addAction(cancelAction)
alertController.addAction(confirmAction)
if Reachability.isConnectedToNetwork() == true{
self.present(alertController, animated: true, completion: nil)
}else{
popUpMessage(value: 15)
}
}
}
extension ProfileGeneralViewController: UIPickerViewDataSource, UIPickerViewDelegate {
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return pickerData.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return pickerData[row]
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
editTextField!.text = pickerData[row]
if indexTag == 5 {
selectedOccupation = pickerData[row]
selectedOccupationIndex = row
} else if indexTag == 3 {
selectedProvince = pickerData[row]
selectedProvinceIndex = row
}
}
}
extension ProfileGeneralViewController: UITextFieldDelegate {
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
if self.indexTag == 3 || self.indexTag == 5 {
return false
}
return true
}
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
if textField.tag == 51 {
textField.tag = 0
return false
}
return true
}
}