From a24f49c73785d5b618726bc7822831ef7a4a742e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81lvaro=20Brey?= Date: Wed, 29 Jun 2022 17:22:51 +0200 Subject: [PATCH] Introduce DisposableSet to handle multiple disposables at once MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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`, but the benefit should be more visible in places where Disposables are currently handled separately. Signed-off-by: Álvaro Brey --- .../talk/controllers/ChatController.kt | 26 +++++-------- .../nextcloud/talk/utils/rx/DisposableSet.kt | 37 +++++++++++++++++++ 2 files changed, 47 insertions(+), 16 deletions(-) create mode 100644 app/src/main/java/com/nextcloud/talk/utils/rx/DisposableSet.kt diff --git a/app/src/main/java/com/nextcloud/talk/controllers/ChatController.kt b/app/src/main/java/com/nextcloud/talk/controllers/ChatController.kt index 14edb69a5..4ffb93eec 100644 --- a/app/src/main/java/com/nextcloud/talk/controllers/ChatController.kt +++ b/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() + val disposables = DisposableSet() var roomToken: String? = null val conversationUser: UserEntity? @@ -336,7 +337,7 @@ class ChatController(args: Bundle) : ?.observeOn(AndroidSchedulers.mainThread()) ?.subscribe(object : Observer { 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 { 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 { 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 { 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> { 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> { override fun onSubscribe(d: Disposable) { - disposableList.add(d) + disposables.add(d) } @Suppress("Detekt.TooGenericExceptionCaught") diff --git a/app/src/main/java/com/nextcloud/talk/utils/rx/DisposableSet.kt b/app/src/main/java/com/nextcloud/talk/utils/rx/DisposableSet.kt new file mode 100644 index 000000000..e0bf7f497 --- /dev/null +++ b/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 . + */ + +package com.nextcloud.talk.utils.rx + +import io.reactivex.disposables.Disposable + +class DisposableSet { + private val disposables = mutableSetOf() + + fun add(disposable: Disposable) { + disposables.add(disposable) + } + + fun dispose() { + disposables.forEach { it.dispose() } + disposables.clear() + } +}