diff --git a/app/src/main/java/de/mygrades/main/processor/GradesProcessor.java b/app/src/main/java/de/mygrades/main/processor/GradesProcessor.java index fcddf78d..f7cd7282 100644 --- a/app/src/main/java/de/mygrades/main/processor/GradesProcessor.java +++ b/app/src/main/java/de/mygrades/main/processor/GradesProcessor.java @@ -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)); + } } /** diff --git a/app/src/main/java/de/mygrades/view/activity/GradeDetailedActivity.java b/app/src/main/java/de/mygrades/view/activity/GradeDetailedActivity.java index ee3c27be..d8002f1d 100644 --- a/app/src/main/java/de/mygrades/view/activity/GradeDetailedActivity.java +++ b/app/src/main/java/de/mygrades/view/activity/GradeDetailedActivity.java @@ -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(); + } } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index b475d248..00688a14 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -140,6 +140,8 @@ Eintrag löschen Möchtest du diesen Eintrag wirklich löschen? + Eintrag verwerfen + Dieses Modul wurde noch nicht gespeichert. Möchtest du es verwerfen? Selbsterstelltes Modul.