Permalink
Browse files

Android: Refactor the settings managemnt

1. Create Settings class the encaupslate the loading/saving of all settings
2. Decouple the logic of saving the settings into 3 different config files
from the UI code.
  • Loading branch information...
mahdihijazi committed Jul 23, 2018
1 parent 61835a7 commit c664bb7458659c71d7a69f5587f2c61fc7e10a20
Showing with 844 additions and 843 deletions.
  1. +2 −2 ...e/Android/app/src/main/java/org/dolphinemu/dolphinemu/features/settings/model/BooleanSetting.java
  2. +2 −2 Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/features/settings/model/FloatSetting.java
  3. +4 −4 Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/features/settings/model/IntSetting.java
  4. +1 −12 Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/features/settings/model/Setting.java
  5. +136 −0 Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/features/settings/model/Settings.java
  6. +2 −2 ...ce/Android/app/src/main/java/org/dolphinemu/dolphinemu/features/settings/model/StringSetting.java
  7. +3 −3 ...oid/app/src/main/java/org/dolphinemu/dolphinemu/features/settings/model/view/CheckBoxSetting.java
  8. +1 −1 ...droid/app/src/main/java/org/dolphinemu/dolphinemu/features/settings/model/view/HeaderSetting.java
  9. +3 −3 ...app/src/main/java/org/dolphinemu/dolphinemu/features/settings/model/view/InputBindingSetting.java
  10. +1 −11 ...ndroid/app/src/main/java/org/dolphinemu/dolphinemu/features/settings/model/view/SettingsItem.java
  11. +5 −5 ...app/src/main/java/org/dolphinemu/dolphinemu/features/settings/model/view/SingleChoiceSetting.java
  12. +5 −5 ...droid/app/src/main/java/org/dolphinemu/dolphinemu/features/settings/model/view/SliderSetting.java
  13. +3 −3 ...c/main/java/org/dolphinemu/dolphinemu/features/settings/model/view/StringSingleChoiceSetting.java
  14. +1 −1 ...roid/app/src/main/java/org/dolphinemu/dolphinemu/features/settings/model/view/SubmenuSetting.java
  15. +4 −5 ...ce/Android/app/src/main/java/org/dolphinemu/dolphinemu/features/settings/ui/SettingsActivity.java
  16. +191 −213 ...d/app/src/main/java/org/dolphinemu/dolphinemu/features/settings/ui/SettingsActivityPresenter.java
  17. +4 −8 ...ndroid/app/src/main/java/org/dolphinemu/dolphinemu/features/settings/ui/SettingsActivityView.java
  18. +26 −26 Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/features/settings/ui/SettingsAdapter.java
  19. +3 −12 ...ce/Android/app/src/main/java/org/dolphinemu/dolphinemu/features/settings/ui/SettingsFragment.java
  20. +427 −447 ...d/app/src/main/java/org/dolphinemu/dolphinemu/features/settings/ui/SettingsFragmentPresenter.java
  21. +3 −4 ...ndroid/app/src/main/java/org/dolphinemu/dolphinemu/features/settings/ui/SettingsFragmentView.java
  22. +17 −74 ...ndroid/app/src/main/java/org/dolphinemu/dolphinemu/{ → features/settings}/utils/SettingsFile.java
