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 ARGUMENT_FEED_ITEM = "feedItem";
private static final String PREF_NAME = "ShareDialog"; private static final String PREF_NAME = "ShareDialog";
private static final String PREF_SHARE_EPISODE_START_AT = "prefShareEpisodeStartAt"; 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() { public ShareDialog() {
// Empty constructor required for DialogFragment // Empty constructor required for DialogFragment
@ -40,60 +33,46 @@ public class ShareDialog extends BottomSheetDialogFragment {
@Override @Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) { @Nullable Bundle savedInstanceState) {
if (getArguments() != null) { if (getArguments() == null) {
ctx = getActivity(); return null;
item = (FeedItem) getArguments().getSerializable(ARGUMENT_FEED_ITEM); }
prefs = getActivity().getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE); 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); if (item.getMedia() != null && item.getMedia().getDownloadUrl() != null) {
viewBinding.shareDialogRadioGroup.setOnCheckedChangeListener((group, checkedId) -> viewBinding.mediaAddressText.setText(item.getMedia().getDownloadUrl());
viewBinding.sharePositionCheckbox.setEnabled(checkedId == viewBinding.shareSocialRadio.getId())); viewBinding.mediaAddressCard.setOnClickListener(v -> {
ShareUtils.shareLink(getContext(), item.getMedia().getDownloadUrl());
dismiss();
});
} else {
viewBinding.mediaAddressCard.setVisibility(View.GONE);
}
setupOptions(); SharedPreferences prefs = getContext().getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
viewBinding.sharePositionCheckbox.setChecked(prefs.getBoolean(PREF_SHARE_EPISODE_START_AT, false));
viewBinding.shareButton.setOnClickListener((v) -> { viewBinding.socialMessageText.setText(ShareUtils.getSocialFeedItemShareText(
boolean includePlaybackPosition = viewBinding.sharePositionCheckbox.isChecked(); getContext(), item, viewBinding.sharePositionCheckbox.isChecked(), true));
int position; viewBinding.sharePositionCheckbox.setOnCheckedChangeListener((buttonView, isChecked) -> {
if (viewBinding.shareSocialRadio.isChecked()) { prefs.edit().putBoolean(PREF_SHARE_EPISODE_START_AT, isChecked).apply();
ShareUtils.shareFeedItemLinkWithDownloadLink(ctx, item, includePlaybackPosition); viewBinding.socialMessageText.setText(
position = 1; ShareUtils.getSocialFeedItemShareText(getContext(), item, isChecked, true));
} else if (viewBinding.shareMediaReceiverRadio.isChecked()) { });
ShareUtils.shareMediaDownloadLink(ctx, item.getMedia()); viewBinding.socialMessageCard.setOnClickListener(v -> {
position = 2; ShareUtils.shareLink(getContext(), ShareUtils.getSocialFeedItemShareText(
} else if (viewBinding.shareMediaFileRadio.isChecked()) { getContext(), item, viewBinding.sharePositionCheckbox.isChecked(), false));
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();
dismiss(); dismiss();
}); });
return viewBinding.getRoot(); 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 */ /** Utility methods for sharing data */
public class ShareUtils { public class ShareUtils {
private static final String TAG = "ShareUtils"; private static final String TAG = "ShareUtils";
private static final int ABBREVIATE_MAX_LENGTH = 50;
private ShareUtils() { private ShareUtils() {
} }
@ -47,32 +48,48 @@ public class ShareUtils {
return item.getLinkWithFallback() != null; return item.getLinkWithFallback() != null;
} }
public static void shareMediaDownloadLink(Context context, FeedMedia media) { public static String getSocialFeedItemShareText(Context context, FeedItem item,
shareLink(context, media.getDownloadUrl()); 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) { if (item.getMedia() != null && withPosition) {
text += "\n" + context.getResources().getString(R.string.share_starting_position_label) + ": "; text += "\n" + context.getResources().getString(R.string.share_starting_position_label) + ": ";
pos = item.getMedia().getPosition(); text += Converter.getDurationStringLong(item.getMedia().getPosition());
text += Converter.getDurationStringLong(pos);
} }
if (hasLinkToShare(item)) { if (hasLinkToShare(item)) {
text += "\n\n" + context.getResources().getString(R.string.share_dialog_episode_website_label) + ": "; if (!abbreviate) {
text += item.getLinkWithFallback(); 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) { if (item.getMedia() != null && item.getMedia().getDownloadUrl() != null) {
text += "\n\n" + context.getResources().getString(R.string.share_dialog_media_file_label) + ": "; if (!abbreviate) {
text += item.getMedia().getDownloadUrl(); 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) { 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) { public static void shareFeedItemFile(Context context, FeedMedia media) {

View File

@ -1,63 +1,172 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<LinearLayout <LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android" 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_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:orientation="vertical" android:orientation="vertical"
android:gravity="center" android:gravity="center"
android:clipToPadding="false"
android:padding="16dp"> android:padding="16dp">
<TextView <TextView
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="@string/share_label" android:text="@string/share_label"
android:layout_marginBottom="8dp" android:layout_marginBottom="16dp"
style="@style/AntennaPod.TextView.Heading" /> style="@style/TextAppearance.Material3.TitleLarge" />
<RadioGroup <androidx.cardview.widget.CardView
android:id="@+id/share_dialog_radio_group" android:id="@+id/socialMessageCard"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" 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 <LinearLayout
android:id="@+id/share_social_radio"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="@string/share_dialog_for_social" android:orientation="horizontal">
android:checked="true" />
<RadioButton <LinearLayout
android:id="@+id/share_media_receiver_radio" android:layout_width="0dp"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:layout_height="wrap_content" android:orientation="vertical"
android:text="@string/share_dialog_media_address" /> android:layout_weight="1">
<RadioButton <LinearLayout
android:id="@+id/share_media_file_radio" android:layout_width="match_parent"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:layout_height="wrap_content" android:paddingStart="16dp"
android:text="@string/share_dialog_media_file_label" /> 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_width="match_parent"
android:layout_height="wrap_content" 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 <LinearLayout
android:id="@+id/share_position_checkbox" 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_width="match_parent"
android:layout_height="wrap_content" 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 <LinearLayout
android:id="@+id/shareButton" android:layout_width="match_parent"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_height="wrap_content" android:padding="16dp"
android:layout_gravity="end" android:orientation="horizontal">
android:text="@string/share_label"
style="@style/Widget.MaterialComponents.Button.TextButton" /> <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> </LinearLayout>