Skip to content

Commit 46b6386

Browse files
author
Эван Крошкин
committed
Refactored
1 parent 668e614 commit 46b6386

File tree

8 files changed

+51
-128
lines changed

8 files changed

+51
-128
lines changed

Task4.xcodeproj/project.pbxproj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
BC70CBE627DF46160003CEC4 /* MapLocationManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = BC70CBE527DF46160003CEC4 /* MapLocationManager.swift */; };
2626
BC70CBE827DF46480003CEC4 /* Time.swift in Sources */ = {isa = PBXBuildFile; fileRef = BC70CBE727DF46480003CEC4 /* Time.swift */; };
2727
BC70CBEA27DF4C5F0003CEC4 /* HTTPModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = BC70CBE927DF4C5F0003CEC4 /* HTTPModel.swift */; };
28-
BC70CBF427DF71830003CEC4 /* MapView.swift in Sources */ = {isa = PBXBuildFile; fileRef = BC70CBF327DF71830003CEC4 /* MapView.swift */; };
28+
BC70CBF427DF71830003CEC4 /* MapAnnotationView.swift in Sources */ = {isa = PBXBuildFile; fileRef = BC70CBF327DF71830003CEC4 /* MapAnnotationView.swift */; };
2929
BC70CBF627DF71F10003CEC4 /* AvailabilityView.swift in Sources */ = {isa = PBXBuildFile; fileRef = BC70CBF527DF71F10003CEC4 /* AvailabilityView.swift */; };
3030
BC70CBF827DF87780003CEC4 /* ListCollectionViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = BC70CBF727DF87780003CEC4 /* ListCollectionViewCell.swift */; };
3131
BC70CBFA27DF887D0003CEC4 /* SectionReusableView.swift in Sources */ = {isa = PBXBuildFile; fileRef = BC70CBF927DF887D0003CEC4 /* SectionReusableView.swift */; };
@@ -62,7 +62,7 @@
6262
BC70CBE527DF46160003CEC4 /* MapLocationManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MapLocationManager.swift; sourceTree = "<group>"; };
6363
BC70CBE727DF46480003CEC4 /* Time.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Time.swift; sourceTree = "<group>"; };
6464
BC70CBE927DF4C5F0003CEC4 /* HTTPModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HTTPModel.swift; sourceTree = "<group>"; };
65-
BC70CBF327DF71830003CEC4 /* MapView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MapView.swift; sourceTree = "<group>"; };
65+
BC70CBF327DF71830003CEC4 /* MapAnnotationView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MapAnnotationView.swift; sourceTree = "<group>"; };
6666
BC70CBF527DF71F10003CEC4 /* AvailabilityView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AvailabilityView.swift; sourceTree = "<group>"; };
6767
BC70CBF727DF87780003CEC4 /* ListCollectionViewCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ListCollectionViewCell.swift; sourceTree = "<group>"; };
6868
BC70CBF927DF887D0003CEC4 /* SectionReusableView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SectionReusableView.swift; sourceTree = "<group>"; };
@@ -209,7 +209,7 @@
209209
isa = PBXGroup;
210210
children = (
211211
BCED481427E1E7260095C595 /* LoadingView.swift */,
212-
BC70CBF327DF71830003CEC4 /* MapView.swift */,
212+
BC70CBF327DF71830003CEC4 /* MapAnnotationView.swift */,
213213
BC70CBF527DF71F10003CEC4 /* AvailabilityView.swift */,
214214
BC70CBF927DF887D0003CEC4 /* SectionReusableView.swift */,
215215
);
@@ -381,7 +381,7 @@
381381
BC70CBEA27DF4C5F0003CEC4 /* HTTPModel.swift in Sources */,
382382
BC70CBDF27DF44CB0003CEC4 /* ATMModel.swift in Sources */,
383383
BC70CBF627DF71F10003CEC4 /* AvailabilityView.swift in Sources */,
384-
BC70CBF427DF71830003CEC4 /* MapView.swift in Sources */,
384+
BC70CBF427DF71830003CEC4 /* MapAnnotationView.swift in Sources */,
385385
BC70CBE827DF46480003CEC4 /* Time.swift in Sources */,
386386
BC5F4AAA27CBBD8200E82425 /* NetworkManager.swift in Sources */,
387387
BC6FED5427F8DFAA001AE091 /* Currency.swift in Sources */,

