1
0
mirror of https://github.com/TeamNewPipe/NewPipe synced 2024-11-16 23:04:56 +00:00

Add confirmation dialog to the playlist sharing process

- 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.
This commit is contained in:
Siddhesh Naik 2023-09-20 15:54:27 +05:30 committed by TobiGr
parent f15c35ba27
commit b196e808a0
5 changed files with 104 additions and 5 deletions

View File

@ -5,6 +5,7 @@ import static org.schabi.newpipe.ktx.ViewUtils.animate;
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;
@ -22,6 +23,7 @@ import android.widget.Toast;
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;
@ -36,6 +38,7 @@ import org.schabi.newpipe.database.playlist.PlaylistStreamEntry;
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;
@ -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);
@ -119,6 +124,7 @@ public class LocalPlaylistFragment extends BaseLocalListFragment<List<PlaylistSt
isLoadingComplete = new AtomicBoolean();
isModified = new AtomicBoolean();
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(requireContext());
}
@Override
@ -346,7 +352,12 @@ public class LocalPlaylistFragment extends BaseLocalListFragment<List<PlaylistSt
@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) {
@ -376,16 +387,25 @@ public class LocalPlaylistFragment extends BaseLocalListFragment<List<PlaylistSt
/**
* 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)));
}
@ -843,5 +863,40 @@ public class LocalPlaylistFragment extends BaseLocalListFragment<List<PlaylistSt
}
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();
}
}

View File

@ -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>

View File

@ -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>

View File

@ -832,4 +832,10 @@
<string name="image_quality_medium">Medium quality</string>
<string name="image_quality_high">High quality</string>
<string name="question_mark">\?</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>

View File

@ -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>