mirror of
https://github.com/SimpleMobileTools/Simple-Notes.git
synced 2025-02-04 08:07:32 +01:00
convert dbhelper to kotlin
This commit is contained in:
parent
70d96bad81
commit
ddc5b01820
@ -43,7 +43,7 @@ public class MainActivity extends SimpleActivity implements OpenNoteDialog.OpenN
|
||||
setContentView(R.layout.activity_main);
|
||||
ButterKnife.bind(this);
|
||||
|
||||
mDb = DBHelper.newInstance(getApplicationContext());
|
||||
mDb = DBHelper.Companion.newInstance(getApplicationContext());
|
||||
mNotes = mDb.getNotes();
|
||||
updateSelectedNote(getConfig().getCurrentNoteId());
|
||||
}
|
||||
|
@ -1,141 +0,0 @@
|
||||
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;
|
||||
|
||||
import com.simplemobiletools.notes.Constants;
|
||||
import com.simplemobiletools.notes.models.Note;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
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_NAME = "notes";
|
||||
private static final String NOTE = "General note";
|
||||
|
||||
private static final String COL_ID = "id";
|
||||
private static final String COL_TITLE = "title";
|
||||
private static final String COL_VALUE = "value";
|
||||
|
||||
private Context mContext;
|
||||
private SQLiteDatabase mDb;
|
||||
|
||||
public static DBHelper newInstance(Context context) {
|
||||
return new DBHelper(context);
|
||||
}
|
||||
|
||||
private DBHelper(Context context) {
|
||||
super(context, DB_NAME, null, DB_VERSION);
|
||||
mContext = context;
|
||||
mDb = getWritableDatabase();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreate(SQLiteDatabase db) {
|
||||
db.execSQL("CREATE TABLE " + TABLE_NAME + " (" +
|
||||
COL_ID + " INTEGER PRIMARY KEY, " +
|
||||
COL_TITLE + " TEXT UNIQUE, " +
|
||||
COL_VALUE + " TEXT" +
|
||||
")"
|
||||
);
|
||||
|
||||
insertFirstNote(db);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
|
||||
|
||||
}
|
||||
|
||||
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(1, NOTE, text);
|
||||
insertNote(note, db);
|
||||
}
|
||||
|
||||
private void insertNote(Note note, SQLiteDatabase db) {
|
||||
final ContentValues values = fillContentValues(note);
|
||||
db.insert(TABLE_NAME, null, values);
|
||||
}
|
||||
|
||||
public int insertNote(Note note) {
|
||||
final ContentValues values = fillContentValues(note);
|
||||
return (int) mDb.insert(TABLE_NAME, null, values);
|
||||
}
|
||||
|
||||
private ContentValues fillContentValues(Note note) {
|
||||
final ContentValues values = new ContentValues();
|
||||
values.put(COL_TITLE, note.getTitle());
|
||||
values.put(COL_VALUE, note.getValue());
|
||||
return values;
|
||||
}
|
||||
|
||||
public void deleteNote(int id) {
|
||||
mDb.delete(TABLE_NAME, COL_ID + " = " + id, null);
|
||||
}
|
||||
|
||||
public boolean doesTitleExist(String title) {
|
||||
final String cols[] = {COL_ID};
|
||||
final String selection = COL_TITLE + " = ?";
|
||||
final String selectionArgs[] = {title};
|
||||
final Cursor cursor = mDb.query(TABLE_NAME, cols, selection, selectionArgs, null, null, null);
|
||||
|
||||
if (cursor == null)
|
||||
return false;
|
||||
|
||||
final int cnt = cursor.getCount();
|
||||
cursor.close();
|
||||
return cnt == 1;
|
||||
}
|
||||
|
||||
public List<Note> getNotes() {
|
||||
final List<Note> notes = new ArrayList<>();
|
||||
final String cols[] = {COL_ID, COL_TITLE, COL_VALUE};
|
||||
final Cursor cursor = mDb.query(TABLE_NAME, cols, null, null, null, null, COL_TITLE + " COLLATE NOCASE ASC");
|
||||
if (cursor != null) {
|
||||
if (cursor.moveToFirst()) {
|
||||
do {
|
||||
final int id = cursor.getInt(cursor.getColumnIndex(COL_ID));
|
||||
final String title = cursor.getString(cursor.getColumnIndex(COL_TITLE));
|
||||
final String value = cursor.getString(cursor.getColumnIndex(COL_VALUE));
|
||||
final Note note = new Note(id, title, value);
|
||||
notes.add(note);
|
||||
} while (cursor.moveToNext());
|
||||
}
|
||||
cursor.close();
|
||||
}
|
||||
|
||||
return notes;
|
||||
}
|
||||
|
||||
public Note getNote(int id) {
|
||||
final String cols[] = {COL_TITLE, COL_VALUE};
|
||||
final String selection = COL_ID + " = ?";
|
||||
final String selectionArgs[] = {String.valueOf(id)};
|
||||
final Cursor cursor = mDb.query(TABLE_NAME, cols, selection, selectionArgs, null, null, null);
|
||||
Note note = null;
|
||||
if (cursor != null) {
|
||||
if (cursor.moveToFirst()) {
|
||||
final String title = cursor.getString(cursor.getColumnIndex(COL_TITLE));
|
||||
final String value = cursor.getString(cursor.getColumnIndex(COL_VALUE));
|
||||
note = new Note(id, title, value);
|
||||
}
|
||||
cursor.close();
|
||||
}
|
||||
return note;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
@ -7,9 +7,7 @@ class Config(context: Context) {
|
||||
private val mPrefs: SharedPreferences
|
||||
|
||||
companion object {
|
||||
fun newInstance(context: Context): Config {
|
||||
return Config(context)
|
||||
}
|
||||
fun newInstance(context: Context) = Config(context)
|
||||
}
|
||||
|
||||
init {
|
||||
|
@ -0,0 +1,126 @@
|
||||
package com.simplemobiletools.notes.databases
|
||||
|
||||
import android.content.ContentValues
|
||||
import android.content.Context
|
||||
import android.database.Cursor
|
||||
import android.database.sqlite.SQLiteDatabase
|
||||
import android.database.sqlite.SQLiteOpenHelper
|
||||
import com.simplemobiletools.notes.Constants
|
||||
import com.simplemobiletools.notes.models.Note
|
||||
import java.util.*
|
||||
|
||||
class DBHelper private constructor(private val mContext: Context) : SQLiteOpenHelper(mContext, DBHelper.DB_NAME, null, DBHelper.DB_VERSION) {
|
||||
private val mDb: SQLiteDatabase
|
||||
|
||||
companion object {
|
||||
private val DB_NAME = "notes.db"
|
||||
private val DB_VERSION = 1
|
||||
private val TABLE_NAME = "notes"
|
||||
private val NOTE = "General note"
|
||||
|
||||
private val COL_ID = "id"
|
||||
private val COL_TITLE = "title"
|
||||
private val COL_VALUE = "value"
|
||||
|
||||
fun newInstance(context: Context) = DBHelper(context)
|
||||
}
|
||||
|
||||
init {
|
||||
mDb = writableDatabase
|
||||
}
|
||||
|
||||
override fun onCreate(db: SQLiteDatabase) {
|
||||
db.execSQL("CREATE TABLE $TABLE_NAME ($COL_ID INTEGER PRIMARY KEY, $COL_TITLE TEXT UNIQUE, $COL_VALUE TEXT)")
|
||||
insertFirstNote(db)
|
||||
}
|
||||
|
||||
override fun onUpgrade(db: SQLiteDatabase, oldVersion: Int, newVersion: Int) {
|
||||
|
||||
}
|
||||
|
||||
private fun insertFirstNote(db: SQLiteDatabase) {
|
||||
val prefs = mContext.getSharedPreferences(Constants.PREFS_KEY, Context.MODE_PRIVATE)
|
||||
val text = prefs.getString(Constants.TEXT, "")
|
||||
val note = Note(1, NOTE, text)
|
||||
insertNote(note, db)
|
||||
}
|
||||
|
||||
private fun insertNote(note: Note, db: SQLiteDatabase) {
|
||||
val values = fillContentValues(note)
|
||||
db.insert(TABLE_NAME, null, values)
|
||||
}
|
||||
|
||||
fun insertNote(note: Note): Int {
|
||||
val values = fillContentValues(note)
|
||||
return mDb.insert(TABLE_NAME, null, values).toInt()
|
||||
}
|
||||
|
||||
private fun fillContentValues(note: Note): ContentValues {
|
||||
return ContentValues().apply {
|
||||
put(COL_TITLE, note.title)
|
||||
put(COL_VALUE, note.value)
|
||||
}
|
||||
}
|
||||
|
||||
fun deleteNote(id: Int) {
|
||||
mDb.delete(TABLE_NAME, COL_ID + " = " + id, null)
|
||||
}
|
||||
|
||||
fun doesTitleExist(title: String): Boolean {
|
||||
val cols = arrayOf(COL_ID)
|
||||
val selection = COL_TITLE + " = ?"
|
||||
val selectionArgs = arrayOf(title)
|
||||
val cursor = mDb.query(TABLE_NAME, cols, selection, selectionArgs, null, null, null) ?: return false
|
||||
val cnt = cursor.count
|
||||
cursor.close()
|
||||
return cnt == 1
|
||||
}
|
||||
|
||||
fun getNotes(): List<Note> {
|
||||
val notes = ArrayList<Note>()
|
||||
val cols = arrayOf(COL_ID, COL_TITLE, COL_VALUE)
|
||||
var cursor: Cursor? = null
|
||||
try {
|
||||
cursor = mDb.query(TABLE_NAME, cols, null, null, null, null, "$COL_TITLE COLLATE NOCASE ASC")
|
||||
if (cursor != null && cursor.moveToFirst()) {
|
||||
do {
|
||||
val id = cursor.getInt(cursor.getColumnIndex(COL_ID))
|
||||
val title = cursor.getString(cursor.getColumnIndex(COL_TITLE))
|
||||
val value = cursor.getString(cursor.getColumnIndex(COL_VALUE))
|
||||
val note = Note(id, title, value)
|
||||
notes.add(note)
|
||||
} while (cursor.moveToNext())
|
||||
}
|
||||
} finally {
|
||||
cursor?.close()
|
||||
}
|
||||
|
||||
return notes
|
||||
}
|
||||
|
||||
fun getNote(id: Int): Note? {
|
||||
val cols = arrayOf(COL_TITLE, COL_VALUE)
|
||||
val selection = "$COL_ID = ?"
|
||||
val selectionArgs = arrayOf(id.toString())
|
||||
var note: Note? = null
|
||||
var cursor: Cursor? = null
|
||||
try {
|
||||
cursor = mDb.query(TABLE_NAME, cols, selection, selectionArgs, null, null, null)
|
||||
if (cursor != null && cursor.moveToFirst()) {
|
||||
val title = cursor.getString(cursor.getColumnIndex(COL_TITLE))
|
||||
val value = cursor.getString(cursor.getColumnIndex(COL_VALUE))
|
||||
note = Note(id, title, value)
|
||||
}
|
||||
} finally {
|
||||
cursor?.close()
|
||||
}
|
||||
return note
|
||||
}
|
||||
|
||||
fun updateNote(note: Note) {
|
||||
val values = fillContentValues(note)
|
||||
val selection = COL_ID + " = ?"
|
||||
val selectionArgs = arrayOf(note.id.toString())
|
||||
mDb.update(TABLE_NAME, values, selection, selectionArgs)
|
||||
}
|
||||
}
|
@ -17,7 +17,7 @@ class OpenNoteDialog(val activity: Activity) : RadioGroup.OnCheckedChangeListene
|
||||
val view = activity.layoutInflater.inflate(R.layout.dialog_radio_group, null) as RadioGroup
|
||||
view.setOnCheckedChangeListener(this)
|
||||
|
||||
val notes = DBHelper.newInstance(activity).notes
|
||||
val notes = DBHelper.newInstance(activity).getNotes()
|
||||
notes.forEach {
|
||||
val radioButton = activity.layoutInflater.inflate(R.layout.radio_button, null) as RadioButton
|
||||
radioButton.apply {
|
||||
|
@ -19,7 +19,7 @@ class WidgetNoteDialog(val activity: Activity) : RadioGroup.OnCheckedChangeListe
|
||||
view.setOnCheckedChangeListener(this)
|
||||
|
||||
val db = DBHelper.newInstance(activity)
|
||||
val notes = db.notes
|
||||
val notes = db.getNotes()
|
||||
notes.forEach {
|
||||
val radioButton = activity.layoutInflater.inflate(R.layout.radio_button, null) as RadioButton
|
||||
radioButton.apply {
|
||||
|
Loading…
x
Reference in New Issue
Block a user