@@ -9,10 +9,23 @@ import UIKit
9
9
10
10
class LocationAttachmentSnapshotView : _View , ThemeProvider {
11
11
struct Content {
12
+ var coordinate : CLLocationCoordinate2D
13
+ var isLive : Bool
14
+ var isSharingLiveLocation : Bool
12
15
var messageId : MessageId ?
13
- var latitude : CLLocationDegrees
14
- var longitude : CLLocationDegrees
15
- var isLive : Bool = false
16
+ var author : ChatUser ?
17
+
18
+ init ( coordinate: CLLocationCoordinate2D , isLive: Bool , isSharingLiveLocation: Bool , messageId: MessageId ? , author: ChatUser ? ) {
19
+ self . coordinate = coordinate
20
+ self . isLive = isLive
21
+ self . isSharingLiveLocation = isSharingLiveLocation
22
+ self . messageId = messageId
23
+ self . author = author
24
+ }
25
+
26
+ var isFromCurrentUser : Bool {
27
+ author? . id == StreamChatWrapper . shared. client? . currentUserId
28
+ }
16
29
}
17
30
18
31
var content : Content ? {
@@ -59,6 +72,18 @@ class LocationAttachmentSnapshotView: _View, ThemeProvider {
59
72
return button
60
73
} ( )
61
74
75
+ lazy var avatarView : ChatUserAvatarView = {
76
+ let view = ChatUserAvatarView ( )
77
+ view. translatesAutoresizingMaskIntoConstraints = false
78
+ view. shouldShowOnlineIndicator = false
79
+ view. layer. masksToBounds = true
80
+ view. layer. cornerRadius = 15
81
+ view. layer. borderWidth = 2
82
+ view. layer. borderColor = UIColor . white. cgColor
83
+ view. isHidden = true
84
+ return view
85
+ } ( )
86
+
62
87
override func setUp( ) {
63
88
super. setUp ( )
64
89
@@ -85,10 +110,16 @@ class LocationAttachmentSnapshotView: _View, ThemeProvider {
85
110
. height ( 35 )
86
111
} . embed ( in: self )
87
112
113
+ addSubview ( avatarView)
114
+
88
115
NSLayoutConstraint . activate ( [
89
- activityIndicatorView. centerXAnchor. constraint ( equalTo: centerXAnchor) ,
90
- activityIndicatorView. centerYAnchor. constraint ( equalTo: centerYAnchor) ,
91
- imageView. widthAnchor. constraint ( equalTo: container. widthAnchor)
116
+ activityIndicatorView. centerXAnchor. constraint ( equalTo: imageView. centerXAnchor) ,
117
+ activityIndicatorView. centerYAnchor. constraint ( equalTo: imageView. centerYAnchor) ,
118
+ imageView. widthAnchor. constraint ( equalTo: container. widthAnchor) ,
119
+ avatarView. centerXAnchor. constraint ( equalTo: imageView. centerXAnchor) ,
120
+ avatarView. centerYAnchor. constraint ( equalTo: imageView. centerYAnchor) ,
121
+ avatarView. widthAnchor. constraint ( equalToConstant: 30 ) ,
122
+ avatarView. heightAnchor. constraint ( equalToConstant: 30 )
92
123
] )
93
124
}
94
125
@@ -103,7 +134,7 @@ class LocationAttachmentSnapshotView: _View, ThemeProvider {
103
134
return
104
135
}
105
136
106
- if content. isLive {
137
+ if content. isSharingLiveLocation && content . isFromCurrentUser {
107
138
stopButton. isHidden = false
108
139
} else {
109
140
stopButton. isHidden = true
@@ -129,10 +160,7 @@ class LocationAttachmentSnapshotView: _View, ThemeProvider {
129
160
}
130
161
131
162
mapOptions. region = . init(
132
- center: CLLocationCoordinate2D (
133
- latitude: content. latitude,
134
- longitude: content. longitude
135
- ) ,
163
+ center: content. coordinate,
136
164
span: MKCoordinateSpan (
137
165
latitudeDelta: 0.01 ,
138
166
longitudeDelta: 0.01
@@ -149,6 +177,7 @@ class LocationAttachmentSnapshotView: _View, ThemeProvider {
149
177
150
178
if let cachedSnapshot = getCachedSnapshot ( ) {
151
179
imageView. image = cachedSnapshot
180
+ updateAnnotationView ( )
152
181
return
153
182
} else {
154
183
imageView. image = nil
@@ -159,39 +188,51 @@ class LocationAttachmentSnapshotView: _View, ThemeProvider {
159
188
snapshotter = MKMapSnapshotter ( options: mapOptions)
160
189
snapshotter? . start { snapshot, _ in
161
190
guard let snapshot = snapshot else { return }
162
- let image = self . generatePinAnnotation ( for: snapshot)
163
191
DispatchQueue . main. async {
164
192
self . activityIndicatorView. stopAnimating ( )
165
- self . imageView. image = image
166
- self . setCachedSnapshot ( image: image)
193
+
194
+ if let content = self . content, !content. isLive {
195
+ let image = self . drawPinOnSnapshot ( snapshot)
196
+ self . imageView. image = image
197
+ self . setCachedSnapshot ( image: image)
198
+ } else {
199
+ self . imageView. image = snapshot. image
200
+ self . setCachedSnapshot ( image: snapshot. image)
201
+ }
202
+
203
+ self . updateAnnotationView ( )
167
204
}
168
205
}
169
206
}
170
207
171
- private func generatePinAnnotation(
172
- for snapshot: MKMapSnapshotter . Snapshot
173
- ) -> UIImage {
174
- let image = UIGraphicsImageRenderer ( size: mapOptions. size) . image { _ in
208
+ private func drawPinOnSnapshot( _ snapshot: MKMapSnapshotter . Snapshot ) -> UIImage {
209
+ UIGraphicsImageRenderer ( size: mapOptions. size) . image { _ in
175
210
snapshot. image. draw ( at: . zero)
211
+
212
+ guard let content = self . content else { return }
176
213
177
214
let pinView = MKPinAnnotationView ( annotation: nil , reuseIdentifier: nil )
178
215
let pinImage = pinView. image
179
-
180
- guard let content = self . content else {
181
- return
182
- }
183
-
184
- var point = snapshot. point ( for: CLLocationCoordinate2D (
185
- latitude: content. latitude,
186
- longitude: content. longitude
187
- ) )
216
+
217
+ var point = snapshot. point ( for: content. coordinate)
188
218
point. x -= pinView. bounds. width / 2
189
219
point. y -= pinView. bounds. height / 2
190
220
point. x += pinView. centerOffset. x
191
221
point. y += pinView. centerOffset. y
222
+
192
223
pinImage? . draw ( at: point)
193
224
}
194
- return image
225
+ }
226
+
227
+ private func updateAnnotationView( ) {
228
+ guard let content = self . content else { return }
229
+
230
+ if content. isLive, let user = content. author {
231
+ avatarView. isHidden = false
232
+ avatarView. content = user
233
+ } else {
234
+ avatarView. isHidden = true
235
+ }
195
236
}
196
237
197
238
@objc func handleStopButtonTap( ) {
0 commit comments