From 31bb9eaac8adf4454d33849f946c7212d9e24012 Mon Sep 17 00:00:00 2001
From: Maxime NATUREL <maxime.naturel@niji.fr>
Date: Wed, 15 Jun 2022 14:34:13 +0200
Subject: [PATCH] Forward error to UI in timeline screen

---
 .../features/home/room/detail/TimelineViewModel.kt |  4 ++++
 .../features/location/LocationSharingService.kt    | 14 ++++++++++----
 .../location/LocationSharingServiceConnection.kt   | 13 ++++++++++---
 .../location/live/map/LocationLiveMapViewModel.kt  |  4 ++++
 4 files changed, 28 insertions(+), 7 deletions(-)

diff --git a/vector/src/main/java/im/vector/app/features/home/room/detail/TimelineViewModel.kt b/vector/src/main/java/im/vector/app/features/home/room/detail/TimelineViewModel.kt
index 07b20b4914..99a01211c3 100644
--- a/vector/src/main/java/im/vector/app/features/home/room/detail/TimelineViewModel.kt
+++ b/vector/src/main/java/im/vector/app/features/home/room/detail/TimelineViewModel.kt
@@ -1293,6 +1293,10 @@ class TimelineViewModel @AssistedInject constructor(
         locationSharingServiceConnection.bind(this)
     }
 
+    override fun onLocationServiceError(error: Throwable) {
+        _viewEvents.post(RoomDetailViewEvents.Failure(error))
+    }
+
     override fun onCleared() {
         timeline.dispose()
         timeline.removeAllListeners()
diff --git a/vector/src/main/java/im/vector/app/features/location/LocationSharingService.kt b/vector/src/main/java/im/vector/app/features/location/LocationSharingService.kt
index 27eea498e4..4a15a9d643 100644
--- a/vector/src/main/java/im/vector/app/features/location/LocationSharingService.kt
+++ b/vector/src/main/java/im/vector/app/features/location/LocationSharingService.kt
@@ -55,8 +55,9 @@ class LocationSharingService : VectorService(), LocationTracker.Callback {
     /**
      * Keep track of a map between beacon event Id starting the live and RoomArgs.
      */
-    private var roomArgsMap = mutableMapOf<String, RoomArgs>()
-    private var timers = mutableListOf<Timer>()
+    private val roomArgsMap = mutableMapOf<String, RoomArgs>()
+    private val timers = mutableListOf<Timer>()
+    var callback: Callback? = null
 
     override fun onCreate() {
         super.onCreate()
@@ -103,6 +104,7 @@ class LocationSharingService : VectorService(), LocationTracker.Callback {
                             locationTracker.requestLastKnownLocation()
                         }
                         is UpdateLiveLocationShareResult.Failure -> {
+                            callback?.onServiceError(result.error)
                             tryToDestroyMe()
                         }
                     }
@@ -132,8 +134,7 @@ class LocationSharingService : VectorService(), LocationTracker.Callback {
         Timber.i("### LocationSharingService.stopSharingLocation for $roomId")
 
         launchInIO { session ->
-            // Send a new beacon info state by setting live field as false
-            when (sendStoppedBeaconInfo(session, roomId)) {
+            when (val result = sendStoppedBeaconInfo(session, roomId)) {
                 is UpdateLiveLocationShareResult.Success -> {
                     synchronized(roomArgsMap) {
                         val beaconIds = roomArgsMap
@@ -144,6 +145,7 @@ class LocationSharingService : VectorService(), LocationTracker.Callback {
                         tryToDestroyMe()
                     }
                 }
+                is UpdateLiveLocationShareResult.Failure -> callback?.onServiceError(result.error)
                 else -> Unit
             }
         }
@@ -224,6 +226,10 @@ class LocationSharingService : VectorService(), LocationTracker.Callback {
         fun getService(): LocationSharingService = this@LocationSharingService
     }
 
+    interface Callback {
+        fun onServiceError(error: Throwable)
+    }
+
     companion object {
         const val EXTRA_ROOM_ARGS = "EXTRA_ROOM_ARGS"
     }
diff --git a/vector/src/main/java/im/vector/app/features/location/LocationSharingServiceConnection.kt b/vector/src/main/java/im/vector/app/features/location/LocationSharingServiceConnection.kt
index 97f447ec4b..af09e0b1e0 100644
--- a/vector/src/main/java/im/vector/app/features/location/LocationSharingServiceConnection.kt
+++ b/vector/src/main/java/im/vector/app/features/location/LocationSharingServiceConnection.kt
@@ -25,12 +25,12 @@ import javax.inject.Inject
 
 class LocationSharingServiceConnection @Inject constructor(
         private val context: Context
-) : ServiceConnection {
+) : ServiceConnection, LocationSharingService.Callback {
 
     interface Callback {
-        // TODO add onLocationServiceError()
         fun onLocationServiceRunning()
         fun onLocationServiceStopped()
+        fun onLocationServiceError(error: Throwable)
     }
 
     private var callback: Callback? = null
@@ -58,14 +58,21 @@ class LocationSharingServiceConnection @Inject constructor(
     }
 
     override fun onServiceConnected(className: ComponentName, binder: IBinder) {
-        locationSharingService = (binder as LocationSharingService.LocalBinder).getService()
+        locationSharingService = (binder as LocationSharingService.LocalBinder).getService().also {
+            it.callback = this
+        }
         isBound = true
         callback?.onLocationServiceRunning()
     }
 
     override fun onServiceDisconnected(className: ComponentName) {
         isBound = false
+        locationSharingService?.callback = null
         locationSharingService = null
         callback?.onLocationServiceStopped()
     }
+
+    override fun onServiceError(error: Throwable) {
+        callback?.onLocationServiceError(error)
+    }
 }
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 eb5bccff0f..9ef6449ea0 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
@@ -80,4 +80,8 @@ class LocationLiveMapViewModel @AssistedInject constructor(
     override fun onLocationServiceStopped() {
         // NOOP
     }
+
+    override fun onLocationServiceError(error: Throwable) {
+        // TODO
+    }
 }