aboutsummaryrefslogtreecommitdiff
path: root/foray/Scenes/ForayDetailView.swift
blob: cd70ffca75625e00661249297e5af26ca82dbc11 (plain) (blame)
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
//
//  ForayDetailView.swift
//  foray
//
//  Created by Nicholas Tay on 24/3/2022.
//

import UIKit

class ForayDetailView: UIView {

    let scrollView: UIScrollView = {
        let sv = UIScrollView()
        sv.alwaysBounceVertical = true // just for fun
        return sv
    }()
    let container = UIView()
    
    let nameLabel: UILabel = {
        let l = UILabel()
        l.font = UIFont.preferredFont(forTextStyle: .largeTitle)
        l.adjustsFontForContentSizeCategory = true
        l.numberOfLines = 3
        l.textAlignment = .center
        return l
    }()
    
    let itemImageView: UIImageView = {
        let iv = UIImageView()
        iv.contentMode = .scaleAspectFit
        return iv
    }()
    
    let descLabel: UILabel = {
        let l = UILabel()
        l.font = UIFont.preferredFont(forTextStyle: .body)
        l.adjustsFontForContentSizeCategory = true
        l.numberOfLines = 10
        return l
    }()

    private lazy var shareButton: UIButton = {
        let b = UIButton()
        b.addTarget(self, action: #selector(onShareButton), for: .touchUpInside)
        b.setTitle("Share", for: .normal)
        b.setTitleColor(UIColor.systemBlue, for: .normal)
        b.backgroundColor = UIColor.clear
        return b
    }()

    enum ActionType {
        case onShare
    }

    var onAction: ((ActionType) -> Void)?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        initialiseViews()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func initialiseViews() {
        backgroundColor = .systemBackground
        
        addSubview(scrollView)
        scrollView.addSubview(container)
        
        container.addSubview(nameLabel)
        container.addSubview(itemImageView)
        container.addSubview(shareButton)
        container.addSubview(descLabel)
        
        setupConstraints()
    }
    
    private func setupConstraints() {
        scrollView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        container.snp.makeConstraints { (make) in
            make.top.bottom.equalToSuperview()
            make.leading.equalTo(snp.leadingMargin)
            make.trailing.equalTo(snp.trailingMargin)
        }
        
        nameLabel.snp.makeConstraints { (make) in
            make.top.equalToSuperview().inset(16)
            make.leading.trailing.equalToSuperview().inset(8)
        }
        itemImageView.snp.makeConstraints { (make) in
            make.top.equalTo(nameLabel.snp.bottom).offset(32)
            make.leading.trailing.equalToSuperview()
            make.height.equalTo(150)
        }
        shareButton.snp.makeConstraints { (make) in
            make.top.equalTo(itemImageView.snp.bottom).offset(16)
            make.width.equalTo(50)
            make.height.equalTo(25)
            make.centerX.equalToSuperview()
        }
        descLabel.snp.makeConstraints { (make) in
            make.top.equalTo(shareButton.snp.bottom).offset(32)
            make.leading.trailing.equalToSuperview()
            make.bottom.equalTo(container.snp.bottom).inset(16)
        }
    }
    
    public func setDetails(name: String, description: String, image: UIImage) {
        nameLabel.text = name
        descLabel.text = description
        itemImageView.image = image
    }

    @objc
    private func onShareButton() {
        onAction?(.onShare)
    }

}