FormCreateViewController.swift 6.1 KB
Newer Older
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
1
2
3
4
5
6
7
8
//
//  FormCreateViewController.swift
//  App16
//
//  Created by Grigor Aghabalyan on 3/25/20.
//  Copyright © 2020 X-TECH. All rights reserved.
//

Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
9
10
11
12
13
14
15
enum FormViewType {
    
    case viewFromList
    case viewFromQr
    case creta
}

Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
16
import UIKit
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
17
import IQKeyboardManagerSwift
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
18

Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
19

Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
20
class FormCreateViewController: UIViewController {
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
21
22
    
    // MARK: - IBOutlets
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
23
    @IBOutlet weak var curentDate: UILabel!
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
24
25
26
27
    @IBOutlet weak var outDateTimeTextFiled: UITextField!
    @IBOutlet weak var outAddressTextFiled: UITextField!
    @IBOutlet weak var destinationAddressTextField: UITextField!
    @IBOutlet weak var planneDateTimeTextField: UITextField!
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
28
    @IBOutlet weak var destinationTypeTextField: UITextField!
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
29
    
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
30
    @IBOutlet weak var createButton: UIButton!
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
31
    @IBOutlet weak var activityIndicator: UIActivityIndicatorView!
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
32
33
34
    
    @IBOutlet weak var outDateButtonAction: UIButton!
    @IBOutlet weak var planneDateTimeButton: UIButton!
35
    @IBOutlet weak var nameLabel: UILabel!
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
36
    
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
37
38
39
40
41
42
    @IBOutlet weak var outDateTimeTextLabel: UILabel!
    @IBOutlet weak var outAddressTextLabel: UILabel!
    @IBOutlet weak var destinationAddressTextLabel: UILabel!
    @IBOutlet weak var planneDateTimeTextLabel: UILabel!
    @IBOutlet weak var destinationTypeTextLabel: UILabel!
    
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
43
44
45
    // MARK: - Variables
    var data: FormResponse!
    var formViewType: FormViewType = .creta
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
46
    let date = Date().toString(dateFormat: DateFormat.StandartDate.rawValue)
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
47
    
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
48
49
50
    // MARK: - LifeCycle
    override func viewDidLoad() {
        super.viewDidLoad()
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
51
        
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
52
        setUI()
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
53
54
55
    }
    
    private func setUI() {
56
57
58
59
60
61
        
        if let firstName = UserDefaultsHelper.getString(for: .firstName),
            let lastName = UserDefaultsHelper.getString(for: .lstName),
            let middleName = UserDefaultsHelper.getString(for: .middleName) {
            nameLabel.text = "\(firstName) \(lastName) \(middleName)"
        }
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
62
        
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
63
        self.title = "CREATE_FORM".localized()
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
64
65
66
67
68
69
70
71
        createButton.setTitle("CREATE".localized(), for: .normal)
        outDateTimeTextLabel.text = "OUT_ADDRESS".localized()
        outAddressTextLabel.text = "OUT_DATETIME".localized()
        destinationAddressTextLabel.text = "Address of the place of visit/Title".localized()
        planneDateTimeTextLabel.text = "Estimated Return Time".localized()
        destinationTypeTextLabel.text = "Purpose of the visit".localized()
    }
    
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
72
    // MARK: - Actions
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
    @IBAction func outDateButtonAction(_ sender: UIButton) {
        
        let vc = DatePickerViewController()
        vc.modalPresentationStyle = .overFullScreen
        vc.modalTransitionStyle = .crossDissolve
        
        vc.getData { [weak self] (date) in
            self?.outDateTimeTextFiled.text = date
        }
        self.present(vc, animated: false, completion: nil)
    }
    
    @IBAction func planneDateTimeButtonAction(_ sender: UIButton) {
        
        let vc = DatePickerViewController()
        vc.modalPresentationStyle = .overFullScreen
        vc.modalTransitionStyle = .crossDissolve
        
        vc.getData { [weak self] (date) in
            self?.planneDateTimeTextField.text = date
        }
        self.present(vc, animated: false, completion: nil)
    }
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
96
    
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
97
98
99
100
101
102
    @IBAction func createButtonAction(_ sender: UIButton) {
        
        if outDateTimeTextFiled.text == ""
            || outAddressTextFiled.text == ""
            || destinationAddressTextField.text == "" || planneDateTimeTextField.text == "" {
            
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
103
            let alertController = AlertControllerHelper.showAlert(title: nil, message: "Please fill in all fields".localized())
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
104
105
            self.present(alertController, animated: true, completion: nil)
        }else {
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
106
            createForm()
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
107
108
        }
    }
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
109
    
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
110
111
112
113
114
    private func openQrView() {
        
        let controller = QrViewController()
        changeRoot(UINavigationController(rootViewController: controller))
    }
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
115
116
117
118
119
120
121
122
123
    
    private func createForm() {
        
        activityIndicator.isHidden = false
        activityIndicator.startAnimating()
        
        let firstName = UserDefaultsHelper.getString(for: .firstName)
        let lastName = UserDefaultsHelper.getString(for: .lstName)
        let middleName = UserDefaultsHelper.getString(for: .middleName)
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
124
125
        let deviceId = UIDevice.current.identifierForVendor?.uuidString
        let date = Date().toString(dateFormat: DateFormat.StandartDate.rawValue)
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
126
        
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
127
        let form = FormCreateRequestForm(deviceToken: deviceId,
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
128
129
130
131
132
133
                                         firstName: firstName,
                                         lastName: lastName,
                                         middleName: middleName,
                                         outAddress: outAddressTextFiled.text,
                                         outLatitude: nil,
                                         outLongitude: nil,
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
134
                                         outDatetime: outDateTimeTextFiled.text != nil ? date + " " + outDateTimeTextFiled.text! : nil,
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
135
136
137
138
                                         visitingAddressAndName: destinationAddressTextField.text,
                                         visitingLatitude: nil,
                                         visitingLongitude: nil,
                                         visitingReason: destinationTypeTextField.text,
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
139
                                         plannedReturnDatetime: planneDateTimeTextField.text != nil ? date + " " + planneDateTimeTextField.text! : nil)
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
        
        FormCreateService.shered.createForm(data: form) { (weaterResponseData) in
            switch weaterResponseData {
            case .base(response: let baseResposne):
                self.activityIndicator.stopAnimating()
                CheckBaseHelper.checkBaseResponse(baseResposne, viewController: self)
            case .success(let resultsData):
                self.activityIndicator.stopAnimating()
                print(resultsData)
                self.openQrView()
            case .isOffline:
                self.activityIndicator.stopAnimating()
                return
            case .conflict:
                self.activityIndicator.stopAnimating()
                return
            }
        }
    }
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
159
}