InfoViewController.swift 7.29 KB
Newer Older
domenicw's avatar
domenicw committed
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
//
//  InfoViewController.swift
//  Amiv
//
//  Created by Domenic Wüthrich on 19.09.18.
//  Copyright © 2018 Amiv an der ETH. All rights reserved.
//

import Foundation
import UIKit

public class InfoViewController: UIViewController {
    
    // MARK: - Variables
    
    public var delegate: InfoViewControllerDelegate?
    
    // MARK: - View Variables
    
    public private(set) var titleLabel: UILabel!
    
    public private(set) var infoViewCollection: [InfoItemView] = []
    
    public private(set) var button: UIButton!
    
    public private(set) var smallButton: UIButton?
    
    // MARK: - Initializers
    
    public init(model: InfoViewControllerModel) {
        super.init(nibName: nil, bundle: nil)
        self.view.backgroundColor = .white
        
        // View Creation
        self.titleLabel = self.createTitleLabel(model.title)
        self.view.addSubview(self.titleLabel)
        
        self.button = self.createButton(model.buttonTitle)
        self.view.addSubview(self.button)
        
        if let title = model.smallButtonTitle {
            let button = self.createSmallButton(title)
            self.smallButton = button
            self.view.addSubview(button)
        }
        
        for index in 0..<model.infoItems.count {
            let view = self.createInfoView(model.infoItems[index])
            self.infoViewCollection.append(view)
            self.view.addSubview(view)
        }
        
        // Apply Constraints()
        self.applyTitleLabelConstraints()
        self.applyButtonConstraints()
        self.applySmallButtonConstraints()
        for index in 0..<self.infoViewCollection.count {
            self.applyInfoViewConstraints(index)
        }
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - View Creation
    
    private func createTitleLabel(_ title: String) -> UILabel {
        let label = UILabel()
        label.text = title
        label.textColor = .amivRed
        label.numberOfLines = 2
        label.textAlignment = .center
        label.font = UIFont.preferredFont(forTextStyle: .largeTitle).bold()
        label.translatesAutoresizingMaskIntoConstraints = false
        
        return label
    }
    
    private func applyTitleLabelConstraints() {
        NSLayoutConstraint(item: self.titleLabel, attribute: .top, relatedBy: .equal, toItem: self.view, attribute: .topMargin, multiplier: 1, constant: 30).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 createInfoView(_ model: InfoItemModel) -> InfoItemView {
        let view = InfoItemView(model: model)
        view.translatesAutoresizingMaskIntoConstraints = false
        
        return view
    }
    
    private func applyInfoViewConstraints(_ index: Int) {
        if index == 0 {
            NSLayoutConstraint(item: self.infoViewCollection[index], attribute: .top, relatedBy: .equal, toItem: self.titleLabel, attribute: .bottom, multiplier: 1, constant: 40).isActive = true
        } else {
            NSLayoutConstraint(item: self.infoViewCollection[index], attribute: .top, relatedBy: .equal, toItem: self.infoViewCollection[index-1], attribute: .bottom, multiplier: 1, constant: 20).isActive = true
        }
        
        NSLayoutConstraint(item: self.infoViewCollection[index], attribute: .leading, relatedBy: .equal, toItem: self.view, attribute: .leadingMargin, multiplier: 1, constant: 10).isActive = true
        NSLayoutConstraint(item: self.infoViewCollection[index], attribute: .trailing, relatedBy: .equal, toItem: self.view, attribute: .trailingMargin, multiplier: 1, constant: -10).isActive = true
    }
    
    private func createButton(_ title: String) -> UIButton {
        let button = UIButton()
        button.setTitle(title, for: .normal)
        button.setTitleColor(.white, for: .normal)
        button.setTitleColor(.lightGray, for: .highlighted)
        button.backgroundColor = .amivRed
        button.layer.cornerRadius = 12
        button.addTarget(self, action: #selector(self.buttonAction), for: .touchUpInside)
        button.translatesAutoresizingMaskIntoConstraints = false
        
        return button
    }
    
    private func applyButtonConstraints() {
        if self.smallButton ==
            nil {
            //NSLayoutConstraint(item: self.button, attribute: .top, relatedBy: .greaterThanOrEqual, toItem: self.infoViewCollection.last, attribute: .bottom, multiplier: 1, constant: 30).isActive = true
        }
        NSLayoutConstraint(item: self.button, attribute: .leading, relatedBy: .equal, toItem: self.view, attribute: .leadingMargin, multiplier: 1, constant: 10).isActive = true
        NSLayoutConstraint(item: self.button, attribute: .trailing, relatedBy: .equal, toItem: self.view, attribute: .trailingMargin, multiplier: 1, constant: -10).isActive = true
        NSLayoutConstraint(item: self.button, attribute: .bottom, relatedBy: .equal, toItem: self.view, attribute: .bottomMargin, multiplier: 1, constant: -10).isActive = true
        NSLayoutConstraint(item: self.button, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 50).isActive = true
    }
    
    private func createSmallButton(_ title: String) -> UIButton {
        let button = UIButton()
        button.setTitle(title, for: .normal)
        button.setTitleColor(.amivRed, for: .normal)
        button.setTitleColor(.lightGray, for: .highlighted)
        button.addTarget(self, action: #selector(self.smallButtonAction), for: .touchUpInside)
        button.translatesAutoresizingMaskIntoConstraints = false
        
        return button
    }
    
    private func applySmallButtonConstraints() {
        guard let smallButton = self.smallButton else {
            return
        }
        
        //NSLayoutConstraint(item: smallButton, attribute: .top, relatedBy: .greaterThanOrEqual, toItem: self.infoViewCollection.last, attribute: .bottom, multiplier: 1, constant: 30).isActive = true
        NSLayoutConstraint(item: smallButton, attribute: .leading, relatedBy: .equal, toItem: self.view, attribute: .leadingMargin, multiplier: 1, constant: 10).isActive = true
        NSLayoutConstraint(item: smallButton, attribute: .trailing, relatedBy: .equal, toItem: self.view, attribute: .trailingMargin, multiplier: 1, constant: -10).isActive = true
        NSLayoutConstraint(item: smallButton, attribute: .bottom, relatedBy: .equal, toItem: self.button, attribute: .top, multiplier: 1, constant: -15).isActive = true
    }
    
    // MARK: - View Setup
    
    public override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        self.navigationController?.navigationBar.isHidden = true
    }
    
    // MARK: - View Interaction
    
    @objc private func buttonAction() {
        self.delegate?.buttonPressed()
        debugPrint("button pressed")
    }
    
    @objc private func smallButtonAction() {
        self.delegate?.smallButtonPressed()
        debugPrint("smallButton pressed")
    }
    
}