display a dialog at leaving with unsaved changes

This commit is contained in:
tibbi 2016-08-21 23:10:49 +02:00
parent e09423bc68
commit 83566a07e8
9 changed files with 64 additions and 6 deletions

View File

@ -37,4 +37,12 @@ public class Config {
public void setIsAutosaveEnabled(boolean enabled) {
mPrefs.edit().putBoolean(Constants.AUTOSAVE, enabled).apply();
}
public boolean getShouldPromptAutosave() {
return mPrefs.getBoolean(Constants.PROMPT_AUTOSAVE, true);
}
public void setShouldPromptAutosave(boolean prompt) {
mPrefs.edit().putBoolean(Constants.PROMPT_AUTOSAVE, prompt).apply();
}
}

View File

@ -8,6 +8,7 @@ public class Constants {
public static final String IS_FIRST_RUN = "is_first_run";
public static final String IS_DARK_THEME = "is_dark_theme";
public static final String AUTOSAVE = "autosave";
public static final String PROMPT_AUTOSAVE = "prompt_autosave";
public static final String WIDGET_BG_COLOR = "widget_bg_color";
public static final String WIDGET_TEXT_COLOR = "widget_text_color";
}

View File

@ -3,10 +3,12 @@ package com.simplemobiletools.notes.activities;
import android.appwidget.AppWidgetManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.view.Menu;
import android.view.MenuItem;
import android.view.inputmethod.InputMethodManager;
@ -33,8 +35,7 @@ public class MainActivity extends SimpleActivity {
ButterKnife.bind(this);
mPrefs = getSharedPreferences(Constants.PREFS_KEY, Context.MODE_PRIVATE);
final String text = mPrefs.getString(Constants.TEXT, "");
mNotesView.setText(text);
mNotesView.setText(getSavedNote());
}
@Override
@ -51,6 +52,16 @@ public class MainActivity extends SimpleActivity {
}
}
@Override
public void onBackPressed() {
if (mConfig.getShouldPromptAutosave() && !getCurrentNote().equals(getSavedNote())) {
mConfig.setShouldPromptAutosave(false);
displayAutosavePrompt();
} else {
super.onBackPressed();
}
}
@Override
protected void onDestroy() {
super.onDestroy();
@ -86,8 +97,23 @@ public class MainActivity extends SimpleActivity {
}
}
private void displayAutosavePrompt() {
final AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
alertDialog.setTitle(getString(R.string.unsaved_changes));
alertDialog.setMessage(getString(R.string.autosave_prompt_msg));
alertDialog.setNegativeButton(R.string.cancel, null);
alertDialog.setPositiveButton(R.string.enable_autosave, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
alertDialog.create().show();
}
private void saveText(boolean showToast) {
final String text = mNotesView.getText().toString().trim();
final String text = getCurrentNote();
mPrefs.edit().putString(Constants.TEXT, text).apply();
if (showToast) {
@ -99,7 +125,7 @@ public class MainActivity extends SimpleActivity {
}
private void shareText() {
final String text = mNotesView.getText().toString().trim();
final String text = getCurrentNote();
if (text.isEmpty()) {
Utils.showToast(getApplicationContext(), R.string.cannot_share_empty_text);
return;
@ -115,6 +141,14 @@ public class MainActivity extends SimpleActivity {
startActivity(Intent.createChooser(sendIntent, shareTitle));
}
private String getCurrentNote() {
return mNotesView.getText().toString().trim();
}
private String getSavedNote() {
return mPrefs.getString(Constants.TEXT, "");
}
private void hideKeyboard() {
final InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(mNotesView.getWindowToken(), 0);

View File

@ -26,6 +26,7 @@ public class SettingsActivity extends SimpleActivity {
setupDarkTheme();
setupAutosave();
mConfig.setShouldPromptAutosave(false);
}
private void setupDarkTheme() {

View File

@ -7,6 +7,10 @@
<string name="cannot_share_empty_text">Impossibile condividere un testo vuoto</string>
<string name="text_saved">Testo salvato</string>
<string name="simple_note">Simple Note</string>
<string name="enable_autosave">Enable autosave</string>
<string name="cancel">Cancel</string>
<string name="unsaved_changes">Unsaved changes</string>
<string name="autosave_prompt_msg">Changes made to the note have not be saved. Do you want to enable autosave?</string>
<!-- Settings -->
<string name="settings">Impostazioni</string>

View File

@ -7,6 +7,10 @@
<string name="cannot_share_empty_text">空のテキストは共有できません</string>
<string name="text_saved">テキストを保存しました</string>
<string name="simple_note">シンプル メモ</string>
<string name="enable_autosave">Enable autosave</string>
<string name="cancel">Cancel</string>
<string name="unsaved_changes">Unsaved changes</string>
<string name="autosave_prompt_msg">Changes made to the note have not be saved. Do you want to enable autosave?</string>
<!-- Settings -->
<string name="settings">設定</string>

View File

@ -7,6 +7,10 @@
<string name="cannot_share_empty_text">Det går inte att dela utan text</string>
<string name="text_saved">Text sparad</string>
<string name="simple_note">Simple Note</string>
<string name="enable_autosave">Enable autosave</string>
<string name="cancel">Cancel</string>
<string name="unsaved_changes">Unsaved changes</string>
<string name="autosave_prompt_msg">Changes made to the note have not be saved. Do you want to enable autosave?</string>
<!-- Settings -->
<string name="settings">Inställningar</string>

View File

@ -7,6 +7,10 @@
<string name="cannot_share_empty_text">Cannot share empty text</string>
<string name="text_saved">Text Saved</string>
<string name="simple_note">Simple Note</string>
<string name="enable_autosave">Enable autosave</string>
<string name="cancel">Cancel</string>
<string name="unsaved_changes">Unsaved changes</string>
<string name="autosave_prompt_msg">Changes made to the note have not be saved. Do you want to enable autosave?</string>
<!-- Settings -->
<string name="settings">Settings</string>

View File

@ -5,7 +5,6 @@
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="actionBarStyle">@style/AppTheme.ActionBarStyle</item>
<item name="android:textSize">@dimen/normal_text_size</item>
</style>
<style name="AppTheme.Dark" parent="Theme.AppCompat">
@ -13,7 +12,6 @@
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="actionBarStyle">@style/AppTheme.ActionBarStyle</item>
<item name="android:textSize">@dimen/normal_text_size</item>
<item name="android:windowBackground">@android:color/black</item>
</style>