Skip to content

Commit

Permalink
Save instance state on grade detail activity in edit mode
Browse files Browse the repository at this point in the history
  • Loading branch information
tilmanginzel committed Jul 6, 2016
1 parent 4cdf261 commit 57032f5
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public class GradeDetailedActivity extends AppCompatActivity {
private static final String GRADE_HASH_STATE = "grade_hash";
private static final String EDIT_MODE_ENABLED_STATE = "edit_mode_enabled";
private static final String ADD_NEW_GRADE_ENTRY = "add_new_grade_entry";
private static final String EDIT_HELPER_STATE = "edit_helper_state";

// views
private LinearLayout llRootView; // used to show snackbar
Expand Down Expand Up @@ -88,6 +89,7 @@ public class GradeDetailedActivity extends AppCompatActivity {
private boolean receivedGradeEntryEvent;
private boolean editModeEnabled;
private boolean addNewGradeEntry;
private Bundle editHelperState;

// snackbar listener
private View.OnClickListener tryAgainListener;
Expand Down Expand Up @@ -280,6 +282,9 @@ public void onEventMainThread(GradeEntryEvent gradeEntryEvent) {
editHelper.setSemesterToNumberMap(gradeEntryEvent.getSemesterToSemesterNumberMap());
editHelper.init();
editHelper.updateValues();
if (editModeEnabled) {
editHelper.restoreInstanceState(editHelperState);
}

if (addNewGradeEntry) {
editHelper.enableEditMode(true);
Expand Down Expand Up @@ -417,6 +422,7 @@ protected void onSaveInstanceState(Bundle outState) {
outState.putString(GRADE_HASH_STATE, gradeHash);
outState.putBoolean(EDIT_MODE_ENABLED_STATE, editModeEnabled);
outState.putBoolean(ADD_NEW_GRADE_ENTRY, addNewGradeEntry);
outState.putBundle(EDIT_HELPER_STATE, editHelper.getInstanceState());
}

@Override
Expand All @@ -428,6 +434,7 @@ protected void onRestoreInstanceState(Bundle savedInstanceState) {
ptrHeader.restoreInstanceState(savedInstanceState, ptrFrame);
editModeEnabled = savedInstanceState.getBoolean(EDIT_MODE_ENABLED_STATE);
addNewGradeEntry = savedInstanceState.getBoolean(ADD_NEW_GRADE_ENTRY);
editHelperState = savedInstanceState.getBundle(EDIT_HELPER_STATE);
}

private class MyValueFormatter implements ValueFormatter {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package de.mygrades.view.activity;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
Expand All @@ -25,6 +26,19 @@
public class GradeDetailedActivityEditHelper {
private static final String TAG = GradeDetailedActivityEditHelper.class.getSimpleName();

// instance state
public static final String ET_EXAM_ID_TEXT = "et_exam_id_text";
public static final String ET_STATE_TEXT = "et_state_text";
public static final String ET_CREDIT_POINTS_TEXT = "et_credit_points_text";
public static final String ET_GRADE_TEXT = "et_grade_text";
public static final String ET_ANNOTATION_TEXT = "et_annotation_text";
public static final String ET_EXAM_DATE_TEXT = "et_exam_date_text";
public static final String ET_TESTER_TEXT = "et_tester_text";
public static final String ET_WEIGHT_TEXT = "et_weight_text";
public static final String ET_NAME_TEXT = "et_name_text";
public static final String SP_ATTEMPT_POSITION = "sp_attempt_position";
public static final String SP_SEMESTER_POSITION = "sp_semester_position";

private Activity activity;
private MainServiceHelper mainServiceHelper;
private boolean editModeEnabled;
Expand Down Expand Up @@ -718,6 +732,49 @@ private Double stringToDouble(String s) {
return null;
}

/**
* Get the instance state for all edit texts and spinner.
*
* @return instance state as Bundle
*/
public Bundle getInstanceState() {
Bundle state = new Bundle();
state.putString(ET_EXAM_ID_TEXT, etExamId.getText().toString());
state.putString(ET_STATE_TEXT, etState.getText().toString());
state.putString(ET_CREDIT_POINTS_TEXT, etCreditPoints.getText().toString());
state.putString(ET_GRADE_TEXT, etGrade.getText().toString());
state.putString(ET_ANNOTATION_TEXT, etAnnotation.getText().toString());
state.putString(ET_EXAM_DATE_TEXT, etExamDate.getText().toString());
state.putString(ET_TESTER_TEXT, etTester.getText().toString());
state.putString(ET_WEIGHT_TEXT, etWeight.getText().toString());
state.putString(ET_NAME_TEXT, etName.getText().toString());
state.putInt(SP_ATTEMPT_POSITION, spAttempt.getSelectedItemPosition());
state.putInt(SP_SEMESTER_POSITION, spSemester.getSelectedItemPosition());

return state;
}

/**
* Restore the instance state for all edit texts and spinner.
*
* @param savedState Bundle
*/
public void restoreInstanceState(Bundle savedState) {
if (savedState == null) return;

etExamId.setText(savedState.getString(ET_EXAM_ID_TEXT));
etState.setText(savedState.getString(ET_STATE_TEXT));
etCreditPoints.setText(savedState.getString(ET_CREDIT_POINTS_TEXT));
etGrade.setText(savedState.getString(ET_GRADE_TEXT));
etAnnotation.setText(savedState.getString(ET_ANNOTATION_TEXT));
etExamDate.setText(savedState.getString(ET_EXAM_DATE_TEXT));
etTester.setText(savedState.getString(ET_TESTER_TEXT));
etWeight.setText(savedState.getString(ET_WEIGHT_TEXT));
etName.setText(savedState.getString(ET_NAME_TEXT));
spAttempt.setSelection(savedState.getInt(SP_ATTEMPT_POSITION));
spSemester.setSelection(savedState.getInt(SP_SEMESTER_POSITION));
}

public void setGradeEntry(GradeEntry gradeEntry) {
this.gradeEntry = gradeEntry;
}
Expand Down

0 comments on commit 57032f5

Please sign in to comment.