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

import UIKit

class FormListViewController: UIViewController {

    // MARK: - IBOutlet
    @IBOutlet weak var tableView: UITableView!
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
15
    @IBOutlet weak var activityIndicator: UIActivityIndicatorView!
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
16
    
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
17
18
    // MARK: - Varibales
    private let formListCell = "FormListCell"
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
19
    var dataSource: [FormResponse] = []
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
20
    
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
21
22
23
24
    // MARK: - LifeCycle
    override func viewDidLoad() {
        super.viewDidLoad()

Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
25
26
27
28
        tableView.delegate = self
        tableView.dataSource = self
        
        registerCelll()
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
29
        self.title = "HISTORY".localized()
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
30
31
32
33
        
        if let deviceId = UIDevice.current.identifierForVendor?.uuidString {
            retriveCurentForm(deviceToken: deviceId)
        }
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
34
35
36
37
38
39
40
41
    }
    
    // MARK: - Register
    private func registerCelll() {
        
        let nib = UINib(nibName: formListCell, bundle: nil)
        tableView.register(nib, forCellReuseIdentifier: formListCell)
    }
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
42
43
44
    
    private func retriveCurentForm(deviceToken: String) {
        
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
45
46
47
        activityIndicator.isHidden = false
        activityIndicator.startAnimating()
        
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
48
49
50
51
52
        let form = CurentFormRequestForm(deviceToken: UIDevice.current.identifierForVendor?.uuidString)
        
        FormsService.shered.retriveForms(data: form) { (responseData) in
            switch responseData {
            case .base(response: let baseResposne):
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
53
                self.activityIndicator.stopAnimating()
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
54
55
                CheckBaseHelper.checkBaseResponse(baseResposne, viewController: self)
            case .success(let response):
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
56
                self.activityIndicator.stopAnimating()
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
57
58
59
60
61
                self.dataSource = response.data ?? []
                DispatchQueue.main.async {
                    self.tableView.reloadData()
                }
            case .isOffline:
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
62
                self.activityIndicator.stopAnimating()
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
63
64
                return
            case .conflict:
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
65
                self.activityIndicator.stopAnimating()
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
66
67
68
69
70
                return
            }
        }
        
    }
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
71
72
73
74
75
76
77
}

extension FormListViewController: UITableViewDataSource {
    
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
78
        return dataSource.count
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
79
80
81
82
83
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: formListCell, for: indexPath) as? FormListCell
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
84
85
86
        cell?.outDateLabel.text = dataSource[indexPath.row].outDatetime
        cell?.planeDateLabel.text = dataSource[indexPath.row].outDatetime
        cell?.createdDateLabel.text = dataSource[indexPath.row].createdAt
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
87
88
89
90
91
92
93
        return cell ?? UITableViewCell()
    }
}

extension FormListViewController: UITableViewDelegate {
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
94
        openCreateFormView(index: indexPath.row)
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
95
96
    }
    
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
97
    private func openCreateFormView(index: Int) {
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
98
99
        
        let controller = FormCreateViewController()
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
100
101
        controller.formViewType = .viewFromList
        controller.data = dataSource[index]
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
102
        self.navigationController?.navigationBar.topItem?.title = " "
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
103
        self.navigationController?.pushViewController(controller, animated: true)
Grigor Aghabalyan's avatar
Grigor Aghabalyan committed
104
105
    }
}