diff --git a/Ring/Ring/Features/Conversations/Conversation/Cells/MessageCellLocationSharing.swift b/Ring/Ring/Features/Conversations/Conversation/Cells/MessageCellLocationSharing.swift index 88aee01ea72db602ab5c869d281f19ea2645e457..e176261697f1749720e606491fcb7a38422222eb 100644 --- a/Ring/Ring/Features/Conversations/Conversation/Cells/MessageCellLocationSharing.swift +++ b/Ring/Ring/Features/Conversations/Conversation/Cells/MessageCellLocationSharing.swift @@ -24,6 +24,8 @@ import RxCocoa class MessageCellLocationSharing: MessageCell { + typealias MarkerAndComponentObject = (marker: MaplyScreenMarker, componentObject: MaplyComponentObject?) + private static let osmCopyrightAndLicenseURL = "https://www.openstreetmap.org/copyright" private static let remoteTileSourceBaseUrl = MessageCellLocationSharing.getBaseURL() @@ -159,9 +161,11 @@ extension MessageCellLocationSharing { imageData: Data?, username: String?, marker: MaplyScreenMarker, - markerDump: MaplyComponentObject?) -> MaplyComponentObject? { + markerDump: MaplyComponentObject?, + tryToAnimateToMarker: Bool = true) -> MaplyComponentObject? { // only the first time - if markerDump != nil { + if markerDump == nil { + marker.layoutImportance = MAXFLOAT if let imageData = imageData, let circledImage = UIImage(data: imageData)?.circleMasked { marker.image = circledImage } else { @@ -183,7 +187,7 @@ extension MessageCellLocationSharing { } dumpToReturn = self.maplyViewController!.addScreenMarkers([marker], desc: nil) - if !locationTapped.value.1 { + if tryToAnimateToMarker && !locationTapped.value.1 { mapViewC.animate(toPosition: maplyCoordonate, time: 0.1) } } diff --git a/Ring/Ring/Features/Conversations/Conversation/Cells/MessageCellLocationSharingReceived.swift b/Ring/Ring/Features/Conversations/Conversation/Cells/MessageCellLocationSharingReceived.swift index b6abef26cbdca5d99d8532ba8991df6acbf6b53f..28803428f45f4f22d97bc14789618ce9a5349eb0 100644 --- a/Ring/Ring/Features/Conversations/Conversation/Cells/MessageCellLocationSharingReceived.swift +++ b/Ring/Ring/Features/Conversations/Conversation/Cells/MessageCellLocationSharingReceived.swift @@ -23,8 +23,10 @@ import Reusable class MessageCellLocationSharingReceived: MessageCellLocationSharing { - private var myContactsLocationMarker = MaplyScreenMarker() - private var markerComponentObject: MaplyComponentObject? + /// Primary location + private var myContactsLocation: MarkerAndComponentObject = (marker: MaplyScreenMarker(), componentObject: nil) + /// Secondary location + private var myLocation: MarkerAndComponentObject = (marker: MaplyScreenMarker(), componentObject: nil) @IBOutlet weak var receivedBubbleLeading: NSLayoutConstraint! @IBOutlet weak var receivedBubbleTrailling: NSLayoutConstraint! @@ -32,15 +34,36 @@ class MessageCellLocationSharingReceived: MessageCellLocationSharing { override func configureFromItem(_ conversationViewModel: ConversationViewModel, _ items: [MessageViewModel]?, cellForRowAt indexPath: IndexPath) { super.configureFromItem(conversationViewModel, items, cellForRowAt: indexPath) + // Primary location conversationViewModel.myContactsLocation .subscribe(onNext: { [weak self, weak conversationViewModel] location in guard let self = self, let location = location else { return } - self.markerComponentObject = self.updateLocationAndMarker(location: location, - imageData: conversationViewModel?.profileImageData.value, - username: conversationViewModel?.userName.value, - marker: self.myContactsLocationMarker, - markerDump: self.markerComponentObject) + self.myContactsLocation.componentObject = self.updateLocationAndMarker(location: location, + imageData: conversationViewModel?.profileImageData.value, + username: conversationViewModel?.userName.value, + marker: self.myContactsLocation.marker, + markerDump: self.myContactsLocation.componentObject) + }) + .disposed(by: self.disposeBag) + + // Secondary location + conversationViewModel.myLocation + .subscribe(onNext: { [weak self, weak conversationViewModel] location in + guard let self = self else { return } + + if let location = location?.coordinate { + self.myLocation.componentObject = self.updateLocationAndMarker(location: location, + imageData: conversationViewModel?.myOwnProfileImageData, + username: conversationViewModel?.userName.value, + marker: self.myLocation.marker, + markerDump: self.myLocation.componentObject, + tryToAnimateToMarker: false) + } else if let componentObject = self.myLocation.componentObject, + let maplyViewController = self.maplyViewController { + maplyViewController.remove(componentObject) + self.myLocation.componentObject = nil + } }) .disposed(by: self.disposeBag) } diff --git a/Ring/Ring/Features/Conversations/Conversation/Cells/MessageCellLocationSharingSent.swift b/Ring/Ring/Features/Conversations/Conversation/Cells/MessageCellLocationSharingSent.swift index 78a3777ce27d04cb85d657a0f747f517478d71b2..7ca9332355b392f6367c76369f45b05cdaff4e2e 100644 --- a/Ring/Ring/Features/Conversations/Conversation/Cells/MessageCellLocationSharingSent.swift +++ b/Ring/Ring/Features/Conversations/Conversation/Cells/MessageCellLocationSharingSent.swift @@ -24,8 +24,10 @@ import RxCocoa class MessageCellLocationSharingSent: MessageCellLocationSharing { - private var myLocationMarker = MaplyScreenMarker() - private var markerComponentObject: MaplyComponentObject? + /// Primary location + private var myLocation: MarkerAndComponentObject = (marker: MaplyScreenMarker(), componentObject: nil) + /// Secondary location + private var myContactsLocation: MarkerAndComponentObject = (marker: MaplyScreenMarker(), componentObject: nil) @IBOutlet weak var sentBubbleLeading: NSLayoutConstraint! @@ -37,15 +39,36 @@ class MessageCellLocationSharingSent: MessageCellLocationSharing { override func configureFromItem(_ conversationViewModel: ConversationViewModel, _ items: [MessageViewModel]?, cellForRowAt indexPath: IndexPath) { super.configureFromItem(conversationViewModel, items, cellForRowAt: indexPath) + // Primary location conversationViewModel.myLocation .subscribe(onNext: { [weak self, weak conversationViewModel] location in guard let self = self, let location = location?.coordinate else { return } - self.markerComponentObject = self.updateLocationAndMarker(location: location, - imageData: conversationViewModel?.myOwnProfileImageData, - username: conversationViewModel?.userName.value, - marker: self.myLocationMarker, - markerDump: self.markerComponentObject) + self.myLocation.componentObject = self.updateLocationAndMarker(location: location, + imageData: conversationViewModel?.myOwnProfileImageData, + username: conversationViewModel?.userName.value, + marker: self.myLocation.marker, + markerDump: self.myLocation.componentObject) + }) + .disposed(by: self.disposeBag) + + // Secondary location + conversationViewModel.myContactsLocation + .subscribe(onNext: { [weak self, weak conversationViewModel] location in + guard let self = self else { return } + + if let location = location { + self.myContactsLocation.componentObject = self.updateLocationAndMarker(location: location, + imageData: conversationViewModel?.profileImageData.value, + username: conversationViewModel?.userName.value, + marker: self.myContactsLocation.marker, + markerDump: self.myContactsLocation.componentObject, + tryToAnimateToMarker: false) + } else if let componentObject = self.myContactsLocation.componentObject, + let maplyViewController = self.maplyViewController { + maplyViewController.remove(componentObject) + self.myContactsLocation.componentObject = nil + } }) .disposed(by: self.disposeBag) @@ -61,7 +84,7 @@ class MessageCellLocationSharingSent: MessageCellLocationSharing { override func myPositionButtonAction(sender: UIButton!) { if let mapViewC = self.maplyViewController as? MaplyViewController { - mapViewC.animate(toPosition: self.myLocationMarker.loc, time: 0.5) + mapViewC.animate(toPosition: self.myLocation.marker.loc, time: 0.5) } } diff --git a/Ring/Ring/Features/Conversations/Conversation/ConversationViewModel.swift b/Ring/Ring/Features/Conversations/Conversation/ConversationViewModel.swift index c791050cbc575b10334bf3526248166d6fca9201..79a41f7f8f9b77480593950a60c1459c491afdea 100644 --- a/Ring/Ring/Features/Conversations/Conversation/ConversationViewModel.swift +++ b/Ring/Ring/Features/Conversations/Conversation/ConversationViewModel.swift @@ -111,7 +111,8 @@ class ConversationViewModel: Stateable, ViewModel { self.locationSharingService .peerUriAndLocationReceived .subscribe(onNext: { [weak self] tuple in - guard let self = self, let peerUri = tuple.0, let coordinates = tuple.1, let conversation = self.conversation else { return } + guard let self = self, let peerUri = tuple.0, let conversation = self.conversation else { return } + let coordinates = tuple.1 if peerUri == conversation.value.participantUri { self.myContactsLocation.onNext(coordinates) } diff --git a/Ring/Ring/Services/LocationSharingService.swift b/Ring/Ring/Services/LocationSharingService.swift index 363c7edaecd67034f05ec8aacc80634bddfdaa23..f574db8f9bf547376fb4ffe94d7ca534a5f61364 100644 --- a/Ring/Ring/Services/LocationSharingService.swift +++ b/Ring/Ring/Services/LocationSharingService.swift @@ -250,6 +250,7 @@ extension LocationSharingService { if self.outgoingInstances.isEmpty { self.locationManager.stopUpdatingLocation() + self.currentLocation.accept(nil) } self.triggerDeleteLocation(accountId: accountId, peerUri: contactUri, incoming: false, shouldRefreshConversations: true) @@ -304,6 +305,8 @@ extension LocationSharingService { } func stopReceivingLocation(accountId: String, contactUri: String) { + self.peerUriAndLocationReceived.accept((contactUri, nil)) + self.triggerDeleteLocation(accountId: accountId, peerUri: contactUri, incoming: true, shouldRefreshConversations: true) _ = self.incomingInstances.remove(accountId, contactUri)