Task4/Cells/ListCollectionViewCell.swift

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,16 +69,32 @@ class ListCollectionViewCell: UICollectionViewCell {
6969
return stack
7070
}()
7171

72+
override init(frame: CGRect) {
73+
super.init(frame: frame)
74+
activateConstraints()
75+
}
76+
77+
required init?(coder: NSCoder) {
78+
fatalError("init(coder:) has not been implemented")
79+
}
80+
7281
override func prepareForReuse() {
7382
super.prepareForReuse()
7483
model = nil
7584
}
7685

77-
func setupCell() {
86+
func setup(viewModel: ATM?) {
87+
guard let atmModel = viewModel else { return }
88+
addressLabel.text = atmModel.address.addressLine
89+
currencyLabel.text = "\(Constants.Strings.currency) \(atmModel.currency.rawValue)"
90+
self.model = viewModel
91+
}
92+
93+
func activateConstraints() {
94+
addSubview(contentStackView)
7895
availabilityButton.addTarget(self,
7996
action: #selector(didTapButton),
8097
for: .touchUpInside)
81-
addSubview(contentStackView)
8298
[addressLabel, currencyLabel, availabilityButton].forEach { contentStackView.addArrangedSubview($0) }
8399
self.backgroundColor = .lightGray
84100
self.layer.cornerRadius = Constants.Dimensions.defaultCornerRadius
@@ -88,9 +104,6 @@ class ListCollectionViewCell: UICollectionViewCell {
88104
make.trailing.equalToSuperview().offset(-Constants.Dimensions.defaultPadding)
89105
make.bottom.equalToSuperview().offset(-Constants.Dimensions.defaultPadding)
90106
}
91-
guard let atmModel = model else { return }
92-
addressLabel.text = atmModel.address.addressLine
93-
currencyLabel.text = "\(Constants.Strings.currency) \(atmModel.currency.rawValue)"
94107
}
95108

96109
@objc private func didTapButton() {

Task4/Controllers/ListViewController.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,7 @@ extension ListViewController: UICollectionViewDataSource {
109109
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: ListCollectionViewCell.reuseIdentifier, for: indexPath)
110110
if let cell = cell as? ListCollectionViewCell {
111111
let key = townNames[indexPath.section]
112-
cell.model = modelForWork[key]?[indexPath.row]
113-
cell.setupCell()
112+
cell.setup(viewModel: modelForWork[key]?[indexPath.row])
114113
}
115114
return cell
116115
}

Task4/Controllers/MapViewController.swift

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class MapViewController: UIViewController {
1818
let mapView = MKMapView()
1919
mapView.delegate = self
2020
mapView.register(MKMarkerAnnotationView.self,
21-
forAnnotationViewWithReuseIdentifier: NSStringFromClass(MapAnnotation.self))
21+
forAnnotationViewWithReuseIdentifier: NSStringFromClass(MapAnnotationView.self))
2222
view.addSubview(mapView)
2323
return mapView
2424
}()
@@ -82,7 +82,7 @@ class MapViewController: UIViewController {
8282
model.forEach { [weak self] atm in
8383
let latitude = Double(atm.address.geolocation.geographicCoordinates.latitude) ?? 0
8484
let longtitude = Double(atm.address.geolocation.geographicCoordinates.longitude) ?? 0
85-
let annotation = MapAnnotation()
85+
let annotation = MapAnnotationView()
8686
annotation.title = atm.address.addressLine
8787
annotation.coordinate = CLLocationCoordinate2D(latitude: latitude,
8888
longitude: longtitude)
@@ -138,25 +138,20 @@ extension MapViewController: MKMapViewDelegate {
138138
guard !annotation.isKind(of: MKUserLocation.self) else { return nil }
139139

140140
var annotationView: MKAnnotationView?
141-
if let annotation = annotation as? MapAnnotation {
141+
if let annotation = annotation as? MapAnnotationView {
142142
annotationView = setupAnnotationView(for: annotation, on: mapView)
143143
}
144144
return annotationView
145145
}
146146

147-
private func setupAnnotationView(for annotation: MapAnnotation, on mapView: MKMapView) -> MKAnnotationView {
148-
let identifier = NSStringFromClass(MapAnnotation.self)
147+
private func setupAnnotationView(for annotation: MapAnnotationView, on mapView: MKMapView) -> MKAnnotationView {
148+
let identifier = NSStringFromClass(MapAnnotationView.self)
149149
let view = mapView.dequeueReusableAnnotationView(withIdentifier: identifier, for: annotation)
150150
if let markerAnnotationView = view as? MKMarkerAnnotationView {
151151
markerAnnotationView.animatesWhenAdded = true
152152
markerAnnotationView.canShowCallout = true
153153
markerAnnotationView.markerTintColor = Constants.Colors.appMainColor
154-
155-
let rightButton = UIButton(type: .detailDisclosure)
156-
markerAnnotationView.rightCalloutAccessoryView = rightButton
157-
158154
let selectedATM = model?.first(where: { $0.address.addressLine == view.annotation?.title })
159-
160155
markerAnnotationView.detailCalloutAccessoryView = setupMarkerDetails(for: selectedATM)
161156
}
162157
return view

Task4/SupportFiles/Constans/Constants.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ class Constants {
3636
static let interItemSpacing = CGFloat(10)
3737
static let maxLayoutWidth = CGFloat(300)
3838
static let sizeContent = UIEdgeInsets(top: 10,
39-
left: 30,
40-
bottom: 20,
41-
right: 20)
39+
left: 30,
40+
bottom: 20,
41+
right: 20)
4242
}
4343

4444
class Location {

Task4/Views/AvailabilityView.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,9 @@ class AvailabilityView: UIView {
129129
private func setupView() {
130130
addSubview(mainStackview)
131131
[shortNameStackView, dynamicStackView].forEach { mainStackview.addArrangedSubview($0) }
132-
let gesture = UITapGestureRecognizer(target: self,
133-
action: #selector(didTapClose(_:)))
134-
addGestureRecognizer(gesture)
132+
let tap = UITapGestureRecognizer(target: self,
133+
action: #selector(didTapClose(_:)))
134+
addGestureRecognizer(tap)
135135
backgroundColor = .white
136136
configureLabels()
137137
mainStackview.snp.makeConstraints { make in

Task4/Views/MapAnnotationView.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//
2+
// MapView.swift
3+
// Task4
4+
//
5+
// Created by Эван Крошкин on 27.02.22.
6+
//
7+
8+
import UIKit
9+
import MapKit
10+
import SnapKit
11+
12+
class MapAnnotationView: NSObject, MKAnnotation {
13+
var coordinate = CLLocationCoordinate2D()
14+
var title: String?
15+
var subtitle: String?
16+
}

Task4/Views/MapView.swift

Lines changed: 1 addition & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -9,108 +9,8 @@ import UIKit
99
import MapKit
1010
import SnapKit
1111

12-
class MapAnnotation: NSObject, MKAnnotation {
12+
class MapAnnotationView: NSObject, MKAnnotation {
1313
var coordinate = CLLocationCoordinate2D()
1414
var title: String?
1515
var subtitle: String?
1616
}
17-
18-
class MapAnnotationView: MKAnnotationView {
19-
var atmModel: ATM?
20-
21-
private let blurEffect = UIBlurEffect(style: .systemThickMaterial)
22-
23-
private lazy var visualView: UIVisualEffectView = {
24-
let view = UIVisualEffectView(effect: blurEffect)
25-
view.translatesAutoresizingMaskIntoConstraints = false
26-
return view
27-
}()
28-
29-
private lazy var stackView: UIStackView = {
30-
let stackView = UIStackView(arrangedSubviews: [placeLabel, availabilityView])
31-
stackView.axis = .vertical
32-
stackView.alignment = .top
33-
stackView.spacing = Constants.Dimensions.interItemSpacing
34-
stackView.translatesAutoresizingMaskIntoConstraints = false
35-
return stackView
36-
}()
37-
38-
private lazy var placeLabel: UILabel = {
39-
let label = UILabel(frame: .zero)
40-
label.text = atmModel?.address.addressLine
41-
label.font = UIFont.preferredFont(forTextStyle: .caption1)
42-
label.translatesAutoresizingMaskIntoConstraints = false
43-
label.lineBreakMode = .byWordWrapping
44-
label.numberOfLines = 0
45-
label.preferredMaxLayoutWidth = Constants.Dimensions.maxLayoutWidth
46-
return label
47-
}()
48-
49-
private lazy var availabilityView: AvailabilityView = {
50-
let days = self.atmModel?.availability.standardAvailability.day
51-
let view = AvailabilityView(for: days)
52-
view.isUserInteractionEnabled = false
53-
view.translatesAutoresizingMaskIntoConstraints = false
54-
return view
55-
}()
56-
57-
override init(annotation: MKAnnotation?, reuseIdentifier: String?) {
58-
super.init(annotation: annotation, reuseIdentifier: reuseIdentifier)
59-
setupView()
60-
}
61-
62-
required init?(coder aDecoder: NSCoder) {
63-
super.init(coder: aDecoder)
64-
}
65-
66-
override func layoutSubviews() {
67-
super.layoutSubviews()
68-
invalidateIntrinsicContentSize()
69-
let contentSize = intrinsicContentSize
70-
frame.size = intrinsicContentSize
71-
centerOffset = CGPoint(x: contentSize.width / 1.5,
72-
y: contentSize.height / 1.5)
73-
let shapeLayer = CAShapeLayer()
74-
let path = CGMutablePath()
75-
let pointBezierPath = UIBezierPath()
76-
pointBezierPath.move(to: CGPoint(x: Constants.Dimensions.pointTableInSet,
77-
y: 0))
78-
pointBezierPath.addLine(to: CGPoint.zero)
79-
pointBezierPath.addLine(to: CGPoint(x: Constants.Dimensions.pointTableInSet,
80-
y: Constants.Dimensions.pointTableInSet))
81-
let box = CGRect(x: Constants.Dimensions.pointTableInSet,
82-
y: 0,
83-
width: self.frame.size.width - Constants.Dimensions.pointTableInSet,
84-
height: self.frame.size.height)
85-
let roundedRect = UIBezierPath(roundedRect: box,
86-
byRoundingCorners: [.topRight, .bottomLeft, .bottomRight],
87-
cornerRadii: CGSize(width: 5, height: 5))
88-
path.addPath(roundedRect.cgPath)
89-
path.addPath(pointBezierPath.cgPath)
90-
shapeLayer.path = path
91-
visualView.layer.mask = shapeLayer
92-
}
93-
94-
override var intrinsicContentSize: CGSize {
95-
var size = stackView.systemLayoutSizeFitting(UIView.layoutFittingCompressedSize)
96-
let sizeContent = Constants.Dimensions.sizeContent
97-
size.width += sizeContent.left + sizeContent.right
98-
size.height += sizeContent.top + sizeContent.bottom
99-
return size
100-
}
101-
102-
private func setupView() {
103-
backgroundColor = UIColor.white
104-
addSubview(visualView)
105-
addSubview(stackView)
106-
addSubview(placeLabel)
107-
addSubview(availabilityView)
108-
visualView.contentView.addSubview(stackView)
109-
visualView.snp.makeConstraints { make in
110-
make.edges.equalToSuperview()
111-
}
112-
stackView.snp.makeConstraints { make in
113-
make.edges.equalTo(visualView)
114-
}
115-
}
116-
}

0 commit comments

Comments
 (0)