@@ -4,9 +4,9 @@
{
private boolean mValue;
public BooleanSetting(String key, String section, int file, boolean value)
public BooleanSetting(String key, String section, boolean value)
{
super(key, section, file);
super(key, section);
mValue = value;
}
@@ -4,9 +4,9 @@
{
private float mValue;
public FloatSetting(String key, String section, int file, float value)
public FloatSetting(String key, String section, float value)
{
super(key, section, file);
super(key, section);
mValue = value;
}
@@ -7,15 +7,15 @@
private int mValue;
private MenuTag menuTag;
public IntSetting(String key, String section, int file, int value)
public IntSetting(String key, String section, int value)
{
super(key, section, file);
super(key, section);
mValue = value;
}
public IntSetting(String key, String section, int file, int value, MenuTag menuTag)
public IntSetting(String key, String section, int value, MenuTag menuTag)
{
super(key, section, file);
super(key, section);
mValue = value;
this.menuTag = menuTag;
}
@@ -10,20 +10,17 @@
{
private String mKey;
private String mSection;
private int mFile;
/**
* Base constructor.
*
* @param key Everything to the left of the = in a line from the ini file.
* @param section The corresponding recent section header; e.g. [Core] or [Enhancements] without the brackets.
* @param file The ini file the Setting is stored in.
*/
public Setting(String key, String section, int file)
public Setting(String key, String section)
{
mKey = key;
mSection = section;
mFile = file;
}
/**
@@ -44,14 +41,6 @@ public String getSection()
return mSection;
}
/**
*
* @return The ini file the Setting is stored in.
*/
public int getFile()
{
return mFile;
}
/**
* @return A representation of this Setting's backing value converted to a String (e.g. for serialization).
@@ -0,0 +1,136 @@
package org.dolphinemu.dolphinemu.features.settings.model;
import android.text.TextUtils;
import org.dolphinemu.dolphinemu.features.settings.ui.SettingsActivityView;
import org.dolphinemu.dolphinemu.features.settings.utils.SettingsFile;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
public class Settings
{
public static final String SECTION_INI_CORE = "Core";
public static final String SECTION_INI_INTERFACE = "Interface";
public static final String SECTION_GFX_SETTINGS = "Settings";
public static final String SECTION_GFX_ENHANCEMENTS = "Enhancements";
public static final String SECTION_GFX_HACKS = "Hacks";
public static final String SECTION_STEREOSCOPY = "Stereoscopy";
public static final String SECTION_WIIMOTE = "Wiimote";
public static final String SECTION_BINDINGS = "Android";
private String gameId;
private static final Map<String, List<String>> configFileSectionsMap = new HashMap<>();
static
{
configFileSectionsMap.put(SettingsFile.FILE_NAME_DOLPHIN, Arrays.asList(SECTION_INI_CORE, SECTION_INI_INTERFACE, SECTION_BINDINGS));
configFileSectionsMap.put(SettingsFile.FILE_NAME_GFX, Arrays.asList(SECTION_GFX_SETTINGS, SECTION_GFX_ENHANCEMENTS, SECTION_GFX_HACKS, SECTION_STEREOSCOPY));
configFileSectionsMap.put(SettingsFile.FILE_NAME_WIIMOTE, Arrays.asList(SECTION_WIIMOTE + 1, SECTION_WIIMOTE + 2, SECTION_WIIMOTE + 3, SECTION_WIIMOTE + 4));
}
/**
* A HashMap<String, SettingSection> that constructs a new SettingSection instead of returning null
* when getting a key not already in the map
*/
public static final class SettingsSectionMap extends HashMap<String, SettingSection>
{
@Override
public SettingSection get(Object key)
{
if (!(key instanceof String))
{
return null;
}
String stringKey = (String) key;
if (!super.containsKey(stringKey))
{
SettingSection section = new SettingSection(stringKey);
super.put(stringKey, section);
return section;
}
return super.get(key);
}
}
private HashMap<String, SettingSection> sections = new Settings.SettingsSectionMap();
public SettingSection getSection(String sectionName)
{
return sections.get(sectionName);
}
public boolean isEmpty()
{
return sections.isEmpty();
}
public HashMap<String, SettingSection> getSections()
{
return sections;
}
public void loadSettings(SettingsActivityView view)
{
sections = new Settings.SettingsSectionMap();
if (TextUtils.isEmpty(gameId))
{
for (Map.Entry<String, List<String>> entry : configFileSectionsMap.entrySet())
{
String fileName = entry.getKey();
sections.putAll(SettingsFile.readFile(fileName, view));
}
}
else
{
// custom game settings
sections.putAll(SettingsFile.readFile("../GameSettings/" + gameId, view));
}
}
public void loadSettings(String gameId, SettingsActivityView view)
{
this.gameId = gameId;
loadSettings(view);
}
public void saveSettings(SettingsActivityView view)
{
if (TextUtils.isEmpty(gameId))
{
view.showToastMessage("Saved settings to INI files");
for (Map.Entry<String, List<String>> entry : configFileSectionsMap.entrySet())
{
String fileName = entry.getKey();
List<String> sectionNames = entry.getValue();
TreeMap<String, SettingSection> iniSections = new TreeMap<>();
for (String section : sectionNames)
{
iniSections.put(section, sections.get(section));
}
SettingsFile.saveFile(fileName, iniSections, view);
}
}
else
{
// custom game settings
view.showToastMessage("Saved settings for " + gameId);
TreeMap<String, SettingSection> iniSections = new TreeMap<>(sections);
SettingsFile.saveFile("../GameSettings/" + gameId, iniSections, view);
}
}
}
@@ -4,9 +4,9 @@
{
private String mValue;
public StringSetting(String key, String section, int file, String value)
public StringSetting(String key, String section, String value)
{
super(key, section, file);
super(key, section);
mValue = value;
}
@@ -7,9 +7,9 @@
{
private boolean mDefaultValue;
public CheckBoxSetting(String key, String section, int file, int titleId, int descriptionId, boolean defaultValue, Setting setting)
public CheckBoxSetting(String key, String section, int titleId, int descriptionId, boolean defaultValue, Setting setting)
{
super(key, section, file, setting, titleId, descriptionId);
super(key, section, setting, titleId, descriptionId);
mDefaultValue = defaultValue;
}
@@ -35,7 +35,7 @@ public BooleanSetting setChecked(boolean checked)
{
if (getSetting() == null)
{
BooleanSetting setting = new BooleanSetting(getKey(), getSection(), getFile(), checked);
BooleanSetting setting = new BooleanSetting(getKey(), getSection(), checked);
setSetting(setting);
return setting;
}
@@ -6,7 +6,7 @@
{
public HeaderSetting(String key, Setting setting, int titleId, int descriptionId)
{
super(key, null, 0, setting, titleId, descriptionId);
super(key, null, setting, titleId, descriptionId);
}
@Override
@@ -5,9 +5,9 @@
public final class InputBindingSetting extends SettingsItem
{
public InputBindingSetting(String key, String section, int file, int titleId, Setting setting)
public InputBindingSetting(String key, String section, int titleId, Setting setting)
{
super(key, section, file, setting, titleId, 0);
super(key, section, setting, titleId, 0);
}
public String getValue()
@@ -32,7 +32,7 @@ public StringSetting setValue(String bind)
{
if (getSetting() == null)
{
StringSetting setting = new StringSetting(getKey(), getSection(), getFile(), bind);
StringSetting setting = new StringSetting(getKey(), getSection(), bind);
setSetting(setting);
return setting;
}
@@ -22,7 +22,6 @@
private String mKey;
private String mSection;
private int mFile;
private Setting mSetting;
@@ -39,11 +38,10 @@
* @param nameId Resource ID for a text string to be displayed as this setting's name.
* @param descriptionId Resource ID for a text string to be displayed as this setting's description.
*/
public SettingsItem(String key, String section, int file, Setting setting, int nameId, int descriptionId)
public SettingsItem(String key, String section, Setting setting, int nameId, int descriptionId)
{
mKey = key;
mSection = section;
mFile = file;
mSetting = setting;
mNameId = nameId;
mDescriptionId = descriptionId;
@@ -67,14 +65,6 @@ public String getSection()
return mSection;
}
/**
*
* @return The file the backing Setting is saved to.
*/
public int getFile()
{
return mFile;
}
/**
*
@@ -12,18 +12,18 @@
private int mValuesId;
private MenuTag menuTag;
public SingleChoiceSetting(String key, String section, int file, int titleId, int descriptionId, int choicesId, int valuesId, int defaultValue, Setting setting, MenuTag menuTag)
public SingleChoiceSetting(String key, String section, int titleId, int descriptionId, int choicesId, int valuesId, int defaultValue, Setting setting, MenuTag menuTag)
{
super(key, section, file, setting, titleId, descriptionId);
super(key, section, setting, titleId, descriptionId);
mValuesId = valuesId;
mChoicesId = choicesId;
mDefaultValue = defaultValue;
this.menuTag = menuTag;
}
public SingleChoiceSetting(String key, String section, int file, int titleId, int descriptionId, int choicesId, int valuesId, int defaultValue, Setting setting)
public SingleChoiceSetting(String key, String section, int titleId, int descriptionId, int choicesId, int valuesId, int defaultValue, Setting setting)
{
this(key, section, file, titleId, descriptionId, choicesId, valuesId, defaultValue, setting, null);
this(key, section, titleId, descriptionId, choicesId, valuesId, defaultValue, setting, null);
}
public int getChoicesId()
@@ -65,7 +65,7 @@ public IntSetting setSelectedValue(int selection)
{
if (getSetting() == null)
{
IntSetting setting = new IntSetting(getKey(), getSection(), getFile(), selection);
IntSetting setting = new IntSetting(getKey(), getSection(), selection);
setSetting(setting);
return setting;
}
@@ -4,7 +4,7 @@
import org.dolphinemu.dolphinemu.features.settings.model.IntSetting;
import org.dolphinemu.dolphinemu.features.settings.model.Setting;
import org.dolphinemu.dolphinemu.utils.Log;
import org.dolphinemu.dolphinemu.utils.SettingsFile;
import org.dolphinemu.dolphinemu.features.settings.utils.SettingsFile;
public final class SliderSetting extends SettingsItem
{
@@ -13,9 +13,9 @@
private String mUnits;
public SliderSetting(String key, String section, int file, int titleId, int descriptionId, int max, String units, int defaultValue, Setting setting)
public SliderSetting(String key, String section, int titleId, int descriptionId, int max, String units, int defaultValue, Setting setting)
{
super(key, section, file, setting, titleId, descriptionId);
super(key, section, setting, titleId, descriptionId);
mMax = max;
mUnits = units;
mDefaultValue = defaultValue;
@@ -71,7 +71,7 @@ public IntSetting setSelectedValue(int selection)
{
if (getSetting() == null)
{
IntSetting setting = new IntSetting(getKey(), getSection(), getFile(), selection);
IntSetting setting = new IntSetting(getKey(), getSection(), selection);
setSetting(setting);
return setting;
}
@@ -94,7 +94,7 @@ public FloatSetting setSelectedValue(float selection)
{
if (getSetting() == null)
{
FloatSetting setting = new FloatSetting(getKey(), getSection(), getFile(), selection);
FloatSetting setting = new FloatSetting(getKey(), getSection(), selection);
setSetting(setting);
return setting;
}
Oops, something went wrong.

0 comments on commit c664bb7

Please sign in to comment.