-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStoreFrontContentViewController.swift
More file actions
134 lines (106 loc) · 4.64 KB
/
StoreFrontContentViewController.swift
File metadata and controls
134 lines (106 loc) · 4.64 KB
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
//
// ViewController.swift
// Funbox
//
// Created by Антон Кочетков on 23.11.2021.
//
import UIKit
class StoreFrontContentViewController: UIViewController {
private var nameDeviceLabel: UILabel?
private var priceLabel: UILabel?
private var countLabel: UILabel?
private var buyButton: UIButton?
var activity: UIActivityIndicatorView?
var pageNumber: Int = 0
var buy: ((Device) -> Void)?
var device: Device! {
didSet {
configure()
}
}
override func viewDidLoad() {
super.viewDidLoad()
createMainStackView()
view.backgroundColor = .white
buyButton?.addTarget(self, action: #selector(buyAction), for: .touchUpInside)
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
configure()
}
private func configure() {
nameDeviceLabel?.text = device.name
priceLabel?.text = "\(device.price) руб."
countLabel?.text = "\(device.count) шт."
}
private func createMainStackView() {
//name device label
let nameDeviceLabel = UILabel.createLabel(title: "-")
nameDeviceLabel.font = UIFont.systemFont(ofSize: 30)
self.nameDeviceLabel = nameDeviceLabel
//price stack
let (priceStackView, priceLabel) = createHorizontalStackView(title: "Цена")
self.priceLabel = priceLabel
self.priceLabel?.text = "-"
//count stack
let (countStackView, countLabel) = createHorizontalStackView(title: "Количество")
self.countLabel = countLabel
self.countLabel?.text = "-"
//button
let viewButton = createViewButton()
let mainStackView = UIStackView(arrangedSubviews: [nameDeviceLabel, priceStackView, countStackView, viewButton])
mainStackView.axis = .vertical
mainStackView.distribution = .fillEqually
mainStackView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(mainStackView)
NSLayoutConstraint.activate([
mainStackView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
mainStackView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 16),
mainStackView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -16),
mainStackView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor)])
}
private func createHorizontalStackView(title: String) -> (UIStackView, UILabel) {
let staticLabel = UILabel.createLabel(title: title)
staticLabel.font = UIFont.systemFont(ofSize: 30)
let label = UILabel.createLabel(title: title)
label.textAlignment = .right
label.font = UIFont.systemFont(ofSize: 30)
label.textColor = .systemGray2
let stackView = UIStackView(arrangedSubviews: [staticLabel, label])
stackView.axis = .horizontal
stackView.distribution = .fillEqually
return (stackView, label)
}
private func createViewButton() -> UIView {
let button = UIButton()
button.setTitle("Купить", for: .normal)
button.setTitleColor(.black, for: .normal)
button.backgroundColor = .systemGray4
button.layer.cornerRadius = 10
button.layer.borderWidth = 3
button.layer.borderColor = UIColor.black.cgColor
button.titleLabel?.font = UIFont.systemFont(ofSize: 30)
button.translatesAutoresizingMaskIntoConstraints = false
self.buyButton = button
let activity = UIActivityIndicatorView()
activity.isHidden = true
activity.color = .blue
activity.translatesAutoresizingMaskIntoConstraints = false
self.activity = activity
let viewButton = UIView()
viewButton.addSubview(button)
viewButton.addSubview(activity)
NSLayoutConstraint.activate([
button.centerXAnchor.constraint(equalTo: viewButton.centerXAnchor),
button.centerYAnchor.constraint(equalTo: viewButton.centerYAnchor),
button.widthAnchor.constraint(equalTo: viewButton.widthAnchor, multiplier: 0.5),
button.heightAnchor.constraint(equalTo: viewButton.heightAnchor, multiplier: 0.35),
activity.centerXAnchor.constraint(equalTo: viewButton.centerXAnchor),
activity.topAnchor.constraint(equalTo: button.bottomAnchor, constant: 16)
])
return viewButton
}
@objc private func buyAction() {
buy?(device)
}
}