Skip to content

Commit 83566a0

Browse files
committed
display a dialog at leaving with unsaved changes
1 parent e09423b commit 83566a0

File tree

9 files changed

+64
-6
lines changed

9 files changed

+64
-6
lines changed

app/src/main/java/com/simplemobiletools/notes/Config.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,12 @@ public boolean getIsAutosaveEnabled() {
3737
public void setIsAutosaveEnabled(boolean enabled) {
3838
mPrefs.edit().putBoolean(Constants.AUTOSAVE, enabled).apply();
3939
}
40+
41+
public boolean getShouldPromptAutosave() {
42+
return mPrefs.getBoolean(Constants.PROMPT_AUTOSAVE, true);
43+
}
44+
45+
public void setShouldPromptAutosave(boolean prompt) {
46+
mPrefs.edit().putBoolean(Constants.PROMPT_AUTOSAVE, prompt).apply();
47+
}
4048
}

app/src/main/java/com/simplemobiletools/notes/Constants.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ public class Constants {
88
public static final String IS_FIRST_RUN = "is_first_run";
99
public static final String IS_DARK_THEME = "is_dark_theme";
1010
public static final String AUTOSAVE = "autosave";
11+
public static final String PROMPT_AUTOSAVE = "prompt_autosave";
1112
public static final String WIDGET_BG_COLOR = "widget_bg_color";
1213
public static final String WIDGET_TEXT_COLOR = "widget_text_color";
1314
}

app/src/main/java/com/simplemobiletools/notes/activities/MainActivity.java

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33
import android.appwidget.AppWidgetManager;
44
import android.content.ComponentName;
55
import android.content.Context;
6+
import android.content.DialogInterface;
67
import android.content.Intent;
78
import android.content.SharedPreferences;
89
import android.content.res.Resources;
910
import android.os.Bundle;
11+
import android.support.v7.app.AlertDialog;
1012
import android.view.Menu;
1113
import android.view.MenuItem;
1214
import android.view.inputmethod.InputMethodManager;
@@ -33,8 +35,7 @@ protected void onCreate(Bundle savedInstanceState) {
3335
ButterKnife.bind(this);
3436

3537
mPrefs = getSharedPreferences(Constants.PREFS_KEY, Context.MODE_PRIVATE);
36-
final String text = mPrefs.getString(Constants.TEXT, "");
37-
mNotesView.setText(text);
38+
mNotesView.setText(getSavedNote());
3839
}
3940

4041
@Override
@@ -51,6 +52,16 @@ protected void onPause() {
5152
}
5253
}
5354

55+
@Override
56+
public void onBackPressed() {
57+
if (mConfig.getShouldPromptAutosave() && !getCurrentNote().equals(getSavedNote())) {
58+
mConfig.setShouldPromptAutosave(false);
59+
displayAutosavePrompt();
60+
} else {
61+
super.onBackPressed();
62+
}
63+
}
64+
5465
@Override
5566
protected void onDestroy() {
5667
super.onDestroy();
@@ -86,8 +97,23 @@ public boolean onOptionsItemSelected(MenuItem item) {
8697
}
8798
}
8899

