Skip to content

Commit

Permalink
Fix bug with preferences.
Browse files Browse the repository at this point in the history
  • Loading branch information
arnowelzel committed Aug 14, 2018
1 parent 10fd6f3 commit 5b7dd73
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ private void calendarUpdate() {
* Handler for "previous month" button in main view
*/
@SuppressWarnings({"UnusedParameters", "SameParameterValue"})
public void goPrev(View v) {
private void goPrev(View v) {
// Update calendar
monthCurrent--;
if (monthCurrent < 1) {
Expand All @@ -473,7 +473,7 @@ public void goPrev(View v) {
* Handler for "next month" button in main view
*/
@SuppressWarnings({"UnusedParameters", "SameParameterValue"})
public void goNext(View v) {
private void goNext(View v) {
// Update calendar
monthCurrent++;
if (monthCurrent > 12) {
Expand Down Expand Up @@ -825,7 +825,6 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {

// Options modified
case SET_OPTIONS:
dbMain.savePreferences();
handleDatabaseEdit();
calendarUpdate();
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@
*/
@SuppressLint("DefaultLocale")
class PeriodicalDatabase {
public final Integer DEFAULT_PERIOD_LENGTH = 4;
public final Integer DEFAULT_LUTEAL_LENGTH = 14;
public final Integer DEFAULT_CYCLE_LENGTH = 183;
public final Integer DEFAULT_START_OF_WEEK = 0;
public final Boolean DEFAULT_DIRECT_DETAILS = false;
public final Boolean DEFAULT_SHOW_CYCLE = true;

/**
* Helper to create or open database
Expand Down Expand Up @@ -161,20 +167,20 @@ public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// based on the global period length setting
PreferenceUtils preferences = new PreferenceUtils(context);
int periodlength;
periodlength = preferences.getInt("period_length", 4);
periodlength = preferences.getInt("period_length", DEFAULT_PERIOD_LENGTH);

String statement;

// Workaround for a bug introduced in release 0.35 which stored the
// maximum cycle as "period length", so it is not usable at all :-(
String option = "maximum_cycle_length";
SharedPreferences.Editor editor = preferences.edit();
editor.putInt(option, 183);
editor.putInt(option, DEFAULT_CYCLE_LENGTH);
editor.apply();
statement = "delete from options where name = ?";
db.execSQL(statement, new String[]{option});
statement = "insert into options (name, value) values (?, ?)";
db.execSQL(statement, new String[]{option, "183"});
db.execSQL(statement, new String[]{option, DEFAULT_CYCLE_LENGTH.toString()});

// Fill database with additional entries for the period days
statement = "select eventtype, eventdate from data order by eventdate desc";
Expand Down Expand Up @@ -404,7 +410,7 @@ void addPeriod(GregorianCalendar date) {
int periodlength;

PreferenceUtils preferences = new PreferenceUtils(context);
periodlength = preferences.getInt("period_length", 4);
periodlength = preferences.getInt("period_length", DEFAULT_PERIOD_LENGTH);

type = DayEntry.PERIOD_START;
dateLocal.setTime(date.getTime());
Expand Down Expand Up @@ -500,9 +506,9 @@ void loadCalculatedData() {

// Get default values from preferences
PreferenceUtils preferences = new PreferenceUtils(context);
periodlength = preferences.getInt("period_length", 4);
luteallength = preferences.getInt("luteal_length", 14);
maximumcyclelength = preferences.getInt("maximum_cycle_length", 183);
periodlength = preferences.getInt("period_length", DEFAULT_PERIOD_LENGTH);
luteallength = preferences.getInt("luteal_length", DEFAULT_LUTEAL_LENGTH);
maximumcyclelength = preferences.getInt("maximum_cycle_length", DEFAULT_CYCLE_LENGTH);

// Just a safety measure: limit maximum cycle lengths to the allowed minimum value
if(maximumcyclelength < 60) maximumcyclelength = 60;
Expand Down Expand Up @@ -1010,7 +1016,7 @@ private boolean getOption(String name, boolean defaultvalue) {
* @param value
* Value of the option to store
*/
private void setOption(String name, String value) {
public void setOption(String name, String value) {
String statement;

db.beginTransaction();
Expand All @@ -1027,7 +1033,7 @@ private void setOption(String name, String value) {
db.endTransaction();
}

private void setOption(String name, Integer value) {
public void setOption(String name, Integer value) {
String statement;
String valueStr;

Expand All @@ -1046,7 +1052,7 @@ private void setOption(String name, Integer value) {
db.endTransaction();
}

private void setOption(String name, boolean value) {
public void setOption(String name, boolean value) {
String statement;

db.beginTransaction();
Expand Down Expand Up @@ -1152,45 +1158,34 @@ private boolean backupRestore(boolean backup) {
return ok;
}

/**
* Save application preferences to the database
*
* <br><br><i>(Just a hack for now - in the future we might want to get rid of shared preferences)</i>
*/
void savePreferences() {
PreferenceUtils preferences = new PreferenceUtils(context);
setOption("period_length", preferences.getInt("period_length", 4));
setOption("startofweek", preferences.getInt("startofweek", 0));
setOption("maximum_cycle_length", preferences.getInt("maximum_cycle_length", 183));
setOption("direct_details", preferences.getBoolean("pref_direct_details", false));
setOption("show_cycle", preferences.getBoolean("show_cycle", true));
}

/**
* Restore application preferences from the database
*
* <br><br><i>(Just a hack for now - in the future we might want to get rid of shared preferences)</i>
*/
void restorePreferences() {
Integer period_length = getOption("period_length", 4);
Integer startofweek = getOption("startofweek", 0);
if(startofweek != 0 && startofweek != 1) startofweek = 0;
Integer maximum_cycle_length = getOption("maximum_cycle_length", 183);
boolean direct_details = getOption("direct_details", false);
boolean show_cycle = getOption("show_cycle", true);
Integer period_length = getOption("period_length", DEFAULT_PERIOD_LENGTH);
Integer luteal_length = getOption("luteal_length ", DEFAULT_LUTEAL_LENGTH);
Integer startofweek = getOption("startofweek", DEFAULT_START_OF_WEEK);
if(startofweek != DEFAULT_START_OF_WEEK && startofweek != 1) startofweek = DEFAULT_START_OF_WEEK;
Integer maximum_cycle_length = getOption("maximum_cycle_length", DEFAULT_CYCLE_LENGTH);
boolean direct_details = getOption("direct_details", DEFAULT_DIRECT_DETAILS);
boolean show_cycle = getOption("show_cycle", DEFAULT_SHOW_CYCLE);

PreferenceUtils preferences = new PreferenceUtils(context);
SharedPreferences.Editor editor = preferences.edit();

// Make sure, there are no existing values which may cause problems
editor.remove("period_length");
editor.remove("luteal_length");
editor.remove("startofweek");
editor.remove("maximum_cycle_length");
editor.remove("direct_details");
editor.remove("show_cycle");

// Store values
editor.putString("period_length", period_length.toString());
editor.putString("luteal_length", luteal_length.toString());
editor.putString("startofweek", startofweek.toString());
editor.putString("maximum_cycle_length", maximum_cycle_length.toString());
editor.putBoolean("direct_details", direct_details);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@
* Activity to handle the "Preferences" command
*/
public class PreferenceActivity extends AppCompatPreferenceActivity implements SharedPreferences.OnSharedPreferenceChangeListener {

private PeriodicalDatabase dbMain;

/**
* Called when activity starts
*/
Expand All @@ -43,7 +44,10 @@ public void onCreate(Bundle savedInstanceState) {

final Context context = getApplicationContext();
assert context != null;


// We get/store preferences in the database
dbMain = new PeriodicalDatabase(context);

addPreferencesFromResource(R.xml.preferences);
initSummary(getPreferenceScreen());

Expand Down Expand Up @@ -152,13 +156,36 @@ protected void onPause() {
}

/**
* Update summary of changed preferences
* Handle preference changes
*/
@SuppressWarnings("deprecation")
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
PreferenceUtils preferenceUtils = new PreferenceUtils(sharedPreferences);
Preference pref = findPreference(key);

updatePrefSummary(pref);

// Store setting to database
switch(key) {
case "period_length":
dbMain.setOption(key, preferenceUtils.getInt(key, dbMain.DEFAULT_PERIOD_LENGTH));
break;
case "luteal_length":
dbMain.setOption(key, preferenceUtils.getInt(key, dbMain.DEFAULT_LUTEAL_LENGTH));
break;
case "startofweek":
dbMain.setOption(key, preferenceUtils.getInt(key, dbMain.DEFAULT_START_OF_WEEK));
break;
case "maximum_cycle_length":
dbMain.setOption(key, preferenceUtils.getInt(key, dbMain.DEFAULT_CYCLE_LENGTH));
break;
case "direct_details":
dbMain.setOption(key, preferenceUtils.getBoolean(key, dbMain.DEFAULT_DIRECT_DETAILS));
break;
case "show_cycle":
dbMain.setOption(key, preferenceUtils.getBoolean(key, dbMain.DEFAULT_SHOW_CYCLE));
break;
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,19 @@ class PreferenceUtils {
* Application context
*/
PreferenceUtils(Context context) {
/* Private reference to application context */
this.preferences = PreferenceManager.getDefaultSharedPreferences(context);
}

/**
* Constructor, will use an existing shared preference object
*
* @param sharedPreferences
* Shared preferences to be used
*/
PreferenceUtils(SharedPreferences sharedPreferences) {
this.preferences = sharedPreferences;
}

/**
* Get integer preference
*
Expand Down

0 comments on commit 5b7dd73

Please sign in to comment.