Skip to content

Commit

Permalink
Add confirmation dialog to the playlist sharing process
Browse files Browse the repository at this point in the history
- Added a confirmation dialog for users to choose between sharing
  playlist formats.
- Users can choose to save their sharing format preference to skip the
  confirmation dialog.
- Added an option in the settings to update the sharing preference.
  • Loading branch information
snaik20 committed Sep 20, 2023
1 parent ee7e6cc commit b5274a7
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import static org.schabi.newpipe.util.ThemeHelper.shouldUseGridLayout;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Parcelable;
import android.text.InputType;
Expand All @@ -22,6 +23,7 @@
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.preference.PreferenceManager;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewbinding.ViewBinding;
Expand All @@ -36,6 +38,7 @@
import org.schabi.newpipe.database.playlist.model.PlaylistEntity;
import org.schabi.newpipe.database.stream.model.StreamEntity;
import org.schabi.newpipe.databinding.DialogEditTextBinding;
import org.schabi.newpipe.databinding.DialogSharePlaylistBinding;
import org.schabi.newpipe.databinding.LocalPlaylistHeaderBinding;
import org.schabi.newpipe.databinding.PlaylistControlBinding;
import org.schabi.newpipe.error.ErrorInfo;
Expand Down Expand Up @@ -99,6 +102,8 @@ public class LocalPlaylistFragment extends BaseLocalListFragment<List<PlaylistSt
/* Flag to prevent simultaneous rewrites of the playlist */
private boolean isRewritingPlaylist = false;

private SharedPreferences sharedPreferences;

