add the Autosave option

This commit is contained in:
tibbi 2016-07-11 21:44:04 +02:00
parent be6b1d5ef8
commit a7180ea1ee
11 changed files with 149 additions and 5 deletions

View File

@ -35,6 +35,10 @@
android:name=".activities.LicenseActivity"
android:label="@string/third_party_licences"/>
<activity
android:name=".activities.SettingsActivity"
android:label="@string/settings"/>
<receiver
android:name=".MyWidgetProvider"
android:icon="@mipmap/widget_preview">

View File

@ -21,4 +21,12 @@ public class Config {
public void setIsFirstRun(boolean firstRun) {
mPrefs.edit().putBoolean(Constants.IS_FIRST_RUN, firstRun).apply();
}
public boolean getIsAutosaveEnabled() {
return mPrefs.getBoolean(Constants.AUTOSAVE, false);
}
public void setIsAutosaveEnabled(boolean enabled) {
mPrefs.edit().putBoolean(Constants.AUTOSAVE, enabled).apply();
}
}

View File

@ -6,6 +6,7 @@ public class Constants {
// shared preferences
public static final String PREFS_KEY = "Notes";
public static final String IS_FIRST_RUN = "is_first_run";
public static final String AUTOSAVE = "autosave";
public static final String WIDGET_BG_COLOR = "widget_bg_color";
public static final String WIDGET_TEXT_COLOR = "widget_text_color";
}

View File

@ -8,6 +8,8 @@ import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Html;
import android.text.method.LinkMovementMethod;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
@ -40,6 +42,24 @@ public class AboutActivity extends AppCompatActivity {
setupRateUs();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_about, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.settings:
final Intent intent = new Intent(getApplicationContext(), SettingsActivity.class);
startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private void setupEmail() {
final String email = mRes.getString(R.string.email);
final String appName = mRes.getString(R.string.app_name);

View File

@ -12,7 +12,6 @@ import android.view.Menu;
import android.view.MenuItem;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.Toast;
import com.simplemobiletools.notes.Config;
import com.simplemobiletools.notes.Constants;
@ -39,6 +38,20 @@ public class MainActivity extends AppCompatActivity {
mNotesView.setText(text);
}
@Override
protected void onResume() {
super.onResume();
invalidateOptionsMenu();
}
@Override
protected void onPause() {
super.onPause();
if (Config.newInstance(getApplicationContext()).getIsAutosaveEnabled()) {
saveText(false);
}
}
@Override
protected void onDestroy() {
super.onDestroy();
@ -48,6 +61,9 @@ public class MainActivity extends AppCompatActivity {
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
if (Config.newInstance(getApplicationContext()).getIsAutosaveEnabled())
menu.findItem(R.id.save).setVisible(false);
return true;
}
@ -55,7 +71,7 @@ public class MainActivity extends AppCompatActivity {
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.save:
saveText();
saveText(true);
return true;
case R.id.share:
shareText();
@ -69,11 +85,14 @@ public class MainActivity extends AppCompatActivity {
}
}
private void saveText() {
private void saveText(boolean showToast) {
final String text = mNotesView.getText().toString().trim();
mPrefs.edit().putString(Constants.TEXT, text).apply();
Toast.makeText(this, getResources().getString(R.string.text_saved), Toast.LENGTH_SHORT).show();
if (showToast) {
Utils.showToast(getApplicationContext(), R.string.text_saved);
}
hideKeyboard();
updateWidget();
}
@ -81,7 +100,7 @@ public class MainActivity extends AppCompatActivity {
private void shareText() {
final String text = mNotesView.getText().toString().trim();
if (text.isEmpty()) {
Utils.showToast(this, R.string.cannot_share_empty_text);
Utils.showToast(getApplicationContext(), R.string.cannot_share_empty_text);
return;
}

View File

@ -0,0 +1,38 @@
package com.simplemobiletools.notes.activities;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.SwitchCompat;
import com.simplemobiletools.notes.Config;
import com.simplemobiletools.notes.R;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
public class SettingsActivity extends AppCompatActivity {
@BindView(R.id.settings_autosave) SwitchCompat mAutosaveSwitch;
private static Config mConfig;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
mConfig = Config.newInstance(getApplicationContext());
ButterKnife.bind(this);
setupAutosave();
}
private void setupAutosave() {
mAutosaveSwitch.setChecked(mConfig.getIsAutosaveEnabled());
}
@OnClick(R.id.settings_autosave_holder)
public void handleAutosave() {
mAutosaveSwitch.setChecked(!mAutosaveSwitch.isChecked());
mConfig.setIsAutosaveEnabled(mAutosaveSwitch.isChecked());
}
}

View File

@ -0,0 +1,40 @@
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
android:id="@+id/settings_scrollview"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/settings_holder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:id="@+id/settings_autosave_holder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/settings_padding"
android:background="?android:attr/selectableItemBackground"
android:padding="@dimen/activity_margin">
<TextView
android:id="@+id/settings_autosave_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:paddingLeft="@dimen/settings_padding"
android:text="@string/autosave"/>
<android.support.v7.widget.SwitchCompat
android:id="@+id/settings_autosave"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:background="@null"
android:clickable="false"/>
</RelativeLayout>
</LinearLayout>
</ScrollView>

View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/settings"
android:title="@string/settings"
app:showAsAction="never"/>
</menu>

View File

@ -1,6 +1,7 @@
<resources>
<dimen name="social_padding">12dp</dimen>
<dimen name="social_logo">50dp</dimen>
<dimen name="settings_padding">12dp</dimen>
<dimen name="normal_text_size">18sp</dimen>
<dimen name="config_text_size">22sp</dimen>

View File

@ -2,6 +2,7 @@
<dimen name="activity_margin">16dp</dimen>
<dimen name="social_padding">8dp</dimen>
<dimen name="social_logo">40dp</dimen>
<dimen name="settings_padding">8dp</dimen>
<dimen name="normal_text_size">14sp</dimen>
<dimen name="config_text_size">18sp</dimen>

View File

@ -8,6 +8,10 @@
<string name="text_saved">Text Saved</string>
<string name="simple_note">Simple Note</string>
<!-- Settings -->
<string name="settings">Settings</string>
<string name="autosave">Autosave</string>
<!-- About -->
<string name="about">About</string>
<string name="website">More simple apps and source code at:\nhttp://simplemobiletools.com</string>