Show preview on share dialog (#7425)

This commit is contained in:
ByteHamster 2024-09-26 22:48:11 +02:00 committed by GitHub
parent f6bc8d2c51
commit 7309f8e8b2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 207 additions and 102 deletions

View File

@ -16,13 +16,6 @@ public class ShareDialog extends BottomSheetDialogFragment {
private static final String ARGUMENT_FEED_ITEM = "feedItem";
private static final String PREF_NAME = "ShareDialog";
private static final String PREF_SHARE_EPISODE_START_AT = "prefShareEpisodeStartAt";
private static final String PREF_SHARE_EPISODE_TYPE = "prefShareEpisodeType";
private Context ctx;
private FeedItem item;
private SharedPreferences prefs;
private ShareEpisodeDialogBinding viewBinding;
public ShareDialog() {
// Empty constructor required for DialogFragment
@ -40,60 +33,46 @@ public class ShareDialog extends BottomSheetDialogFragment {
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
if (getArguments() != null) {
ctx = getActivity();
item = (FeedItem) getArguments().getSerializable(ARGUMENT_FEED_ITEM);
prefs = getActivity().getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
if (getArguments() == null) {
return null;
}
FeedItem item = (FeedItem) getArguments().getSerializable(ARGUMENT_FEED_ITEM);
ShareEpisodeDialogBinding viewBinding = ShareEpisodeDialogBinding.inflate(inflater);
if (item.getMedia() != null && item.getMedia().isDownloaded()) {
viewBinding.mediaFileCardCard.setOnClickListener(v -> {
ShareUtils.shareFeedItemFile(getContext(), item.getMedia());
dismiss();
});
} else {
viewBinding.mediaFileCardCard.setVisibility(View.GONE);
}
viewBinding = ShareEpisodeDialogBinding.inflate(inflater);
viewBinding.shareDialogRadioGroup.setOnCheckedChangeListener((group, checkedId) ->
viewBinding.sharePositionCheckbox.setEnabled(checkedId == viewBinding.shareSocialRadio.getId()));
if (item.getMedia() != null && item.getMedia().getDownloadUrl() != null) {
viewBinding.mediaAddressText.setText(item.getMedia().getDownloadUrl());
viewBinding.mediaAddressCard.setOnClickListener(v -> {
ShareUtils.shareLink(getContext(), item.getMedia().getDownloadUrl());
dismiss();
});
} else {
viewBinding.mediaAddressCard.setVisibility(View.GONE);
}
setupOptions();
viewBinding.shareButton.setOnClickListener((v) -> {
boolean includePlaybackPosition = viewBinding.sharePositionCheckbox.isChecked();
int position;
if (viewBinding.shareSocialRadio.isChecked()) {
ShareUtils.shareFeedItemLinkWithDownloadLink(ctx, item, includePlaybackPosition);
position = 1;
} else if (viewBinding.shareMediaReceiverRadio.isChecked()) {
ShareUtils.shareMediaDownloadLink(ctx, item.getMedia());
position = 2;
} else if (viewBinding.shareMediaFileRadio.isChecked()) {
ShareUtils.shareFeedItemFile(ctx, item.getMedia());
position = 3;
} else {
throw new IllegalStateException("Unknown share method");
}
prefs.edit()
.putBoolean(PREF_SHARE_EPISODE_START_AT, includePlaybackPosition)
.putInt(PREF_SHARE_EPISODE_TYPE, position)
.apply();
SharedPreferences prefs = getContext().getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
viewBinding.sharePositionCheckbox.setChecked(prefs.getBoolean(PREF_SHARE_EPISODE_START_AT, false));
viewBinding.socialMessageText.setText(ShareUtils.getSocialFeedItemShareText(
getContext(), item, viewBinding.sharePositionCheckbox.isChecked(), true));
viewBinding.sharePositionCheckbox.setOnCheckedChangeListener((buttonView, isChecked) -> {
prefs.edit().putBoolean(PREF_SHARE_EPISODE_START_AT, isChecked).apply();
viewBinding.socialMessageText.setText(
ShareUtils.getSocialFeedItemShareText(getContext(), item, isChecked, true));
});
viewBinding.socialMessageCard.setOnClickListener(v -> {
ShareUtils.shareLink(getContext(), ShareUtils.getSocialFeedItemShareText(
getContext(), item, viewBinding.sharePositionCheckbox.isChecked(), false));
dismiss();
});
return viewBinding.getRoot();
}
private void setupOptions() {
final boolean hasMedia = item.getMedia() != null;
boolean downloaded = hasMedia && item.getMedia().isDownloaded();
viewBinding.shareMediaFileRadio.setVisibility(downloaded ? View.VISIBLE : View.GONE);
boolean hasDownloadUrl = hasMedia && item.getMedia().getDownloadUrl() != null;
if (!hasDownloadUrl) {
viewBinding.shareMediaReceiverRadio.setVisibility(View.GONE);
}
int type = prefs.getInt(PREF_SHARE_EPISODE_TYPE, 1);
if ((type == 2 && !hasDownloadUrl) || (type == 3 && !downloaded)) {
type = 1;
}
viewBinding.shareSocialRadio.setChecked(type == 1);
viewBinding.shareMediaReceiverRadio.setChecked(type == 2);
viewBinding.shareMediaFileRadio.setChecked(type == 3);
boolean switchIsChecked = prefs.getBoolean(PREF_SHARE_EPISODE_START_AT, false);
viewBinding.sharePositionCheckbox.setChecked(switchIsChecked);
}
}

View File

@ -21,6 +21,7 @@ import de.danoeh.antennapod.model.feed.FeedMedia;
/** Utility methods for sharing data */
public class ShareUtils {
private static final String TAG = "ShareUtils";
private static final int ABBREVIATE_MAX_LENGTH = 50;
private ShareUtils() {
}
@ -47,32 +48,48 @@ public class ShareUtils {
return item.getLinkWithFallback() != null;
}
public static void shareMediaDownloadLink(Context context, FeedMedia media) {
shareLink(context, media.getDownloadUrl());
}
public static String getSocialFeedItemShareText(Context context, FeedItem item,
boolean withPosition, boolean abbreviate) {
String text = item.getFeed().getTitle() + ": ";
if (abbreviate && item.getTitle().length() > ABBREVIATE_MAX_LENGTH) {
text += item.getTitle().substring(0, ABBREVIATE_MAX_LENGTH) + "";
} else {
text += item.getTitle();
}
public static void shareFeedItemLinkWithDownloadLink(Context context, FeedItem item, boolean withPosition) {
String text = item.getFeed().getTitle() + ": " + item.getTitle();
int pos = 0;
if (item.getMedia() != null && withPosition) {
text += "\n" + context.getResources().getString(R.string.share_starting_position_label) + ": ";
pos = item.getMedia().getPosition();
text += Converter.getDurationStringLong(pos);
text += Converter.getDurationStringLong(item.getMedia().getPosition());
}
if (hasLinkToShare(item)) {
text += "\n\n" + context.getResources().getString(R.string.share_dialog_episode_website_label) + ": ";
text += item.getLinkWithFallback();
if (!abbreviate) {
text += "\n";
}
text += "\n" + context.getResources().getString(R.string.share_dialog_episode_website_label) + ": ";
if (abbreviate && item.getLinkWithFallback().length() > ABBREVIATE_MAX_LENGTH) {
text += item.getLinkWithFallback().substring(0, ABBREVIATE_MAX_LENGTH) + "";
} else {
text += item.getLinkWithFallback();
}
}
if (item.getMedia() != null && item.getMedia().getDownloadUrl() != null) {
text += "\n\n" + context.getResources().getString(R.string.share_dialog_media_file_label) + ": ";
text += item.getMedia().getDownloadUrl();
if (!abbreviate) {
text += "\n";
}
text += "\n" + context.getResources().getString(R.string.share_dialog_media_file_label) + ": ";
if (abbreviate && item.getMedia().getDownloadUrl().length() > ABBREVIATE_MAX_LENGTH) {
text += item.getMedia().getDownloadUrl().substring(0, ABBREVIATE_MAX_LENGTH) + "";
} else {
text += item.getMedia().getDownloadUrl();
}
if (withPosition) {
text += "#t=" + pos / 1000;
text += "#t=" + item.getMedia().getPosition() / 1000;
}
}
shareLink(context, text);
return text;
}
public static void shareFeedItemFile(Context context, FeedMedia media) {

View File

@ -1,63 +1,172 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
android:clipToPadding="false"
android:padding="16dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/share_label"
android:layout_marginBottom="8dp"
style="@style/AntennaPod.TextView.Heading" />
android:layout_marginBottom="16dp"
style="@style/TextAppearance.Material3.TitleLarge" />
<RadioGroup
android:id="@+id/share_dialog_radio_group"
<androidx.cardview.widget.CardView
android:id="@+id/socialMessageCard"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
android:elevation="1dp"
android:foreground="?attr/selectableItemBackground"
android:layout_marginBottom="16dp"
app:cardBackgroundColor="?attr/colorSurfaceContainer"
app:cardCornerRadius="4dp">
<RadioButton
android:id="@+id/share_social_radio"
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/share_dialog_for_social"
android:checked="true" />
android:orientation="horizontal">
<RadioButton
android:id="@+id/share_media_receiver_radio"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/share_dialog_media_address" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_weight="1">
<RadioButton
android:id="@+id/share_media_file_radio"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/share_dialog_media_file_label" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingStart="16dp"
android:paddingEnd="0dp"
android:paddingTop="16dp"
android:orientation="vertical">
</RadioGroup>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/share_dialog_for_social"
style="@style/TextAppearance.Material3.TitleMedium" />
<com.google.android.material.divider.MaterialDivider
<TextView
android:id="@+id/socialMessageText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/share_dialog_for_social"
style="@style/TextAppearance.Material3.BodyMedium" />
</LinearLayout>
<CheckBox
android:id="@+id/share_position_checkbox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="8dp"
android:text="@string/share_playback_position_dialog_label" />
</LinearLayout>
<ImageView
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_margin="16dp"
android:layout_gravity="center_vertical"
app:srcCompat="@drawable/ic_share" />
</LinearLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:id="@+id/mediaAddressCard"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginVertical="8dp" />
android:elevation="1dp"
android:foreground="?attr/selectableItemBackground"
android:layout_marginBottom="16dp"
app:cardBackgroundColor="?attr/colorSurfaceContainer"
app:cardCornerRadius="4dp">
<CheckBox
android:id="@+id/share_position_checkbox"
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_weight="1">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/share_dialog_media_address"
style="@style/TextAppearance.Material3.TitleMedium" />
<TextView
android:id="@+id/mediaAddressText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:maxLines="2"
android:ellipsize="end"
android:text="@string/share_dialog_media_address"
style="@style/TextAppearance.Material3.BodyMedium" />
</LinearLayout>
<ImageView
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_gravity="center_vertical"
app:srcCompat="@drawable/ic_share" />
</LinearLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:id="@+id/mediaFileCardCard"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/share_playback_position_dialog_label" />
android:elevation="1dp"
android:foreground="?attr/selectableItemBackground"
app:cardBackgroundColor="?attr/colorSurfaceContainer"
app:cardCornerRadius="4dp">
<Button
android:id="@+id/shareButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:text="@string/share_label"
style="@style/Widget.MaterialComponents.Button.TextButton" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_weight="1">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/share_dialog_media_file_label"
style="@style/TextAppearance.Material3.TitleMedium" />
</LinearLayout>
<ImageView
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_gravity="center_vertical"
app:srcCompat="@drawable/ic_share" />
</LinearLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>