mirror of
https://github.com/SimpleMobileTools/Simple-Notes.git
synced 2025-02-07 15:08:40 +01:00
store the note in database instead of shared prefs
This commit is contained in:
parent
50cee9dcc5
commit
00f361ffc5
@ -2,7 +2,6 @@ package com.simplemobiletools.notes.activities;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.SharedPreferences;
|
||||
import android.content.res.Resources;
|
||||
import android.os.Bundle;
|
||||
import android.util.TypedValue;
|
||||
@ -11,9 +10,10 @@ import android.view.MenuItem;
|
||||
import android.view.inputmethod.InputMethodManager;
|
||||
import android.widget.EditText;
|
||||
|
||||
import com.simplemobiletools.notes.Constants;
|
||||
import com.simplemobiletools.notes.R;
|
||||
import com.simplemobiletools.notes.Utils;
|
||||
import com.simplemobiletools.notes.databases.DBHelper;
|
||||
import com.simplemobiletools.notes.models.Note;
|
||||
|
||||
import butterknife.BindView;
|
||||
import butterknife.ButterKnife;
|
||||
@ -21,7 +21,8 @@ import butterknife.ButterKnife;
|
||||
public class MainActivity extends SimpleActivity {
|
||||
@BindView(R.id.notes_view) EditText mNotesView;
|
||||
|
||||
private SharedPreferences mPrefs;
|
||||
private DBHelper mDb;
|
||||
private Note mCurrentNote;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
@ -29,8 +30,9 @@ public class MainActivity extends SimpleActivity {
|
||||
setContentView(R.layout.activity_main);
|
||||
ButterKnife.bind(this);
|
||||
|
||||
mPrefs = getSharedPreferences(Constants.PREFS_KEY, Context.MODE_PRIVATE);
|
||||
mNotesView.setText(getSavedNote());
|
||||
mDb = DBHelper.newInstance(getApplicationContext());
|
||||
mCurrentNote = mDb.getGeneralNote();
|
||||
mNotesView.setText(mCurrentNote.getValue());
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -77,12 +79,13 @@ public class MainActivity extends SimpleActivity {
|
||||
|
||||
private void saveText() {
|
||||
final String newText = getCurrentNote();
|
||||
final String oldText = mPrefs.getString(Constants.TEXT, "");
|
||||
final String oldText = mCurrentNote.getValue();
|
||||
if (!newText.equals(oldText)) {
|
||||
Utils.showToast(getApplicationContext(), R.string.note_saved);
|
||||
mCurrentNote.setValue(newText);
|
||||
mDb.updateNote(mCurrentNote);
|
||||
}
|
||||
|
||||
mPrefs.edit().putString(Constants.TEXT, newText).apply();
|
||||
hideKeyboard();
|
||||
Utils.updateWidget(getApplicationContext());
|
||||
}
|
||||
@ -108,10 +111,6 @@ public class MainActivity extends SimpleActivity {
|
||||
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);
|
||||
|
@ -3,6 +3,7 @@ package com.simplemobiletools.notes.databases;
|
||||
import android.content.ContentValues;
|
||||
import android.content.Context;
|
||||
import android.content.SharedPreferences;
|
||||
import android.database.Cursor;
|
||||
import android.database.sqlite.SQLiteDatabase;
|
||||
import android.database.sqlite.SQLiteOpenHelper;
|
||||
|
||||
@ -12,12 +13,12 @@ import com.simplemobiletools.notes.models.Note;
|
||||
public class DBHelper extends SQLiteOpenHelper {
|
||||
private static final String DB_NAME = "notes.db";
|
||||
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 COL_ID = "id";
|
||||
private static final String COL_NAME = "name";
|
||||
private static final String COL_TEXT = "value";
|
||||
private static final String COL_TITLE = "title";
|
||||
private static final String COL_VALUE = "value";
|
||||
|
||||
private Context mContext;
|
||||
private SQLiteDatabase mDb;
|
||||
@ -34,10 +35,10 @@ public class DBHelper extends SQLiteOpenHelper {
|
||||
|
||||
@Override
|
||||
public void onCreate(SQLiteDatabase db) {
|
||||
db.execSQL("CREATE TABLE " + TABLE + " (" +
|
||||
db.execSQL("CREATE TABLE " + TABLE_NAME + " (" +
|
||||
COL_ID + " INTEGER PRIMARY KEY, " +
|
||||
COL_NAME + " TEXT, " +
|
||||
COL_TEXT + " TEXT" +
|
||||
COL_TITLE + " TEXT UNIQUE, " +
|
||||
COL_VALUE + " TEXT" +
|
||||
")"
|
||||
);
|
||||
|
||||
@ -52,24 +53,51 @@ public class DBHelper extends SQLiteOpenHelper {
|
||||
private void insertFirstNote(SQLiteDatabase db) {
|
||||
final SharedPreferences prefs = mContext.getSharedPreferences(Constants.PREFS_KEY, Context.MODE_PRIVATE);
|
||||
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);
|
||||
}
|
||||
|
||||
private void insertNote(Note note, SQLiteDatabase db) {
|
||||
final ContentValues values = fillContentValues(note);
|
||||
db.insert(TABLE, null, values);
|
||||
db.insert(TABLE_NAME, null, values);
|
||||
}
|
||||
|
||||
public void insertNote(Note note) {
|
||||
final ContentValues values = fillContentValues(note);
|
||||
mDb.insert(TABLE, null, values);
|
||||
mDb.insert(TABLE_NAME, null, values);
|
||||
}
|
||||
|
||||
private ContentValues fillContentValues(Note note) {
|
||||
final ContentValues values = new ContentValues();
|
||||
values.put(COL_NAME, note.getName());
|
||||
values.put(COL_TEXT, note.getText());
|
||||
values.put(COL_TITLE, note.getTitle());
|
||||
values.put(COL_VALUE, note.getValue());
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
@ -2,24 +2,41 @@ package com.simplemobiletools.notes.models;
|
||||
|
||||
public class Note {
|
||||
private int mId;
|
||||
private String mName;
|
||||
private String mText;
|
||||
private String mTitle;
|
||||
private String mValue;
|
||||
|
||||
public Note(int id, String name, String text) {
|
||||
public Note(int id, String title, String value) {
|
||||
mId = id;
|
||||
mName = name;
|
||||
mText = text;
|
||||
mTitle = title;
|
||||
mValue = value;
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return mId;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return mName;
|
||||
public String getTitle() {
|
||||
return mTitle;
|
||||
}
|
||||
|
||||
public String getText() {
|
||||
return mText;
|
||||
public String getValue() {
|
||||
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() +
|
||||
"}";
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user