Android: Convert ThreadUtil to Kotlin

This commit is contained in:
Charles Lombardo 2023-02-28 22:21:18 -05:00
parent 8d16aed581
commit 8d1cf14565
2 changed files with 43 additions and 61 deletions

View File

@ -1,61 +0,0 @@
// SPDX-License-Identifier: GPL-2.0-or-later
package org.dolphinemu.dolphinemu.utils;
import android.app.Activity;
import android.content.DialogInterface;
import android.content.res.Resources;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import com.google.android.material.dialog.MaterialAlertDialogBuilder;
import org.dolphinemu.dolphinemu.R;
import java.util.function.Supplier;
public class ThreadUtil
{
public static void runOnThreadAndShowResult(Activity activity, int progressTitle,
int progressMessage, @NonNull Supplier<String> f)
{
runOnThreadAndShowResult(activity, progressTitle, progressMessage, f, null);
}
public static void runOnThreadAndShowResult(Activity activity, int progressTitle,
int progressMessage, @NonNull Supplier<String> f,
@Nullable DialogInterface.OnDismissListener onResultDismiss)
{
Resources resources = activity.getResources();
AlertDialog progressDialog = new MaterialAlertDialogBuilder(activity)
.setTitle(progressTitle)
.setView(R.layout.dialog_indeterminate_progress)
.setCancelable(false)
.create();
if (progressMessage != 0)
progressDialog.setMessage(resources.getString(progressMessage));
progressDialog.show();
new Thread(() ->
{
String result = f.get();
activity.runOnUiThread(() ->
{
progressDialog.dismiss();
if (result != null)
{
new MaterialAlertDialogBuilder(activity)
.setMessage(result)
.setPositiveButton(R.string.ok, (dialog, i) -> dialog.dismiss())
.setOnDismissListener(onResultDismiss)
.show();
}
});
}, resources.getString(progressTitle)).start();
}
}

View File

@ -0,0 +1,43 @@
// SPDX-License-Identifier: GPL-2.0-or-later
package org.dolphinemu.dolphinemu.utils
import android.app.Activity
import android.content.DialogInterface
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import org.dolphinemu.dolphinemu.R
import java.util.function.Supplier
object ThreadUtil {
@JvmStatic
@JvmOverloads
fun runOnThreadAndShowResult(
activity: Activity,
progressTitle: Int,
progressMessage: Int,
f: Supplier<String?>,
onResultDismiss: DialogInterface.OnDismissListener? = null
) {
val resources = activity.resources
val progressDialog = MaterialAlertDialogBuilder(activity)
.setTitle(progressTitle)
.setView(R.layout.dialog_indeterminate_progress)
.setCancelable(false)
.create()
if (progressMessage != 0) progressDialog.setMessage(resources.getString(progressMessage))
progressDialog.show()
Thread({
val result = f.get()
activity.runOnUiThread {
progressDialog.dismiss()
if (result != null) {
MaterialAlertDialogBuilder(activity)
.setMessage(result)
.setPositiveButton(R.string.ok) { dialog: DialogInterface, _: Int -> dialog.dismiss() }
.setOnDismissListener(onResultDismiss)
.show()
}
}
}, resources.getString(progressTitle)).start()
}
}