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

import UIKit
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
10
import AlamofireImage
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
11
12
13
14
15
16
17

class QrViewController: UIViewController {
    
    // MARK: - IBOutlets
    @IBOutlet weak var qrImageView: UIImageView!
    @IBOutlet weak var fomrButton: UIButton!
    @IBOutlet weak var finishButton: UIButton!
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
18
19
    @IBOutlet weak var activityIndicator: UIActivityIndicatorView!
    
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
20
21
22
    // MARK: - Variables
    let downloader = ImageDownloader()
    var urlRequest: URLRequest?
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
23
24
25
26
    
    // MARK: - LifeCycle
    override func viewDidLoad() {
        super.viewDidLoad()
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
27
        
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
28
        downloaderQR()
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
29
        setUI()
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
30
31
    }
    
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
32
33
34
35
36
37
    private func setUI() {
        
        fomrButton.setTitle("FORM".localized(), for: .normal)
        finishButton.setTitle("FINISH".localized(), for: .normal)
    }
    
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
    private func downloaderQR() {
        
        self.activityIndicator.isHidden = false
        self.activityIndicator.startAnimating()
        if let teviceId = UIDevice.current.identifierForVendor?.uuidString {
            urlRequest = URLRequest(url: URL(string: "https://app16.x-tech.am/api/v1/applications/qr_code?device_token=\(teviceId)")!)
            downloader.download(urlRequest!) { response in
                if case .success(let image) = response.result {
                    print(image)
                    self.qrImageView.image = image
                    self.activityIndicator.stopAnimating()
                }
            }
        }
    }
    
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
54
55
    // MARK: - Actions
    @IBAction func formButtonAction(_ sender: UIButton) {
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
56
        openCreateFormView()
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
57
58
59
    }
    
    @IBAction func finshButtonAction(_ sender: UIButton) {
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
60
        finishCurentForm()
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
61
62
63
64
65
66
67
68
69
70
    }
    
    private func openWelcomeView() {
        
        let controller = WelcomeViewController()
        changeRoot(UINavigationController(rootViewController: controller))
    }
    
    private func openCreateFormView() {
        
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
71
72
        let controller = SingleFormViewController()
        
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
73
        self.navigationController?.navigationBar.topItem?.title = " "
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
74
        self.navigationController?.pushViewController(controller, animated: true)
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
75
    }
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
    
    private func setImage(_ imageURL: String, imageView: UIImageView) {
        
        if let imageURL = URL(string: imageURL) {
            imageView.af_setImage(withURL: imageURL,
                                  placeholderImage: nil,
                                  filter: nil,
                                  progress: nil,
                                  progressQueue: .main,
                                  imageTransition: .crossDissolve(0.1),
                                  runImageTransitionIfCached: true) { (response) in
                                    self.activityIndicator.stopAnimating()
            }
        }
    }
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
91
    
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
92
93
94
    // MARK: - Request
    private func finishCurentForm() {
        
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
95
96
        let form = CurentFormRequestForm(deviceToken: UIDevice.current.identifierForVendor?.uuidString)
        FormFinishService.shered.finishForm(data: form) { (responseData) in
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
97
98
            switch responseData {
            case .base(response: let baseResposne):
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
99
                CheckBaseHelper.checkBaseResponse(baseResposne, viewController: self)
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
100
            case .success(_):
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
101
                self.openWelcomeView()
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
102
103
104
            case .isOffline:
                return
            case .conflict:
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
105
                return
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
106
107
108
            }
        }
    }
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
109
}