Skip to content

Commit 4208e77

Browse files
authored
Merge pull request #32 from kut7728/feature/#30
화면 연결 및 데이터 흐름 작업
2 parents b4a8e7b + b068aa8 commit 4208e77

File tree

9 files changed

+109
-37
lines changed

9 files changed

+109
-37
lines changed

Pinit/Pinit/Models/PinEntity.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@ import UIKit
99

1010
struct PinEntity {
1111
let pin_id: UUID
12-
let title: String
12+
var title: String
1313
let latitude: Double
1414
let longitude: Double
1515
let address: String
1616
let date: Date
1717
let weather: String
18-
let description: String?
19-
let mediaPath: UIImage?
18+
var description: String?
19+
var mediaPath: UIImage?
2020

2121
static let sampleData: [PinEntity] = [
2222
PinEntity(pin_id: UUID(uuidString: "b44a6eaf-a5f8-426c-8200-0cf93a18c2ca")!,

Pinit/Pinit/Models/ProducerEntity.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ extension PinEntity {
2626
//데이터 부분들
2727

2828
PinEntity(
29-
pin_id: UUID(),
29+
pin_id: UUID(uuidString: "11111111-1111-1111-1111-111111111111")!,
3030
title: "JustHm",
3131
latitude: 37.9244577,
3232
longitude: 128.800009,//본인 지역의 의,경도
@@ -38,7 +38,7 @@ extension PinEntity {
3838
),
3939

4040
PinEntity(
41-
pin_id: UUID(),
41+
pin_id: UUID(uuidString: "22222222-2222-2222-2222-222222222222")!,
4242
title: "Ikhwan0204",
4343
latitude: 37.506610,
4444
longitude: 126.885332,
@@ -50,7 +50,7 @@ extension PinEntity {
5050
),
5151

5252
PinEntity(
53-
pin_id: UUID(),
53+
pin_id: UUID(uuidString: "33333333-3333-3333-3333-333333333333")!,
5454
title: "IntakHan304",
5555
latitude: 37.434981,
5656
longitude: 126.902328,
@@ -62,7 +62,7 @@ extension PinEntity {
6262
),
6363

6464
PinEntity(
65-
pin_id: UUID(),
65+
pin_id: UUID(uuidString: "44444444-4444-4444-4444-444444444444")!,
6666
title: "HISEHOONAN",
6767
latitude: 37.508645,
6868
longitude: 126.703513,

Pinit/Pinit/Views/Home/CustomAnnotationView.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ final class CustomAnnotationView: MKAnnotationView {
1414
override init(annotation: MKAnnotation?, reuseIdentifier: String?){
1515
super.init(annotation: annotation, reuseIdentifier: reuseIdentifier)
1616
setupUI()
17+
clusteringIdentifier = "pinCluster"
1718
}
1819

1920
required init?(coder aDecoder: NSCoder) {
@@ -31,7 +32,7 @@ final class CustomAnnotationView: MKAnnotationView {
3132
let originalImage = UIImage(named: "recordPin2")!
3233
let resizedImage = resizeImage(originalImage, targetSize: CGSize(width: 40, height: 40))
3334
self.image = resizedImage
34-
self.centerOffset = CGPoint(x: 0, y: frame.size.height / 2)
35+
self.centerOffset = CGPoint(x: 0, y: self.bounds.minY - (frame.size.height / 2))
3536
self.layer.shadowColor = UIColor.black.withAlphaComponent(0.25).cgColor
3637
self.layer.shadowOpacity = 1
3738
self.layer.shadowRadius = 4
@@ -49,7 +50,6 @@ final class CustomAnnotationView: MKAnnotationView {
4950
titleLabel.attributedText = NSAttributedString(string: annotation.pinData.title, attributes: attributes)
5051
titleLabel.sizeToFit()
5152

52-
5353
titleLabel.snp.makeConstraints {
5454
$0.centerX.equalToSuperview()
5555
$0.top.equalTo(self.snp.bottom)
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//
2+
// CustomClusterAnnotationView.swift
3+
// Pinit
4+
//
5+
// Created by 안정흠 on 3/21/25.
6+
//
7+
8+
import UIKit
9+
import MapKit
10+
11+
final class CustomClusterAnnotationView: MKMarkerAnnotationView {
12+
static let identifier = "CustomClusterAnnotationView"
13+
14+
override var annotation: MKAnnotation? {
15+
didSet {
16+
configure()
17+
}
18+
}
19+
20+
private func configure() {
21+
guard let cluster = annotation as? MKClusterAnnotation else { return }
22+
23+
// 1️⃣ 클러스터 안에 있는 핀 개수 텍스트로 표시
24+
glyphText = "\(cluster.memberAnnotations.count)"
25+
markerTintColor = .systemBlue // 원하는 색
26+
displayPriority = .defaultHigh
27+
}
28+
}

Pinit/Pinit/Views/Home/HomeViewController.swift

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,9 @@ class HomeViewController: UIViewController {
7171
animated: true
7272
)
7373
mapView.isRotateEnabled = false
74-
mapView.register(CustomAnnotationView.self, forAnnotationViewWithReuseIdentifier: CustomAnnotationView.identifier)
75-
// mapView.register(CustomAnnotationView.self, forAnnotationViewWithReuseIdentifier: "ClusterView")
74+
// mapView.register(CustomAnnotationView.self, forAnnotationViewWithReuseIdentifier: CustomAnnotationView.identifier)
75+
mapView.register(MKMarkerAnnotationView.self, forAnnotationViewWithReuseIdentifier: "annotation")
76+
mapView.register(CustomClusterAnnotationView.self, forAnnotationViewWithReuseIdentifier: CustomClusterAnnotationView.identifier)
7677

7778
loadAnnotations()
7879
}
@@ -146,32 +147,23 @@ extension HomeViewController: MKMapViewDelegate {
146147
}
147148

148149
private func createCustomAnnotationView(for annotation: CustomAnnotation, in mapView: MKMapView) -> MKAnnotationView {
149-
let identifier = CustomAnnotationView.identifier
150-
var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier) as? CustomAnnotationView
151-
152-
if annotationView == nil {
153-
annotationView = CustomAnnotationView(annotation: annotation, reuseIdentifier: identifier)
154-
} else {
155-
annotationView?.annotation = annotation
156-
}
157-
annotationView?.configure(with: annotation)
158-
return annotationView!
150+
let view = mapView.dequeueReusableAnnotationView(withIdentifier: "annotation", for: annotation) as! MKMarkerAnnotationView
151+
view.annotation = annotation
152+
view.clusteringIdentifier = "pinCluster" // 클러스터링 가능하게
153+
154+
return view
159155
}
160156

161157
private func createClusterView(for cluster: MKClusterAnnotation) -> MKAnnotationView {
162-
let identifier = "ClusterView"
163-
var clusterView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier) as? MKMarkerAnnotationView
158+
let identifier = CustomClusterAnnotationView.identifier
159+
var clusterView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier) as? CustomClusterAnnotationView
164160

165161
if clusterView == nil {
166-
clusterView = MKMarkerAnnotationView(annotation: cluster, reuseIdentifier: identifier)
162+
clusterView = CustomClusterAnnotationView(annotation: cluster, reuseIdentifier: identifier)
167163
} else {
168164
clusterView?.annotation = cluster
169165
}
170166

171-
clusterView?.markerTintColor = .systemBlue
172-
clusterView?.glyphText = "\(cluster.memberAnnotations.count)" // 클러스터 내 개수 표시
173-
clusterView?.displayPriority = .defaultHigh // 클러스터를 우선적으로 표시
174-
175167
return clusterView!
176168
}
177169

@@ -183,6 +175,7 @@ extension HomeViewController: MKMapViewDelegate {
183175
func mapView(_ mapView: MKMapView, regionDidChangeAnimated animated: Bool) {
184176
let visibleAnnotations = mapView.annotations(in: mapView.visibleMapRect)
185177
let visibleMarkers = visibleAnnotations.compactMap { $0 as? CustomAnnotation }
178+
// let visibleClusters = visibleAnnotations.compactMap{ $0 as? MKClusterAnnotation } // 할필요없음
186179
// BottomSheet의 CollectionView 업데이트
187180
adapter?.data = visibleMarkers.map{ $0.pinData }
188181
bottomSheet.collectionView.reloadData()

Pinit/Pinit/Views/Home/PinRecordCell.swift

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
//
77

88
import UIKit
9+
import MapKit
910

1011
final class PinRecordCell: UICollectionViewCell {
1112
//그림자 뷰 추가
@@ -42,8 +43,23 @@ final class PinRecordCell: UICollectionViewCell {
4243
func configure(model: PinEntity) {
4344
pinDateLabel.text = model.date.formatted()
4445
pinTitleLabel.text = model.title
45-
thumbnailImageView.image = UIImage(systemName: "house")
46-
//thumbnailImageView.image = model.mediaPath ?? UIImage(systemName: "house")
46+
47+
if let image = model.mediaPath {
48+
thumbnailImageView.image = image
49+
}
50+
else {
51+
captureMapSnapshotWithPin(
52+
center: CLLocationCoordinate2D(
53+
latitude: model.latitude,
54+
longitude: model.longitude
55+
),
56+
imageSize: CGSize(
57+
width: contentView.frame.height,
58+
height: contentView.frame.width
59+
)) { image in
60+
self.thumbnailImageView.image = image ?? UIImage(systemName: "house")
61+
}
62+
}
4763

4864
setupLayout()
4965
}
@@ -77,3 +93,24 @@ final class PinRecordCell: UICollectionViewCell {
7793

7894
}
7995
}
96+
97+
98+
extension PinRecordCell {
99+
func captureMapSnapshotWithPin(center: CLLocationCoordinate2D, imageSize: CGSize, completion: @escaping (UIImage?) -> Void) {
100+
let options = MKMapSnapshotter.Options()
101+
options.region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.003, longitudeDelta: 0.003))
102+
options.size = imageSize
103+
options.mapType = .standard
104+
105+
let snapshotter = MKMapSnapshotter(options: options)
106+
snapshotter.start { snapshot, error in
107+
guard let snapshot = snapshot, error == nil else {
108+
print("스냅샷 생성 실패")
109+
completion(nil)
110+
return
111+
}
112+
113+
completion(snapshot.image)
114+
}
115+
}
116+
}

Pinit/Pinit/Views/PastPin/PastPinViewController.swift

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import SnapKit
1212
final class PastPinViewController: UIViewController {
1313

1414
//MARK: - 모든 PinEntity를 가져옵니다.
15-
var pinData = PinEntity.sampleData
15+
var pinData: [PinEntity] = []
1616

1717
private let usecase: UseCase
1818

@@ -47,6 +47,18 @@ final class PastPinViewController: UIViewController {
4747
SetUI()
4848
setupAdapter()
4949
calendarUI()
50+
usecase.fetchPinsByDate(date: Date()) { items in
51+
self.adapter?.data = items
52+
self.PinCollectionView.reloadData()
53+
}
54+
}
55+
56+
override func viewWillAppear(_ animated: Bool) {
57+
super.viewWillAppear(animated)
58+
usecase.fetchPinsByDate(date: Date()) { items in
59+
self.adapter?.data = items
60+
self.PinCollectionView.reloadData()
61+
}
5062
}
5163

5264
//MARK: - init

Pinit/Pinit/Views/PinDetail/PinDetailViewController.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,12 @@ final class PinDetailViewController: UIViewController {
7171
let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.005, longitudeDelta: 0.005))
7272

7373
map.setRegion(region, animated: true)
74-
map.showsUserLocation = true
74+
map.showsUserLocation = false
75+
map.isUserInteractionEnabled = false
7576

7677
let annotation = MKPointAnnotation()
7778
annotation.coordinate = CLLocationCoordinate2D(latitude: lat, longitude: long) // San Francisco, CA
7879
annotation.title = pinEntity.title
79-
annotation.subtitle = pinEntity.weather
8080
map.addAnnotation(annotation)
8181

8282
return map

Pinit/Pinit/Views/PinEdit/PinEditViewController.swift

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -177,9 +177,9 @@ final class PinEditViewController: UIViewController, UITextViewDelegate {
177177
contentTextView.inputAccessoryView = keyboardToolBar
178178
contentTextView.delegate = self
179179

180-
closeButton.addTarget(PinEditViewController.self, action: #selector(dismissButtonTapped), for: .touchUpInside)
180+
closeButton.addTarget(self, action: #selector(dismissButtonTapped), for: .touchUpInside)
181181
saveButton.addTarget(self, action: #selector(saveButtonTapped), for: .touchUpInside)
182-
cameraButton.addTarget(PinEditViewController.self, action: #selector(cameraButtonTapped), for: .touchUpInside)
182+
cameraButton.addTarget(self, action: #selector(cameraButtonTapped), for: .touchUpInside)
183183

184184
self.view.addSubviews(mapView, saveButton, contentTextView, titleTextField, cameraButton, weatherImage, dateLabel, closeButton)
185185

@@ -247,8 +247,10 @@ final class PinEditViewController: UIViewController, UITextViewDelegate {
247247

248248
//MARK: 저장버튼 눌림
249249
@objc private func saveButtonTapped() {
250-
guard let newPin = pinEntity else { return }
251-
isAdded?(newPin)
250+
pinEntity?.title = titleTextField.text ?? ""
251+
pinEntity?.description = contentTextView.text ?? ""
252+
// pinEntity?.mediaPath = image ????
253+
isAdded?(pinEntity!)
252254
dismiss(animated: true)
253255
}
254256

0 commit comments

Comments
 (0)