Android: Refactor MainPresenter.installWad

Also replacing a toast with a dialog so that you have proper
time to read the message.
This commit is contained in:
JosJuice 2021-01-17 22:18:31 +01:00
parent 4a394ffc9c
commit b08306d0b8
2 changed files with 29 additions and 22 deletions

View File

@ -7,7 +7,6 @@ import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.IntentFilter; import android.content.IntentFilter;
import android.net.Uri; import android.net.Uri;
import android.widget.Toast;
import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AlertDialog;
import androidx.localbroadcastmanager.content.LocalBroadcastManager; import androidx.localbroadcastmanager.content.LocalBroadcastManager;
@ -24,6 +23,7 @@ import org.dolphinemu.dolphinemu.utils.FileBrowserHelper;
import org.dolphinemu.dolphinemu.utils.WiiUtils; import org.dolphinemu.dolphinemu.utils.WiiUtils;
import java.util.Arrays; import java.util.Arrays;
import java.util.function.Supplier;
public final class MainPresenter public final class MainPresenter
{ {
@ -150,32 +150,38 @@ public final class MainPresenter
mDirToAdd = uri.toString(); mDirToAdd = uri.toString();
} }
public void installWAD(String file) public void installWAD(String path)
{
runOnThreadAndShowResult(R.string.import_in_progress, () ->
{
boolean success = WiiUtils.installWAD(path);
int message = success ? R.string.wad_install_success : R.string.wad_install_failure;
return mContext.getResources().getString(message);
});
}
private void runOnThreadAndShowResult(int progressMessage, Supplier<String> f)
{ {
final Activity mainPresenterActivity = (Activity) mContext; final Activity mainPresenterActivity = (Activity) mContext;
AlertDialog dialog = new AlertDialog.Builder(mContext, R.style.DolphinDialogBase).create(); AlertDialog progressDialog = new AlertDialog.Builder(mContext, R.style.DolphinDialogBase)
dialog.setTitle("Installing WAD"); .create();
dialog.setMessage("Installing..."); progressDialog.setTitle(progressMessage);
dialog.setCancelable(false); progressDialog.setCancelable(false);
dialog.show(); progressDialog.show();
Thread installWADThread = new Thread(() -> new Thread(() ->
{ {
if (WiiUtils.installWAD(file)) String result = f.get();
mainPresenterActivity.runOnUiThread(() ->
{ {
mainPresenterActivity.runOnUiThread( progressDialog.dismiss();
() -> Toast.makeText(mContext, R.string.wad_install_success, Toast.LENGTH_SHORT)
.show()); AlertDialog.Builder builder = new AlertDialog.Builder(mContext, R.style.DolphinDialogBase);
} builder.setMessage(result);
else builder.setPositiveButton(R.string.ok, (dialog, i) -> dialog.dismiss());
{ builder.show();
mainPresenterActivity.runOnUiThread( });
() -> Toast.makeText(mContext, R.string.wad_install_failure, Toast.LENGTH_SHORT) }, mContext.getResources().getString(progressMessage)).start();
.show());
}
mainPresenterActivity.runOnUiThread(dialog::dismiss);
}, "InstallWAD");
installWADThread.start();
} }
} }

View File

@ -329,6 +329,7 @@
<string name="grid_menu_refresh">Refresh Library</string> <string name="grid_menu_refresh">Refresh Library</string>
<string name="grid_menu_open_file">Open File</string> <string name="grid_menu_open_file">Open File</string>
<string name="grid_menu_install_wad">Install WAD</string> <string name="grid_menu_install_wad">Install WAD</string>
<string name="import_in_progress">Importing...</string>
<string name="wad_install_success">Successfully installed this title to the NAND.</string> <string name="wad_install_success">Successfully installed this title to the NAND.</string>
<string name="wad_install_failure">Failed to install this title to the NAND.</string> <string name="wad_install_failure">Failed to install this title to the NAND.</string>