Skip to content

Commit

Permalink
Merge pull request #1273 from mkanilsson/remove-podcast-media
Browse files Browse the repository at this point in the history
Add "remove podcast" in toolbar and "Downloaded podcasts" special folder
  • Loading branch information
David-Development authored Jan 17, 2024
2 parents d1ca921 + f8a9d56 commit 36a1d82
Show file tree
Hide file tree
Showing 16 changed files with 223 additions and 40 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 @@ -49,6 +49,7 @@
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import java.io.File;
import java.lang.ref.WeakReference;
import java.util.HashSet;
import java.util.Set;
Expand All @@ -63,6 +64,7 @@
import de.luhmer.owncloudnewsreader.helper.ThemeUtils;
import de.luhmer.owncloudnewsreader.model.PodcastItem;
import de.luhmer.owncloudnewsreader.model.TTSItem;
import de.luhmer.owncloudnewsreader.services.PodcastDownloadService;
import de.luhmer.owncloudnewsreader.view.PodcastSlidingUpPanelLayout;
import de.luhmer.owncloudnewsreader.widget.WidgetProvider;

Expand Down Expand Up @@ -90,6 +92,7 @@ public class NewsDetailActivity extends PodcastFragmentActivity {
private int currentPosition;

private MenuItem menuItem_PlayPodcast;
private MenuItem menuItem_RemovePodcast;
private MenuItem menuItem_Starred;
private MenuItem menuItem_Read;
private MenuItem menuItem_Incognito;
Expand Down Expand Up @@ -401,6 +404,11 @@ public void updateActionBarIcons() {
menuItem_PlayPodcast.setVisible(podcastAvailable);
}

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

if (menuItem_Starred != null) {
int res = isStarred ? R.drawable.ic_star_24_theme_aware : R.drawable.ic_star_border_24dp_theme_aware;
menuItem_Starred.setIcon(res);
Expand Down Expand Up @@ -442,6 +450,7 @@ public boolean onCreateOptionsMenu(Menu menu) {
menuItem_Starred = menu.findItem(R.id.action_starred);
menuItem_Read = menu.findItem(R.id.action_read);
menuItem_PlayPodcast = menu.findItem(R.id.action_playPodcast);
menuItem_RemovePodcast = menu.findItem(R.id.action_removePodcast);
menuItem_Incognito = menu.findItem(R.id.action_incognito_mode);

if (mShowFastActions) {
Expand Down Expand Up @@ -493,6 +502,12 @@ public boolean onOptionsItemSelected(MenuItem item) {
this.openInBrowser(currentPosition);
} else if (itemId == R.id.action_playPodcast) {
openPodcast(rssItem);
} else if (itemId == R.id.action_removePodcast) {
removePodcastMedia(rssItem, (result) -> {
if (menuItem_RemovePodcast != null) {
menuItem_RemovePodcast.setVisible(!result);
}
});
} else if (itemId == R.id.action_tts) {
this.startTTS(currentPosition);
} else if (itemId == R.id.action_ShareItem) {
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 @@ -28,6 +28,7 @@
import org.greenrobot.eventbus.Subscribe;

import java.io.File;
import java.util.function.Consumer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -294,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 @@ -319,6 +320,35 @@ 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.fingerprint, podcastItem.link, false));

if (!file.exists()) {
callback.accept(true);
}

AlertDialog.Builder alertDialog = new AlertDialog.Builder(this)
.setNegativeButton("Remove", (dialogInterface, i) -> {
boolean success = file.delete() && file.getParentFile().delete(); // remove audio file and parent folder
if (!success) {
Toast.makeText(PodcastFragmentActivity.this, "Failed to remove media for \"" + podcastItem.title + "\"", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(PodcastFragmentActivity.this, "Media for \"" + podcastItem.title + "\" has been removed", Toast.LENGTH_SHORT).show();
}

callback.accept(success);
})
.setNeutralButton("Cancel", (dialogInterface, i) -> {
callback.accept(false);
})
.setTitle("Are you sure?")
.setMessage("Do you want to remove downloaded media for \"" + podcastItem.title + "?\"");

alertDialog.show();
}


@Override
public void pausePodcast() {
MediaControllerCompat.getMediaController(PodcastFragmentActivity.this).getTransportControls().pause();
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 36a1d82

Please sign in to comment.