Skip to content

Commit

Permalink
Add remove podcast media in toolbar
Browse files Browse the repository at this point in the history
This patch adds a Trash icon in the toolbar for podcasts that have
downloaded media.

Signed-off-by: Marcus Nilsson <[email protected]>
  • Loading branch information
mkanilsson committed Jan 13, 2024
1 parent 7cfe909 commit 7e0729d
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 0 deletions.
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.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 @@ -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 @@ -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.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
@@ -0,0 +1,6 @@
<vector android:height="24dp"
android:viewportHeight="24" android:viewportWidth="24"
android:tint="?attr/colorControlNormal"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="@android:color/white" android:pathData="M6,19c0,1.1 0.9,2 2,2h8c1.1,0 2,-0.9 2,-2V7H6v12zM19,4h-3.5l-1,-1h-5l-1,1H5v2h14V4z"/>
</vector>
6 changes: 6 additions & 0 deletions News-Android-App/src/main/res/menu/news_detail.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@
android:icon="@drawable/ic_action_open_in_browser_24_theme_aware"
android:title="@string/action_openInBrowser"/>

<item
android:id="@+id/action_removePodcast"
app:showAsAction="always"
android:icon="@drawable/ic_action_delete_24_theme_aware"
android:title="@string/action_removePodcast"/>

<item
android:id="@+id/action_playPodcast"
app:showAsAction="always"
Expand Down
1 change: 1 addition & 0 deletions News-Android-App/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
<string name="action_starred">Starred</string>
<string name="action_read">Read</string>
<string name="action_playPodacst">Play Podcast</string>
<string name="action_removePodcast">Remove Podcast Media</string>
<string name="action_openInBrowser">Open in Web browser</string>
<string name="action_Share">Share</string>
<string name="action_login">Server Settings</string>
Expand Down

0 comments on commit 7e0729d

Please sign in to comment.