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 <[email protected]>
  • Loading branch information
mkanilsson committed Jan 13, 2024
1 parent 7e0729d commit 7a5a66d
Show file tree
Hide file tree
Showing 14 changed files with 160 additions and 42 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 @@ -77,13 +78,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 @@ -115,6 +117,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 @@ -159,6 +162,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 @@ -302,9 +307,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 @@ -437,6 +446,7 @@ public void ReloadAdapterAsync() {

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 @@ -452,6 +462,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 @@ -492,7 +503,7 @@ protected void onPostExecute(Void aVoid) {
}
}

notifyCountDataSetChanged(unreadCountFoldersTemp, unreadCountFeedsTemp, starredCountFeedsTemp);
notifyCountDataSetChanged(unreadCountFoldersTemp, unreadCountFeedsTemp, starredCountFeedsTemp, downloadedPodcastsCountFeedsTemp);
super.onPostExecute(aVoid);
}
}
Expand Down Expand Up @@ -524,12 +535,79 @@ protected void onPostExecute(Tuple<ArrayList<AbstractItem>, SparseArray<ArrayLis

}

public Tuple<ArrayList<AbstractItem>, SparseArray<ArrayList<ConcreteFeedItem>>> ReloadAdapter()
{
showOnlyUnread = mPrefs.getBoolean(SettingsActivity.CB_SHOWONLYUNREAD_STRING, false);

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();
sw.start();

List<Folder> folderList;
//if(showOnlyUnread) {
// folderList = dbConn.getListOfFoldersWithUnreadItems();
//} else {
folderList = dbConn.getListOfFolders();
//}

sw.stop();
Log.v(TAG, "Time needed (fetch folder list): " + sw.toString());


for(Folder folder : folderList) {
mCategoriesArrayListAsync.add(new FolderSubscribtionItem(folder.getLabel(), null, folder.getId()));
}

for(Feed feed : dbConn.getListOfFeedsWithoutFolders(showOnlyUnread)) {
mCategoriesArrayListAsync.add(new ConcreteFeedItem(feed.getFeedTitle(), (long) ITEMS_WITHOUT_FOLDER.getValue(), feed.getId(), feed.getFaviconUrl(), feed.getId()));
}

SparseArray<ArrayList<ConcreteFeedItem>> mItemsArrayListAsync = new SparseArray<>();

for(int groupPosition = 0; groupPosition < mCategoriesArrayListAsync.size(); groupPosition++) {
//int parent_id = (int)getGroupId(groupPosition);
int parent_id = (int) mCategoriesArrayListAsync.get(groupPosition).id_database;
mItemsArrayListAsync.append(parent_id, new ArrayList<>());

List<Feed> feedItemList = null;

if(parent_id == ALL_UNREAD_ITEMS.getValue()) {
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
feedItemList = dbConn.getAllFeedsWithUnreadRssItemsForFolder(folder.getId());
break;
}
}
}

if(feedItemList != null) {
for (Feed feed : feedItemList) {
ConcreteFeedItem newItem = new ConcreteFeedItem(feed.getFeedTitle(), (long) parent_id, feed.getId(), feed.getFaviconUrl(), feed.getId());
mItemsArrayListAsync.get(parent_id).add(newItem);
}
}
}

return new Tuple<>(mCategoriesArrayListAsync, mItemsArrayListAsync);
}

@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()) {
items = items.stream().filter((rss) -> {
var podcast = DatabaseConnectionOrm.ParsePodcastItemFromRssItem(mActivity, rss);
return podcast.offlineCached;
}).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 @@ -746,6 +746,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 @@ -834,6 +836,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 @@ -295,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 @@ -322,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
Loading

0 comments on commit 7a5a66d

Please sign in to comment.