diff --git a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/utils/AfterDirectoryInitializationRunner.java b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/utils/AfterDirectoryInitializationRunner.java deleted file mode 100644 index 0312955156..0000000000 --- a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/utils/AfterDirectoryInitializationRunner.java +++ /dev/null @@ -1,73 +0,0 @@ -// SPDX-License-Identifier: GPL-2.0-or-later - -package org.dolphinemu.dolphinemu.utils; - -import androidx.lifecycle.LifecycleOwner; -import androidx.lifecycle.Observer; - -import org.dolphinemu.dolphinemu.utils.DirectoryInitialization.DirectoryInitializationState; - -public class AfterDirectoryInitializationRunner -{ - private Observer mObserver; - - /** - * Executes a Runnable once directory initialization finishes. - * - * If this is called when directory initialization already has finished, the Runnable will - * be executed immediately. If this is called before directory initialization has finished, - * the Runnable will be executed after directory initialization finishes. - * - * If the passed-in LifecycleOwner gets destroyed before this operation finishes, - * the operation will be automatically canceled. - */ - public void runWithLifecycle(LifecycleOwner lifecycleOwner, Runnable runnable) - { - if (DirectoryInitialization.areDolphinDirectoriesReady()) - { - runnable.run(); - } - else - { - mObserver = createObserver(runnable); - DirectoryInitialization.getDolphinDirectoriesState().observe(lifecycleOwner, mObserver); - } - } - - /** - * Executes a Runnable once directory initialization finishes. - * - * If this is called when directory initialization already has finished, the Runnable will - * be executed immediately. If this is called before directory initialization has finished, - * the Runnable will be executed after directory initialization finishes. - */ - public void runWithoutLifecycle(Runnable runnable) - { - if (DirectoryInitialization.areDolphinDirectoriesReady()) - { - runnable.run(); - } - else - { - mObserver = createObserver(runnable); - DirectoryInitialization.getDolphinDirectoriesState().observeForever(mObserver); - } - } - - private Observer createObserver(Runnable runnable) - { - return (state) -> - { - if (state == DirectoryInitializationState.DOLPHIN_DIRECTORIES_INITIALIZED) - { - cancel(); - runnable.run(); - } - }; - } - - public void cancel() - { - DirectoryInitialization.getDolphinDirectoriesState().removeObserver(mObserver); - } -} diff --git a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/utils/AfterDirectoryInitializationRunner.kt b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/utils/AfterDirectoryInitializationRunner.kt new file mode 100644 index 0000000000..bb6b1dafa9 --- /dev/null +++ b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/utils/AfterDirectoryInitializationRunner.kt @@ -0,0 +1,64 @@ +// SPDX-License-Identifier: GPL-2.0-or-later + +package org.dolphinemu.dolphinemu.utils + +import androidx.lifecycle.LifecycleOwner +import androidx.lifecycle.Observer +import org.dolphinemu.dolphinemu.utils.DirectoryInitialization.DirectoryInitializationState + +class AfterDirectoryInitializationRunner { + private var observer: Observer? = null + + /** + * Executes a Runnable once directory initialization finishes. + * + * If this is called when directory initialization already has finished, the Runnable will + * be executed immediately. If this is called before directory initialization has finished, + * the Runnable will be executed after directory initialization finishes. + * + * If the passed-in LifecycleOwner gets destroyed before this operation finishes, + * the operation will be automatically canceled. + */ + fun runWithLifecycle(lifecycleOwner: LifecycleOwner, runnable: Runnable) { + if (DirectoryInitialization.areDolphinDirectoriesReady()) { + runnable.run() + } else { + val newObserver = createObserver(runnable) + observer = newObserver + DirectoryInitialization.getDolphinDirectoriesState() + .observe(lifecycleOwner, newObserver) + } + } + + /** + * Executes a Runnable once directory initialization finishes. + * + * If this is called when directory initialization already has finished, the Runnable will + * be executed immediately. If this is called before directory initialization has finished, + * the Runnable will be executed after directory initialization finishes. + */ + fun runWithoutLifecycle(runnable: Runnable) { + if (DirectoryInitialization.areDolphinDirectoriesReady()) { + runnable.run() + } else { + val newObserver = createObserver(runnable) + observer = newObserver + DirectoryInitialization.getDolphinDirectoriesState().observeForever(newObserver) + } + } + + private fun createObserver(runnable: Runnable): Observer { + return Observer { state -> + if (state == DirectoryInitializationState.DOLPHIN_DIRECTORIES_INITIALIZED) { + cancel() + runnable.run() + } + } + } + + fun cancel() { + observer?.let { + DirectoryInitialization.getDolphinDirectoriesState().removeObserver(it) + } + } +}