FormCreateViewController.swift 8.69 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
53
        setUI()
        
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
54
55
56
        switch formViewType {
        case .creta:
            
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
57
            createButton.isHidden = false
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
58
            self.title = "CREATE_FORM".localized()
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
59
            curentDate.text = date
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
60
61
        case .viewFromList:
            
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
62
            self.title = "FORM".localized()
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
63
64
65
66
            createButton.isHidden = true
            setData(response: data)
        case .viewFromQr:
            
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
67
            self.title = "FORM".localized()
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
68
69
70
            createButton.isHidden = true
            retriveCurentForm()
        }
71
72
73
74
75
76
        
        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
77
    }
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
78
    
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
79
80
81
82
83
84
85
86
87
88
    private func setUI() {
        
        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
89
    // MARK: - Actions
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
    @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
113
    
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
114
115
116
117
118
119
120
121
122
    @IBAction func createButtonAction(_ sender: UIButton) {
        
        if outDateTimeTextFiled.text == ""
            || outAddressTextFiled.text == ""
            || destinationAddressTextField.text == "" || planneDateTimeTextField.text == "" {
            
            let alertController = AlertControllerHelper.showAlert(title: nil, message: "Խնդում ենք լրացրեք բոլոր դաշտերը")
            self.present(alertController, animated: true, completion: nil)
        }else {
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
123
            createForm()
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
124
125
        }
    }
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
126
    
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
127
128
129
130
131
    private func openQrView() {
        
        let controller = QrViewController()
        changeRoot(UINavigationController(rootViewController: controller))
    }
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
132
133
134
135
136
137
138
139
140
    
    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
141
142
        let deviceId = UIDevice.current.identifierForVendor?.uuidString
        let date = Date().toString(dateFormat: DateFormat.StandartDate.rawValue)
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
143
        
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
144
        let form = FormCreateRequestForm(deviceToken: deviceId,
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
145
146
147
148
149
150
                                         firstName: firstName,
                                         lastName: lastName,
                                         middleName: middleName,
                                         outAddress: outAddressTextFiled.text,
                                         outLatitude: nil,
                                         outLongitude: nil,
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
151
                                         outDatetime: outDateTimeTextFiled.text != nil ? date + " " + outDateTimeTextFiled.text! : nil,
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
152
153
154
155
                                         visitingAddressAndName: destinationAddressTextField.text,
                                         visitingLatitude: nil,
                                         visitingLongitude: nil,
                                         visitingReason: destinationTypeTextField.text,
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
156
                                         plannedReturnDatetime: planneDateTimeTextField.text != nil ? date + " " + planneDateTimeTextField.text! : nil)
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
        
        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
176
177
    
    private func retriveCurentForm() {
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
178
179
180
181
182
183
184
185
186
        
        activityIndicator.isHidden = false
        activityIndicator.startAnimating()
        
        let form = CurentFormRequestForm(deviceToken: UIDevice.current.identifierForVendor?.uuidString)
        
        CurrentFormService.shered.retrive(data: form) { (responseData) in
            switch responseData {
            case .base(response: let baseResposne):
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
187
                
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
188
189
190
191
192
                self.activityIndicator.stopAnimating()
                CheckBaseHelper.checkBaseResponse(baseResposne, viewController: self)
            case .success(let response):
                
                self.activityIndicator.stopAnimating()
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
193
                DispatchQueue.main.async {
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
194
                    self.setData(response: response.data )
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
195
                }
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
196
            case .isOffline:
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
197
                
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
198
199
200
                self.activityIndicator.stopAnimating()
                return
            case .conflict:
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
201
                
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
202
203
204
205
206
                self.activityIndicator.stopAnimating()
                return
            }
        }
    }
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
207
    
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
208
    private func setData(response: FormResponse?) {
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
209
210
211
212
213
214
215
216
        
        destinationAddressTextField.isUserInteractionEnabled = false
        planneDateTimeTextField.isUserInteractionEnabled = false
        destinationTypeTextField.isUserInteractionEnabled = false
        destinationAddressTextField.isUserInteractionEnabled = false
        
        outDateButtonAction.isUserInteractionEnabled = false
        planneDateTimeButton.isUserInteractionEnabled = false
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
217
        outAddressTextFiled.isUserInteractionEnabled = false
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
218
219
220
221
        
        outDateTimeTextFiled.isUserInteractionEnabled = false
        planneDateTimeTextField.isUserInteractionEnabled = false
        
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
222
223
        outDateTimeTextFiled.text = response?.outDatetime
        outAddressTextFiled.text = response?.outAddress
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
224
        
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
225
226
227
        destinationAddressTextField.text = response?.visitingAddressAndName
        planneDateTimeTextField.text = response?.plannedReturnDatetime
        destinationTypeTextField.text = response?.visitingReason
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
228
        curentDate.text = response?.createdAt
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
229
    }
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
230
}