@@ -31,6 +31,10 @@ class LocationDetailViewController: UIViewController, ThemeProvider {
31
31
return view
32
32
} ( )
33
33
34
+ var isLiveLocationAttachment : Bool {
35
+ messageController. message? . liveLocationAttachments. first != nil
36
+ }
37
+
34
38
override func viewDidLoad( ) {
35
39
super. viewDidLoad ( )
36
40
@@ -39,7 +43,7 @@ class LocationDetailViewController: UIViewController, ThemeProvider {
39
43
40
44
mapView. register (
41
45
UserAnnotationView . self,
42
- forAnnotationViewWithReuseIdentifier: " UserAnnotation "
46
+ forAnnotationViewWithReuseIdentifier: UserAnnotationView . reuseIdentifier
43
47
)
44
48
mapView. showsUserLocation = false
45
49
mapView. delegate = self
@@ -82,15 +86,15 @@ class LocationDetailViewController: UIViewController, ThemeProvider {
82
86
override func viewDidAppear( _ animated: Bool ) {
83
87
super. viewDidAppear ( animated)
84
88
85
- setupBottomSheet ( )
89
+ presentLocationControlSheet ( )
86
90
}
87
91
88
92
func updateUserLocation(
89
93
_ coordinate: CLLocationCoordinate2D
90
94
) {
91
95
if let existingAnnotation = userAnnotation {
92
- // By setting the duration to 5, and since we update the location every 3s
93
- // this will make sure the annotation moves smoothly and has a constant animation .
96
+ // Since we update the location every 3s, by updating the coordinate with 5s animation
97
+ // this will make sure the annotation moves smoothly.
94
98
UIView . animate ( withDuration: 5 ) {
95
99
existingAnnotation. coordinate = coordinate
96
100
}
@@ -108,20 +112,23 @@ class LocationDetailViewController: UIViewController, ThemeProvider {
108
112
}
109
113
}
110
114
111
- func setupBottomSheet( ) {
112
- if #available( iOS 16 . 0 , * ) {
113
- let bottomSheet = LocationBottomSheetViewController (
114
- messageController: messageController
115
+ func presentLocationControlSheet( ) {
116
+ if #available( iOS 16 . 0 , * ) , isLiveLocationAttachment, messageController. message? . isSentByCurrentUser == true {
117
+ let locationControlSheet = LocationControlSheetViewController (
118
+ messageController: messageController. client. messageController (
119
+ cid: messageController. cid,
120
+ messageId: messageController. messageId
121
+ )
115
122
)
116
- bottomSheet . modalPresentationStyle = . pageSheet
123
+ locationControlSheet . modalPresentationStyle = . pageSheet
117
124
let detent = UISheetPresentationController . Detent. custom ( resolver: { _ in 60 } )
118
- bottomSheet . sheetPresentationController? . detents = [ detent]
119
- bottomSheet . sheetPresentationController? . prefersGrabberVisible = false
120
- bottomSheet . sheetPresentationController? . preferredCornerRadius = 16
121
- bottomSheet . sheetPresentationController? . prefersScrollingExpandsWhenScrolledToEdge = false
122
- bottomSheet . sheetPresentationController? . largestUndimmedDetentIdentifier = detent. identifier
123
- bottomSheet . isModalInPresentation = true
124
- present ( bottomSheet , animated: true )
125
+ locationControlSheet . sheetPresentationController? . detents = [ detent]
126
+ locationControlSheet . sheetPresentationController? . prefersGrabberVisible = false
127
+ locationControlSheet . sheetPresentationController? . preferredCornerRadius = 16
128
+ locationControlSheet . sheetPresentationController? . prefersScrollingExpandsWhenScrolledToEdge = false
129
+ locationControlSheet . sheetPresentationController? . largestUndimmedDetentIdentifier = detent. identifier
130
+ locationControlSheet . isModalInPresentation = true
131
+ present ( locationControlSheet , animated: true )
125
132
}
126
133
}
127
134
}
@@ -143,6 +150,12 @@ extension LocationDetailViewController: ChatMessageControllerDelegate {
143
150
updateUserLocation (
144
151
locationCoordinate
145
152
)
153
+
154
+ let isLiveLocationSharingStopped = liveLocationAttachment. stoppedSharing == true
155
+ if isLiveLocationSharingStopped, let userAnnotation = self . userAnnotation {
156
+ let userAnnotationView = mapView. view ( for: userAnnotation) as? UserAnnotationView
157
+ userAnnotationView? . stopPulsingAnimation ( )
158
+ }
146
159
}
147
160
}
148
161
@@ -155,9 +168,8 @@ extension LocationDetailViewController: MKMapViewDelegate {
155
168
return nil
156
169
}
157
170
158
- let identifier = " UserAnnotation "
159
171
let annotationView = mapView. dequeueReusableAnnotationView (
160
- withIdentifier: identifier ,
172
+ withIdentifier: UserAnnotationView . reuseIdentifier ,
161
173
for: userAnnotation
162
174
) as? UserAnnotationView
163
175
@@ -174,7 +186,7 @@ extension LocationDetailViewController: MKMapViewDelegate {
174
186
}
175
187
}
176
188
177
- class LocationBottomSheetViewController : UIViewController , ThemeProvider {
189
+ class LocationControlSheetViewController : UIViewController , ThemeProvider {
178
190
let messageController : ChatMessageController
179
191
180
192
init (
@@ -195,7 +207,6 @@ class LocationBottomSheetViewController: UIViewController, ThemeProvider {
195
207
196
208
lazy var locationUpdateLabel : UILabel = {
197
209
let label = UILabel ( )
198
- label. text = " Location updated 5 minutes ago "
199
210
label. font = appearance. fonts. footnote
200
211
label. textColor = appearance. colorPalette. subtitleText
201
212
return label
@@ -209,6 +220,9 @@ class LocationBottomSheetViewController: UIViewController, ThemeProvider {
209
220
override func viewDidLoad( ) {
210
221
super. viewDidLoad ( )
211
222
223
+ messageController. synchronize ( )
224
+ messageController. delegate = self
225
+
212
226
view. backgroundColor = appearance. colorPalette. background6
213
227
214
228
let container = VContainer ( spacing: 2 , alignment: . center) {
@@ -228,3 +242,35 @@ class LocationBottomSheetViewController: UIViewController, ThemeProvider {
228
242
messageController. stopLiveLocationSharing ( )
229
243
}
230
244
}
245
+
246
+ extension LocationControlSheetViewController : ChatMessageControllerDelegate {
247
+ func messageController(
248
+ _ controller: ChatMessageController ,
249
+ didChangeMessage change: EntityChange < ChatMessage >
250
+ ) {
251
+ guard let liveLocationAttachment = controller. message? . liveLocationAttachments. first else {
252
+ return
253
+ }
254
+
255
+ let isSharingLiveLocation = liveLocationAttachment. stoppedSharing == false
256
+ sharingButton. isEnabled = isSharingLiveLocation
257
+ sharingButton. setTitle (
258
+ isSharingLiveLocation ? " Stop Sharing " : " Live location ended " ,
259
+ for: . normal
260
+ )
261
+
262
+ let buttonColor = appearance. colorPalette. alert
263
+ sharingButton. setTitleColor (
264
+ isSharingLiveLocation ? buttonColor : buttonColor. withAlphaComponent ( 0.6 ) ,
265
+ for: . normal
266
+ )
267
+
268
+ if isSharingLiveLocation {
269
+ locationUpdateLabel. text = " Location sharing is active "
270
+ } else {
271
+ let lastUpdated = messageController. message? . updatedAt ?? Date ( )
272
+ let formatter = appearance. formatters. channelListMessageTimestamp
273
+ locationUpdateLabel. text = " Location last updated at \( formatter. format ( lastUpdated) ) "
274
+ }
275
+ }
276
+ }
0 commit comments