BarcodeViewController.swift 4.19 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
//
//  BarcodeViewController.swift
//  Amiv
//
//  Created by Denis Zuppiger on 12.10.18.
//  Copyright © 2018 Amiv an der ETH. All rights reserved.
//

import Foundation
import UIKit
import AVFoundation


public class BarcodeViewController: UIViewController {
    public var delegate: BarcodeViewControllerDelegate?
    
    public private(set) var titleLabel: UILabel!
    public private(set) var barcodeLabel: UIImage!
    public private(set) var barcodeLabelView: UIImageView!
    public private(set) var button: UIButton!
    public private(set) var textView: UITextView!
    
    public init(model: AmivMicroAppModel){
        super.init(nibName: nil, bundle: nil)
        self.title = model.title
        
        // UI
        self.titleLabel = self.createTitleLabel()
        self.view.addSubview(self.titleLabel)
        
        let leftBarButton = UIBarButtonItem(title: "Done", style: UIBarButtonItem.Style.plain, target: self, action: #selector(goBack))
        self.navigationItem.leftBarButtonItem = leftBarButton
        self.barcodeLabel = UIImage(cgImage: self.createBarcodeLabel().cgImage! ,scale: 0.1 ,orientation: UIImage.Orientation.right)
        self.barcodeLabel = RSAbstractCodeGenerator.resizeImage(self.barcodeLabel, targetSize: self.barcodeLabel.size, contentMode: .scaleToFill)
        self.barcodeLabelView = UIImageView()
        self.barcodeLabelView.image = self.barcodeLabel
        self.barcodeLabelView.translatesAutoresizingMaskIntoConstraints = false
        self.view.addSubview(self.barcodeLabelView)
        
        // Layout
        self.setLabelLayout()
        self.setBarcodeLabelLayout()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func createTitleLabel() -> UILabel {
        let label = UILabel()
        label.text = "LEGI-BARCODE:"
        label.textColor = .amivRed
        label.numberOfLines = 2
        label.textAlignment = .center
        label.font = UIFont.preferredFont(forTextStyle: .largeTitle).bold()
        label.translatesAutoresizingMaskIntoConstraints = false
        return label
    }
    
    private func setLabelLayout(){
        NSLayoutConstraint(item: self.titleLabel, attribute: .top, relatedBy: .equal, toItem: self.view, attribute: .topMargin, multiplier: 1, constant: 10).isActive = true
        NSLayoutConstraint(item: self.titleLabel, attribute: .leading, relatedBy: .equal, toItem: self.view, attribute: .leadingMargin, multiplier: 1, constant: 10).isActive = true
        NSLayoutConstraint(item: self.titleLabel, attribute: .trailing, relatedBy: .equal, toItem: self.view, attribute: .trailingMargin, multiplier: 1, constant: -10).isActive = true
    }
    
    private func createBarcodeLabel() -> UIImage {
        let codeInput = "PLACEHOLDER"
        let errorImage = UIImage(named: "xcode_error_icon")
        // Code 39 Generator Library RSBarcodes
        guard let barcode = RSUnifiedCodeGenerator.shared.generateCode(codeInput, machineReadableCodeObjectType: AVMetadataObject.ObjectType.code39.rawValue) else {
            return errorImage!
        }
        return barcode
    }
    
    private func setBarcodeLabelLayout(){
        NSLayoutConstraint(item: self.barcodeLabelView, attribute: .top, relatedBy: .equal, toItem: self.titleLabel, attribute: .bottom, multiplier: 1, constant: 10).isActive = true
        NSLayoutConstraint(item: self.barcodeLabelView, attribute: .leading, relatedBy: .equal, toItem: self.view, attribute: .leadingMargin, multiplier: 1, constant: 10).isActive = true
        NSLayoutConstraint(item: self.barcodeLabelView, attribute: .trailing, relatedBy: .equal, toItem: self.view, attribute: .trailingMargin, multiplier: 1, constant: -10).isActive = true
        NSLayoutConstraint(item: self.barcodeLabelView, attribute: .bottom, relatedBy: .lessThanOrEqual, toItem: self.view, attribute: .bottomMargin, multiplier: 1, constant: -10).isActive = true
    }
    
    @objc private func goBack() {
        dismiss(animated: true, completion: nil)
    }
    
    override public func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        
        self.view.backgroundColor = .white
    }
}