Эх сурвалжийг харах

Introduce DisposableSet to handle multiple disposables at once

This should be used in places where multiple Disposables are handled simultaneously as a simple way of
ensuring that all disposables are cleared on exit. For example on ViewModels and Controllers should likely use one
of these and clear it on their respective lifecycle end.

I've included an example use in ChatController, where it replaces an existing `ArrayList<Disposable>`, but the benefit
should be more visible in places where Disposables are currently handled separately.

Signed-off-by: Álvaro Brey <alvaro.brey@nextcloud.com>
Álvaro Brey 2 жил өмнө
parent
commit
a24f49c737

+ 10 - 16
app/src/main/java/com/nextcloud/talk/controllers/ChatController.kt

@@ -170,6 +170,7 @@ import com.nextcloud.talk.utils.bundle.BundleKeys.KEY_ROOM_TOKEN
 import com.nextcloud.talk.utils.bundle.BundleKeys.KEY_USER_ENTITY
 import com.nextcloud.talk.utils.database.user.UserUtils
 import com.nextcloud.talk.utils.permissions.PlatformPermissionUtil
+import com.nextcloud.talk.utils.rx.DisposableSet
 import com.nextcloud.talk.utils.singletons.ApplicationWideCurrentRoomHolder
 import com.nextcloud.talk.utils.text.Spans
 import com.nextcloud.talk.webrtc.MagicWebSocketInstance
@@ -235,7 +236,7 @@ class ChatController(args: Bundle) :
     @Inject
     lateinit var permissionUtil: PlatformPermissionUtil
 
-    val disposableList = ArrayList<Disposable>()
+    val disposables = DisposableSet()
 
     var roomToken: String? = null
     val conversationUser: UserEntity?
@@ -336,7 +337,7 @@ class ChatController(args: Bundle) :
                 ?.observeOn(AndroidSchedulers.mainThread())
                 ?.subscribe(object : Observer<RoomOverall> {
                     override fun onSubscribe(d: Disposable) {
-                        disposableList.add(d)
+                        disposables.add(d)
                     }
 
                     @Suppress("Detekt.TooGenericExceptionCaught")
@@ -402,7 +403,7 @@ class ChatController(args: Bundle) :
             ?.subscribeOn(Schedulers.io())?.observeOn(AndroidSchedulers.mainThread())
             ?.subscribe(object : Observer<RoomsOverall> {
                 override fun onSubscribe(d: Disposable) {
-                    disposableList.add(d)
+                    disposables.add(d)
                 }
 
                 override fun onNext(roomsOverall: RoomsOverall) {
@@ -1823,14 +1824,7 @@ class ChatController(args: Bundle) :
 
         adapter = null
         inConversation = false
-    }
-
-    private fun dispose() {
-        for (disposable in disposableList) {
-            if (!disposable.isDisposed()) {
-                disposable.dispose()
-            }
-        }
+        disposables.dispose()
     }
 
     private fun joinRoomWithPassword() {
@@ -1859,7 +1853,7 @@ class ChatController(args: Bundle) :
                 ?.retry(RETRIES)
                 ?.subscribe(object : Observer<RoomOverall> {
                     override fun onSubscribe(d: Disposable) {
-                        disposableList.add(d)
+                        disposables.add(d)
                     }
 
                     @Suppress("Detekt.TooGenericExceptionCaught")
@@ -1947,7 +1941,7 @@ class ChatController(args: Bundle) :
             ?.observeOn(AndroidSchedulers.mainThread())
             ?.subscribe(object : Observer<GenericOverall> {
                 override fun onSubscribe(d: Disposable) {
-                    disposableList.add(d)
+                    disposables.add(d)
                 }
 
                 override fun onNext(genericOverall: GenericOverall) {
@@ -1976,7 +1970,7 @@ class ChatController(args: Bundle) :
 
                 override fun onComplete() {
                     Log.d(TAG, "leaveRoom - leaveRoom - completed: " + startNanoTime)
-                    dispose()
+                    disposables.dispose()
                 }
             })
     }
@@ -2158,7 +2152,7 @@ class ChatController(args: Bundle) :
                 ?.observeOn(AndroidSchedulers.mainThread())
                 ?.subscribe(object : Observer<Response<*>> {
                     override fun onSubscribe(d: Disposable) {
-                        disposableList.add(d)
+                        disposables.add(d)
                     }
 
                     @Suppress("Detekt.TooGenericExceptionCaught")
@@ -2200,7 +2194,7 @@ class ChatController(args: Bundle) :
                 ?.observeOn(AndroidSchedulers.mainThread())
                 ?.subscribe(object : Observer<Response<*>> {
                     override fun onSubscribe(d: Disposable) {
-                        disposableList.add(d)
+                        disposables.add(d)
                     }
 
                     @Suppress("Detekt.TooGenericExceptionCaught")

+ 37 - 0
app/src/main/java/com/nextcloud/talk/utils/rx/DisposableSet.kt

@@ -0,0 +1,37 @@
+/*
+ * Nextcloud Talk application
+ *
+ * @author Álvaro Brey
+ * Copyright (C) 2022 Álvaro Brey
+ * Copyright (C) 2022 Nextcloud GmbH
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ */
+
+package com.nextcloud.talk.utils.rx
+
+import io.reactivex.disposables.Disposable
+
+class DisposableSet {
+    private val disposables = mutableSetOf<Disposable>()
+
+    fun add(disposable: Disposable) {
+        disposables.add(disposable)
+    }
+
+    fun dispose() {
+        disposables.forEach { it.dispose() }
+        disposables.clear()
+    }
+}