Android: Refactor GamePropertiesDialog.onCreateDialog

This commit is contained in:
JosJuice 2020-06-27 12:20:58 +02:00
parent 5d13f3675f
commit 23ea47d4df
4 changed files with 91 additions and 68 deletions

View File

@ -16,6 +16,7 @@ import org.dolphinemu.dolphinemu.features.settings.ui.MenuTag;
import org.dolphinemu.dolphinemu.features.settings.ui.SettingsActivity;
import org.dolphinemu.dolphinemu.model.GameFile;
import org.dolphinemu.dolphinemu.ui.platform.Platform;
import org.dolphinemu.dolphinemu.utils.AlertDialogItemsBuilder;
import org.dolphinemu.dolphinemu.utils.DirectoryInitialization;
import org.dolphinemu.dolphinemu.utils.Log;
@ -47,62 +48,56 @@ public class GamePropertiesDialog extends DialogFragment
@Override
public Dialog onCreateDialog(Bundle savedInstanceState)
{
AlertDialog.Builder builder = new AlertDialog.Builder(requireContext(),
R.style.DolphinDialogBase);
String path = requireArguments().getString(ARG_PATH);
String gameId = requireArguments().getString(ARG_GAMEID);
int revision = requireArguments().getInt(ARG_REVISION);
int platform = requireArguments().getInt(ARG_PLATFORM);
AlertDialogItemsBuilder itemsBuilder = new AlertDialogItemsBuilder(requireContext());
itemsBuilder.add(R.string.properties_details, (dialog, i) ->
GameDetailsDialog.newInstance(path).show(requireActivity()
.getSupportFragmentManager(), "game_details"));
itemsBuilder.add(R.string.properties_convert, (dialog, i) ->
ConvertActivity.launch(getContext(), path));
itemsBuilder.add(R.string.properties_set_default_iso, (dialog, i) ->
{
try (Settings settings = new Settings())
{
settings.loadSettings(null);
StringSetting.MAIN_DEFAULT_ISO.setString(settings, path);
settings.saveSettings(null, getContext());
}
});
itemsBuilder.add(R.string.properties_core_settings, (dialog, i) ->
SettingsActivity.launch(getContext(), MenuTag.CONFIG, gameId, revision));
itemsBuilder.add(R.string.properties_gfx_settings, (dialog, i) ->
SettingsActivity.launch(getContext(), MenuTag.GRAPHICS, gameId, revision));
itemsBuilder.add(R.string.properties_gc_controller, (dialog, i) ->
SettingsActivity.launch(getContext(), MenuTag.GCPAD_TYPE, gameId, revision));
if (platform != Platform.GAMECUBE.toInt())
{
itemsBuilder.add(R.string.properties_wii_controller, (dialog, i) ->
SettingsActivity.launch(getActivity(), MenuTag.WIIMOTE, gameId, revision));
}
itemsBuilder.add(R.string.properties_clear_game_settings, (dialog, i) ->
clearGameSettings(gameId));
AlertDialog.Builder builder = new AlertDialog.Builder(requireContext(),
R.style.DolphinDialogBase);
itemsBuilder.applyToBuilder(builder);
builder.setTitle(requireContext()
.getString(R.string.preferences_game_properties) + ": " + gameId)
.setItems(platform == Platform.GAMECUBE.toInt() ?
R.array.gameSettingsMenusGC :
R.array.gameSettingsMenusWii, (dialog, which) ->
{
switch (which)
{
case 0:
GameDetailsDialog.newInstance(path).show((requireActivity())
.getSupportFragmentManager(), "game_details");
break;
case 1:
ConvertActivity.launch(getContext(), path);
break;
case 2:
try (Settings settings = new Settings())
{
settings.loadSettings(null);
StringSetting.MAIN_DEFAULT_ISO.setString(settings, path);
settings.saveSettings(null, getContext());
}
break;
case 3:
SettingsActivity.launch(getContext(), MenuTag.CONFIG, gameId, revision);
break;
case 4:
SettingsActivity.launch(getContext(), MenuTag.GRAPHICS, gameId, revision);
break;
case 5:
SettingsActivity.launch(getContext(), MenuTag.GCPAD_TYPE, gameId, revision);
break;
case 6:
// Clear option for GC, Wii controls for else
if (platform == Platform.GAMECUBE.toInt())
clearGameSettings(gameId);
else
SettingsActivity.launch(getActivity(), MenuTag.WIIMOTE, gameId, revision);
break;
case 7:
clearGameSettings(gameId);
break;
}
});
.getString(R.string.preferences_game_properties) + ": " + gameId);
return builder.create();
}
private void clearGameSettings(String gameId)
{
String gameSettingsPath =

View File

@ -0,0 +1,40 @@
package org.dolphinemu.dolphinemu.utils;
import androidx.appcompat.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface.OnClickListener;
import java.util.ArrayList;
public class AlertDialogItemsBuilder
{
private Context mContext;
private ArrayList<CharSequence> mLabels = new ArrayList<>();
private ArrayList<OnClickListener> mListeners = new ArrayList<>();
public AlertDialogItemsBuilder(Context context)
{
mContext = context;
}
public void add(int stringId, OnClickListener listener)
{
mLabels.add(mContext.getResources().getString(stringId));
mListeners.add(listener);
}
public void add(CharSequence label, OnClickListener listener)
{
mLabels.add(label);
mListeners.add(listener);
}
public void applyToBuilder(AlertDialog.Builder builder)
{
CharSequence[] labels = new CharSequence[mLabels.size()];
labels = mLabels.toArray(labels);
builder.setItems(labels, (dialog, i) -> mListeners.get(i).onClick(dialog, i));
}
}

View File

@ -385,26 +385,6 @@
<item>Classic A</item>
</string-array>
<string-array name="gameSettingsMenusGC">
<item>Details</item>
<item>Convert File</item>
<item>Set as Default ISO</item>
<item>Core Settings</item>
<item>GFX Settings</item>
<item>GameCube Controller Settings</item>
<item>Clear Game Settings</item>
</string-array>
<string-array name="gameSettingsMenusWii">
<item>Details</item>
<item>Convert File</item>
<item>Set as Default ISO</item>
<item>Core Settings</item>
<item>GFX Settings</item>
<item>GameCube Controller Settings</item>
<item>Wii Controller Settings</item>
<item>Clear Game Settings</item>
</string-array>
<string-array name="orientationEntries">
<item>Landscape</item>
<item>Portrait</item>

View File

@ -313,7 +313,15 @@
<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>
<!-- Preferences Screen -->
<!-- Game Properties Screen -->
<string name="properties_details">Details</string>
<string name="properties_convert">Convert File</string>
<string name="properties_set_default_iso">Set as Default ISO</string>
<string name="properties_core_settings">Core Settings</string>
<string name="properties_gfx_settings">GFX Settings</string>
<string name="properties_gc_controller">GameCube Controller Settings</string>
<string name="properties_wii_controller">Wii Controller Settings</string>
<string name="properties_clear_game_settings">Clear Game Settings</string>
<string name="preferences_save_exit">Save and Exit</string>
<string name="preferences_settings">Settings</string>
<string name="preferences_game_properties">Game Properties</string>