store the note in database instead of shared prefs

This commit is contained in:
tibbi 2016-10-01 10:12:48 +02:00
parent 50cee9dcc5
commit 00f361ffc5
3 changed files with 75 additions and 31 deletions

View File

@ -2,7 +2,6 @@ package com.simplemobiletools.notes.activities;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Resources; import android.content.res.Resources;
import android.os.Bundle; import android.os.Bundle;
import android.util.TypedValue; import android.util.TypedValue;
@ -11,9 +10,10 @@ import android.view.MenuItem;
import android.view.inputmethod.InputMethodManager; import android.view.inputmethod.InputMethodManager;
import android.widget.EditText; import android.widget.EditText;
import com.simplemobiletools.notes.Constants;
import com.simplemobiletools.notes.R; import com.simplemobiletools.notes.R;
import com.simplemobiletools.notes.Utils; import com.simplemobiletools.notes.Utils;
import com.simplemobiletools.notes.databases.DBHelper;
import com.simplemobiletools.notes.models.Note;
import butterknife.BindView; import butterknife.BindView;
import butterknife.ButterKnife; import butterknife.ButterKnife;
@ -21,7 +21,8 @@ import butterknife.ButterKnife;
public class MainActivity extends SimpleActivity { public class MainActivity extends SimpleActivity {
@BindView(R.id.notes_view) EditText mNotesView; @BindView(R.id.notes_view) EditText mNotesView;
private SharedPreferences mPrefs; private DBHelper mDb;
private Note mCurrentNote;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
@ -29,8 +30,9 @@ public class MainActivity extends SimpleActivity {
setContentView(R.layout.activity_main); setContentView(R.layout.activity_main);
ButterKnife.bind(this); ButterKnife.bind(this);
mPrefs = getSharedPreferences(Constants.PREFS_KEY, Context.MODE_PRIVATE); mDb = DBHelper.newInstance(getApplicationContext());
mNotesView.setText(getSavedNote()); mCurrentNote = mDb.getGeneralNote();
mNotesView.setText(mCurrentNote.getValue());
} }
@Override @Override
@ -77,12 +79,13 @@ public class MainActivity extends SimpleActivity {
private void saveText() { private void saveText() {
final String newText = getCurrentNote(); final String newText = getCurrentNote();
final String oldText = mPrefs.getString(Constants.TEXT, ""); final String oldText = mCurrentNote.getValue();
if (!newText.equals(oldText)) { if (!newText.equals(oldText)) {
Utils.showToast(getApplicationContext(), R.string.note_saved); Utils.showToast(getApplicationContext(), R.string.note_saved);
mCurrentNote.setValue(newText);
mDb.updateNote(mCurrentNote);
} }
mPrefs.edit().putString(Constants.TEXT, newText).apply();
hideKeyboard(); hideKeyboard();
Utils.updateWidget(getApplicationContext()); Utils.updateWidget(getApplicationContext());
} }
@ -108,10 +111,6 @@ public class MainActivity extends SimpleActivity {
return mNotesView.getText().toString().trim(); return mNotesView.getText().toString().trim();
} }
private String getSavedNote() {
return mPrefs.getString(Constants.TEXT, "");
}
private void hideKeyboard() { private void hideKeyboard() {
final InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); final InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(mNotesView.getWindowToken(), 0); imm.hideSoftInputFromWindow(mNotesView.getWindowToken(), 0);

View File

@ -3,6 +3,7 @@ package com.simplemobiletools.notes.databases;
import android.content.ContentValues; import android.content.ContentValues;
import android.content.Context; import android.content.Context;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper; import android.database.sqlite.SQLiteOpenHelper;
@ -12,12 +13,12 @@ import com.simplemobiletools.notes.models.Note;
public class DBHelper extends SQLiteOpenHelper { public class DBHelper extends SQLiteOpenHelper {
private static final String DB_NAME = "notes.db"; private static final String DB_NAME = "notes.db";
private static final int DB_VERSION = 1; private static final int DB_VERSION = 1;
private static final String TABLE = "notes"; private static final String TABLE_NAME = "notes";
private static final String NOTE = "General note"; private static final String NOTE = "General note";
private static final String COL_ID = "id"; private static final String COL_ID = "id";
private static final String COL_NAME = "name"; private static final String COL_TITLE = "title";
private static final String COL_TEXT = "value"; private static final String COL_VALUE = "value";
private Context mContext; private Context mContext;
private SQLiteDatabase mDb; private SQLiteDatabase mDb;
@ -34,10 +35,10 @@ public class DBHelper extends SQLiteOpenHelper {
@Override @Override
public void onCreate(SQLiteDatabase db) { public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + TABLE + " (" + db.execSQL("CREATE TABLE " + TABLE_NAME + " (" +
COL_ID + " INTEGER PRIMARY KEY, " + COL_ID + " INTEGER PRIMARY KEY, " +
COL_NAME + " TEXT, " + COL_TITLE + " TEXT UNIQUE, " +
COL_TEXT + " TEXT" + COL_VALUE + " TEXT" +
")" ")"
); );
@ -52,24 +53,51 @@ public class DBHelper extends SQLiteOpenHelper {
private void insertFirstNote(SQLiteDatabase db) { private void insertFirstNote(SQLiteDatabase db) {
final SharedPreferences prefs = mContext.getSharedPreferences(Constants.PREFS_KEY, Context.MODE_PRIVATE); final SharedPreferences prefs = mContext.getSharedPreferences(Constants.PREFS_KEY, Context.MODE_PRIVATE);
final String text = prefs.getString(Constants.TEXT, ""); final String text = prefs.getString(Constants.TEXT, "");
final Note note = new Note(0, NOTE, text); final Note note = new Note(1, NOTE, text);
insertNote(note, db); insertNote(note, db);
} }
private void insertNote(Note note, SQLiteDatabase db) { private void insertNote(Note note, SQLiteDatabase db) {
final ContentValues values = fillContentValues(note); final ContentValues values = fillContentValues(note);
db.insert(TABLE, null, values); db.insert(TABLE_NAME, null, values);
} }
public void insertNote(Note note) { public void insertNote(Note note) {
final ContentValues values = fillContentValues(note); final ContentValues values = fillContentValues(note);
mDb.insert(TABLE, null, values); mDb.insert(TABLE_NAME, null, values);
} }
private ContentValues fillContentValues(Note note) { private ContentValues fillContentValues(Note note) {
final ContentValues values = new ContentValues(); final ContentValues values = new ContentValues();
values.put(COL_NAME, note.getName()); values.put(COL_TITLE, note.getTitle());
values.put(COL_TEXT, note.getText()); values.put(COL_VALUE, note.getValue());
return values; return values;
} }
public void updateNote(Note note) {
final ContentValues values = fillContentValues(note);
final String selection = COL_ID + " = ?";
final String selectionArgs[] = new String[]{String.valueOf(note.getId())};
mDb.update(TABLE_NAME, values, selection, selectionArgs);
}
public Note getGeneralNote() {
final String cols[] = {COL_ID, COL_TITLE, COL_VALUE};
final String selection = COL_TITLE + " = ?";
final String selectionArgs[] = {NOTE};
Cursor cursor = mDb.query(TABLE_NAME, cols, selection, selectionArgs, null, null, null);
if (cursor == null)
return null;
if (cursor.moveToFirst()) {
final int id = cursor.getInt(cursor.getColumnIndex(COL_ID));
final String name = cursor.getString(cursor.getColumnIndex(COL_TITLE));
final String text = cursor.getString(cursor.getColumnIndex(COL_VALUE));
cursor.close();
return new Note(id, name, text);
}
cursor.close();
return null;
}
} }

View File

@ -2,24 +2,41 @@ package com.simplemobiletools.notes.models;
public class Note { public class Note {
private int mId; private int mId;
private String mName; private String mTitle;
private String mText; private String mValue;
public Note(int id, String name, String text) { public Note(int id, String title, String value) {
mId = id; mId = id;
mName = name; mTitle = title;
mText = text; mValue = value;
} }
public int getId() { public int getId() {
return mId; return mId;
} }
public String getName() { public String getTitle() {
return mName; return mTitle;
} }
public String getText() { public String getValue() {
return mText; return mValue;
}
public void setValue(String value) {
mValue = value;
}
public void setTitle(String title) {
mTitle = title;
}
@Override
public String toString() {
return "Note {" +
"id=" + getId() +
", title=" + getTitle() +
", value=" + getValue() +
"}";
} }
} }