20 changed files with 467 additions and 493 deletions
-
229app/src/main/java/awais/instagrabber/db/AppDatabase.java
-
33app/src/main/java/awais/instagrabber/db/Converters.java
-
34app/src/main/java/awais/instagrabber/db/dao/AccountDao.java
-
35app/src/main/java/awais/instagrabber/db/dao/FavoriteDao.java
-
162app/src/main/java/awais/instagrabber/db/datasources/AccountDataSource.java
-
164app/src/main/java/awais/instagrabber/db/datasources/FavoriteDataSource.java
-
28app/src/main/java/awais/instagrabber/db/entities/Account.java
-
25app/src/main/java/awais/instagrabber/db/entities/Favorite.java
-
4app/src/main/java/awais/instagrabber/db/repositories/AccountRepository.java
-
14app/src/main/java/awais/instagrabber/db/repositories/FavoriteRepository.java
-
5app/src/main/java/awais/instagrabber/dialogs/AccountSwitcherDialogFragment.java
-
11app/src/main/java/awais/instagrabber/fragments/FavoritesFragment.java
-
8app/src/main/java/awais/instagrabber/fragments/HashTagFragment.java
-
7app/src/main/java/awais/instagrabber/fragments/LocationFragment.java
-
10app/src/main/java/awais/instagrabber/fragments/main/ProfileFragment.java
-
3app/src/main/java/awais/instagrabber/fragments/settings/MorePreferencesFragment.java
-
31app/src/main/java/awais/instagrabber/utils/AppExecutors.java
-
2app/src/main/java/awais/instagrabber/utils/CookieUtils.java
-
140app/src/main/java/awais/instagrabber/utils/DataBox.java
-
11app/src/main/java/awais/instagrabber/utils/ExportImportUtils.java
@ -0,0 +1,229 @@ |
|||||
|
package awais.instagrabber.db; |
||||
|
|
||||
|
import android.content.ContentValues; |
||||
|
import android.content.Context; |
||||
|
import android.database.Cursor; |
||||
|
import android.database.sqlite.SQLiteDatabase; |
||||
|
import android.util.Log; |
||||
|
import android.util.Pair; |
||||
|
|
||||
|
import androidx.annotation.NonNull; |
||||
|
import androidx.room.Database; |
||||
|
import androidx.room.Room; |
||||
|
import androidx.room.RoomDatabase; |
||||
|
import androidx.room.TypeConverters; |
||||
|
import androidx.room.migration.Migration; |
||||
|
import androidx.sqlite.db.SupportSQLiteDatabase; |
||||
|
|
||||
|
import java.util.ArrayList; |
||||
|
import java.util.Date; |
||||
|
import java.util.List; |
||||
|
|
||||
|
import awais.instagrabber.db.dao.AccountDao; |
||||
|
import awais.instagrabber.db.dao.FavoriteDao; |
||||
|
import awais.instagrabber.db.entities.Account; |
||||
|
import awais.instagrabber.db.entities.Favorite; |
||||
|
import awais.instagrabber.models.enums.FavoriteType; |
||||
|
import awais.instagrabber.utils.Utils; |
||||
|
|
||||
|
@Database(entities = {Account.class, Favorite.class}, |
||||
|
version = 4) |
||||
|
@TypeConverters({Converters.class}) |
||||
|
public abstract class AppDatabase extends RoomDatabase { |
||||
|
private static final String TAG = AppDatabase.class.getSimpleName(); |
||||
|
|
||||
|
private static AppDatabase INSTANCE; |
||||
|
|
||||
|
public abstract AccountDao accountDao(); |
||||
|
|
||||
|
public abstract FavoriteDao favoriteDao(); |
||||
|
|
||||
|
public static AppDatabase getDatabase(final Context context) { |
||||
|
if (INSTANCE == null) { |
||||
|
synchronized (AppDatabase.class) { |
||||
|
if (INSTANCE == null) { |
||||
|
INSTANCE = Room.databaseBuilder(context.getApplicationContext(), AppDatabase.class, "cookiebox.db") |
||||
|
.addMigrations(MIGRATION_1_2, MIGRATION_2_3, MIGRATION_3_4) |
||||
|
.build(); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
return INSTANCE; |
||||
|
} |
||||
|
|
||||
|
static final Migration MIGRATION_1_2 = new Migration(1, 2) { |
||||
|
@Override |
||||
|
public void migrate(@NonNull SupportSQLiteDatabase db) { |
||||
|
db.execSQL("ALTER TABLE cookies ADD " + Account.COL_FULL_NAME + " TEXT"); |
||||
|
db.execSQL("ALTER TABLE cookies ADD " + Account.COL_PROFILE_PIC + " TEXT"); |
||||
|
} |
||||
|
}; |
||||
|
|
||||
|
static final Migration MIGRATION_2_3 = new Migration(2, 3) { |
||||
|
@Override |
||||
|
public void migrate(@NonNull SupportSQLiteDatabase db) { |
||||
|
final List<Favorite> oldFavorites = backupOldFavorites(db); |
||||
|
// recreate with new columns (as there will be no doubt about the `query_display` column being present or not in the future versions) |
||||
|
db.execSQL("DROP TABLE " + Favorite.TABLE_NAME); |
||||
|
db.execSQL("CREATE TABLE " + Favorite.TABLE_NAME + " (" |
||||
|
+ Favorite.COL_ID + " INTEGER PRIMARY KEY," |
||||
|
+ Favorite.COL_QUERY + " TEXT," |
||||
|
+ Favorite.COL_TYPE + " TEXT," |
||||
|
+ Favorite.COL_DISPLAY_NAME + " TEXT," |
||||
|
+ Favorite.COL_PIC_URL + " TEXT," |
||||
|
+ Favorite.COL_DATE_ADDED + " INTEGER)"); |
||||
|
// add the old favorites back |
||||
|
for (final Favorite oldFavorite : oldFavorites) { |
||||
|
insertOrUpdateFavorite(db, oldFavorite); |
||||
|
} |
||||
|
} |
||||
|
}; |
||||
|
|
||||
|
static final Migration MIGRATION_3_4 = new Migration(3, 4) { |
||||
|
@Override |
||||
|
public void migrate(@NonNull SupportSQLiteDatabase db) { |
||||
|
// Required when migrating to Room. |
||||
|
// The original table primary keys were not 'NOT NULL', so the migration to Room were failing without the below migration. |
||||
|
// Taking this opportunity to rename cookies table to accounts |
||||
|
|
||||
|
// Create new table with name 'accounts' |
||||
|
db.execSQL("CREATE TABLE " + Account.TABLE_NAME + " (" |
||||
|
+ Account.COL_ID + " INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT," |
||||
|
+ Account.COL_UID + " TEXT," |
||||
|
+ Account.COL_USERNAME + " TEXT," |
||||
|
+ Account.COL_COOKIE + " TEXT," |
||||
|
+ Account.COL_FULL_NAME + " TEXT," |
||||
|
+ Account.COL_PROFILE_PIC + " TEXT)"); |
||||
|
// Insert all data from table 'cookies' to 'accounts' |
||||
|
db.execSQL("INSERT INTO " + Account.TABLE_NAME + " (" |
||||
|
+ Account.COL_UID + "," |
||||
|
+ Account.COL_USERNAME + "," |
||||
|
+ Account.COL_COOKIE + "," |
||||
|
+ Account.COL_FULL_NAME + "," |
||||
|
+ Account.COL_PROFILE_PIC + ") " |
||||
|
+ "SELECT " |
||||
|
+ Account.COL_UID + "," |
||||
|
+ Account.COL_USERNAME + "," |
||||
|
+ Account.COL_COOKIE + "," |
||||
|
+ Account.COL_FULL_NAME + "," |
||||
|
+ Account.COL_PROFILE_PIC |
||||
|
+ " FROM cookies"); |
||||
|
// Drop old cookies table |
||||
|
db.execSQL("DROP TABLE cookies"); |
||||
|
|
||||
|
// Create favorite backup table |
||||
|
db.execSQL("CREATE TABLE " + Favorite.TABLE_NAME + "_backup (" |
||||
|
+ Favorite.COL_ID + " INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT," |
||||
|
+ Favorite.COL_QUERY + " TEXT," |
||||
|
+ Favorite.COL_TYPE + " TEXT," |
||||
|
+ Favorite.COL_DISPLAY_NAME + " TEXT," |
||||
|
+ Favorite.COL_PIC_URL + " TEXT," |
||||
|
+ Favorite.COL_DATE_ADDED + " INTEGER)"); |
||||
|
// Insert all data from table 'favorite' to 'favorite_backup' |
||||
|
db.execSQL("INSERT INTO " + Favorite.TABLE_NAME + "_backup (" |
||||
|
+ Favorite.COL_QUERY + "," |
||||
|
+ Favorite.COL_TYPE + "," |
||||
|
+ Favorite.COL_DISPLAY_NAME + "," |
||||
|
+ Favorite.COL_PIC_URL + "," |
||||
|
+ Favorite.COL_DATE_ADDED + ") " |
||||
|
+ "SELECT " |
||||
|
+ Favorite.COL_QUERY + "," |
||||
|
+ Favorite.COL_TYPE + "," |
||||
|
+ Favorite.COL_DISPLAY_NAME + "," |
||||
|
+ Favorite.COL_PIC_URL + "," |
||||
|
+ Favorite.COL_DATE_ADDED |
||||
|
+ " FROM " + Favorite.TABLE_NAME); |
||||
|
// Drop favorites |
||||
|
db.execSQL("DROP TABLE " + Favorite.TABLE_NAME); |
||||
|
// Rename favorite_backup to favorites |
||||
|
db.execSQL("ALTER TABLE " + Favorite.TABLE_NAME + "_backup RENAME TO " + Favorite.TABLE_NAME); |
||||
|
} |
||||
|
}; |
||||
|
|
||||
|
@NonNull |
||||
|
private static List<Favorite> backupOldFavorites(@NonNull final SupportSQLiteDatabase db) { |
||||
|
// check if old favorites table had the column query_display |
||||
|
final boolean queryDisplayExists = checkColumnExists(db, Favorite.TABLE_NAME, "query_display"); |
||||
|
Log.d(TAG, "backupOldFavorites: queryDisplayExists: " + queryDisplayExists); |
||||
|
final List<Favorite> oldModels = new ArrayList<>(); |
||||
|
final String sql = "SELECT " |
||||
|
+ "query_text," |
||||
|
+ "date_added" |
||||
|
+ (queryDisplayExists ? ",query_display" : "") |
||||
|
+ " FROM " + Favorite.TABLE_NAME; |
||||
|
try (final Cursor cursor = db.query(sql)) { |
||||
|
if (cursor != null && cursor.moveToFirst()) { |
||||
|
do { |
||||
|
try { |
||||
|
final String queryText = cursor.getString(cursor.getColumnIndex("query_text")); |
||||
|
final Pair<FavoriteType, String> favoriteTypeQueryPair = Utils.migrateOldFavQuery(queryText); |
||||
|
if (favoriteTypeQueryPair == null) continue; |
||||
|
final FavoriteType type = favoriteTypeQueryPair.first; |
||||
|
final String query = favoriteTypeQueryPair.second; |
||||
|
oldModels.add(new Favorite( |
||||
|
-1, |
||||
|
query, |
||||
|
type, |
||||
|
queryDisplayExists ? cursor.getString(cursor.getColumnIndex("query_display")) |
||||
|
: null, |
||||
|
null, |
||||
|
new Date(cursor.getLong(cursor.getColumnIndex("date_added"))) |
||||
|
)); |
||||
|
} catch (Exception e) { |
||||
|
Log.e(TAG, "onUpgrade", e); |
||||
|
} |
||||
|
} while (cursor.moveToNext()); |
||||
|
} |
||||
|
} catch (Exception e) { |
||||
|
Log.e(TAG, "onUpgrade", e); |
||||
|
} |
||||
|
Log.d(TAG, "backupOldFavorites: oldModels:" + oldModels); |
||||
|
return oldModels; |
||||
|
} |
||||
|
|
||||
|
private static synchronized void insertOrUpdateFavorite(@NonNull final SupportSQLiteDatabase db, @NonNull final Favorite model) { |
||||
|
final ContentValues values = new ContentValues(); |
||||
|
values.put(Favorite.COL_QUERY, model.getQuery()); |
||||
|
values.put(Favorite.COL_TYPE, model.getType().toString()); |
||||
|
values.put(Favorite.COL_DISPLAY_NAME, model.getDisplayName()); |
||||
|
values.put(Favorite.COL_PIC_URL, model.getPicUrl()); |
||||
|
values.put(Favorite.COL_DATE_ADDED, model.getDateAdded().getTime()); |
||||
|
int rows; |
||||
|
if (model.getId() >= 1) { |
||||
|
rows = db.update(Favorite.TABLE_NAME, |
||||
|
SQLiteDatabase.CONFLICT_IGNORE, |
||||
|
values, |
||||
|
Favorite.COL_ID + "=?", |
||||
|
new String[]{String.valueOf(model.getId())}); |
||||
|
} else { |
||||
|
rows = db.update(Favorite.TABLE_NAME, |
||||
|
SQLiteDatabase.CONFLICT_IGNORE, |
||||
|
values, |
||||
|
Favorite.COL_QUERY + "=?" + " AND " + Favorite.COL_TYPE + "=?", |
||||
|
new String[]{model.getQuery(), model.getType().toString()}); |
||||
|
} |
||||
|
if (rows != 1) { |
||||
|
db.insert(Favorite.TABLE_NAME, SQLiteDatabase.CONFLICT_IGNORE, values); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
private static boolean checkColumnExists(@NonNull final SupportSQLiteDatabase db, |
||||
|
@NonNull final String tableName, |
||||
|
@NonNull final String columnName) { |
||||
|
boolean exists = false; |
||||
|
try (Cursor cursor = db.query("PRAGMA table_info(" + tableName + ")")) { |
||||
|
if (cursor.moveToFirst()) { |
||||
|
do { |
||||
|
final String currentColumn = cursor.getString(cursor.getColumnIndex("name")); |
||||
|
if (currentColumn.equals(columnName)) { |
||||
|
exists = true; |
||||
|
} |
||||
|
} while (cursor.moveToNext()); |
||||
|
|
||||
|
} |
||||
|
} catch (Exception ex) { |
||||
|
Log.e(TAG, "checkColumnExists", ex); |
||||
|
} |
||||
|
return exists; |
||||
|
} |
||||
|
} |
@ -0,0 +1,33 @@ |
|||||
|
package awais.instagrabber.db; |
||||
|
|
||||
|
import androidx.room.TypeConverter; |
||||
|
|
||||
|
import java.util.Date; |
||||
|
|
||||
|
import awais.instagrabber.models.enums.FavoriteType; |
||||
|
|
||||
|
public class Converters { |
||||
|
@TypeConverter |
||||
|
public static Date fromTimestamp(Long value) { |
||||
|
return value == null ? null : new Date(value); |
||||
|
} |
||||
|
|
||||
|
@TypeConverter |
||||
|
public static Long dateToTimestamp(Date date) { |
||||
|
return date == null ? null : date.getTime(); |
||||
|
} |
||||
|
|
||||
|
@TypeConverter |
||||
|
public static FavoriteType fromFavoriteTypeString(String value) { |
||||
|
try { |
||||
|
return FavoriteType.valueOf(value); |
||||
|
} catch (Exception e) { |
||||
|
return null; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
@TypeConverter |
||||
|
public static String favoriteTypeToString(FavoriteType favoriteType) { |
||||
|
return favoriteType == null ? null : favoriteType.toString(); |
||||
|
} |
||||
|
} |
@ -0,0 +1,34 @@ |
|||||
|
package awais.instagrabber.db.dao; |
||||
|
|
||||
|
import androidx.room.Dao; |
||||
|
import androidx.room.Delete; |
||||
|
import androidx.room.Insert; |
||||
|
import androidx.room.OnConflictStrategy; |
||||
|
import androidx.room.Query; |
||||
|
import androidx.room.Update; |
||||
|
|
||||
|
import java.util.List; |
||||
|
|
||||
|
import awais.instagrabber.db.entities.Account; |
||||
|
|
||||
|
@Dao |
||||
|
public interface AccountDao { |
||||
|
|
||||
|
@Query("SELECT * FROM accounts") |
||||
|
List<Account> getAllAccounts(); |
||||
|
|
||||
|
@Query("SELECT * FROM accounts WHERE uid = :uid") |
||||
|
Account findAccountByUid(String uid); |
||||
|
|
||||
|
@Insert(onConflict = OnConflictStrategy.REPLACE) |
||||
|
List<Long> insertAccounts(Account... accounts); |
||||
|
|
||||
|
@Update |
||||
|
void updateAccounts(Account... accounts); |
||||
|
|
||||
|
@Delete |
||||
|
void deleteAccounts(Account... accounts); |
||||
|
|
||||
|
@Query("DELETE from accounts") |
||||
|
void deleteAllAccounts(); |
||||
|
} |
@ -0,0 +1,35 @@ |
|||||
|
package awais.instagrabber.db.dao; |
||||
|
|
||||
|
import androidx.room.Dao; |
||||
|
import androidx.room.Delete; |
||||
|
import androidx.room.Insert; |
||||
|
import androidx.room.OnConflictStrategy; |
||||
|
import androidx.room.Query; |
||||
|
import androidx.room.Update; |
||||
|
|
||||
|
import java.util.List; |
||||
|
|
||||
|
import awais.instagrabber.db.entities.Favorite; |
||||
|
import awais.instagrabber.models.enums.FavoriteType; |
||||
|
|
||||
|
@Dao |
||||
|
public interface FavoriteDao { |
||||
|
|
||||
|
@Query("SELECT * FROM favorites") |
||||
|
List<Favorite> getAllFavorites(); |
||||
|
|
||||
|
@Query("SELECT * FROM favorites WHERE query_text = :query and type = :type") |
||||
|
Favorite findFavoriteByQueryAndType(String query, FavoriteType type); |
||||
|
|
||||
|
@Insert(onConflict = OnConflictStrategy.REPLACE) |
||||
|
List<Long> insertFavorites(Favorite... favorites); |
||||
|
|
||||
|
@Update |
||||
|
void updateFavorites(Favorite... favorites); |
||||
|
|
||||
|
@Delete |
||||
|
void deleteFavorites(Favorite... favorites); |
||||
|
|
||||
|
@Query("DELETE from favorites") |
||||
|
void deleteAllFavorites(); |
||||
|
} |
@ -1,182 +1,68 @@ |
|||||
package awais.instagrabber.db.datasources; |
package awais.instagrabber.db.datasources; |
||||
|
|
||||
import android.content.ContentValues; |
|
||||
import android.content.Context; |
import android.content.Context; |
||||
import android.database.Cursor; |
|
||||
import android.database.sqlite.SQLiteDatabase; |
|
||||
import android.util.Log; |
|
||||
|
|
||||
import androidx.annotation.NonNull; |
import androidx.annotation.NonNull; |
||||
import androidx.annotation.Nullable; |
import androidx.annotation.Nullable; |
||||
|
|
||||
import java.util.ArrayList; |
|
||||
import java.util.List; |
import java.util.List; |
||||
|
|
||||
import awais.instagrabber.BuildConfig; |
|
||||
|
import awais.instagrabber.db.AppDatabase; |
||||
|
import awais.instagrabber.db.dao.AccountDao; |
||||
import awais.instagrabber.db.entities.Account; |
import awais.instagrabber.db.entities.Account; |
||||
import awais.instagrabber.utils.DataBox; |
|
||||
import awais.instagrabber.utils.TextUtils; |
|
||||
|
|
||||
import static awais.instagrabber.utils.DataBox.KEY_COOKIE; |
|
||||
import static awais.instagrabber.utils.DataBox.KEY_FULL_NAME; |
|
||||
import static awais.instagrabber.utils.DataBox.KEY_ID; |
|
||||
import static awais.instagrabber.utils.DataBox.KEY_PROFILE_PIC; |
|
||||
import static awais.instagrabber.utils.DataBox.KEY_UID; |
|
||||
import static awais.instagrabber.utils.DataBox.KEY_USERNAME; |
|
||||
import static awais.instagrabber.utils.DataBox.TABLE_COOKIES; |
|
||||
|
|
||||
public class AccountDataSource { |
public class AccountDataSource { |
||||
private static final String TAG = AccountDataSource.class.getSimpleName(); |
private static final String TAG = AccountDataSource.class.getSimpleName(); |
||||
|
|
||||
private static AccountDataSource INSTANCE; |
private static AccountDataSource INSTANCE; |
||||
|
|
||||
private final DataBox dataBox; |
|
||||
|
private final AccountDao accountDao; |
||||
|
|
||||
private AccountDataSource(@NonNull Context context) { |
|
||||
dataBox = DataBox.getInstance(context); |
|
||||
|
private AccountDataSource(final AccountDao accountDao) { |
||||
|
this.accountDao = accountDao; |
||||
} |
} |
||||
|
|
||||
public static synchronized AccountDataSource getInstance(@NonNull Context context) { |
|
||||
|
public static AccountDataSource getInstance(@NonNull Context context) { |
||||
|
if (INSTANCE == null) { |
||||
|
synchronized (AccountDataSource.class) { |
||||
if (INSTANCE == null) { |
if (INSTANCE == null) { |
||||
INSTANCE = new AccountDataSource(context); |
|
||||
|
final AppDatabase database = AppDatabase.getDatabase(context); |
||||
|
INSTANCE = new AccountDataSource(database.accountDao()); |
||||
|
} |
||||
|
} |
||||
} |
} |
||||
return INSTANCE; |
return INSTANCE; |
||||
} |
} |
||||
|
|
||||
@Nullable |
@Nullable |
||||
public final Account getAccount(final String uid) { |
public final Account getAccount(final String uid) { |
||||
Account cookie = null; |
|
||||
try (final SQLiteDatabase db = dataBox.getReadableDatabase(); |
|
||||
final Cursor cursor = db.query(TABLE_COOKIES, |
|
||||
new String[]{ |
|
||||
KEY_ID, |
|
||||
KEY_UID, |
|
||||
KEY_USERNAME, |
|
||||
KEY_COOKIE, |
|
||||
KEY_FULL_NAME, |
|
||||
KEY_PROFILE_PIC |
|
||||
}, |
|
||||
KEY_UID + "=?", |
|
||||
new String[]{uid}, |
|
||||
null, |
|
||||
null, |
|
||||
null)) { |
|
||||
if (cursor != null && cursor.moveToFirst()) |
|
||||
cookie = new Account( |
|
||||
cursor.getInt(cursor.getColumnIndex(KEY_ID)), |
|
||||
cursor.getString(cursor.getColumnIndex(KEY_UID)), |
|
||||
cursor.getString(cursor.getColumnIndex(KEY_USERNAME)), |
|
||||
cursor.getString(cursor.getColumnIndex(KEY_COOKIE)), |
|
||||
cursor.getString(cursor.getColumnIndex(KEY_FULL_NAME)), |
|
||||
cursor.getString(cursor.getColumnIndex(KEY_PROFILE_PIC)) |
|
||||
); |
|
||||
} |
|
||||
return cookie; |
|
||||
|
return accountDao.findAccountByUid(uid); |
||||
} |
} |
||||
|
|
||||
@NonNull |
@NonNull |
||||
public final List<Account> getAllAccounts() { |
public final List<Account> getAllAccounts() { |
||||
final List<Account> cookies = new ArrayList<>(); |
|
||||
try (final SQLiteDatabase db = dataBox.getReadableDatabase(); |
|
||||
final Cursor cursor = db.query(TABLE_COOKIES, |
|
||||
new String[]{ |
|
||||
KEY_ID, |
|
||||
KEY_UID, |
|
||||
KEY_USERNAME, |
|
||||
KEY_COOKIE, |
|
||||
KEY_FULL_NAME, |
|
||||
KEY_PROFILE_PIC |
|
||||
}, |
|
||||
null, |
|
||||
null, |
|
||||
null, |
|
||||
null, |
|
||||
null)) { |
|
||||
if (cursor != null && cursor.moveToFirst()) { |
|
||||
do { |
|
||||
cookies.add(new Account( |
|
||||
cursor.getInt(cursor.getColumnIndex(KEY_ID)), |
|
||||
cursor.getString(cursor.getColumnIndex(KEY_UID)), |
|
||||
cursor.getString(cursor.getColumnIndex(KEY_USERNAME)), |
|
||||
cursor.getString(cursor.getColumnIndex(KEY_COOKIE)), |
|
||||
cursor.getString(cursor.getColumnIndex(KEY_FULL_NAME)), |
|
||||
cursor.getString(cursor.getColumnIndex(KEY_PROFILE_PIC)) |
|
||||
)); |
|
||||
} while (cursor.moveToNext()); |
|
||||
} |
|
||||
} |
|
||||
return cookies; |
|
||||
|
return accountDao.getAllAccounts(); |
||||
} |
} |
||||
|
|
||||
// public final void insertOrUpdateAccount(@NonNull final Account account) { |
|
||||
// insertOrUpdateAccount( |
|
||||
// account.getUid(), |
|
||||
// account.getUsername(), |
|
||||
// account.getCookie(), |
|
||||
// account.getFullName(), |
|
||||
// account.getProfilePic() |
|
||||
// ); |
|
||||
// } |
|
||||
|
|
||||
public final void insertOrUpdateAccount(final String uid, |
public final void insertOrUpdateAccount(final String uid, |
||||
final String username, |
final String username, |
||||
final String cookie, |
final String cookie, |
||||
final String fullName, |
final String fullName, |
||||
final String profilePicUrl) { |
final String profilePicUrl) { |
||||
if (TextUtils.isEmpty(uid)) return; |
|
||||
try (final SQLiteDatabase db = dataBox.getWritableDatabase()) { |
|
||||
db.beginTransaction(); |
|
||||
try { |
|
||||
final ContentValues values = new ContentValues(); |
|
||||
values.put(KEY_USERNAME, username); |
|
||||
values.put(KEY_COOKIE, cookie); |
|
||||
values.put(KEY_UID, uid); |
|
||||
values.put(KEY_FULL_NAME, fullName); |
|
||||
values.put(KEY_PROFILE_PIC, profilePicUrl); |
|
||||
final int rows = db.update(TABLE_COOKIES, values, KEY_UID + "=?", new String[]{uid}); |
|
||||
if (rows != 1) { |
|
||||
db.insert(TABLE_COOKIES, null, values); |
|
||||
} |
|
||||
db.setTransactionSuccessful(); |
|
||||
} catch (final Exception e) { |
|
||||
if (BuildConfig.DEBUG) Log.e(TAG, "Error", e); |
|
||||
} finally { |
|
||||
db.endTransaction(); |
|
||||
|
final Account account = getAccount(uid); |
||||
|
final Account toUpdate = new Account(account == null ? 0 : account.getId(), uid, username, cookie, fullName, profilePicUrl); |
||||
|
if (account != null) { |
||||
|
accountDao.updateAccounts(toUpdate); |
||||
|
return; |
||||
} |
} |
||||
|
accountDao.insertAccounts(toUpdate); |
||||
} |
} |
||||
} |
|
||||
|
|
||||
public final synchronized void deleteAccount(@NonNull final Account account) { |
|
||||
final String cookieModelUid = account.getUid(); |
|
||||
if (!TextUtils.isEmpty(cookieModelUid)) { |
|
||||
try (final SQLiteDatabase db = dataBox.getWritableDatabase()) { |
|
||||
db.beginTransaction(); |
|
||||
try { |
|
||||
final int rowsDeleted = db.delete(TABLE_COOKIES, KEY_UID + "=? AND " + KEY_USERNAME + "=? AND " + KEY_COOKIE + "=?", |
|
||||
new String[]{cookieModelUid, account.getUsername(), account.getCookie()}); |
|
||||
|
|
||||
if (rowsDeleted > 0) db.setTransactionSuccessful(); |
|
||||
} catch (final Exception e) { |
|
||||
if (BuildConfig.DEBUG) Log.e("AWAISKING_APP", "", e); |
|
||||
} finally { |
|
||||
db.endTransaction(); |
|
||||
} |
|
||||
} |
|
||||
|
public final void deleteAccount(@NonNull final Account account) { |
||||
|
accountDao.deleteAccounts(account); |
||||
} |
} |
||||
} |
|
||||
|
|
||||
public final synchronized void deleteAllAccounts() { |
|
||||
try (final SQLiteDatabase db = dataBox.getWritableDatabase()) { |
|
||||
db.beginTransaction(); |
|
||||
try { |
|
||||
final int rowsDeleted = db.delete(TABLE_COOKIES, null, null); |
|
||||
|
|
||||
if (rowsDeleted > 0) db.setTransactionSuccessful(); |
|
||||
} catch (final Exception e) { |
|
||||
if (BuildConfig.DEBUG) Log.e("AWAISKING_APP", "", e); |
|
||||
} finally { |
|
||||
db.endTransaction(); |
|
||||
} |
|
||||
} |
|
||||
|
public final void deleteAllAccounts() { |
||||
|
accountDao.deleteAllAccounts(); |
||||
} |
} |
||||
} |
} |
@ -1,180 +1,62 @@ |
|||||
package awais.instagrabber.db.datasources; |
package awais.instagrabber.db.datasources; |
||||
|
|
||||
import android.content.Context; |
import android.content.Context; |
||||
import android.database.Cursor; |
|
||||
import android.database.sqlite.SQLiteDatabase; |
|
||||
import android.util.Log; |
|
||||
|
|
||||
import androidx.annotation.NonNull; |
import androidx.annotation.NonNull; |
||||
import androidx.annotation.Nullable; |
import androidx.annotation.Nullable; |
||||
|
|
||||
import java.util.ArrayList; |
|
||||
import java.util.Date; |
|
||||
import java.util.List; |
import java.util.List; |
||||
|
|
||||
import awais.instagrabber.BuildConfig; |
|
||||
|
import awais.instagrabber.db.AppDatabase; |
||||
|
import awais.instagrabber.db.dao.FavoriteDao; |
||||
import awais.instagrabber.db.entities.Favorite; |
import awais.instagrabber.db.entities.Favorite; |
||||
import awais.instagrabber.models.enums.FavoriteType; |
import awais.instagrabber.models.enums.FavoriteType; |
||||
import awais.instagrabber.utils.DataBox; |
|
||||
import awais.instagrabber.utils.TextUtils; |
|
||||
import awaisomereport.LogCollector; |
|
||||
|
|
||||
import static awais.instagrabber.utils.DataBox.FAV_COL_DATE_ADDED; |
|
||||
import static awais.instagrabber.utils.DataBox.FAV_COL_DISPLAY_NAME; |
|
||||
import static awais.instagrabber.utils.DataBox.FAV_COL_ID; |
|
||||
import static awais.instagrabber.utils.DataBox.FAV_COL_PIC_URL; |
|
||||
import static awais.instagrabber.utils.DataBox.FAV_COL_QUERY; |
|
||||
import static awais.instagrabber.utils.DataBox.FAV_COL_TYPE; |
|
||||
import static awais.instagrabber.utils.DataBox.TABLE_FAVORITES; |
|
||||
import static awais.instagrabber.utils.Utils.logCollector; |
|
||||
|
|
||||
public class FavoriteDataSource { |
public class FavoriteDataSource { |
||||
private static final String TAG = FavoriteDataSource.class.getSimpleName(); |
private static final String TAG = FavoriteDataSource.class.getSimpleName(); |
||||
|
|
||||
private static FavoriteDataSource INSTANCE; |
private static FavoriteDataSource INSTANCE; |
||||
|
|
||||
private final DataBox dataBox; |
|
||||
|
private final FavoriteDao favoriteDao; |
||||
|
|
||||
private FavoriteDataSource(@NonNull Context context) { |
|
||||
dataBox = DataBox.getInstance(context); |
|
||||
|
private FavoriteDataSource(final FavoriteDao favoriteDao) { |
||||
|
this.favoriteDao = favoriteDao; |
||||
} |
} |
||||
|
|
||||
public static synchronized FavoriteDataSource getInstance(@NonNull Context context) { |
public static synchronized FavoriteDataSource getInstance(@NonNull Context context) { |
||||
if (INSTANCE == null) { |
if (INSTANCE == null) { |
||||
INSTANCE = new FavoriteDataSource(context); |
|
||||
|
synchronized (FavoriteDataSource.class) { |
||||
|
if (INSTANCE == null) { |
||||
|
final AppDatabase database = AppDatabase.getDatabase(context); |
||||
|
INSTANCE = new FavoriteDataSource(database.favoriteDao()); |
||||
|
} |
||||
|
} |
||||
} |
} |
||||
return INSTANCE; |
return INSTANCE; |
||||
} |
} |
||||
|
|
||||
|
|
||||
@Nullable |
@Nullable |
||||
public final Favorite getFavorite(@NonNull final String query, @NonNull final FavoriteType type) { |
public final Favorite getFavorite(@NonNull final String query, @NonNull final FavoriteType type) { |
||||
try (final SQLiteDatabase db = dataBox.getReadableDatabase(); |
|
||||
final Cursor cursor = db.query(TABLE_FAVORITES, |
|
||||
new String[]{ |
|
||||
FAV_COL_ID, |
|
||||
FAV_COL_QUERY, |
|
||||
FAV_COL_TYPE, |
|
||||
FAV_COL_DISPLAY_NAME, |
|
||||
FAV_COL_PIC_URL, |
|
||||
FAV_COL_DATE_ADDED |
|
||||
}, |
|
||||
FAV_COL_QUERY + "=?" + " AND " + FAV_COL_TYPE + "=?", |
|
||||
new String[]{ |
|
||||
query, |
|
||||
type.toString() |
|
||||
}, |
|
||||
null, |
|
||||
null, |
|
||||
null)) { |
|
||||
if (cursor != null && cursor.moveToFirst()) { |
|
||||
FavoriteType favoriteType = null; |
|
||||
try { |
|
||||
favoriteType = FavoriteType.valueOf(cursor.getString(cursor.getColumnIndex(FAV_COL_TYPE))); |
|
||||
} catch (IllegalArgumentException ignored) {} |
|
||||
return new Favorite( |
|
||||
cursor.getInt(cursor.getColumnIndex(FAV_COL_ID)), |
|
||||
cursor.getString(cursor.getColumnIndex(FAV_COL_QUERY)), |
|
||||
favoriteType, |
|
||||
cursor.getString(cursor.getColumnIndex(FAV_COL_DISPLAY_NAME)), |
|
||||
cursor.getString(cursor.getColumnIndex(FAV_COL_PIC_URL)), |
|
||||
new Date(cursor.getLong(cursor.getColumnIndex(FAV_COL_DATE_ADDED))) |
|
||||
); |
|
||||
} |
|
||||
} |
|
||||
return null; |
|
||||
|
return favoriteDao.findFavoriteByQueryAndType(query, type); |
||||
} |
} |
||||
|
|
||||
@NonNull |
@NonNull |
||||
public final List<Favorite> getAllFavorites() { |
public final List<Favorite> getAllFavorites() { |
||||
final List<Favorite> favorites = new ArrayList<>(); |
|
||||
try (final SQLiteDatabase db = dataBox.getWritableDatabase()) { |
|
||||
try (final Cursor cursor = db.query(TABLE_FAVORITES, |
|
||||
new String[]{ |
|
||||
FAV_COL_ID, |
|
||||
FAV_COL_QUERY, |
|
||||
FAV_COL_TYPE, |
|
||||
FAV_COL_DISPLAY_NAME, |
|
||||
FAV_COL_PIC_URL, |
|
||||
FAV_COL_DATE_ADDED |
|
||||
}, |
|
||||
null, |
|
||||
null, |
|
||||
null, |
|
||||
null, |
|
||||
null)) { |
|
||||
if (cursor != null && cursor.moveToFirst()) { |
|
||||
db.beginTransaction(); |
|
||||
Favorite tempFav; |
|
||||
do { |
|
||||
FavoriteType type = null; |
|
||||
try { |
|
||||
type = FavoriteType.valueOf(cursor.getString(cursor.getColumnIndex(FAV_COL_TYPE))); |
|
||||
} catch (IllegalArgumentException ignored) {} |
|
||||
tempFav = new Favorite( |
|
||||
cursor.getInt(cursor.getColumnIndex(FAV_COL_ID)), |
|
||||
cursor.getString(cursor.getColumnIndex(FAV_COL_QUERY)), |
|
||||
type, |
|
||||
cursor.getString(cursor.getColumnIndex(FAV_COL_DISPLAY_NAME)), |
|
||||
cursor.getString(cursor.getColumnIndex(FAV_COL_PIC_URL)), |
|
||||
new Date(cursor.getLong(cursor.getColumnIndex(FAV_COL_DATE_ADDED))) |
|
||||
); |
|
||||
favorites.add(tempFav); |
|
||||
} while (cursor.moveToNext()); |
|
||||
db.endTransaction(); |
|
||||
} |
|
||||
} catch (final Exception e) { |
|
||||
Log.e(TAG, "", e); |
|
||||
} |
|
||||
} |
|
||||
return favorites; |
|
||||
|
return favoriteDao.getAllFavorites(); |
||||
} |
} |
||||
|
|
||||
public final synchronized Favorite insertOrUpdateFavorite(@NonNull final Favorite model) { |
|
||||
final String query = model.getQuery(); |
|
||||
if (!TextUtils.isEmpty(query)) { |
|
||||
try (final SQLiteDatabase db = dataBox.getWritableDatabase()) { |
|
||||
db.beginTransaction(); |
|
||||
try { |
|
||||
dataBox.insertOrUpdateFavorite(db, model); |
|
||||
db.setTransactionSuccessful(); |
|
||||
} catch (Exception e) { |
|
||||
if (logCollector != null) { |
|
||||
logCollector.appendException(e, LogCollector.LogFile.DATA_BOX_FAVORITES, "insertOrUpdateFavorite"); |
|
||||
|
public final void insertOrUpdateFavorite(@NonNull final Favorite favorite) { |
||||
|
if (favorite.getId() > 0) { |
||||
|
favoriteDao.updateFavorites(favorite); |
||||
|
return; |
||||
} |
} |
||||
if (BuildConfig.DEBUG) { |
|
||||
Log.e(TAG, "Error adding/updating favorite", e); |
|
||||
|
favoriteDao.insertFavorites(favorite); |
||||
} |
} |
||||
} finally { |
|
||||
db.endTransaction(); |
|
||||
} |
|
||||
} |
|
||||
return getFavorite(model.getQuery(), model.getType()); |
|
||||
} |
|
||||
return null; |
|
||||
} |
|
||||
|
|
||||
public final synchronized void deleteFavorite(@NonNull final String query, @NonNull final FavoriteType type) { |
|
||||
if (!TextUtils.isEmpty(query)) { |
|
||||
try (final SQLiteDatabase db = dataBox.getWritableDatabase()) { |
|
||||
db.beginTransaction(); |
|
||||
try { |
|
||||
final int rowsDeleted = db.delete(TABLE_FAVORITES, |
|
||||
FAV_COL_QUERY + "=?" + |
|
||||
" AND " + FAV_COL_TYPE + "=?", |
|
||||
new String[]{query, type.toString()}); |
|
||||
|
|
||||
if (rowsDeleted > 0) db.setTransactionSuccessful(); |
|
||||
} catch (final Exception e) { |
|
||||
if (logCollector != null) { |
|
||||
logCollector.appendException(e, LogCollector.LogFile.DATA_BOX_FAVORITES, "deleteFavorite"); |
|
||||
} |
|
||||
if (BuildConfig.DEBUG) { |
|
||||
Log.e(TAG, "Error", e); |
|
||||
} |
|
||||
} finally { |
|
||||
db.endTransaction(); |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
|
public final void deleteFavorite(@NonNull final String query, @NonNull final FavoriteType type) { |
||||
|
final Favorite favorite = getFavorite(query, type); |
||||
|
if (favorite == null) return; |
||||
|
favoriteDao.deleteFavorites(favorite); |
||||
} |
} |
||||
} |
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue