@@ -26,6 +26,7 @@ class HomeViewController: UIViewController {
2626 let image = UIImage ( systemName: " pencil.line " )
2727 button. setImage ( UIImage ( systemName: " pencil.line " ) , for: . normal)
2828 button. backgroundColor = . secondarySystemBackground
29+ button. tintColor = DesignSystemColor . Purple. value
2930 button. layer. cornerRadius = circleButtonSize / 2
3031 button. clipsToBounds = true
3132 return button
@@ -34,6 +35,7 @@ class HomeViewController: UIViewController {
3435 let button = UIButton ( )
3536 button. setImage ( UIImage ( systemName: " dot.scope " ) , for: . normal)
3637 button. backgroundColor = . secondarySystemBackground
38+ button. tintColor = DesignSystemColor . Purple. value
3739 button. layer. cornerRadius = circleButtonSize / 2
3840 button. clipsToBounds = true
3941 return button
@@ -56,6 +58,11 @@ class HomeViewController: UIViewController {
5658 setupLayout ( )
5759 }
5860
61+ override func viewWillAppear( _ animated: Bool ) {
62+ super. viewWillAppear ( animated)
63+ loadAnnotations ( )
64+ }
65+
5966 private func setupMapLocation( ) {
6067 locationmanager. delegate = self
6168 mapView. delegate = self
@@ -71,18 +78,21 @@ class HomeViewController: UIViewController {
7178 animated: true
7279 )
7380 mapView. isRotateEnabled = false
74- // mapView.register(CustomAnnotationView.self, forAnnotationViewWithReuseIdentifier: CustomAnnotationView.identifier)
81+ // mapView.register(CustomAnnotationView.self, forAnnotationViewWithReuseIdentifier: CustomAnnotationView.identifier)
7582 mapView. register ( MKMarkerAnnotationView . self, forAnnotationViewWithReuseIdentifier: " annotation " )
7683 mapView. register ( CustomClusterAnnotationView . self, forAnnotationViewWithReuseIdentifier: CustomClusterAnnotationView . identifier)
7784
7885 loadAnnotations ( )
7986 }
8087
8188 private func loadAnnotations( ) {
89+ mapView. removeAnnotations ( mapView. annotations)
8290 usecase. fetchAllPins { pins in
8391 let annotations = pins. map { CustomAnnotation ( pinData: $0) }
8492 self . mapView. addAnnotations ( annotations)
8593 self . mapView ( self . mapView, regionDidChangeAnimated: true )
94+ self . adapter? . data = pins. sorted ( by: { $0. date > $1. date } )
95+ self . bottomSheet. collectionView. reloadData ( )
8696 }
8797 }
8898
@@ -150,7 +160,7 @@ extension HomeViewController: MKMapViewDelegate {
150160 let view = mapView. dequeueReusableAnnotationView ( withIdentifier: " annotation " , for: annotation) as! MKMarkerAnnotationView
151161 view. annotation = annotation
152162 view. clusteringIdentifier = " pinCluster " // 클러스터링 가능하게
153-
163+
154164 return view
155165 }
156166
@@ -174,10 +184,14 @@ extension HomeViewController: MKMapViewDelegate {
174184 }
175185 func mapView( _ mapView: MKMapView , regionDidChangeAnimated animated: Bool ) {
176186 let visibleAnnotations = mapView. annotations ( in: mapView. visibleMapRect)
177- let visibleMarkers = visibleAnnotations. compactMap { $0 as? CustomAnnotation }
178- // let visibleClusters = visibleAnnotations.compactMap{ $0 as? MKClusterAnnotation } // 할필요없음
179- // BottomSheet의 CollectionView 업데이트
180- adapter? . data = visibleMarkers. map { $0. pinData }
187+ let visibleMarkers = visibleAnnotations. compactMap { ( $0 as? CustomAnnotation ) ? . pinData }
188+
189+ guard let adapterData = adapter? . data else { return }
190+ let adapterSet = Set ( adapterData. map ( \. pin_id) )
191+ let temp = Set ( visibleMarkers. map { $0. pin_id} ) . symmetricDifference ( adapterSet) . count
192+ guard temp != 0 else { return }
193+
194+ adapter? . data = visibleMarkers. sorted ( by: { $0. date > $1. date } )
181195 bottomSheet. collectionView. reloadData ( )
182196 }
183197}
@@ -205,11 +219,11 @@ extension HomeViewController: CLLocationManagerDelegate {
205219
206220// MARK: PinCollectionViewAdapterDelegate
207221extension HomeViewController : PinCollectionViewAdapterDelegate {
208- func selectedItem( selected: PinEntity ) {
222+ func selectedItem( selected: PinEntity , indexPath : IndexPath ) {
209223 presentPinDetailViewController ( selected: selected)
210224 }
211225
212- func deletedItem( deleted: PinEntity ? ) {
226+ func deletedItem( deleted: PinEntity ? , indexPath : IndexPath ) {
213227 guard let deleted = deleted else { return }
214228 usecase. deletePin ( pinID: deleted. pin_id)
215229 removePinEntity ( pinEntity: deleted)
@@ -251,6 +265,8 @@ extension HomeViewController {
251265 mapView. removeAnnotation ( annotationToRemove)
252266 }
253267 mapView ( mapView, regionDidChangeAnimated: true )
268+ adapter? . data = adapter? . data. filter { $0. pin_id != pinEntity. pin_id} ?? [ ]
269+ bottomSheet. collectionView. reloadData ( )
254270 }
255271}
256272
0 commit comments