various updates

This commit is contained in:
Austin Huang 2020-07-30 11:52:41 -04:00
parent 21ad1e3748
commit e1c07c23a7
No known key found for this signature in database
GPG Key ID: 84C23AA04587A91F
7 changed files with 34 additions and 33 deletions

View File

@ -237,7 +237,7 @@ public final class MainHelper implements SwipeRefreshLayout.OnRefreshListener {
final Object tag = v.getTag();
if (tag instanceof FeedStoryModel) {
final FeedStoryModel feedStoryModel = (FeedStoryModel) tag;
final StoryModel[] storyModels = feedStoryModel.getStoryModels();
StoryModel[] storyModels = feedStoryModel.getStoryModels();
final int index = indexOfIntArray(stories, feedStoryModel);
main.startActivity(new Intent(main, StoryViewer.class)

View File

@ -88,7 +88,8 @@ public final class FeedFetcher extends AsyncTask<Void, Void, FeedModel[]> {
final boolean isVideo = feedItem.optBoolean("is_video");
final long videoViews = feedItem.optLong("video_view_count", 0);
final String displayUrl = feedItem.getString("display_url");
final String displayUrl = feedItem.optString("display_url");
if (Utils.isEmpty(displayUrl)) continue;
final String resourceUrl;
if (isVideo) resourceUrl = feedItem.getString("video_url");

View File

@ -6,6 +6,7 @@ import android.content.Context;
import android.content.DialogInterface;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

View File

@ -10,7 +10,6 @@ public final class Constants {
public static final String APP_THEME = "app_theme";
public static final String APP_LANGUAGE = "app_language";
public static final String PREV_INSTALL_VERSION = "prevVersion";
public static final String DB_TO_MIGRATE = "dbToMigrate";
public static final String PROFILE_FETCH_MODE = "profile_fetch_mode";
// boolean prefs
public static final String DOWNLOAD_USER_FOLDER = "download_user_folder";

View File

@ -6,6 +6,7 @@ import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
@ -16,7 +17,6 @@ import awais.instagrabber.BuildConfig;
import awaisomereport.LogCollector;
import static awais.instagrabber.utils.Utils.logCollector;
import static awais.instagrabber.utils.Utils.settingsHelper;
public final class DataBox extends SQLiteOpenHelper {
private static DataBox sInstance;
@ -29,6 +29,7 @@ public final class DataBox extends SQLiteOpenHelper {
private final static String KEY_USERNAME = Constants.EXTRAS_USERNAME;
private final static String KEY_COOKIE = "cookie";
private final static String KEY_UID = "uid";
private Context c;
public static synchronized DataBox getInstance(final Context context) {
if (sInstance == null) sInstance = new DataBox(context.getApplicationContext());
@ -37,11 +38,11 @@ public final class DataBox extends SQLiteOpenHelper {
public DataBox(@Nullable final Context context) {
super(context, "cookiebox.db", null, VERSION);
c = context;
}
@Override
public void onCreate(@NonNull final SQLiteDatabase db) {
settingsHelper.putBoolean(Constants.DB_TO_MIGRATE, false);
db.execSQL("CREATE TABLE cookies (id INTEGER PRIMARY KEY, uid TEXT, username TEXT, cookie TEXT)");
db.execSQL("CREATE TABLE favorites (id INTEGER PRIMARY KEY, query_text TEXT, date_added INTEGER, query_display TEXT)");
}
@ -81,17 +82,19 @@ public final class DataBox extends SQLiteOpenHelper {
public final synchronized void delFavorite(@NonNull final FavoriteModel favoriteModel) {
final String query = favoriteModel.getQuery();
final String display = favoriteModel.getDisplayName();
if (!Utils.isEmpty(query)) {
try (final SQLiteDatabase db = getWritableDatabase()) {
db.beginTransaction();
try {
final int rowsDeleted = db.delete(TABLE_FAVORITES, KEY_QUERY_TEXT + "=? AND "
+ KEY_DATE_ADDED + "=? AND "
+ KEY_QUERY_DISPLAY + "=?",
new String[]{query, Long.toString(favoriteModel.getDate()), display});
+ KEY_DATE_ADDED + "=?",
new String[]{query, Long.toString(favoriteModel.getDate())});
if (rowsDeleted > 0) db.setTransactionSuccessful();
final int rowsDeletedTwo = db.delete(TABLE_FAVORITES, KEY_QUERY_TEXT + "=? AND "
+ KEY_DATE_ADDED + "=?",
new String[]{query.replaceAll("@", ""), Long.toString(favoriteModel.getDate())});
if (rowsDeleted > 0 || rowsDeletedTwo > 0) db.setTransactionSuccessful();
} catch (final Exception e) {
if (logCollector != null)
logCollector.appendException(e, LogCollector.LogFile.DATA_BOX_FAVORITES, "delFavorite");
@ -107,33 +110,21 @@ public final class DataBox extends SQLiteOpenHelper {
public final ArrayList<FavoriteModel> getAllFavorites() {
ArrayList<FavoriteModel> favorites = null;
FavoriteModel tempFav;
final SQLiteDatabase db = getWritableDatabase();
if (Utils.settingsHelper.getBoolean(Constants.DB_TO_MIGRATE) == true) {
try (final SQLiteDatabase db = getWritableDatabase()) {
try {
db.beginTransaction();
db.execSQL("ALTER TABLE favorites ADD query_display TEXT");
} catch (final Exception e) {
if (logCollector != null)
logCollector.appendException(e, LogCollector.LogFile.DATA_BOX_FAVORITES, "migrate");
if (BuildConfig.DEBUG) Log.e("AWAISKING_APP", "", e);
} finally {
db.endTransaction();
settingsHelper.putBoolean(Constants.DB_TO_MIGRATE, false);
}
}
}
try (final SQLiteDatabase db = getWritableDatabase();
final Cursor cursor = db.rawQuery("SELECT query_text, date_added, query_display FROM favorites ORDER BY date_added DESC", null)) {
try (final Cursor cursor = db.rawQuery("SELECT query_text, date_added, query_display FROM favorites ORDER BY date_added DESC", null)) {
if (cursor != null && cursor.moveToFirst()) {
db.beginTransaction();
favorites = new ArrayList<>();
do {
tempFav = new FavoriteModel(
cursor.getString(0), // query text
(cursor.getString(0).charAt(0) == '@' || cursor.getString(0).contains("/"))
? cursor.getString(0)
: "@" + cursor.getString(0), // query text
cursor.getLong(1), // date added
cursor.getString(2) == null ? cursor.getString(0) : cursor.getString(2) // display
cursor.getString(2) == null ? (cursor.getString(0).charAt(0) == '@' || cursor.getString(0).contains("/"))
? cursor.getString(0)
: "@" + cursor.getString(0) : cursor.getString(2) // display
);
if (cursor.getString(2) == null) {
try {
@ -156,6 +147,17 @@ public final class DataBox extends SQLiteOpenHelper {
} while (cursor.moveToNext());
db.endTransaction();
}
} catch (final Exception x) {
Log.e("austin_debug", "", x);
try {
db.execSQL("ALTER TABLE favorites ADD query_display TEXT");
Toast.makeText(c, "DB has migrated, launch quick access again.", Toast.LENGTH_SHORT).show();
} catch (final Exception e) {
if (logCollector != null)
logCollector.appendException(e, LogCollector.LogFile.DATA_BOX_FAVORITES, "migrate");
Toast.makeText(c, "DB migration failed, contact maintainer.", Toast.LENGTH_SHORT).show();
if (BuildConfig.DEBUG) Log.e("AWAISKING_APP", "", e);
}
}
return favorites;

View File

@ -55,8 +55,6 @@ public final class FlavorTown {
public static void changelogCheck(@NonNull final Context context) {
if (settingsHelper.getInteger(Constants.PREV_INSTALL_VERSION) < BuildConfig.VERSION_CODE) {
if (settingsHelper.getInteger(Constants.PREV_INSTALL_VERSION) < 36)
settingsHelper.putBoolean(Constants.DB_TO_MIGRATE, true);
Toast.makeText(context, R.string.updated, Toast.LENGTH_SHORT).show();
settingsHelper.putInteger(Constants.PREV_INSTALL_VERSION, BuildConfig.VERSION_CODE);
}

View File

@ -979,7 +979,7 @@ public final class Utils {
final String displayUrl = model.getDisplayUrl();
final int index = displayUrl.indexOf('?');
final String fileName = model.getPostId() + '_' + model.getTimestamp();
final String fileName = model.getPostId() + '_' + model.getPosition();
final String extension = displayUrl.substring(index - 4, index);
final String fileWithoutPrefix = fileName + extension;