public static LocalPlaylistFragment getInstance(final long playlistId, final String name) {
final LocalPlaylistFragment instance = new LocalPlaylistFragment();
instance.setInitialData(playlistId, name);
Expand All @@ -119,6 +124,7 @@ public void onCreate(final Bundle savedInstanceState) {

isLoadingComplete = new AtomicBoolean();
isModified = new AtomicBoolean();
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(requireContext());
}

@Override
Expand Down Expand Up @@ -346,7 +352,12 @@ public void onComplete() {
@Override
public boolean onOptionsItemSelected(final MenuItem item) {
if (item.getItemId() == R.id.menu_item_share_playlist) {
sharePlaylist();
if (sharedPreferences.getBoolean(requireContext().getString(
R.string.share_playlist_with_details_can_show_dialog_key), true)) {
createShareConfirmationDialog();
} else {
sharePlaylist();
}
} else if (item.getItemId() == R.id.menu_item_rename_playlist) {
createRenameDialog();
} else if (item.getItemId() == R.id.menu_item_remove_watched) {
Expand Down Expand Up @@ -376,16 +387,25 @@ public boolean onOptionsItemSelected(final MenuItem item) {
/**
* Share the playlist as a newline-separated list of stream URLs and video names.
*/
public void sharePlaylist() {
private void sharePlaylist() {
final boolean shouldSharePlaylistDetails = sharedPreferences.getBoolean(
requireContext().getString(R.string.share_playlist_with_details_key), false);
disposables.add(playlistManager.getPlaylistStreams(playlistId)
.flatMapSingle(playlist -> Single.just(playlist.stream()
.map(PlaylistStreamEntry::getStreamEntity)
.map(streamEntity -> String.format("- %s: %s",
streamEntity.getTitle(), streamEntity.getUrl()))
.map(streamEntity -> {
if (shouldSharePlaylistDetails) {
return String.format("- %s: %s",
streamEntity.getTitle(), streamEntity.getUrl());
} else {
return streamEntity.getUrl();
}
})
.collect(Collectors.joining("\n"))))
.observeOn(AndroidSchedulers.mainThread())
.subscribe(urlsText -> ShareUtils.shareText(
requireContext(), name, String.format("%s\n%s", name, urlsText)),
requireContext(), name, shouldSharePlaylistDetails
? String.format("%s\n%s", name, urlsText) : urlsText),
throwable -> showUiErrorSnackbar(this, "Sharing playlist", throwable)));
}

Expand Down Expand Up @@ -843,5 +863,40 @@ private PlayQueue getPlayQueue(final int index) {
}
return new SinglePlayQueue(streamInfoItems, index);
}

private void createShareConfirmationDialog() {
if (getContext() == null) {
return;
}

final DialogSharePlaylistBinding dialogBinding = DialogSharePlaylistBinding
.inflate(getLayoutInflater());

new AlertDialog.Builder(getContext())
.setTitle(R.string.share_playlist)
.setCancelable(true)
.setView(dialogBinding.getRoot())
.setPositiveButton(R.string.share_playlist_with_details, (dialog, which) -> {
sharedPreferences.edit()
.putBoolean(requireContext().getString(
R.string.share_playlist_with_details_can_show_dialog_key),
!dialogBinding.rememberChoiceCheckBox.isChecked())
.putBoolean(requireContext().getString(
R.string.share_playlist_with_details_key), true)
.commit();
sharePlaylist();
})
.setNegativeButton(R.string.share_playlist_with_list, (dialog, which) -> {
sharedPreferences.edit()
.putBoolean(requireContext().getString(
R.string.share_playlist_with_details_can_show_dialog_key),
!dialogBinding.rememberChoiceCheckBox.isChecked())
.putBoolean(requireContext().getString(
R.string.share_playlist_with_details_key), false)
.commit();
sharePlaylist();
})
.show();
}
}

28 changes: 28 additions & 0 deletions app/src/main/res/layout/dialog_share_playlist.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/itemRoot"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="false"
android:paddingStart="@dimen/video_item_search_padding"
android:paddingTop="@dimen/video_item_search_padding"
android:paddingEnd="@dimen/video_item_search_padding">

<org.schabi.newpipe.views.NewPipeTextView
android:id="@+id/sharePlaylistMessageText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:scrollHorizontally="true"
android:text="@string/share_playlist_with_details_message"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="@dimen/channel_item_detail_title_text_size" />

<CheckBox
android:id="@+id/rememberChoiceCheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/sharePlaylistMessageText"
android:layout_alignParentEnd="true"
android:text="@string/remember_my_choice" />
</RelativeLayout>
2 changes: 2 additions & 0 deletions app/src/main/res/values/settings_keys.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
<string name="screen_brightness_timestamp_key">screen_brightness_timestamp_key</string>
<string name="clear_queue_confirmation_key">clear_queue_confirmation_key</string>
<string name="ignore_hardware_media_buttons_key">ignore_hardware_media_buttons_key</string>
<string name="share_playlist_with_details_key">share_playlist_with_details_key</string>
<string name="share_playlist_with_details_can_show_dialog_key">share_playlist_with_details_can_show_dialog_key</string>

<string name="popup_saved_width_key">popup_saved_width</string>
<string name="popup_saved_x_key">popup_saved_x</string>
Expand Down
6 changes: 6 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -811,4 +811,10 @@
<string name="channel_tab_about">About</string>
<string name="show_channel_tabs">Channel tabs</string>
<string name="show_channel_tabs_summary">What tabs are shown on the channel pages</string>
<string name="share_playlist">Share Playlist</string>
<string name="share_playlist_with_details_message">Share playlist with details such as playlist name and video titles Or a simply share a list of video links</string>
<string name="share_playlist_with_details">Share with details</string>
<string name="share_playlist_with_list">Share list</string>
<string name="remember_my_choice">Remember my choice</string>
<string name="share_playlist_with_details_settings_item">Share playlist with details such as playlist name and video titles</string>
</resources>
8 changes: 8 additions & 0 deletions app/src/main/res/xml/video_audio_settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -249,5 +249,13 @@
android:title="@string/ignore_hardware_media_buttons_title"
app:singleLineTitle="false"
app:iconSpaceReserved="false" />

<SwitchPreferenceCompat
android:defaultValue="false"
android:key="@string/share_playlist_with_details_key"
android:summary="@string/share_playlist_with_details_settings_item"
android:title="@string/share_playlist"
app:singleLineTitle="false"
app:iconSpaceReserved="false" />
</PreferenceCategory>
</PreferenceScreen>

0 comments on commit b5274a7

Please sign in to comment.