updates for share dialog and implementing new instance pattern

This commit is contained in:
Gerardo Vázquez 2020-07-10 00:57:18 -05:00
parent 0cb17304e7
commit df19ca2741
8 changed files with 61 additions and 57 deletions
app/src
androidTest/java/de/test/antennapod/dialogs
main
core/src/main
java/de/danoeh/antennapod/core/feed
res/values

@ -4,10 +4,8 @@ import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.view.View; import android.view.View;
import org.hamcrest.Description;
import org.hamcrest.Matcher; import org.hamcrest.Matcher;
import org.hamcrest.Matchers; import org.hamcrest.Matchers;
import org.hamcrest.TypeSafeMatcher;
import org.junit.Before; import org.junit.Before;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
@ -43,7 +41,7 @@ import static de.test.antennapod.NthMatcher.first;
import static org.hamcrest.CoreMatchers.allOf; import static org.hamcrest.CoreMatchers.allOf;
/** /**
* User interface tests for queue fragment. * User interface tests for share dialog.
*/ */
@RunWith(AndroidJUnit4.class) @RunWith(AndroidJUnit4.class)
public class ShareDialogTest { public class ShareDialogTest {

@ -391,7 +391,8 @@ public abstract class MediaplayerActivity extends CastEnabledActivity implements
break; break;
case R.id.share_item: case R.id.share_item:
if (feedItem != null) { if (feedItem != null) {
new ShareDialog(this, feedItem).show(getSupportFragmentManager(), "ShareEpisodeDialog"); ShareDialog shareDialog = ShareDialog.newInstance(feedItem);
shareDialog.show(getSupportFragmentManager(), "ShareEpisodeDialog");
} }
break; break;
default: default:

@ -5,12 +5,10 @@ import android.content.Context;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.os.Bundle; import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View; import android.view.View;
import android.widget.CheckBox;
import android.widget.RadioButton; import android.widget.RadioButton;
import android.widget.RadioGroup; import android.widget.RadioGroup;
import android.widget.Switch;
import android.widget.Toast;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
@ -18,34 +16,46 @@ import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.DialogFragment; import androidx.fragment.app.DialogFragment;
import de.danoeh.antennapod.R; import de.danoeh.antennapod.R;
import de.danoeh.antennapod.core.feed.FeedItem; import de.danoeh.antennapod.core.feed.FeedItem;
import de.danoeh.antennapod.core.preferences.UserPreferences;
import de.danoeh.antennapod.core.util.ShareUtils; import de.danoeh.antennapod.core.util.ShareUtils;
public class ShareDialog extends DialogFragment { public class ShareDialog extends DialogFragment {
private static final String ARGUMENT_FEED_ITEM = "feedItem";
private static final String TAG = "ShareDialog"; private static final String TAG = "ShareDialog";
private final Context ctx; private Context ctx;
private FeedItem item; private FeedItem item;
private static final String PREF_SHARE_EPISODE_WEBSITE = "prefShareEpisodeWebsite"; private static final String PREF_SHARE_DIALOG_OPTION = "prefShareDialogOption";
private static final String PREF_SHARE_EPISODE_MEDIA = "prefShareEpisodeMedia";
private static final String PREF_SHARE_EPISODE_START_AT = "prefShareEpisodeStartAt"; private static final String PREF_SHARE_EPISODE_START_AT = "prefShareEpisodeStartAt";
private RadioGroup radioGroup; private RadioGroup radioGroup;
private RadioButton radioEpisodeWebsite; private RadioButton radioEpisodeWebsite;
private RadioButton radioMediaFile; private RadioButton radioMediaFile;
private Switch switchStartAt; private CheckBox checkBoxStartAt;
private SharedPreferences prefs; private SharedPreferences prefs;
public ShareDialog(Context ctx, FeedItem item) { public ShareDialog() {
this.ctx = ctx; // Empty constructor required for DialogFragment
this.item = item; }
prefs = ctx.getSharedPreferences("SHARE_DIALOG_PREFS", Context.MODE_PRIVATE);
public static ShareDialog newInstance(FeedItem item) {
Bundle arguments = new Bundle();
arguments.putSerializable(ARGUMENT_FEED_ITEM, item);
ShareDialog dialog = new ShareDialog();
dialog.setArguments(arguments);
return dialog;
} }
@NonNull @NonNull
@Override @Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) { public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
if (getArguments() != null) {
ctx = getActivity();
item = (FeedItem) getArguments().getSerializable(ARGUMENT_FEED_ITEM);
prefs = getActivity().getSharedPreferences("ShareDialog", Context.MODE_PRIVATE);
}
View content = View.inflate(ctx, R.layout.share_episode_dialog, null); View content = View.inflate(ctx, R.layout.share_episode_dialog, null);
AlertDialog.Builder builder = new AlertDialog.Builder(ctx); AlertDialog.Builder builder = new AlertDialog.Builder(ctx);
builder.setTitle(R.string.share_label); builder.setTitle(R.string.share_label);
@ -54,34 +64,30 @@ public class ShareDialog extends DialogFragment {
radioGroup = content.findViewById(R.id.share_dialog_radio_group); radioGroup = content.findViewById(R.id.share_dialog_radio_group);
radioEpisodeWebsite = content.findViewById(R.id.share_episode_website_radio); radioEpisodeWebsite = content.findViewById(R.id.share_episode_website_radio);
radioMediaFile = content.findViewById(R.id.share_media_file_radio); radioMediaFile = content.findViewById(R.id.share_media_file_radio);
switchStartAt = content.findViewById(R.id.share_start_at_timer_dialog); checkBoxStartAt = content.findViewById(R.id.share_start_at_timer_dialog);
setupOptions(); setupOptions();
builder builder.setPositiveButton(R.string.share_label, new DialogInterface.OnClickListener() {
.setPositiveButton(R.string.share_label, new DialogInterface.OnClickListener() { @Override
@Override public void onClick(DialogInterface dialog, int id) {
public void onClick(DialogInterface dialog, int id) { boolean includePlaybackPosition = checkBoxStartAt.isChecked();
if (radioEpisodeWebsite.isChecked()) { if (radioEpisodeWebsite.isChecked()) {
ShareUtils.shareFeedItemLink(ctx, item, switchStartAt.isChecked()); ShareUtils.shareFeedItemLink(ctx, item, includePlaybackPosition);
prefs.edit().putBoolean(PREF_SHARE_EPISODE_START_AT, switchStartAt.isChecked()).apply(); prefs.edit().putString(PREF_SHARE_DIALOG_OPTION, "website").apply();
} else {
ShareUtils.shareFeedItemDownloadLink(ctx, item, includePlaybackPosition);
prefs.edit().putString(PREF_SHARE_DIALOG_OPTION, "media").apply();
}
prefs.edit().putBoolean(PREF_SHARE_EPISODE_START_AT, includePlaybackPosition).apply();
}
})
.setNegativeButton(R.string.cancel_label, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
}
});
prefs.edit().putBoolean(PREF_SHARE_EPISODE_WEBSITE, true).apply();
prefs.edit().putBoolean(PREF_SHARE_EPISODE_MEDIA, false).apply();
} else {
ShareUtils.shareFeedItemLink(ctx, item, switchStartAt.isChecked());
prefs.edit().putBoolean(PREF_SHARE_EPISODE_START_AT, switchStartAt.isChecked()).apply();
prefs.edit().putBoolean(PREF_SHARE_EPISODE_WEBSITE, false).apply();
prefs.edit().putBoolean(PREF_SHARE_EPISODE_MEDIA, true).apply();
}
}
})
.setNegativeButton(R.string.cancel_label, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
}
});
return builder.create(); return builder.create();
} }
@ -99,19 +105,17 @@ public class ShareDialog extends DialogFragment {
} }
if (radioEpisodeWebsite.getVisibility() == View.VISIBLE && radioMediaFile.getVisibility() == View.VISIBLE) { if (radioEpisodeWebsite.getVisibility() == View.VISIBLE && radioMediaFile.getVisibility() == View.VISIBLE) {
boolean radioEpisodeWebsiteIsChecked = prefs.getBoolean(PREF_SHARE_EPISODE_WEBSITE, false); String option = prefs.getString(PREF_SHARE_DIALOG_OPTION, "website");
radioEpisodeWebsite.setChecked(radioEpisodeWebsiteIsChecked); if (option.equals("website")) {
boolean radioMediaIsChecked = prefs.getBoolean(PREF_SHARE_EPISODE_MEDIA, false);
radioMediaFile.setChecked(radioMediaIsChecked);
if (!radioEpisodeWebsiteIsChecked && !radioMediaIsChecked) {
radioGroup.clearCheck();
radioEpisodeWebsite.setChecked(true); radioEpisodeWebsite.setChecked(true);
radioMediaFile.setChecked(false);
} else {
radioEpisodeWebsite.setChecked(false);
radioMediaFile.setChecked(true);
} }
} }
boolean switchIsChecked = prefs.getBoolean(PREF_SHARE_EPISODE_START_AT, false); boolean switchIsChecked = prefs.getBoolean(PREF_SHARE_EPISODE_START_AT, false);
switchStartAt.setChecked(switchIsChecked); checkBoxStartAt.setChecked(switchIsChecked);
} }
} }

