mirror of
https://github.com/SimpleMobileTools/Simple-Notes.git
synced 2025-06-05 17:00:23 +02:00
display a dialog at leaving with unsaved changes
This commit is contained in:
@ -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();
|
||||
}
|
||||
}
|
||||
|
@ -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";
|
||||
}
|
||||
|
@ -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);
|
||||
|
@ -26,6 +26,7 @@ public class SettingsActivity extends SimpleActivity {
|
||||
|
||||
setupDarkTheme();
|
||||
setupAutosave();
|
||||
mConfig.setShouldPromptAutosave(false);
|
||||
}
|
||||
|
||||
private void setupDarkTheme() {
|
||||
|
Reference in New Issue
Block a user