Skip to content

Commit

Permalink
Show dialog if new grade entry was not saved onBackPressed
Browse files Browse the repository at this point in the history
tilmanginzel committed Jul 6, 2016

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent e41b9ec commit 4cdf261
Showing 3 changed files with 42 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -388,10 +388,12 @@ public void updateGradeEntryVisibility(String gradeHash, boolean hidden) {
public void deleteGradeEntry(String gradeHash) {
// find grade entry by hash
GradeEntry gradeEntry = daoSession.getGradeEntryDao().load(gradeHash);
gradeEntry.delete();
if (gradeEntry != null) {
gradeEntry.delete();

// post event to ui
EventBus.getDefault().postSticky(new DeleteGradeEvent(gradeEntry));
// post event to ui
EventBus.getDefault().postSticky(new DeleteGradeEvent(gradeEntry));
}
}

/**
Original file line number Diff line number Diff line change
@@ -59,6 +59,7 @@ public class GradeDetailedActivity extends AppCompatActivity {
private static final String IS_OVERVIEW_POSSIBLE_STATE = "is_overview_possible_state";
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";

// views
private LinearLayout llRootView; // used to show snackbar
@@ -273,6 +274,7 @@ private void showBarChart(OverviewEvent overviewEvent) {
@SuppressWarnings("unused")
public void onEventMainThread(GradeEntryEvent gradeEntryEvent) {
gradeEntry = gradeEntryEvent.getGradeEntry();
gradeHash = gradeEntry.getHash();

editHelper.setGradeEntry(gradeEntry);
editHelper.setSemesterToNumberMap(gradeEntryEvent.getSemesterToSemesterNumberMap());
@@ -414,6 +416,7 @@ protected void onSaveInstanceState(Bundle outState) {
outState.putBoolean(IS_OVERVIEW_POSSIBLE_STATE, isOverviewPossible);
outState.putString(GRADE_HASH_STATE, gradeHash);
outState.putBoolean(EDIT_MODE_ENABLED_STATE, editModeEnabled);
outState.putBoolean(ADD_NEW_GRADE_ENTRY, addNewGradeEntry);
}

@Override
@@ -424,6 +427,7 @@ protected void onRestoreInstanceState(Bundle savedInstanceState) {
gradeHash = savedInstanceState.getString(GRADE_HASH_STATE, "");
ptrHeader.restoreInstanceState(savedInstanceState, ptrFrame);
editModeEnabled = savedInstanceState.getBoolean(EDIT_MODE_ENABLED_STATE);
addNewGradeEntry = savedInstanceState.getBoolean(ADD_NEW_GRADE_ENTRY);
}

private class MyValueFormatter implements ValueFormatter {
@@ -471,6 +475,7 @@ public boolean onOptionsItemSelected(MenuItem item) {
case R.id.grade_detail_save:
editHelper.enableEditMode(false);
editHelper.saveEdits();
addNewGradeEntry = false;
editModeEnabled = false;
invalidateOptionsMenu();
return true;
@@ -487,6 +492,11 @@ public boolean onOptionsItemSelected(MenuItem item) {

@Override
public void onBackPressed() {
if (addNewGradeEntry) {
showDiscardDialog();
return;
}

if (editModeEnabled) {
editHelper.enableEditMode(false);
editModeEnabled = false;
@@ -550,4 +560,29 @@ public void onClick(DialogInterface dialog, int which) {
AlertDialog dialog = builder.create();
dialog.show();
}

/**
* Show dialog to ask the user whether he wants to discard this new GradeEntry.
*/
private void showDiscardDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(R.string.dialog_discard_grade_entry);
builder.setTitle(getString(R.string.dialog_discard_grade_entry_title));
builder.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
});

builder.setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});

AlertDialog dialog = builder.create();
dialog.show();
}
}
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -140,6 +140,8 @@
<string name="info_edit_mode_message"><![CDATA[Über das Icon oben rechts kannst du hier und auf der Detailseite in den neuen Editiermodus wechseln. Dort kannst du neue Module hinzufügen, Module ausblenden, in andere Semester verschieben, Gewichtungen anpassen oder auch alle anderen Werte ändern.]]></string>
<string name="dialog_delete_grade_entry_title">Eintrag löschen</string>
<string name="dialog_delete_grade_entry">Möchtest du diesen Eintrag wirklich löschen?</string>
<string name="dialog_discard_grade_entry_title">Eintrag verwerfen</string>
<string name="dialog_discard_grade_entry">Dieses Modul wurde noch nicht gespeichert. Möchtest du es verwerfen?</string>
<string name="tv_custom_grade_entry">Selbsterstelltes Modul.</string>

<string-array name="grade_attempt_spinner">

0 comments on commit 4cdf261

Please sign in to comment.