Skip to content

Commit 1fd210d

Browse files
committed
FIx bottom sheet logic
1 parent 5a8c084 commit 1fd210d

File tree

2 files changed

+68
-23
lines changed

2 files changed

+68
-23
lines changed

DemoApp/StreamChat/Components/CustomAttachments/LocationAttachment/LocationDetailViewController.swift

Lines changed: 66 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ class LocationDetailViewController: UIViewController, ThemeProvider {
3131
return view
3232
}()
3333

34+
var isLiveLocationAttachment: Bool {
35+
messageController.message?.liveLocationAttachments.first != nil
36+
}
37+
3438
override func viewDidLoad() {
3539
super.viewDidLoad()
3640

@@ -39,7 +43,7 @@ class LocationDetailViewController: UIViewController, ThemeProvider {
3943

4044
mapView.register(
4145
UserAnnotationView.self,
42-
forAnnotationViewWithReuseIdentifier: "UserAnnotation"
46+
forAnnotationViewWithReuseIdentifier: UserAnnotationView.reuseIdentifier
4347
)
4448
mapView.showsUserLocation = false
4549
mapView.delegate = self
@@ -82,15 +86,15 @@ class LocationDetailViewController: UIViewController, ThemeProvider {
8286
override func viewDidAppear(_ animated: Bool) {
8387
super.viewDidAppear(animated)
8488

85-
setupBottomSheet()
89+
presentLocationControlSheet()
8690
}
8791

8892
func updateUserLocation(
8993
_ coordinate: CLLocationCoordinate2D
9094
) {
9195
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.
9498
UIView.animate(withDuration: 5) {
9599
existingAnnotation.coordinate = coordinate
96100
}
@@ -108,20 +112,23 @@ class LocationDetailViewController: UIViewController, ThemeProvider {
108112
}
109113
}
110114

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+
)
115122
)
116-
bottomSheet.modalPresentationStyle = .pageSheet
123+
locationControlSheet.modalPresentationStyle = .pageSheet
117124
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)
125132
}
126133
}
127134
}
@@ -143,6 +150,12 @@ extension LocationDetailViewController: ChatMessageControllerDelegate {
143150
updateUserLocation(
144151
locationCoordinate
145152
)
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+
}
146159
}
147160
}
148161

@@ -155,9 +168,8 @@ extension LocationDetailViewController: MKMapViewDelegate {
155168
return nil
156169
}
157170

158-
let identifier = "UserAnnotation"
159171
let annotationView = mapView.dequeueReusableAnnotationView(
160-
withIdentifier: identifier,
172+
withIdentifier: UserAnnotationView.reuseIdentifier,
161173
for: userAnnotation
162174
) as? UserAnnotationView
163175

@@ -174,7 +186,7 @@ extension LocationDetailViewController: MKMapViewDelegate {
174186
}
175187
}
176188

177-
class LocationBottomSheetViewController: UIViewController, ThemeProvider {
189+
class LocationControlSheetViewController: UIViewController, ThemeProvider {
178190
let messageController: ChatMessageController
179191

180192
init(
@@ -195,7 +207,6 @@ class LocationBottomSheetViewController: UIViewController, ThemeProvider {
195207

196208
lazy var locationUpdateLabel: UILabel = {
197209
let label = UILabel()
198-
label.text = "Location updated 5 minutes ago"
199210
label.font = appearance.fonts.footnote
200211
label.textColor = appearance.colorPalette.subtitleText
201212
return label
@@ -209,6 +220,9 @@ class LocationBottomSheetViewController: UIViewController, ThemeProvider {
209220
override func viewDidLoad() {
210221
super.viewDidLoad()
211222

223+
messageController.synchronize()
224+
messageController.delegate = self
225+
212226
view.backgroundColor = appearance.colorPalette.background6
213227

214228
let container = VContainer(spacing: 2, alignment: .center) {
@@ -228,3 +242,35 @@ class LocationBottomSheetViewController: UIViewController, ThemeProvider {
228242
messageController.stopLiveLocationSharing()
229243
}
230244
}
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+
}

DemoApp/StreamChat/Components/CustomAttachments/LocationAttachment/UserAnnotationView.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import StreamChat
77
import StreamChatUI
88

99
class UserAnnotationView: MKAnnotationView {
10+
static let reuseIdentifier = "UserAnnotationView"
11+
1012
private lazy var avatarView: ChatUserAvatarView = {
1113
let view = ChatUserAvatarView()
1214
view.shouldShowOnlineIndicator = false
@@ -74,9 +76,6 @@ class UserAnnotationView: MKAnnotationView {
7476
}
7577

7678
func stopPulsingAnimation() {
77-
guard pulseLayer != nil else {
78-
return
79-
}
8079
pulseLayer?.removeFromSuperlayer()
8180
pulseLayer = nil
8281
}

0 commit comments

Comments
 (0)