sort in podcast screen - db persistence

This commit is contained in:
orionlee 2019-10-18 14:16:06 -07:00
parent be7db6cef1
commit 562ddee7d7
6 changed files with 54 additions and 8 deletions

View File

@ -44,8 +44,8 @@ class DBTestUtils {
PodDBAdapter adapter = PodDBAdapter.getInstance();
adapter.open();
for (int i = 0; i < numFeeds; i++) {
Feed f = new Feed(0, null, "feed " + i, null, "link" + i, "descr", null, null,
null, null, "id" + i, null, null, "url" + i, false, false, null, null, false);
Feed f = new Feed(0, null, "feed " + i, "link" + i, "descr", null, null,
null, null, "id" + i, null, null, "url" + i, false);
f.setItems(new ArrayList<>());
for (int j = 0; j < numItems; j++) {
FeedItem item = new FeedItem(0, "item " + j, "id" + j, "link" + j, new Date(),

View File

@ -12,6 +12,7 @@ import java.util.List;
import de.danoeh.antennapod.core.asynctask.ImageResource;
import de.danoeh.antennapod.core.storage.DBWriter;
import de.danoeh.antennapod.core.storage.PodDBAdapter;
/**
* Data Object for a whole feed
*
@ -97,7 +98,7 @@ public class Feed extends FeedFile implements ImageResource {
public Feed(long id, String lastUpdate, String title, String customTitle, String link, String description, String paymentLink,
String author, String language, String type, String feedIdentifier, String imageUrl, String fileUrl,
String downloadUrl, boolean downloaded, boolean paged, String nextPageLink,
String filter, boolean lastUpdateFailed) {
String filter, @Nullable IntraFeedSortOrder sortOrder, boolean lastUpdateFailed) {
super(fileUrl, downloadUrl, downloaded);
this.id = id;
this.feedTitle = title;
@ -119,6 +120,7 @@ public class Feed extends FeedFile implements ImageResource {
} else {
this.itemfilter = new FeedItemFilter(new String[0]);
}
this.sortOrder = sortOrder;
this.lastUpdateFailed = lastUpdateFailed;
}
@ -129,7 +131,7 @@ public class Feed extends FeedFile implements ImageResource {
String author, String language, String type, String feedIdentifier, String imageUrl, String fileUrl,
String downloadUrl, boolean downloaded) {
this(id, lastUpdate, title, null, link, description, paymentLink, author, language, type, feedIdentifier, imageUrl,
fileUrl, downloadUrl, downloaded, false, null, null, false);
fileUrl, downloadUrl, downloaded, false, null, null, null, false);
}
/**
@ -184,6 +186,7 @@ public class Feed extends FeedFile implements ImageResource {
int indexIsPaged = cursor.getColumnIndex(PodDBAdapter.KEY_IS_PAGED);
int indexNextPageLink = cursor.getColumnIndex(PodDBAdapter.KEY_NEXT_PAGE_LINK);
int indexHide = cursor.getColumnIndex(PodDBAdapter.KEY_HIDE);
int indexSortOrder = cursor.getColumnIndex(PodDBAdapter.KEY_SORT_ORDER);
int indexLastUpdateFailed = cursor.getColumnIndex(PodDBAdapter.KEY_LAST_UPDATE_FAILED);
int indexImageUrl = cursor.getColumnIndex(PodDBAdapter.KEY_IMAGE_URL);
@ -206,6 +209,7 @@ public class Feed extends FeedFile implements ImageResource {
cursor.getInt(indexIsPaged) > 0,
cursor.getString(indexNextPageLink),
cursor.getString(indexHide),
IntraFeedSortOrder.fromCode(cursor.getInt(indexSortOrder)),
cursor.getInt(indexLastUpdateFailed) > 0
);

View File

@ -1,5 +1,7 @@
package de.danoeh.antennapod.core.feed;
import androidx.annotation.Nullable;
/**
* Provides sort orders to sort a list of episodes within a feed.
*/
@ -11,6 +13,9 @@ public enum IntraFeedSortOrder {
DURATION_SHORT_LONG(5),
DURATION_LONG_SHORT(6);
// The constant SHOULD NEVER be changed, as it is used in db DDLs
public static final int CODE_UNSPECIFIED = 0;
public final int code;
IntraFeedSortOrder(int code) {
@ -28,4 +33,22 @@ public enum IntraFeedSortOrder {
return defaultValue;
}
}
@Nullable
public static IntraFeedSortOrder fromCode(int code) {
if (code == CODE_UNSPECIFIED) { // sort order not specified
return null;
}
for (IntraFeedSortOrder sortOrder : values()) {
if (sortOrder.code == code) {
return sortOrder;
}
}
throw new IllegalArgumentException("Unsupported code: " + code);
}
public static int toCode(@Nullable IntraFeedSortOrder sortOrder) {
return sortOrder != null ? sortOrder.code : CODE_UNSPECIFIED;
}
}

View File

@ -9,6 +9,7 @@ import android.util.Log;
import de.danoeh.antennapod.core.feed.FeedItem;
import static de.danoeh.antennapod.core.feed.FeedPreferences.SPEED_USE_GLOBAL;
import static de.danoeh.antennapod.core.feed.IntraFeedSortOrder.CODE_UNSPECIFIED;
class DBUpgrader {
/**
@ -294,6 +295,11 @@ class DBUpgrader {
db.execSQL("ALTER TABLE " + PodDBAdapter.TABLE_NAME_FEEDS
+ " ADD COLUMN " + PodDBAdapter.KEY_FEED_PLAYBACK_SPEED + " REAL DEFAULT " + SPEED_USE_GLOBAL);
}
if (oldVersion < 1070401) {
db.execSQL("ALTER TABLE " + PodDBAdapter.TABLE_NAME_FEEDS
+ " ADD COLUMN " + PodDBAdapter.KEY_SORT_ORDER + " INTEGER DEFAULT " + CODE_UNSPECIFIED);
}
}
}

View File

@ -954,7 +954,7 @@ public class DBWriter {
return dbExec.submit(() -> {
PodDBAdapter adapter = PodDBAdapter.getInstance();
adapter.open();
// adapter.setFeedItemSortOrder(feedId, sortOrder);
adapter.setFeedItemSortOrder(feedId, sortOrder);
adapter.close();
EventBus.getDefault().post(new FeedEvent(FeedEvent.Action.SORT_ORDER_CHANGED, feedId));
});

View File

@ -13,11 +13,11 @@ import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Build;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import androidx.annotation.Nullable;
import org.apache.commons.io.FileUtils;
import java.io.File;
@ -32,11 +32,14 @@ import de.danoeh.antennapod.core.feed.Feed;
import de.danoeh.antennapod.core.feed.FeedItem;
import de.danoeh.antennapod.core.feed.FeedMedia;
import de.danoeh.antennapod.core.feed.FeedPreferences;
import de.danoeh.antennapod.core.feed.IntraFeedSortOrder;
import de.danoeh.antennapod.core.preferences.UserPreferences;
import de.danoeh.antennapod.core.service.download.DownloadStatus;
import de.danoeh.antennapod.core.util.LongIntMap;
import static de.danoeh.antennapod.core.feed.FeedPreferences.SPEED_USE_GLOBAL;
import static de.danoeh.antennapod.core.feed.IntraFeedSortOrder.CODE_UNSPECIFIED;
import static de.danoeh.antennapod.core.feed.IntraFeedSortOrder.toCode;
// TODO Remove media column from feeditem table
@ -101,6 +104,7 @@ public class PodDBAdapter {
public static final String KEY_IS_PAGED = "is_paged";
public static final String KEY_NEXT_PAGE_LINK = "next_page_link";
public static final String KEY_HIDE = "hide";
public static final String KEY_SORT_ORDER = "sort_order";
public static final String KEY_LAST_UPDATE_FAILED = "last_update_failed";
public static final String KEY_HAS_EMBEDDED_PICTURE = "has_embedded_picture";
public static final String KEY_LAST_PLAYED_TIME = "last_played_time";
@ -138,6 +142,7 @@ public class PodDBAdapter {
+ KEY_IS_PAGED + " INTEGER DEFAULT 0,"
+ KEY_NEXT_PAGE_LINK + " TEXT,"
+ KEY_HIDE + " TEXT,"
+ KEY_SORT_ORDER + " INTEGER DEFAULT " + CODE_UNSPECIFIED + ","
+ KEY_LAST_UPDATE_FAILED + " INTEGER DEFAULT 0,"
+ KEY_AUTO_DELETE_ACTION + " INTEGER DEFAULT 0,"
+ KEY_FEED_PLAYBACK_SPEED + " REAL DEFAULT " + SPEED_USE_GLOBAL + ")";
@ -234,6 +239,7 @@ public class PodDBAdapter {
TABLE_NAME_FEEDS + "." + KEY_USERNAME,
TABLE_NAME_FEEDS + "." + KEY_PASSWORD,
TABLE_NAME_FEEDS + "." + KEY_HIDE,
TABLE_NAME_FEEDS + "." + KEY_SORT_ORDER,
TABLE_NAME_FEEDS + "." + KEY_LAST_UPDATE_FAILED,
TABLE_NAME_FEEDS + "." + KEY_AUTO_DELETE_ACTION,
TABLE_NAME_FEEDS + "." + KEY_INCLUDE_FILTER,
@ -378,6 +384,7 @@ public class PodDBAdapter {
} else {
values.put(KEY_HIDE, "");
}
values.put(KEY_SORT_ORDER, toCode(feed.getSortOrder()));
values.put(KEY_LAST_UPDATE_FAILED, feed.hasLastUpdateFailed());
if (feed.getId() == 0) {
// Create new entry
@ -416,6 +423,12 @@ public class PodDBAdapter {
db.update(TABLE_NAME_FEEDS, values, KEY_ID + "=?", new String[]{String.valueOf(feedId)});
}
public void setFeedItemSortOrder(long feedId, @Nullable IntraFeedSortOrder sortOrder) {
ContentValues values = new ContentValues();
values.put(KEY_SORT_ORDER, toCode(sortOrder));
db.update(TABLE_NAME_FEEDS, values, KEY_ID + "=?", new String[]{String.valueOf(feedId)});
}
/**
* Inserts or updates a media entry
*
@ -1455,7 +1468,7 @@ public class PodDBAdapter {
*/
private static class PodDBHelper extends SQLiteOpenHelper {
private static final int VERSION = 1070400;
private static final int VERSION = 1070401;
private final Context context;