100+
private void displayAutosavePrompt() {
101+
final AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
102+
alertDialog.setTitle(getString(R.string.unsaved_changes));
103+
alertDialog.setMessage(getString(R.string.autosave_prompt_msg));
104+
105+
alertDialog.setNegativeButton(R.string.cancel, null);
106+
alertDialog.setPositiveButton(R.string.enable_autosave, new DialogInterface.OnClickListener() {
107+
@Override
108+
public void onClick(DialogInterface dialog, int which) {
109+
110+
}
111+
});
112+
alertDialog.create().show();
113+
}
114+
89115
private void saveText(boolean showToast) {
90-
final String text = mNotesView.getText().toString().trim();
116+
final String text = getCurrentNote();
91117
mPrefs.edit().putString(Constants.TEXT, text).apply();
92118

93119
if (showToast) {
@@ -99,7 +125,7 @@ private void saveText(boolean showToast) {
99125
}
100126

101127
private void shareText() {
102-
final String text = mNotesView.getText().toString().trim();
128+
final String text = getCurrentNote();
103129
if (text.isEmpty()) {
104130
Utils.showToast(getApplicationContext(), R.string.cannot_share_empty_text);
105131
return;
@@ -115,6 +141,14 @@ private void shareText() {
115141
startActivity(Intent.createChooser(sendIntent, shareTitle));
116142
}
117143

144+
private String getCurrentNote() {
145+
return mNotesView.getText().toString().trim();
146+
}
147+
148+
private String getSavedNote() {
149+
return mPrefs.getString(Constants.TEXT, "");
150+
}
151+
118152
private void hideKeyboard() {
119153
final InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
120154
imm.hideSoftInputFromWindow(mNotesView.getWindowToken(), 0);

app/src/main/java/com/simplemobiletools/notes/activities/SettingsActivity.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ protected void onCreate(Bundle savedInstanceState) {
2626

2727
setupDarkTheme();
2828
setupAutosave();
29+
mConfig.setShouldPromptAutosave(false);
2930
}
3031

3132
private void setupDarkTheme() {

app/src/main/res/values-it/strings.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
<string name="cannot_share_empty_text">Impossibile condividere un testo vuoto</string>
88
<string name="text_saved">Testo salvato</string>
99
<string name="simple_note">Simple Note</string>
10+
<string name="enable_autosave">Enable autosave</string>
11+
<string name="cancel">Cancel</string>
12+
<string name="unsaved_changes">Unsaved changes</string>
13+
<string name="autosave_prompt_msg">Changes made to the note have not be saved. Do you want to enable autosave?</string>
1014

1115
<!-- Settings -->
1216
<string name="settings">Impostazioni</string>

app/src/main/res/values-ja/strings.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
<string name="cannot_share_empty_text">空のテキストは共有できません</string>
88
<string name="text_saved">テキストを保存しました</string>
99
<string name="simple_note">シンプル メモ</string>
10+
<string name="enable_autosave">Enable autosave</string>
11+
<string name="cancel">Cancel</string>
12+
<string name="unsaved_changes">Unsaved changes</string>
13+
<string name="autosave_prompt_msg">Changes made to the note have not be saved. Do you want to enable autosave?</string>
1014

1115
<!-- Settings -->
1216
<string name="settings">設定</string>

app/src/main/res/values-sv/strings.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
<string name="cannot_share_empty_text">Det går inte att dela utan text</string>
88
<string name="text_saved">Text sparad</string>
99
<string name="simple_note">Simple Note</string>
10+
<string name="enable_autosave">Enable autosave</string>
11+
<string name="cancel">Cancel</string>
12+
<string name="unsaved_changes">Unsaved changes</string>
13+
<string name="autosave_prompt_msg">Changes made to the note have not be saved. Do you want to enable autosave?</string>
1014

1115
<!-- Settings -->
1216
<string name="settings">Inställningar</string>

app/src/main/res/values/strings.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
<string name="cannot_share_empty_text">Cannot share empty text</string>
88
<string name="text_saved">Text Saved</string>
99
<string name="simple_note">Simple Note</string>
10+
<string name="enable_autosave">Enable autosave</string>
11+
<string name="cancel">Cancel</string>
12+
<string name="unsaved_changes">Unsaved changes</string>
13+
<string name="autosave_prompt_msg">Changes made to the note have not be saved. Do you want to enable autosave?</string>
1014

1115
<!-- Settings -->
1216
<string name="settings">Settings</string>

app/src/main/res/values/styles.xml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,13 @@
55
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
66
<item name="colorAccent">@color/colorAccent</item>
77
<item name="actionBarStyle">@style/AppTheme.ActionBarStyle</item>
8-
<item name="android:textSize">@dimen/normal_text_size</item>
98
</style>
109

1110
<style name="AppTheme.Dark" parent="Theme.AppCompat">
1211
<item name="colorPrimary">@color/colorPrimary</item>
1312
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
1413
<item name="colorAccent">@color/colorAccent</item>
1514
<item name="actionBarStyle">@style/AppTheme.ActionBarStyle</item>
16-
<item name="android:textSize">@dimen/normal_text_size</item>
1715
<item name="android:windowBackground">@android:color/black</item>
1816
</style>
1917

0 commit comments

Comments
 (0)