From 2eaf84303169215e6492c05a1b25354374f71313 Mon Sep 17 00:00:00 2001 From: Onuray Sahin Date: Fri, 27 May 2022 16:52:20 +0300 Subject: [PATCH] Implement stop sharing function from bottom sheet. --- .../map/LiveLocationBottomSheetController.kt | 8 ++++++++ .../live/map/LocationLiveMapAction.kt | 1 + .../live/map/LocationLiveMapViewFragment.kt | 10 ++++++++++ .../live/map/LocationLiveMapViewModel.kt | 20 +++++++++++++++++-- 4 files changed, 37 insertions(+), 2 deletions(-) diff --git a/vector/src/main/java/im/vector/app/features/location/live/map/LiveLocationBottomSheetController.kt b/vector/src/main/java/im/vector/app/features/location/live/map/LiveLocationBottomSheetController.kt index 0136dbfff8..9e4d48ff01 100644 --- a/vector/src/main/java/im/vector/app/features/location/live/map/LiveLocationBottomSheetController.kt +++ b/vector/src/main/java/im/vector/app/features/location/live/map/LiveLocationBottomSheetController.kt @@ -26,6 +26,7 @@ import im.vector.app.core.resources.StringProvider import im.vector.app.core.resources.toTimestamp import im.vector.app.core.time.Clock import im.vector.app.features.home.AvatarRenderer +import im.vector.app.features.location.live.map.bottomsheet.LiveLocationUserItem import im.vector.app.features.location.live.map.bottomsheet.liveLocationUserItem import javax.inject.Inject @@ -54,10 +55,17 @@ class LiveLocationBottomSheetController @Inject constructor( val currentUserLocations = userLocations ?: return val host = this + val userItemCallback = object : LiveLocationUserItem.Callback { + override fun onStopSharingClicked() { + host.callback?.onStopLocationClicked() + } + } + currentUserLocations.forEach { liveLocationViewState -> val remainingTime = getFormattedLocalTimeEndOfLive(liveLocationViewState.endOfLiveTimestampMillis) liveLocationUserItem { id(liveLocationViewState.matrixItem.id) + callback(userItemCallback) matrixItem(liveLocationViewState.matrixItem) stringProvider(host.stringProvider) clock(host.clock) diff --git a/vector/src/main/java/im/vector/app/features/location/live/map/LocationLiveMapAction.kt b/vector/src/main/java/im/vector/app/features/location/live/map/LocationLiveMapAction.kt index 16cd3badc6..b88fffc733 100644 --- a/vector/src/main/java/im/vector/app/features/location/live/map/LocationLiveMapAction.kt +++ b/vector/src/main/java/im/vector/app/features/location/live/map/LocationLiveMapAction.kt @@ -21,4 +21,5 @@ import im.vector.app.core.platform.VectorViewModelAction sealed class LocationLiveMapAction : VectorViewModelAction { data class AddMapSymbol(val key: String, val value: Long) : LocationLiveMapAction() data class RemoveMapSymbol(val key: String) : LocationLiveMapAction() + object StopSharing : LocationLiveMapAction() } diff --git a/vector/src/main/java/im/vector/app/features/location/live/map/LocationLiveMapViewFragment.kt b/vector/src/main/java/im/vector/app/features/location/live/map/LocationLiveMapViewFragment.kt index 95328201ac..edfb967f45 100644 --- a/vector/src/main/java/im/vector/app/features/location/live/map/LocationLiveMapViewFragment.kt +++ b/vector/src/main/java/im/vector/app/features/location/live/map/LocationLiveMapViewFragment.kt @@ -71,6 +71,16 @@ class LocationLiveMapViewFragment @Inject constructor( override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) views.bottomSheetRecyclerView.configureWith(bottomSheetController, hasFixedSize = false, disableItemAnimation = true) + + bottomSheetController.callback = object : LiveLocationBottomSheetController.Callback { + override fun onUserSelected(userId: String) { + + } + + override fun onStopLocationClicked() { + viewModel.handle(LocationLiveMapAction.StopSharing) + } + } } override fun onResume() { diff --git a/vector/src/main/java/im/vector/app/features/location/live/map/LocationLiveMapViewModel.kt b/vector/src/main/java/im/vector/app/features/location/live/map/LocationLiveMapViewModel.kt index b14feea667..64802c7090 100644 --- a/vector/src/main/java/im/vector/app/features/location/live/map/LocationLiveMapViewModel.kt +++ b/vector/src/main/java/im/vector/app/features/location/live/map/LocationLiveMapViewModel.kt @@ -23,13 +23,15 @@ import dagger.assisted.AssistedInject import im.vector.app.core.di.MavericksAssistedViewModelFactory import im.vector.app.core.di.hiltMavericksViewModelFactory import im.vector.app.core.platform.VectorViewModel +import im.vector.app.features.location.LocationSharingServiceConnection import kotlinx.coroutines.flow.launchIn import kotlinx.coroutines.flow.onEach class LocationLiveMapViewModel @AssistedInject constructor( @Assisted private val initialState: LocationLiveMapViewState, - getListOfUserLiveLocationUseCase: GetListOfUserLiveLocationUseCase -) : VectorViewModel(initialState) { + getListOfUserLiveLocationUseCase: GetListOfUserLiveLocationUseCase, + private val locationSharingServiceConnection: LocationSharingServiceConnection, +) : VectorViewModel(initialState), LocationSharingServiceConnection.Callback { @AssistedFactory interface Factory : MavericksAssistedViewModelFactory { @@ -42,12 +44,14 @@ class LocationLiveMapViewModel @AssistedInject constructor( getListOfUserLiveLocationUseCase.execute(initialState.roomId) .onEach { setState { copy(userLocations = it) } } .launchIn(viewModelScope) + locationSharingServiceConnection.bind(this) } override fun handle(action: LocationLiveMapAction) { when (action) { is LocationLiveMapAction.AddMapSymbol -> handleAddMapSymbol(action) is LocationLiveMapAction.RemoveMapSymbol -> handleRemoveMapSymbol(action) + LocationLiveMapAction.StopSharing -> handleStopSharing() } } @@ -64,4 +68,16 @@ class LocationLiveMapViewModel @AssistedInject constructor( copy(mapSymbolIds = newMapSymbolIds) } } + + private fun handleStopSharing() { + locationSharingServiceConnection.stopLiveLocationSharing(initialState.roomId) + } + + override fun onLocationServiceRunning() { + // NOOP + } + + override fun onLocationServiceStopped() { + // NOOP + } }