GenericInfoViewController.swift 9.12 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
    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
35
        self.title = model.title
36
37
38
39
40
41
        
        // View Creation
        self.scrollView = self.createScrollView()
        self.view.addSubview(self.scrollView)
        
        if let image = model.image {
domenicw's avatar
domenicw committed
42
43
44
45
            let container = self.createImageContainerView()
            self.imageContainerView = container
            self.scrollView.addSubview(container)
            
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
            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
62
        self.applyImageContainerViewConstraints()
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
        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
92
        view.contentMode = .scaleAspectFill
93
        view.isUserInteractionEnabled = true
94
95
        view.translatesAutoresizingMaskIntoConstraints = false
        
96
97
98
99
        let recognizer = UITapGestureRecognizer(target: self, action: #selector(self.expandImage))
        recognizer.numberOfTapsRequired = 1
        view.addGestureRecognizer(recognizer)
        
100
101
102
103
        return view
    }
    
    private func applyImageViewConstraints() {
domenicw's avatar
domenicw committed
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
        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 {
126
127
128
            return
        }
        
129
130
131
        let top = NSLayoutConstraint(item: containerView, attribute: .top, relatedBy: .equal, toItem: self.scrollView, attribute: .top, multiplier: 1, constant: 0)
        top.priority = UILayoutPriority(1000)
        top.isActive = true
domenicw's avatar
domenicw committed
132
133
134
        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
135
136
137
138
139
140
    }
    
    private func createTextView(_ text: String) -> UITextView {
        let textView = UITextView()
        textView.text = text
        textView.isScrollEnabled = false
domenicw's avatar
domenicw committed
141
        textView.isEditable = false
142
143
144
145
146
147
148
149
        textView.font = UIFont.preferredFont(forTextStyle: .body)
        textView.translatesAutoresizingMaskIntoConstraints = false
        
        return textView
    }
    
    private func applyTextViewConstraints() {
        if let imageView = self.imageView {
domenicw's avatar
domenicw committed
150
            NSLayoutConstraint(item: self.textView, attribute: .top, relatedBy: .equal, toItem: imageView, attribute: .bottom, multiplier: 1, constant: 10).isActive = true
151
        } else {
domenicw's avatar
domenicw committed
152
            NSLayoutConstraint(item: self.textView, attribute: .top, relatedBy: .equal, toItem: self.scrollView, attribute: .top, multiplier: 1, constant: 0).isActive = true
153
154
155
156
157
158
159
160
161
162
163
164
165
166
        }
        
        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)
167
        button.setBackgroundColor(.amivRed, for: .normal)
168
        button.layer.cornerRadius = 12
169
        button.layer.masksToBounds = true
170
171
172
173
174
175
176
177
178
179
180
181
182
183
        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
domenicw's avatar
domenicw committed
184
        NSLayoutConstraint(item: button, attribute: .bottom, relatedBy: .equal, toItem: self.scrollView, attribute: .bottom, multiplier: 1, constant: -10).isActive = true
185
186
187
188
189
190
191
192
        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)
        
domenicw's avatar
domenicw committed
193
        self.navigationItem.largeTitleDisplayMode = .never
194
195
196
197
198
199
200
201
202
    }
    
    // MARK: - View Interaction
    
    @objc private func buttonTapped() {
        debugPrint("button tapped")
        self.delegate?.buttonTapped()
    }
    
203
204
205
206
207
208
209
210
211
212
    @objc private func expandImage() {
        debugPrint("expanding image")
        guard let image = self.imageView?.image else {
            return
        }
        
        let imageViewer = ImageViewerViewController(image: image)
        self.present(imageViewer, animated: false, completion: nil)
    }
    
213
}