Prepares db + menu

This commit is contained in:
tom79 2019-09-12 18:52:17 +02:00
parent fe45028215
commit 964c0d733a
6 changed files with 192 additions and 2 deletions

View File

@ -0,0 +1,47 @@
package app.fedilab.android.client.Entities;
/* Copyright 2019 Thomas Schneider
*
* This file is a part of Fedilab
*
* This program is free software; you can redistribute it and/or modify it under the terms of the
* GNU General Public License as published by the Free Software Foundation; either version 3 of the
* License, or (at your option) any later version.
*
* Fedilab is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*
* You should have received a copy of the GNU General Public License along with Fedilab; if not,
* see <http://www.gnu.org/licenses>. */
import java.util.Date;
public class UserNote {
private String acct;
private String note;
private Date creation_date;
public String getAcct() {
return acct;
}
public void setAcct(String acct) {
this.acct = acct;
}
public String getNote() {
return note;
}
public void setNote(String note) {
this.note = note;
}
public Date getCreation_date() {
return creation_date;
}
public void setCreation_date(Date creation_date) {
this.creation_date = creation_date;
}
}

View File

@ -0,0 +1,120 @@
package app.fedilab.android.sqlite;
/* Copyright 2019 Thomas Schneider
*
* This file is a part of Fedilab
*
* This program is free software; you can redistribute it and/or modify it under the terms of the
* GNU General Public License as published by the Free Software Foundation; either version 3 of the
* License, or (at your option) any later version.
*
* Fedilab is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*
* You should have received a copy of the GNU General Public License along with Fedilab; if not,
* see <http://www.gnu.org/licenses>. */
import android.content.ContentValues;
import android.content.Context;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import java.util.Date;
import app.fedilab.android.client.Entities.UserNote;
import app.fedilab.android.helper.Helper;
/**
* Created by Thomas on 12/09/2019.
* Manage user notes in DB
*/
public class NotesDAO {
private SQLiteDatabase db;
public Context context;
public NotesDAO(Context context, SQLiteDatabase db) {
//Creation of the DB with tables
this.context = context;
this.db = db;
SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
}
//------- INSERTIONS -------
/**
* Insert a note in database
*
* @param userNote UserNote
*/
public void insertInstance(UserNote userNote) {
UserNote notes = getUserNote(userNote.getAcct());
//There is a note
if( userNote.getNote() != null && userNote.getNote().trim().length() > 0) {
if (notes != null) { //Notes already exist, it needs an update
ContentValues values = new ContentValues();
values.put(Sqlite.COL_NOTE, userNote.getNote());
try {
db.update(Sqlite.TABLE_USER_NOTES, values, Sqlite.COL_ACCT + " = ? ", new String[]{userNote.getAcct()});
} catch (Exception ignored) {
}
} else { //notes don't exist, it's an insert
ContentValues values = new ContentValues();
values.put(Sqlite.COL_NOTE, userNote.getNote());
values.put(Sqlite.COL_ACCT, userNote.getAcct());
values.put(Sqlite.COL_DATE_CREATION, Helper.dateToString(new Date()));
//Inserts noted
try {
db.insert(Sqlite.TABLE_USER_NOTES, null, values);
} catch (Exception ignored) {
}
}
}else{ //It's empty, it's a deletion
db.delete(Sqlite.TABLE_USER_NOTES, Sqlite.COL_ACCT + " = \"" + userNote.getAcct() + "\"", null);
}
}
//------- GETTERS -------
/**
* Returns notes from an account
*
* @return UserNote
*/
public UserNote getUserNote(String acct) {
try {
Cursor c = db.query(Sqlite.TABLE_USER_NOTES, null, Sqlite.COL_ACCT + " = \"" + acct + "\"", null, null, null, null, "1");
return cursorToNote(c);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/***
* Method to hydrate notes from database
* @param c Cursor
* @return List<RemoteInstance>
*/
private UserNote cursorToNote(Cursor c) {
//No element found
if (c.getCount() == 0) {
c.close();
return null;
}
UserNote userNote = new UserNote();
c.moveToFirst();
userNote.setAcct(c.getString(c.getColumnIndex(Sqlite.COL_ACCT)));
userNote.setNote(c.getString(c.getColumnIndex(Sqlite.COL_NOTE)));
userNote.setCreation_date(Helper.stringToDate(context, c.getString(c.getColumnIndex(Sqlite.COL_DATE_CREATION))));
//Close the cursor
c.close();
return userNote;
}
}

View File

@ -49,7 +49,7 @@ import static android.content.Context.MODE_PRIVATE;
public class Sqlite extends SQLiteOpenHelper {
public static final int DB_VERSION = 35;
public static final int DB_VERSION = 36;
public static final String DB_NAME = "mastodon_etalab_db";
public static SQLiteDatabase db;
private static Sqlite sInstance;
@ -104,6 +104,9 @@ public class Sqlite extends SQLiteOpenHelper {
//Table for main menu items
public static final String TABLE_MAIN_MENU_ITEMS = "MAIN_MENU_ITEMS";
//Table for taking notes about accounts
public static final String TABLE_USER_NOTES = "USER_NOTES";
static final String COL_USER_ID = "USER_ID";
static final String COL_USERNAME = "USERNAME";
static final String COL_ACCT = "ACCT";
@ -352,6 +355,13 @@ public class Sqlite extends SQLiteOpenHelper {
+ COL_NAV_HOWTO + " INTEGER DEFAULT 1)";
private static final String CREATE_TABLE_USER_NOTES = "CREATE TABLE "
+ TABLE_USER_NOTES + "("
+ COL_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ COL_ACCT + " TEXT NOT NULL, "
+ COL_NOTE + " TEXT, "
+ COL_DATE_CREATION + " TEXT NOT NULL)";
public Sqlite(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
}
@ -382,7 +392,7 @@ public class Sqlite extends SQLiteOpenHelper {
db.execSQL(CREATE_TABLE_TIMELINE_CACHE);
db.execSQL(CREATE_TABLE_NOTIFICATIONS);
db.execSQL(CREATE_TABLE_MAIN_MENU_ITEMS);
db.execSQL(CREATE_TABLE_USER_NOTES);
}
@Override
@ -522,6 +532,8 @@ public class Sqlite extends SQLiteOpenHelper {
db.execSQL("DELETE FROM " + TABLE_STATUSES_CACHE);
db.execSQL("DELETE FROM " + TABLE_NOTIFICATION_CACHE);
db.execSQL(CREATE_UNIQUE_CACHE_INDEX);
case 35:
db.execSQL(CREATE_TABLE_USER_NOTES);
default:
break;
}

View File

@ -0,0 +1,5 @@
<vector android:height="24dp" android:tint="#FFFFFF"
android:viewportHeight="24.0" android:viewportWidth="24.0"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="#FF000000" android:pathData="M22,10l-6,-6L4,4c-1.1,0 -2,0.9 -2,2v12.01c0,1.1 0.9,1.99 2,1.99l16,-0.01c1.1,0 2,-0.89 2,-1.99v-8zM15,5.5l5.5,5.5L15,11L15,5.5z"/>
</vector>

View File

@ -16,6 +16,11 @@
android:title="@string/action_open_in_web"
android:icon="@drawable/ic_open_with"
app:showAsAction="ifRoom" />
<item
android:id="@+id/action_add_notes"
android:title="@string/action_add_notes"
android:icon="@drawable/ic_note"
app:showAsAction="ifRoom" />
<item
android:id="@+id/action_mute"
android:title="@string/more_action_1"

View File

@ -1213,4 +1213,5 @@
<string name="live_delayed">Live notifications delayed</string>
<string name="no_live_notif">No live notifications</string>
<string name="no_live_indication">Notifications will be fetched every 15 minutes.</string>
<string name="action_add_notes">Add notes</string>
</resources>