fedilab-Android-App/app/src/main/java/app/fedilab/android/sqlite/MainMenuDAO.java

250 lines
12 KiB
Java
Raw Normal View History

2019-08-28 12:00:14 +02:00
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;
2019-08-28 16:21:50 +02:00
2019-08-28 12:00:14 +02:00
import app.fedilab.android.client.Entities.MainMenuItem;
import app.fedilab.android.helper.Helper;
/**
* Created by Thomas on 28/08/2019.
* Manage menu items in DB
*/
public class MainMenuDAO {
public Context context;
2021-01-19 17:43:51 +01:00
private final SQLiteDatabase db;
2019-08-28 12:00:14 +02:00
public MainMenuDAO(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 menu configuration name in database
2019-09-06 17:55:14 +02:00
*
2019-08-28 12:00:14 +02:00
* @param mainMenuItem MainMenuItem
2019-09-06 17:55:14 +02:00
* @param userId String
* @param instance String
2019-08-28 12:00:14 +02:00
*/
2019-08-28 14:43:10 +02:00
public void insertMenu(MainMenuItem mainMenuItem, String userId, String instance) {
2019-08-28 12:00:14 +02:00
ContentValues values = new ContentValues();
values.put(Sqlite.COL_INSTANCE, instance);
values.put(Sqlite.COL_USER_ID, userId);
2019-09-06 17:55:14 +02:00
values.put(Sqlite.COL_NAV_NEWS, mainMenuItem.isNav_news() ? 1 : 0);
values.put(Sqlite.COL_NAV_LIST, mainMenuItem.isNav_list() ? 1 : 0);
values.put(Sqlite.COL_NAV_SCHEDULED, mainMenuItem.isNav_scheduled() ? 1 : 0);
values.put(Sqlite.COL_NAV_ARCHIVE, mainMenuItem.isNav_archive() ? 1 : 0);
values.put(Sqlite.COL_NAV_ARCHIVE_NOTIFICATIONS, mainMenuItem.isNav_archive_notifications() ? 1 : 0);
values.put(Sqlite.COL_NAV_PEERTUBE, mainMenuItem.isNav_peertube() ? 1 : 0);
values.put(Sqlite.COL_NAV_FILTERS, mainMenuItem.isNav_filters() ? 1 : 0);
values.put(Sqlite.COL_NAV_HOW_TO_FOLLOW, mainMenuItem.isNav_how_to_follow() ? 1 : 0);
values.put(Sqlite.COL_NAV_BLOCKED, mainMenuItem.isNav_blocked() ? 1 : 0);
values.put(Sqlite.COL_NAV_MUTED, mainMenuItem.isNav_muted() ? 1 : 0);
values.put(Sqlite.COL_NAV_BLOCKED_DOMAINS, mainMenuItem.isNav_blocked() ? 1 : 0);
values.put(Sqlite.COL_NAV_HOWTO, mainMenuItem.isNav_howto() ? 1 : 0);
2019-08-28 12:00:14 +02:00
//Inserts menu conf
2019-09-06 17:55:14 +02:00
try {
2019-08-28 12:00:14 +02:00
db.insert(Sqlite.TABLE_MAIN_MENU_ITEMS, null, values);
2019-09-06 17:55:14 +02:00
} catch (Exception ignored) {
}
2019-08-28 12:00:14 +02:00
}
//------- INSERTIONS -------
/**
* Insert a menu configuration name in database
2019-09-06 17:55:14 +02:00
*
2019-08-28 12:00:14 +02:00
* @param mainMenuItem MainMenuItem
*/
2019-08-28 14:43:10 +02:00
public void insertMenu(MainMenuItem mainMenuItem) {
2019-08-28 12:00:14 +02:00
SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
String userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null);
String instance = Helper.getLiveInstance(context);
ContentValues values = new ContentValues();
values.put(Sqlite.COL_INSTANCE, instance);
values.put(Sqlite.COL_USER_ID, userId);
2019-09-06 17:55:14 +02:00
values.put(Sqlite.COL_NAV_NEWS, mainMenuItem.isNav_news() ? 1 : 0);
values.put(Sqlite.COL_NAV_LIST, mainMenuItem.isNav_list() ? 1 : 0);
values.put(Sqlite.COL_NAV_SCHEDULED, mainMenuItem.isNav_scheduled() ? 1 : 0);
values.put(Sqlite.COL_NAV_ARCHIVE, mainMenuItem.isNav_archive() ? 1 : 0);
values.put(Sqlite.COL_NAV_ARCHIVE_NOTIFICATIONS, mainMenuItem.isNav_archive_notifications() ? 1 : 0);
values.put(Sqlite.COL_NAV_PEERTUBE, mainMenuItem.isNav_peertube() ? 1 : 0);
values.put(Sqlite.COL_NAV_FILTERS, mainMenuItem.isNav_filters() ? 1 : 0);
values.put(Sqlite.COL_NAV_HOW_TO_FOLLOW, mainMenuItem.isNav_how_to_follow() ? 1 : 0);
values.put(Sqlite.COL_NAV_BLOCKED, mainMenuItem.isNav_blocked() ? 1 : 0);
values.put(Sqlite.COL_NAV_MUTED, mainMenuItem.isNav_muted() ? 1 : 0);
values.put(Sqlite.COL_NAV_BLOCKED_DOMAINS, mainMenuItem.isNav_blocked() ? 1 : 0);
values.put(Sqlite.COL_NAV_HOWTO, mainMenuItem.isNav_howto() ? 1 : 0);
2019-12-08 18:48:52 +01:00
values.put(Sqlite.COL_NAV_TRENDS, mainMenuItem.isNav_trends() ? 1 : 0);
2019-08-28 12:00:14 +02:00
//Inserts menu conf
2019-09-06 17:55:14 +02:00
try {
2019-08-28 12:00:14 +02:00
db.insert(Sqlite.TABLE_MAIN_MENU_ITEMS, null, values);
2019-09-06 17:55:14 +02:00
} catch (Exception ignored) {
}
2019-08-28 12:00:14 +02:00
}
//------- UPDATES -------
/**
* update menu items in database
2019-09-06 17:55:14 +02:00
*
2019-08-28 12:00:14 +02:00
* @param mainMenuItem MainMenuItem
*/
2019-08-28 14:43:10 +02:00
public void updateMenu(MainMenuItem mainMenuItem, String userId, String instance) {
2019-08-28 12:00:14 +02:00
ContentValues values = new ContentValues();
values.put(Sqlite.COL_INSTANCE, instance);
values.put(Sqlite.COL_USER_ID, userId);
2019-09-06 17:55:14 +02:00
values.put(Sqlite.COL_NAV_NEWS, mainMenuItem.isNav_news() ? 1 : 0);
values.put(Sqlite.COL_NAV_LIST, mainMenuItem.isNav_list() ? 1 : 0);
values.put(Sqlite.COL_NAV_SCHEDULED, mainMenuItem.isNav_scheduled() ? 1 : 0);
values.put(Sqlite.COL_NAV_ARCHIVE, mainMenuItem.isNav_archive() ? 1 : 0);
values.put(Sqlite.COL_NAV_ARCHIVE_NOTIFICATIONS, mainMenuItem.isNav_archive_notifications() ? 1 : 0);
values.put(Sqlite.COL_NAV_PEERTUBE, mainMenuItem.isNav_peertube() ? 1 : 0);
values.put(Sqlite.COL_NAV_FILTERS, mainMenuItem.isNav_filters() ? 1 : 0);
values.put(Sqlite.COL_NAV_HOW_TO_FOLLOW, mainMenuItem.isNav_how_to_follow() ? 1 : 0);
values.put(Sqlite.COL_NAV_BLOCKED, mainMenuItem.isNav_blocked() ? 1 : 0);
values.put(Sqlite.COL_NAV_MUTED, mainMenuItem.isNav_muted() ? 1 : 0);
values.put(Sqlite.COL_NAV_BLOCKED_DOMAINS, mainMenuItem.isNav_blocked() ? 1 : 0);
values.put(Sqlite.COL_NAV_HOWTO, mainMenuItem.isNav_howto() ? 1 : 0);
2019-12-08 18:48:52 +01:00
values.put(Sqlite.COL_NAV_TRENDS, mainMenuItem.isNav_trends() ? 1 : 0);
2019-09-06 17:55:14 +02:00
try {
db.update(Sqlite.TABLE_MAIN_MENU_ITEMS, values, Sqlite.COL_USER_ID + " = ? AND " + Sqlite.COL_INSTANCE + " = ? ",
2019-08-28 12:00:14 +02:00
new String[]{userId, instance});
2019-09-06 17:55:14 +02:00
} catch (Exception ignored) {
ignored.printStackTrace();
}
2019-08-28 12:00:14 +02:00
}
/**
* update menu items in database
2019-09-06 17:55:14 +02:00
*
2019-08-28 12:00:14 +02:00
* @param mainMenuItem MainMenuItem
*/
2019-08-28 14:43:10 +02:00
public void updateMenu(MainMenuItem mainMenuItem) {
2019-08-28 12:00:14 +02:00
ContentValues values = new ContentValues();
SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
String userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null);
String instance = Helper.getLiveInstance(context);
2019-08-28 16:21:50 +02:00
2019-08-28 12:00:14 +02:00
values.put(Sqlite.COL_INSTANCE, instance);
values.put(Sqlite.COL_USER_ID, userId);
2019-09-06 17:55:14 +02:00
values.put(Sqlite.COL_NAV_NEWS, mainMenuItem.isNav_news() ? 1 : 0);
values.put(Sqlite.COL_NAV_LIST, mainMenuItem.isNav_list() ? 1 : 0);
values.put(Sqlite.COL_NAV_SCHEDULED, mainMenuItem.isNav_scheduled() ? 1 : 0);
values.put(Sqlite.COL_NAV_ARCHIVE, mainMenuItem.isNav_archive() ? 1 : 0);
values.put(Sqlite.COL_NAV_ARCHIVE_NOTIFICATIONS, mainMenuItem.isNav_archive_notifications() ? 1 : 0);
values.put(Sqlite.COL_NAV_PEERTUBE, mainMenuItem.isNav_peertube() ? 1 : 0);
values.put(Sqlite.COL_NAV_FILTERS, mainMenuItem.isNav_filters() ? 1 : 0);
values.put(Sqlite.COL_NAV_HOW_TO_FOLLOW, mainMenuItem.isNav_how_to_follow() ? 1 : 0);
values.put(Sqlite.COL_NAV_BLOCKED, mainMenuItem.isNav_blocked() ? 1 : 0);
values.put(Sqlite.COL_NAV_MUTED, mainMenuItem.isNav_muted() ? 1 : 0);
values.put(Sqlite.COL_NAV_BLOCKED_DOMAINS, mainMenuItem.isNav_blocked() ? 1 : 0);
values.put(Sqlite.COL_NAV_HOWTO, mainMenuItem.isNav_howto() ? 1 : 0);
2019-12-08 18:48:52 +01:00
values.put(Sqlite.COL_NAV_TRENDS, mainMenuItem.isNav_trends() ? 1 : 0);
2019-09-06 17:55:14 +02:00
try {
db.update(Sqlite.TABLE_MAIN_MENU_ITEMS, values, Sqlite.COL_USER_ID + " = ? AND " + Sqlite.COL_INSTANCE + " = ? ",
2019-08-28 12:00:14 +02:00
new String[]{userId, instance});
2019-09-06 17:55:14 +02:00
} catch (Exception ignored) {
ignored.printStackTrace();
}
2019-08-28 12:00:14 +02:00
}
//------- GETTERS -------
/**
* Returns instance by its nale in db
2019-09-06 17:55:14 +02:00
*
* @param userId String
2019-08-28 12:00:14 +02:00
* @param instance String
* @return MainMenuItem
*/
2019-09-06 17:55:14 +02:00
public MainMenuItem getMainMenu(String userId, String instance) {
2019-08-28 12:00:14 +02:00
try {
2019-09-06 17:55:14 +02:00
Cursor c = db.query(Sqlite.TABLE_MAIN_MENU_ITEMS, null, Sqlite.COL_INSTANCE + " = '" + instance + "' AND " + Sqlite.COL_USER_ID + " = '" + userId + "'", null, null, null, null, "1");
2019-08-28 12:00:14 +02:00
return cursorToMainMenu(c);
} catch (Exception e) {
2019-08-28 16:21:50 +02:00
return null;
2019-08-28 12:00:14 +02:00
}
}
/**
* Returns instance by its nale in db
2019-09-06 17:55:14 +02:00
*
2019-08-28 12:00:14 +02:00
* @return MainMenuItem
*/
2019-09-06 17:55:14 +02:00
public MainMenuItem getMainMenu() {
2019-08-28 12:00:14 +02:00
try {
SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
String userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null);
String instance = Helper.getLiveInstance(context);
2019-09-06 17:55:14 +02:00
Cursor c = db.query(Sqlite.TABLE_MAIN_MENU_ITEMS, null, Sqlite.COL_INSTANCE + " = '" + instance + "' AND " + Sqlite.COL_USER_ID + " = '" + userId + "'", null, null, null, null, "1");
2019-08-28 12:00:14 +02:00
return cursorToMainMenu(c);
} catch (Exception e) {
2019-08-28 16:21:50 +02:00
e.printStackTrace();
return null;
2019-08-28 12:00:14 +02:00
}
}
/***
* Method to hydrate main menu items from database
* @param c Cursor
2019-08-28 16:21:50 +02:00
* @return MainMenuItem
2019-08-28 12:00:14 +02:00
*/
2019-09-06 17:55:14 +02:00
private MainMenuItem cursorToMainMenu(Cursor c) {
2019-08-28 12:00:14 +02:00
//No element found
MainMenuItem mainMenuItem = new MainMenuItem();
if (c.getCount() == 0) {
c.close();
2019-08-28 16:21:50 +02:00
return null;
2019-08-28 12:00:14 +02:00
}
c.moveToFirst();
mainMenuItem.setNav_archive(c.getInt(c.getColumnIndex(Sqlite.COL_NAV_ARCHIVE)) == 1);
mainMenuItem.setNav_archive_notifications(c.getInt(c.getColumnIndex(Sqlite.COL_NAV_ARCHIVE_NOTIFICATIONS)) == 1);
mainMenuItem.setNav_blocked(c.getInt(c.getColumnIndex(Sqlite.COL_NAV_BLOCKED)) == 1);
mainMenuItem.setNav_blocked_domains(c.getInt(c.getColumnIndex(Sqlite.COL_NAV_BLOCKED_DOMAINS)) == 1);
mainMenuItem.setNav_filters(c.getInt(c.getColumnIndex(Sqlite.COL_NAV_FILTERS)) == 1);
mainMenuItem.setNav_how_to_follow(c.getInt(c.getColumnIndex(Sqlite.COL_NAV_HOW_TO_FOLLOW)) == 1);
mainMenuItem.setNav_howto(c.getInt(c.getColumnIndex(Sqlite.COL_NAV_HOWTO)) == 1);
mainMenuItem.setNav_list(c.getInt(c.getColumnIndex(Sqlite.COL_NAV_LIST)) == 1);
mainMenuItem.setNav_muted(c.getInt(c.getColumnIndex(Sqlite.COL_NAV_MUTED)) == 1);
mainMenuItem.setNav_news(c.getInt(c.getColumnIndex(Sqlite.COL_NAV_NEWS)) == 1);
mainMenuItem.setNav_peertube(c.getInt(c.getColumnIndex(Sqlite.COL_NAV_PEERTUBE)) == 1);
mainMenuItem.setNav_scheduled(c.getInt(c.getColumnIndex(Sqlite.COL_NAV_SCHEDULED)) == 1);
2019-12-08 18:48:52 +01:00
mainMenuItem.setNav_trends(c.getInt(c.getColumnIndex(Sqlite.COL_NAV_TRENDS)) == 1);
2019-08-28 12:00:14 +02:00
//Close the cursor
c.close();
return mainMenuItem;
}
}