GenericInfoViewController.swift 8.49 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//
//  GenericInfoViewController.swift
//  Amiv
//
//  Created by Domenic Wüthrich on 20.09.18.
//  Copyright © 2018 Amiv an der ETH. All rights reserved.
//

import Foundation
import UIKit

public class GenericInfoViewController: UIViewController {
    
    // MARK: - Variables
    
    public var delegate: GenericInfoViewControllerDelegate?
    
    // MARK: - View Variables
    
    public private(set) var scrollView: UIScrollView!
    
domenicw's avatar
domenicw committed
22
23
    private var imageContainerView: UIView?
    
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
    public private(set) var imageView: UIImageView?
    
    public private(set) var textView: UITextView!
    
    public private(set) var button: UIButton?
    
    // MARK: - Initializers
    
    public init(model: GenericInfoViewControllerModel) {
        super.init(nibName: nil, bundle: nil)
        self.view.backgroundColor = .white
        
        // View Creation
        self.scrollView = self.createScrollView()
        self.view.addSubview(self.scrollView)
        
        if let image = model.image {
domenicw's avatar
domenicw committed
41
42
43
44
            let container = self.createImageContainerView()
            self.imageContainerView = container
            self.scrollView.addSubview(container)
            
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
            let view = self.createImageView(image)
            self.imageView = view
            self.scrollView.addSubview(view)
        }
        
        self.textView = self.createTextView(model.text)
        self.scrollView.addSubview(self.textView)
        
        if let title = model.titleButton {
            let button = self.createButton(title)
            self.button = button
            self.scrollView.addSubview(button)
        }
        
        // View Constraints
        self.applyScrollViewConstraints()
domenicw's avatar
domenicw committed
61
        self.applyImageContainerViewConstraints()
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
        self.applyImageViewConstraints()
        self.applyTextViewConstraints()
        self.applyButtonConstraints()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - View Creation
    
    private func createScrollView() -> UIScrollView {
        let view = UIScrollView()
        view.backgroundColor = .white
        view.translatesAutoresizingMaskIntoConstraints = false
        
        return view
    }
    
    private func applyScrollViewConstraints() {
        NSLayoutConstraint(item: self.scrollView, attribute: .top, relatedBy: .equal, toItem: self.view, attribute: .top, multiplier: 1, constant: 0).isActive = true
        NSLayoutConstraint(item: self.scrollView, attribute: .leading, relatedBy: .equal, toItem: self.view, attribute: .leading, multiplier: 1, constant: 0).isActive = true
        NSLayoutConstraint(item: self.scrollView, attribute: .trailing, relatedBy: .equal, toItem: self.view, attribute: .trailing, multiplier: 1, constant: 0).isActive = true
        NSLayoutConstraint(item: self.scrollView, attribute: .bottom, relatedBy: .equal, toItem: self.view, attribute: .bottom, multiplier: 1, constant: 0).isActive = true
    }
    
    private func createImageView(_ image: UIImage?) -> UIImageView {
        let view = UIImageView()
        view.image = image
domenicw's avatar
domenicw committed
91
        view.contentMode = .scaleAspectFill
92
93
94
95
96
97
        view.translatesAutoresizingMaskIntoConstraints = false
        
        return view
    }
    
    private func applyImageViewConstraints() {
domenicw's avatar
domenicw committed
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
        guard let imageView = self.imageView, let container = self.imageContainerView else {
            return
        }
        
        let top = NSLayoutConstraint(item: imageView, attribute: .top, relatedBy: .equal, toItem: self.view, attribute: .topMargin, multiplier: 1, constant: 0)
        top.priority = UILayoutPriority(rawValue: 900)
        top.isActive = true
        NSLayoutConstraint(item: imageView, attribute: .leading, relatedBy: .equal, toItem: container, attribute: .leadingMargin, multiplier: 1, constant: 0).isActive = true
        NSLayoutConstraint(item: imageView, attribute: .trailing, relatedBy: .equal, toItem: container, attribute: .trailingMargin, multiplier: 1, constant: 0).isActive = true
        NSLayoutConstraint(item: imageView, attribute: .bottom, relatedBy: .equal, toItem: container, attribute: .bottomMargin, multiplier: 1, constant: 0).isActive = true
    }
    
    private func createImageContainerView() -> UIView {
        let view = UIView()
        view.backgroundColor = .white
        view.translatesAutoresizingMaskIntoConstraints = false
        
        return view
    }
    
    private func applyImageContainerViewConstraints() {
        guard let containerView = self.imageContainerView else {
120
121
122
            return
        }
        
domenicw's avatar
domenicw committed
123
124
125
126
        NSLayoutConstraint(item: containerView, attribute: .top, relatedBy: .equal, toItem: self.scrollView, attribute: .topMargin, multiplier: 1, constant: 0).isActive = true
        NSLayoutConstraint(item:  containerView, attribute: .leading, relatedBy: .equal, toItem: self.view, attribute: .leadingMargin, multiplier: 1, constant: 0).isActive = true
        NSLayoutConstraint(item: containerView, attribute: .trailing, relatedBy: .equal, toItem: self.view, attribute: .trailingMargin, multiplier: 1, constant: 0).isActive = true
        NSLayoutConstraint(item: containerView, attribute: .height, relatedBy: .equal, toItem: self.view, attribute: .width, multiplier: 0.7, constant: 0).isActive = true
127
128
129
130
131
132
    }
    
    private func createTextView(_ text: String) -> UITextView {
        let textView = UITextView()
        textView.text = text
        textView.isScrollEnabled = false
domenicw's avatar
domenicw committed
133
        textView.isEditable = false
134
135
136
137
138
139
140
141
        textView.font = UIFont.preferredFont(forTextStyle: .body)
        textView.translatesAutoresizingMaskIntoConstraints = false
        
        return textView
    }
    
    private func applyTextViewConstraints() {
        if let imageView = self.imageView {
domenicw's avatar
domenicw committed
142
            NSLayoutConstraint(item: self.textView, attribute: .top, relatedBy: .equal, toItem: imageView, attribute: .bottom, multiplier: 1, constant: 10).isActive = true
143
        } else {
domenicw's avatar
domenicw committed
144
            NSLayoutConstraint(item: self.textView, attribute: .top, relatedBy: .equal, toItem: self.scrollView, attribute: .top, multiplier: 1, constant: 0).isActive = true
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
        }
        
        NSLayoutConstraint(item: self.textView, attribute: .leading, relatedBy: .equal, toItem: self.view, attribute: .leadingMargin, multiplier: 1, constant: 0).isActive = true
        NSLayoutConstraint(item: self.textView, attribute: .trailing, relatedBy: .equal, toItem: self.view, attribute: .trailingMargin, multiplier: 1, constant: 0).isActive = true
        
        if self.button == nil {
            NSLayoutConstraint(item: self.textView, attribute: .bottom, relatedBy: .equal, toItem: self.scrollView, attribute: .bottom, multiplier: 1, constant: 0).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.buttonTapped), for: .touchUpInside)
        button.translatesAutoresizingMaskIntoConstraints = false
        
        return button
    }
    
    private func applyButtonConstraints() {
        guard let button = self.button else {
            return
        }
        
        NSLayoutConstraint(item: button, attribute: .top, relatedBy: .equal, toItem: self.textView, attribute: .bottom, multiplier: 1, constant: 20).isActive = true
        NSLayoutConstraint(item: button, attribute: .leading, relatedBy: .equal, toItem: self.view, attribute: .leadingMargin, multiplier: 1, constant: 10).isActive = true
        NSLayoutConstraint(item: button, attribute: .trailing, relatedBy: .equal, toItem: self.view, attribute: .trailingMargin, multiplier: 1, constant: -10).isActive = true
        NSLayoutConstraint(item: button, attribute: .bottom, relatedBy: .equal, toItem: self.scrollView, attribute: .bottom, multiplier: 1, constant: 0).isActive = true
        NSLayoutConstraint(item: button, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 50).isActive = true
    }
    
    // MARK: - View Setup
    
    public override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        self.navigationItem.largeTitleDisplayMode = .never
    }
    
    // MARK: - View Interaction
    
    @objc private func buttonTapped() {
        debugPrint("button tapped")
        self.delegate?.buttonTapped()
    }
    
}