@ -9,7 +9,6 @@ import android.view.MenuItem;
import com.google.android.material.snackbar.Snackbar; import com.google.android.material.snackbar.Snackbar;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment; import androidx.fragment.app.Fragment;
import de.danoeh.antennapod.R; import de.danoeh.antennapod.R;
import de.danoeh.antennapod.activity.MainActivity; import de.danoeh.antennapod.activity.MainActivity;
@ -236,8 +235,8 @@ public class FeedItemMenuHandler {
IntentUtils.openInBrowser(context, FeedItemUtil.getLinkWithFallback(selectedItem)); IntentUtils.openInBrowser(context, FeedItemUtil.getLinkWithFallback(selectedItem));
break; break;
case R.id.share_item: case R.id.share_item:
new ShareDialog(context, selectedItem) ShareDialog shareDialog = ShareDialog.newInstance(selectedItem);
.show((fragment.getActivity().getSupportFragmentManager()), "ShareEpisodeDialog"); shareDialog.show((fragment.getActivity().getSupportFragmentManager()), "ShareEpisodeDialog");
break; break;
default: default:
Log.d(TAG, "Unknown menuItemId: " + menuItemId); Log.d(TAG, "Unknown menuItemId: " + menuItemId);

@ -14,13 +14,13 @@
android:orientation="vertical"> android:orientation="vertical">
<RadioButton android:id="@+id/share_episode_website_radio" <RadioButton android:id="@+id/share_episode_website_radio"
android:layout_width="wrap_content" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="@string/share_dialog_episode_website_label" android:text="@string/share_dialog_episode_website_label"
android:checked="true" android:checked="true"
/> />
<RadioButton android:id="@+id/share_media_file_radio" <RadioButton android:id="@+id/share_media_file_radio"
android:layout_width="wrap_content" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="@string/share_dialog_media_file_label" android:text="@string/share_dialog_media_file_label"
/> />
@ -31,7 +31,7 @@
android:layout_height="match_parent" android:layout_height="match_parent"
android:text="@string/share_dialog_include_label"/> android:text="@string/share_dialog_include_label"/>
<Switch <CheckBox
android:id="@+id/share_start_at_timer_dialog" android:id="@+id/share_start_at_timer_dialog"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"

@ -57,7 +57,7 @@
android:id="@+id/share_item" android:id="@+id/share_item"
android:menuCategory="container" android:menuCategory="container"
custom:showAsAction="never" custom:showAsAction="never"
android:title="@string/share_label"> android:title="@string/share_label_with_ellipses">
<menu> <menu>
<item <item
android:id="@+id/share_link_item" android:id="@+id/share_link_item"

@ -7,6 +7,7 @@ import android.text.TextUtils;
import org.apache.commons.lang3.builder.ToStringBuilder; import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle; import org.apache.commons.lang3.builder.ToStringStyle;
import java.io.Serializable;
import java.util.Date; import java.util.Date;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
@ -24,7 +25,7 @@ import de.danoeh.antennapod.core.util.ShownotesProvider;
* *
* @author daniel * @author daniel
*/ */
public class FeedItem extends FeedComponent implements ShownotesProvider, ImageResource { public class FeedItem extends FeedComponent implements ShownotesProvider, ImageResource, Serializable {
/** tag that indicates this item is in the queue */ /** tag that indicates this item is in the queue */
public static final String TAG_QUEUE = "Queue"; public static final String TAG_QUEUE = "Queue";

@ -137,6 +137,7 @@
<string name="rename_feed_label">Rename podcast</string> <string name="rename_feed_label">Rename podcast</string>
<string name="remove_feed_label">Remove podcast</string> <string name="remove_feed_label">Remove podcast</string>
<string name="share_label">Share</string> <string name="share_label">Share</string>
<string name="share_label_with_ellipses">Share…</string>
<string name="share_link_label">Share Episode URL</string> <string name="share_link_label">Share Episode URL</string>
<string name="share_link_with_position_label">Share Episode URL with Position</string> <string name="share_link_with_position_label">Share Episode URL with Position</string>
<string name="share_file_label">Share File</string> <string name="share_file_label">Share File</string>