Skip to content

Commit

Permalink
Add "Downloaded Podcasts" in drawer
Browse files Browse the repository at this point in the history
Signed-off-by: Marcus Nilsson <marcus.nilsson@genarp.com>
  • Loading branch information
mkanilsson committed Sep 23, 2023
1 parent 28ff7bc commit 96f9343
Show file tree
Hide file tree
Showing 14 changed files with 98 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public View getView(final int position, View view, ViewGroup parent) {
});

holder.binding.flDeletePodcastWrapper.setOnClickListener(view13 -> {
if(NewsFileUtils.deletePodcastFile(getContext(), podcastItem.link)) {
if(NewsFileUtils.deletePodcastFile(getContext(), podcastItem.fingerprint, podcastItem.link)) {
podcastItem.offlineCached = false;
podcastItem.downloadProgress = PodcastItem.DOWNLOAD_NOT_STARTED;
notifyDataSetChanged();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

package de.luhmer.owncloudnewsreader.ListView;

import static de.luhmer.owncloudnewsreader.ListView.SubscriptionExpandableListAdapter.SPECIAL_FOLDERS.ALL_DOWNLOADED_PODCASTS;
import static de.luhmer.owncloudnewsreader.ListView.SubscriptionExpandableListAdapter.SPECIAL_FOLDERS.ALL_STARRED_ITEMS;
import static de.luhmer.owncloudnewsreader.ListView.SubscriptionExpandableListAdapter.SPECIAL_FOLDERS.ALL_UNREAD_ITEMS;
import static de.luhmer.owncloudnewsreader.ListView.SubscriptionExpandableListAdapter.SPECIAL_FOLDERS.ITEMS_WITHOUT_FOLDER;
Expand Down Expand Up @@ -78,13 +79,14 @@ public class SubscriptionExpandableListAdapter extends BaseExpandableListAdapter
private boolean showOnlyUnread = false;

private SparseArray<String> starredCountFeeds;
private SparseArray<String> downloadedPodcastsCount;
private SparseArray<String> unreadCountFolders;
private SparseArray<String> unreadCountFeeds;

private final SharedPreferences mPrefs;

public enum SPECIAL_FOLDERS {
ALL_UNREAD_ITEMS(-10), ALL_STARRED_ITEMS(-11), ALL_ITEMS(-12), ITEMS_WITHOUT_FOLDER(-22);
ALL_UNREAD_ITEMS(-10), ALL_STARRED_ITEMS(-11), ALL_ITEMS(-12), ALL_DOWNLOADED_PODCASTS(-13), ITEMS_WITHOUT_FOLDER(-22);

private final int id;
SPECIAL_FOLDERS(int id) {
Expand Down Expand Up @@ -116,6 +118,7 @@ public SubscriptionExpandableListAdapter(Context mContext, DatabaseConnectionOrm
unreadCountFeeds = new SparseArray<>();
unreadCountFolders = new SparseArray<>();
starredCountFeeds = new SparseArray<>();
downloadedPodcastsCount = new SparseArray<>();

mCategoriesArrayList = new ArrayList<>();
mItemsArrayList = new SparseArray<>();
Expand Down Expand Up @@ -160,6 +163,8 @@ public View getChildView(int groupPosition, int childPosition,
String unreadCount;
if(item.idFolder == ALL_STARRED_ITEMS.getValue()) {
unreadCount = starredCountFeeds.get((int) item.id_database);
} else if(item.idFolder == ALL_DOWNLOADED_PODCASTS.getValue()) {
unreadCount = downloadedPodcastsCount.get((int) item.id_database);
} else {
unreadCount = unreadCountFeeds.get((int) item.id_database);
}
Expand Down Expand Up @@ -303,9 +308,13 @@ public View getGroupView(final int groupPosition, final boolean isExpanded, View
} else {
if(group.id_database == ALL_STARRED_ITEMS.getValue()) {
viewHolder.binding.imgViewExpandableIndicator.setVisibility(View.GONE);
viewHolder.binding.imgViewFavicon.setVisibility(View.VISIBLE);
viewHolder.binding.imgViewFavicon.setVisibility(View.VISIBLE);
rotation = 0;
viewHolder.binding.imgViewFavicon.setImageResource(R.drawable.ic_star_border_24dp_theme_aware);
} else if(group.id_database == ALL_DOWNLOADED_PODCASTS.getValue()) {
viewHolder.binding.imgViewExpandableIndicator.setVisibility(View.GONE);
viewHolder.binding.imgViewFavicon.setVisibility(View.VISIBLE);
viewHolder.binding.imgViewFavicon.setImageResource(R.drawable.ic_baseline_play_arrow_24_theme_aware);
} else if (getChildrenCount( groupPosition ) == 0 ) {
viewHolder.binding.imgViewExpandableIndicator.setVisibility(View.GONE);
viewHolder.binding.imgViewFavicon.setVisibility(View.INVISIBLE);
Expand Down Expand Up @@ -368,6 +377,7 @@ public void NotifyDataSetChangedAsync() {

private class NotifyDataSetChangedAsyncTask extends AsyncTask<Void, Void, Void> {
SparseArray<String> starredCountFeedsTemp;
SparseArray<String> downloadedPodcastsCountFeedsTemp;
SparseArray<String> unreadCountFoldersTemp;
SparseArray<String> unreadCountFeedsTemp;
SparseArray<String> urlsToFavIconsTemp;
Expand All @@ -383,6 +393,7 @@ protected Void doInBackground(Void... voids) {
unreadCountFeedsTemp = temp[1]; // dbConn.getUnreadItemCountForFeed();

starredCountFeedsTemp = dbConn.getStarredItemCount();
downloadedPodcastsCountFeedsTemp = dbConn.getDownloadedPodcastsCount(mContext);
urlsToFavIconsTemp = dbConn.getUrlsToFavIcons();

stopwatch.stop();
Expand Down Expand Up @@ -423,7 +434,7 @@ protected void onPostExecute(Void aVoid) {
}
}

notifyCountDataSetChanged(unreadCountFoldersTemp, unreadCountFeedsTemp, starredCountFeedsTemp);
notifyCountDataSetChanged(unreadCountFoldersTemp, unreadCountFeedsTemp, starredCountFeedsTemp, downloadedPodcastsCountFeedsTemp);
super.onPostExecute(aVoid);
}
}
Expand Down Expand Up @@ -473,6 +484,7 @@ public Tuple<ArrayList<AbstractItem>, SparseArray<ArrayList<ConcreteFeedItem>>>
ArrayList<AbstractItem> mCategoriesArrayListAsync = new ArrayList<>();
mCategoriesArrayListAsync.add(new FolderSubscribtionItem(mContext.getString(R.string.allUnreadFeeds), null, ALL_UNREAD_ITEMS.getValue()));
mCategoriesArrayListAsync.add(new FolderSubscribtionItem(mContext.getString(R.string.starredFeeds), null, ALL_STARRED_ITEMS.getValue()));
mCategoriesArrayListAsync.add(new FolderSubscribtionItem(mContext.getString(R.string.downloadedPodcasts), null, ALL_DOWNLOADED_PODCASTS.getValue()));


StopWatch sw = new StopWatch();
Expand Down Expand Up @@ -510,6 +522,8 @@ public Tuple<ArrayList<AbstractItem>, SparseArray<ArrayList<ConcreteFeedItem>>>
feedItemList = dbConn.getAllFeedsWithUnreadRssItems();
} else if(parent_id == ALL_STARRED_ITEMS.getValue()) {
feedItemList = dbConn.getAllFeedsWithStarredRssItems();
} else if (parent_id == ALL_DOWNLOADED_PODCASTS.getValue()) {
feedItemList = dbConn.getAllFeedsWithDownloadedPodcasts(mContext);
} else {
for(Folder folder : folderList) {//Find the current selected folder
if (folder.getId() == parent_id) {//Current item
Expand All @@ -532,10 +546,11 @@ public Tuple<ArrayList<AbstractItem>, SparseArray<ArrayList<ConcreteFeedItem>>>


@SuppressLint("NewApi") // wrongly reports setSelectionFromTop is only available in lollipop
public void notifyCountDataSetChanged(SparseArray<String> unreadCountFolders, SparseArray<String> unreadCountFeeds, SparseArray<String> starredCountFeeds) {
public void notifyCountDataSetChanged(SparseArray<String> unreadCountFolders, SparseArray<String> unreadCountFeeds, SparseArray<String> starredCountFeeds, SparseArray<String> downloadedPodcastsCount) {
this.unreadCountFolders = unreadCountFolders;
this.unreadCountFeeds = unreadCountFeeds;
this.starredCountFeeds = starredCountFeeds;
this.downloadedPodcastsCount = downloadedPodcastsCount;

BlockingExpandableListView bView = (BlockingExpandableListView) listView;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ public void updateActionBarIcons() {
}

if(menuItem_RemovePodcast != null) {
File file = new File(PodcastDownloadService.getUrlToPodcastFile(this, podcastItem.link, false));
File file = new File(PodcastDownloadService.getUrlToPodcastFile(this, podcastItem.fingerprint, podcastItem.link, false));
menuItem_RemovePodcast.setVisible(file.exists());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
package de.luhmer.owncloudnewsreader;

import static java.util.Objects.requireNonNull;
import static de.luhmer.owncloudnewsreader.ListView.SubscriptionExpandableListAdapter.SPECIAL_FOLDERS.ALL_DOWNLOADED_PODCASTS;
import static de.luhmer.owncloudnewsreader.ListView.SubscriptionExpandableListAdapter.SPECIAL_FOLDERS.ALL_STARRED_ITEMS;
import static de.luhmer.owncloudnewsreader.ListView.SubscriptionExpandableListAdapter.SPECIAL_FOLDERS.ALL_UNREAD_ITEMS;
import static de.luhmer.owncloudnewsreader.SettingsActivity.SP_SWIPE_LEFT_ACTION;
Expand Down Expand Up @@ -67,6 +68,7 @@
import com.google.android.material.floatingactionbutton.FloatingActionButton;

import java.util.List;
import java.util.stream.Collectors;

import javax.inject.Inject;

Expand Down Expand Up @@ -515,9 +517,9 @@ protected List<RssItem> doInBackground(Void... voids) {
}
sqlSelectStatement = dbConn.getAllItemsIdsForFeedSQL(idFeed, onlyUnreadItems, onlyStarredItems, sortDirection);
} else if (idFolder != null) {
if (idFolder == ALL_STARRED_ITEMS.getValue())
if (idFolder == ALL_STARRED_ITEMS.getValue() || idFolder == ALL_DOWNLOADED_PODCASTS.getValue())
onlyUnreadItems = false;
sqlSelectStatement = dbConn.getAllItemsIdsForFolderSQL(idFolder, onlyUnreadItems, sortDirection);
sqlSelectStatement = dbConn.getAllItemsIdsForFolderSQL(idFolder, onlyUnreadItems, sortDirection, mActivity);
}
if (sqlSelectStatement != null) {
int index = sqlSelectStatement.indexOf("ORDER BY");
Expand All @@ -533,6 +535,13 @@ protected List<RssItem> doInBackground(Void... voids) {

List<RssItem> items = dbConn.getCurrentRssItemView(0);

if (idFolder == ALL_DOWNLOADED_PODCASTS.getValue()) {
List<RssItem> i = items.stream().filter((rss) -> {
var podcast = DatabaseConnectionOrm.ParsePodcastItemFromRssItem(mActivity, rss);
return podcast.offlineCached;
}).collect(Collectors.toList());
}

sw.stop();
Log.v(TAG, "Time needed (init loading): " + sw);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -689,6 +689,8 @@ private void updateDetailFragmentTitle() {
title = getString(R.string.allUnreadFeeds);
} else if (idFolder == -11) {
title = getString(R.string.starredFeeds);
} else if (idFolder == -13) {
title = getString(R.string.downloadedPodcasts);
}
} else {
Feed feed = dbConn.getFeedById(id);
Expand Down Expand Up @@ -777,6 +779,8 @@ private NewsReaderDetailFragment updateDetailFragment(long id, Boolean folder, L
title = getString(R.string.allUnreadFeeds);
} else if (idFolder == -11) {
title = getString(R.string.starredFeeds);
} else if (idFolder == -13) {
title = getString(R.string.downloadedPodcasts);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ public void onEvent(PodcastDownloadService.DownloadProgressUpdate downloadProgre

if (downloadProgress.podcast.downloadProgress == 100) {
pItem.downloadProgress = PodcastItem.DOWNLOAD_COMPLETED;
File file = new File(PodcastDownloadService.getUrlToPodcastFile(getActivity(), pItem.link, false));
File file = new File(PodcastDownloadService.getUrlToPodcastFile(getActivity(), pItem.fingerprint, pItem.link, false));
pItem.offlineCached = file.exists();
} else
pItem.downloadProgress = downloadProgress.podcast.downloadProgress;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import android.content.res.Resources;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.media.session.MediaControllerCompat;
import android.support.v4.media.session.PlaybackStateCompat;
import android.util.Log;
Expand Down Expand Up @@ -296,7 +295,7 @@ public void openMediaItem(final MediaItem mediaItem) {
public void openPodcast(final RssItem rssItem) {
final PodcastItem podcastItem = DatabaseConnectionOrm.ParsePodcastItemFromRssItem(this, rssItem);

File file = new File(PodcastDownloadService.getUrlToPodcastFile(this, podcastItem.link, false));
File file = new File(PodcastDownloadService.getUrlToPodcastFile(this, podcastItem.fingerprint, podcastItem.link, false));
if(file.exists()) {
podcastItem.link = file.getAbsolutePath();
openMediaItem(podcastItem);
Expand All @@ -323,7 +322,7 @@ public void openPodcast(final RssItem rssItem) {

public void removePodcastMedia(final RssItem rssItem, final Consumer<Boolean> callback) {
final PodcastItem podcastItem = DatabaseConnectionOrm.ParsePodcastItemFromRssItem(this, rssItem);
File file = new File(PodcastDownloadService.getUrlToPodcastFile(this, podcastItem.link, false));
File file = new File(PodcastDownloadService.getUrlToPodcastFile(this, podcastItem.fingerprint, podcastItem.link, false));

if (!file.exists()) {
callback.accept(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ public void setPlaying(boolean playing) {

public void setDownloadPodcastProgressbar() {
float progress;
if (PodcastDownloadService.PodcastAlreadyCached(itemView.getContext(), rssItem.getEnclosureLink())) {
if (PodcastDownloadService.PodcastAlreadyCached(itemView.getContext(), rssItem.getFingerprint(), rssItem.getEnclosureLink())) {
progress = 100;
} else {
progress = downloadProgressList.get(rssItem.getId().intValue(), 0);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package de.luhmer.owncloudnewsreader.database;

import static de.luhmer.owncloudnewsreader.ListView.SubscriptionExpandableListAdapter.SPECIAL_FOLDERS;
import static de.luhmer.owncloudnewsreader.ListView.SubscriptionExpandableListAdapter.SPECIAL_FOLDERS.ALL_DOWNLOADED_PODCASTS;
import static de.luhmer.owncloudnewsreader.ListView.SubscriptionExpandableListAdapter.SPECIAL_FOLDERS.ALL_ITEMS;
import static de.luhmer.owncloudnewsreader.ListView.SubscriptionExpandableListAdapter.SPECIAL_FOLDERS.ALL_STARRED_ITEMS;
import static de.luhmer.owncloudnewsreader.ListView.SubscriptionExpandableListAdapter.SPECIAL_FOLDERS.ALL_UNREAD_ITEMS;
Expand All @@ -19,6 +20,7 @@
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import javax.inject.Inject;
import javax.inject.Named;
Expand All @@ -37,6 +39,7 @@
import de.luhmer.owncloudnewsreader.database.model.RssItem;
import de.luhmer.owncloudnewsreader.database.model.RssItemDao;
import de.luhmer.owncloudnewsreader.helper.AsyncTaskHelper;
import de.luhmer.owncloudnewsreader.helper.NewsFileUtils;
import de.luhmer.owncloudnewsreader.helper.StopWatch;
import de.luhmer.owncloudnewsreader.model.PodcastFeedItem;
import de.luhmer.owncloudnewsreader.model.PodcastItem;
Expand Down Expand Up @@ -183,6 +186,13 @@ public List<Feed> getAllFeedsWithStarredRssItems() {
new WhereCondition.StringCondition(FeedDao.Properties.Id.columnName + " IN " + "(SELECT " + RssItemDao.Properties.FeedId.columnName + " FROM " + RssItemDao.TABLENAME + " WHERE " + RssItemDao.Properties.Starred_temp.columnName + " = 1)")).list();
}

public List<Feed> getAllFeedsWithDownloadedPodcasts(Context context) {
var ids = NewsFileUtils.getDownloadedPodcastsFingerprints(context);
var files = Arrays.stream(ids).map((f) -> "\"" + f + "\"").collect(Collectors.toList());
return daoSession.getFeedDao().queryBuilder().orderAsc(FeedDao.Properties.FeedTitle).where(
new WhereCondition.StringCondition(FeedDao.Properties.Id.columnName + " IN " + "(SELECT " + RssItemDao.Properties.FeedId.columnName + " FROM " + RssItemDao.TABLENAME + " WHERE " + RssItemDao.Properties.Fingerprint.columnName + " in (" + String.join(",", files) + "))")).list();
}

public List<PodcastFeedItem> getListOfFeedsWithAudioPodcasts() {
WhereCondition whereCondition = new WhereCondition.StringCondition(FeedDao.Properties.Id.columnName + " IN " + "(SELECT " + RssItemDao.Properties.FeedId.columnName + " FROM " + RssItemDao.TABLENAME + " WHERE " + RssItemDao.Properties.EnclosureMime.columnName + " IN(\"" + join(ALLOWED_PODCASTS_TYPES, "\",\"") + "\"))");
List<Feed> feedsWithPodcast = daoSession.getFeedDao().queryBuilder().orderAsc(FeedDao.Properties.FeedTitle).where(whereCondition).list();
Expand Down Expand Up @@ -516,6 +526,7 @@ public static PodcastItem ParsePodcastItemFromRssItem(Context context, RssItem r
podcastItem.link = rssItem.getEnclosureLink();
podcastItem.mimeType = rssItem.getEnclosureMime();
podcastItem.favIcon = feed.getFaviconUrl();
podcastItem.fingerprint = rssItem.getFingerprint();

if("image/jpeg".equals(podcastItem.mimeType)) {
// We don't want to accidentally think that enclosed images are podcasts
Expand All @@ -525,7 +536,7 @@ public static PodcastItem ParsePodcastItemFromRssItem(Context context, RssItem r

podcastItem.isVideoPodcast = Arrays.asList(DatabaseConnectionOrm.VIDEO_FORMATS).contains(podcastItem.mimeType);

File file = new File(PodcastDownloadService.getUrlToPodcastFile(context, podcastItem.link, false));
File file = new File(PodcastDownloadService.getUrlToPodcastFile(context, podcastItem.fingerprint, podcastItem.link, false));
podcastItem.offlineCached = file.exists();

return podcastItem;
Expand Down Expand Up @@ -573,11 +584,11 @@ public Long getLowestItemIdByFolder(Long id_folder) {
}


public String getAllItemsIdsForFolderSQL(long ID_FOLDER, boolean onlyUnread, SORT_DIRECTION sortDirection) {
public String getAllItemsIdsForFolderSQL(long ID_FOLDER, boolean onlyUnread, SORT_DIRECTION sortDirection, Context context) {
String buildSQL = "SELECT " + RssItemDao.Properties.Id.columnName +
" FROM " + RssItemDao.TABLENAME;

if(!(ID_FOLDER == ALL_UNREAD_ITEMS.getValue() || ID_FOLDER == ALL_STARRED_ITEMS.getValue()) || ID_FOLDER == ALL_ITEMS.getValue())//Wenn nicht Alle Artikel ausgewaehlt wurde (-10) oder (-11) fuer Starred Feeds
if(!(ID_FOLDER == ALL_UNREAD_ITEMS.getValue() || ID_FOLDER == ALL_STARRED_ITEMS.getValue() || ID_FOLDER == ALL_DOWNLOADED_PODCASTS.getValue()) || ID_FOLDER == ALL_ITEMS.getValue())//Wenn nicht Alle Artikel ausgewaehlt wurde (-10) oder (-11) fuer Starred Feeds
{
buildSQL += " WHERE " + RssItemDao.Properties.FeedId.columnName + " IN " +
"(SELECT sc." + FeedDao.Properties.Id.columnName +
Expand All @@ -592,6 +603,11 @@ else if(ID_FOLDER == ALL_UNREAD_ITEMS.getValue())
buildSQL += " WHERE " + RssItemDao.Properties.Read_temp.columnName + " != 1";
else if(ID_FOLDER == ALL_STARRED_ITEMS.getValue())
buildSQL += " WHERE " + RssItemDao.Properties.Starred_temp.columnName + " = 1";
else if (ID_FOLDER == ALL_DOWNLOADED_PODCASTS.getValue()) {
var ids = NewsFileUtils.getDownloadedPodcastsFingerprints(context);
var files = Arrays.stream(ids).map((f) -> "\"" + f + "\"").collect(Collectors.toList());
buildSQL += " WHERE " + RssItemDao.Properties.Fingerprint.columnName + " in (" + String.join(",", files) + ")";
}

buildSQL += " ORDER BY " + RssItemDao.Properties.PubDate.columnName + " " + sortDirection.toString();

Expand Down Expand Up @@ -717,6 +733,18 @@ public SparseArray<String> getStarredItemCount() {
return getStringSparseArrayFromSQL(buildSQL, 0, 1);
}

public SparseArray<String> getDownloadedPodcastsCount(Context context) {
var ids = NewsFileUtils.getDownloadedPodcastsFingerprints(context);
var files = Arrays.stream(ids).map((f) -> "\"" + f + "\"").collect(Collectors.toList());

String buildSQL = "SELECT " + RssItemDao.Properties.FeedId.columnName + ", COUNT(1)" + // rowid as _id,
" FROM " + RssItemDao.TABLENAME +
" WHERE " + RssItemDao.Properties.Fingerprint.columnName + " in (" + String.join(",", files) + ")" +
" GROUP BY " + RssItemDao.Properties.FeedId.columnName;

return getStringSparseArrayFromSQL(buildSQL, 0, 1);
}

public void clearDatabaseOverSize()
{
//If i have 9023 rows in the database, when i run that query it should delete 8023 rows and leave me with 1000
Expand Down
Loading

0 comments on commit 96f9343

Please sign in to comment.