mirror of
https://github.com/dolphin-emu/dolphin.git
synced 2026-03-18 19:32:41 +00:00
Merge pull request #14426 from Simonx22/android/convert-afterdirectoryinitializationrunner-to-kotlin
Android: Convert AfterDirectoryInitializationRunner to Kotlin
This commit is contained in:
commit
ff84a4b07a
@ -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<DirectoryInitializationState> 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<DirectoryInitializationState> createObserver(Runnable runnable)
|
||||
{
|
||||
return (state) ->
|
||||
{
|
||||
if (state == DirectoryInitializationState.DOLPHIN_DIRECTORIES_INITIALIZED)
|
||||
{
|
||||
cancel();
|
||||
runnable.run();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public void cancel()
|
||||
{
|
||||
DirectoryInitialization.getDolphinDirectoriesState().removeObserver(mObserver);
|
||||
}
|
||||
}
|
||||
@ -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<DirectoryInitializationState>? = 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<DirectoryInitializationState> {
|
||||
return Observer { state ->
|
||||
if (state == DirectoryInitializationState.DOLPHIN_DIRECTORIES_INITIALIZED) {
|
||||
cancel()
|
||||
runnable.run()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun cancel() {
|
||||
observer?.let {
|
||||
DirectoryInitialization.getDolphinDirectoriesState().removeObserver(it)
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user