Android: Create separate InvertedCheckBoxSetting and PercentSliderSetting classes

This way we don't have to hardcode any keys inside the classes.
This commit is contained in:
JosJuice 2020-07-07 15:18:37 +02:00
parent c6a308380c
commit ee9444a8f9
5 changed files with 63 additions and 37 deletions

View File

@ -1,11 +1,10 @@
package org.dolphinemu.dolphinemu.features.settings.model.view;
import org.dolphinemu.dolphinemu.features.settings.model.Settings;
import org.dolphinemu.dolphinemu.features.settings.utils.SettingsFile;
public final class CheckBoxSetting extends SettingsItem
public class CheckBoxSetting extends SettingsItem
{
private boolean mDefaultValue;
protected boolean mDefaultValue;
public CheckBoxSetting(String file, String section, String key, int titleId, int descriptionId,
boolean defaultValue)
@ -16,24 +15,12 @@ public final class CheckBoxSetting extends SettingsItem
public boolean isChecked(Settings settings)
{
return invertIfNeeded(settings.getSection(getFile(), getSection())
.getBoolean(getKey(), invertIfNeeded(mDefaultValue)));
return settings.getSection(getFile(), getSection()).getBoolean(getKey(), mDefaultValue);
}
public void setChecked(Settings settings, boolean checked)
{
settings.getSection(getFile(), getSection()).setBoolean(getKey(), invertIfNeeded(checked));
}
private boolean invertIfNeeded(boolean x)
{
return isInverted() ? !x : x;
}
private boolean isInverted()
{
return getKey().equals(SettingsFile.KEY_SKIP_EFB) ||
getKey().equals(SettingsFile.KEY_IGNORE_FORMAT);
settings.getSection(getFile(), getSection()).setBoolean(getKey(), checked);
}
@Override

View File

@ -1,39 +1,26 @@
package org.dolphinemu.dolphinemu.features.settings.model.view;
import org.dolphinemu.dolphinemu.features.settings.model.Settings;
import org.dolphinemu.dolphinemu.features.settings.utils.SettingsFile;
public final class FloatSliderSetting extends SliderSetting
public class FloatSliderSetting extends SliderSetting
{
private float mDefaultValue;
protected float mDefaultValue;
public FloatSliderSetting(String file, String section, String key, int titleId, int descriptionId,
int max, String units, float defaultValue)
{
super(file, section, key, titleId, descriptionId, max, units);
mDefaultValue = defaultValue;
if (isPercentSetting())
mDefaultValue /= 100;
}
public int getSelectedValue(Settings settings)
{
float value = settings.getSection(getFile(), getSection()).getFloat(getKey(), mDefaultValue);
return Math.round(isPercentSetting() ? value * 100 : value);
return Math.round(value);
}
public void setSelectedValue(Settings settings, float selection)
{
if (isPercentSetting())
selection /= 100;
settings.getSection(getFile(), getSection()).setFloat(getKey(), selection);
}
private boolean isPercentSetting()
{
return getKey().equals(SettingsFile.KEY_OVERCLOCK_PERCENT)
|| getKey().equals(SettingsFile.KEY_SPEED_LIMIT);
}
}

View File

@ -0,0 +1,25 @@
package org.dolphinemu.dolphinemu.features.settings.model.view;
import org.dolphinemu.dolphinemu.features.settings.model.Settings;
import org.dolphinemu.dolphinemu.features.settings.utils.SettingsFile;
public final class InvertedCheckBoxSetting extends CheckBoxSetting
{
public InvertedCheckBoxSetting(String file, String section, String key, int titleId,
int descriptionId, boolean defaultValue)
{
super(file, section, key, titleId, descriptionId, !defaultValue);
}
@Override
public boolean isChecked(Settings settings)
{
return !settings.getSection(getFile(), getSection()).getBoolean(getKey(), mDefaultValue);
}
@Override
public void setChecked(Settings settings, boolean checked)
{
settings.getSection(getFile(), getSection()).setBoolean(getKey(), !checked);
}
}

View File

@ -0,0 +1,25 @@
package org.dolphinemu.dolphinemu.features.settings.model.view;
import org.dolphinemu.dolphinemu.features.settings.model.Settings;
public final class PercentSliderSetting extends FloatSliderSetting
{
public PercentSliderSetting(String file, String section, String key, int titleId,
int descriptionId, int max, String units, float defaultValue)
{
super(file, section, key, titleId, descriptionId, max, units, defaultValue / 100);
}
@Override
public int getSelectedValue(Settings settings)
{
float value = settings.getSection(getFile(), getSection()).getFloat(getKey(), mDefaultValue);
return Math.round(value * 100);
}
@Override
public void setSelectedValue(Settings settings, float selection)
{
settings.getSection(getFile(), getSection()).setFloat(getKey(), selection / 100);
}
}

View File

@ -13,6 +13,8 @@ import org.dolphinemu.dolphinemu.features.settings.model.view.FloatSliderSetting
import org.dolphinemu.dolphinemu.features.settings.model.view.HeaderSetting;
import org.dolphinemu.dolphinemu.features.settings.model.view.InputBindingSetting;
import org.dolphinemu.dolphinemu.features.settings.model.view.IntSliderSetting;
import org.dolphinemu.dolphinemu.features.settings.model.view.InvertedCheckBoxSetting;
import org.dolphinemu.dolphinemu.features.settings.model.view.PercentSliderSetting;
import org.dolphinemu.dolphinemu.features.settings.model.view.RumbleBindingSetting;
import org.dolphinemu.dolphinemu.features.settings.model.view.SettingsItem;
import org.dolphinemu.dolphinemu.features.settings.model.view.SingleChoiceSetting;
@ -226,7 +228,7 @@ public final class SettingsFragmentPresenter
false));
sl.add(new CheckBoxSetting(SettingsFile.FILE_NAME_DOLPHIN, Settings.SECTION_INI_CORE,
SettingsFile.KEY_AUTO_DISC_CHANGE, R.string.auto_disc_change, 0, false));
sl.add(new FloatSliderSetting(SettingsFile.FILE_NAME_DOLPHIN, Settings.SECTION_INI_CORE,
sl.add(new PercentSliderSetting(SettingsFile.FILE_NAME_DOLPHIN, Settings.SECTION_INI_CORE,
SettingsFile.KEY_SPEED_LIMIT, R.string.speed_limit, 0, 200, "%", 100));
sl.add(new CheckBoxSetting(SettingsFile.FILE_NAME_DOLPHIN, Settings.SECTION_ANALYTICS,
SettingsFile.KEY_ANALYTICS_ENABLED, R.string.analytics, 0, false));
@ -356,7 +358,7 @@ public final class SettingsFragmentPresenter
sl.add(new CheckBoxSetting(SettingsFile.FILE_NAME_DOLPHIN, Settings.SECTION_INI_CORE,
SettingsFile.KEY_OVERCLOCK_ENABLE, R.string.overclock_enable,
R.string.overclock_enable_description, false));
sl.add(new FloatSliderSetting(SettingsFile.FILE_NAME_DOLPHIN, Settings.SECTION_INI_CORE,
sl.add(new PercentSliderSetting(SettingsFile.FILE_NAME_DOLPHIN, Settings.SECTION_INI_CORE,
SettingsFile.KEY_OVERCLOCK_PERCENT, R.string.overclock_title,
R.string.overclock_title_description, 400, "%", 100));
}
@ -544,10 +546,10 @@ public final class SettingsFragmentPresenter
private void addHackSettings(ArrayList<SettingsItem> sl)
{
sl.add(new HeaderSetting(null, R.string.embedded_frame_buffer, 0));
sl.add(new CheckBoxSetting(SettingsFile.FILE_NAME_GFX, Settings.SECTION_GFX_HACKS,
sl.add(new InvertedCheckBoxSetting(SettingsFile.FILE_NAME_GFX, Settings.SECTION_GFX_HACKS,
SettingsFile.KEY_SKIP_EFB, R.string.skip_efb_access,
R.string.skip_efb_access_description, false));
sl.add(new CheckBoxSetting(SettingsFile.FILE_NAME_GFX, Settings.SECTION_GFX_HACKS,
sl.add(new InvertedCheckBoxSetting(SettingsFile.FILE_NAME_GFX, Settings.SECTION_GFX_HACKS,
SettingsFile.KEY_IGNORE_FORMAT, R.string.ignore_format_changes,
R.string.ignore_format_changes_description, true));
sl.add(new CheckBoxSetting(SettingsFile.FILE_NAME_GFX, Settings.SECTION_GFX_HACKS,