diff --git a/app/src/main/java/app/fedilab/android/client/Entities/UserNote.java b/app/src/main/java/app/fedilab/android/client/Entities/UserNote.java new file mode 100644 index 000000000..062d4fa7b --- /dev/null +++ b/app/src/main/java/app/fedilab/android/client/Entities/UserNote.java @@ -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 . */ +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; + } +} diff --git a/app/src/main/java/app/fedilab/android/sqlite/NotesDAO.java b/app/src/main/java/app/fedilab/android/sqlite/NotesDAO.java new file mode 100644 index 000000000..d6367efd8 --- /dev/null +++ b/app/src/main/java/app/fedilab/android/sqlite/NotesDAO.java @@ -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 . */ + +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 + */ + 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; + } +} diff --git a/app/src/main/java/app/fedilab/android/sqlite/Sqlite.java b/app/src/main/java/app/fedilab/android/sqlite/Sqlite.java index 252fa2801..6aeac98c9 100644 --- a/app/src/main/java/app/fedilab/android/sqlite/Sqlite.java +++ b/app/src/main/java/app/fedilab/android/sqlite/Sqlite.java @@ -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; } diff --git a/app/src/main/res/drawable/ic_note.xml b/app/src/main/res/drawable/ic_note.xml new file mode 100644 index 000000000..e55f93ca8 --- /dev/null +++ b/app/src/main/res/drawable/ic_note.xml @@ -0,0 +1,5 @@ + + + diff --git a/app/src/main/res/menu/main_showaccount.xml b/app/src/main/res/menu/main_showaccount.xml index 2e0b8ea5c..385f078b9 100644 --- a/app/src/main/res/menu/main_showaccount.xml +++ b/app/src/main/res/menu/main_showaccount.xml @@ -16,6 +16,11 @@ android:title="@string/action_open_in_web" android:icon="@drawable/ic_open_with" app:showAsAction="ifRoom" /> + Live notifications delayed No live notifications Notifications will be fetched every 15 minutes. + Add notes \ No newline at end of file